1 package org.robolectric.shadows; 2 3 import android.net.NetworkInfo; 4 import org.robolectric.annotation.Implementation; 5 import org.robolectric.annotation.Implements; 6 import org.robolectric.shadow.api.Shadow; 7 8 @Implements(NetworkInfo.class) 9 public class ShadowNetworkInfo { 10 private boolean isAvailable; 11 private NetworkInfo.State state; 12 private int connectionType; 13 private int connectionSubType; 14 private NetworkInfo.DetailedState detailedState; 15 16 @Implementation __staticInitializer__()17 protected static void __staticInitializer__() {} 18 19 /** 20 * @deprecated use {@link #newInstance(NetworkInfo.DetailedState, int, int, boolean, 21 * NetworkInfo.State)} instead 22 */ 23 @Deprecated newInstance( NetworkInfo.DetailedState detailedState, int type, int subType, boolean isAvailable, boolean isConnected)24 public static NetworkInfo newInstance( 25 NetworkInfo.DetailedState detailedState, 26 int type, 27 int subType, 28 boolean isAvailable, 29 boolean isConnected) { 30 return newInstance( 31 detailedState, 32 type, 33 subType, 34 isAvailable, 35 isConnected ? NetworkInfo.State.CONNECTED : NetworkInfo.State.DISCONNECTED); 36 } 37 38 /** Allows developers to create a {@link NetworkInfo} instance for testing. */ newInstance( NetworkInfo.DetailedState detailedState, int type, int subType, boolean isAvailable, NetworkInfo.State state)39 public static NetworkInfo newInstance( 40 NetworkInfo.DetailedState detailedState, 41 int type, 42 int subType, 43 boolean isAvailable, 44 NetworkInfo.State state) { 45 NetworkInfo networkInfo = Shadow.newInstanceOf(NetworkInfo.class); 46 final ShadowNetworkInfo info = Shadow.extract(networkInfo); 47 info.setConnectionType(type); 48 info.setSubType(subType); 49 info.setDetailedState(detailedState); 50 info.setAvailableStatus(isAvailable); 51 info.setConnectionStatus(state); 52 return networkInfo; 53 } 54 55 @Implementation isConnected()56 protected boolean isConnected() { 57 return state == NetworkInfo.State.CONNECTED; 58 } 59 60 @Implementation isConnectedOrConnecting()61 protected boolean isConnectedOrConnecting() { 62 return isConnected() || state == NetworkInfo.State.CONNECTING; 63 } 64 65 @Implementation getState()66 protected NetworkInfo.State getState() { 67 return state; 68 } 69 70 @Implementation getDetailedState()71 protected NetworkInfo.DetailedState getDetailedState() { 72 return detailedState; 73 } 74 75 @Implementation getType()76 protected int getType() { 77 return connectionType; 78 } 79 80 @Implementation getSubtype()81 protected int getSubtype() { 82 return connectionSubType; 83 } 84 85 @Implementation isAvailable()86 protected boolean isAvailable() { 87 return isAvailable; 88 } 89 90 /** 91 * Sets up the return value of {@link #isAvailable()}. 92 * 93 * @param isAvailable the value that {@link #isAvailable()} will return. 94 */ setAvailableStatus(boolean isAvailable)95 public void setAvailableStatus(boolean isAvailable) { 96 this.isAvailable = isAvailable; 97 } 98 99 /** 100 * Sets up the return value of {@link #isConnectedOrConnecting()}, {@link #isConnected()}, and 101 * {@link #getState()}. If the input is true, state will be {@link NetworkInfo.State#CONNECTED}, 102 * else it will be {@link NetworkInfo.State#DISCONNECTED}. 103 * 104 * @param isConnected the value that {@link #isConnectedOrConnecting()} and {@link #isConnected()} 105 * will return. 106 * @deprecated use {@link #setConnectionStatus(NetworkInfo.State)} instead 107 */ 108 @Deprecated setConnectionStatus(boolean isConnected)109 public void setConnectionStatus(boolean isConnected) { 110 setConnectionStatus(isConnected ? NetworkInfo.State.CONNECTED : NetworkInfo.State.DISCONNECTED); 111 } 112 113 /** 114 * Sets up the return value of {@link #getState()}. 115 * 116 * @param state the value that {@link #getState()} will return. 117 */ setConnectionStatus(NetworkInfo.State state)118 public void setConnectionStatus(NetworkInfo.State state) { 119 this.state = state; 120 } 121 122 /** 123 * Sets up the return value of {@link #getType()}. 124 * 125 * @param connectionType the value that {@link #getType()} will return. 126 */ setConnectionType(int connectionType)127 public void setConnectionType(int connectionType) { 128 this.connectionType = connectionType; 129 } 130 setSubType(int subType)131 public void setSubType(int subType) { 132 this.connectionSubType = subType; 133 } 134 setDetailedState(NetworkInfo.DetailedState detailedState)135 public void setDetailedState(NetworkInfo.DetailedState detailedState) { 136 this.detailedState = detailedState; 137 } 138 } 139