1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.LOLLIPOP;
4 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
5 import static android.os.Build.VERSION_CODES.M;
6 
7 import android.net.Network;
8 import java.io.FileDescriptor;
9 import java.net.DatagramSocket;
10 import java.net.Socket;
11 import org.robolectric.annotation.Implementation;
12 import org.robolectric.annotation.Implements;
13 import org.robolectric.shadow.api.Shadow;
14 
15 @Implements(value = Network.class, minSdk = LOLLIPOP)
16 public class ShadowNetwork {
17   private int netId;
18 
19   /**
20    * Creates new instance of {@link Network}, because its constructor is hidden.
21    *
22    * @param netId The netId.
23    * @return The Network instance.
24    */
newInstance(int netId)25   public static Network newInstance(int netId) {
26     return Shadow.newInstance(Network.class, new Class[] {int.class}, new Object[] {netId});
27   }
28 
29   @Implementation
__constructor__(int netId)30   protected void __constructor__(int netId) {
31     this.netId = netId;
32   }
33 
34   /**
35    * No-ops. We cannot assume that a Network represents a real network interface on the device
36    * running this test, so we have nothing to bind the socket to.
37    */
38   @Implementation(minSdk = LOLLIPOP_MR1)
bindSocket(DatagramSocket socket)39   protected void bindSocket(DatagramSocket socket) {}
40 
41   /**
42    * No-ops. We cannot assume that a Network represents a real network interface on the device
43    * running this test, so we have nothing to bind the socket to.
44    */
45   @Implementation
bindSocket(Socket socket)46   protected void bindSocket(Socket socket) {}
47 
48   /**
49    * No-ops. We cannot assume that a Network represents a real network interface on the device
50    * running this test, so we have nothing to bind the socket to.
51    */
52   @Implementation(minSdk = M)
bindSocket(FileDescriptor fd)53   protected void bindSocket(FileDescriptor fd) {}
54 
55   /**
56    * Allows to get the stored netId.
57    *
58    * @return The netId.
59    */
getNetId()60   public int getNetId() {
61     return netId;
62   }
63 }
64