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