Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.net.ConnectivityManager;
      4 import android.net.NetworkInfo;
      5 import com.xtremelabs.robolectric.Robolectric;
      6 import com.xtremelabs.robolectric.internal.Implementation;
      7 import com.xtremelabs.robolectric.internal.Implements;
      8 
      9 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     10 
     11 /**
     12  * Shadow of {@code NetworkInfo} which is used by ShadowConnectivityManager.
     13  */
     14 
     15 @Implements(NetworkInfo.class)
     16 public class ShadowNetworkInfo {
     17     private boolean isAvailable;
     18     private boolean isConnected;
     19     private int connectionType;
     20     private int connectionSubType;
     21     private NetworkInfo.DetailedState detailedState;
     22 
     23     public static NetworkInfo newInstance() {
     24         return newInstance(null);
     25     }
     26 
     27     public static NetworkInfo newInstance(NetworkInfo.DetailedState detailedState) {
     28         return newInstance(detailedState, ConnectivityManager.TYPE_MOBILE, 0, true, true);
     29     }
     30 
     31     public static NetworkInfo newInstance(NetworkInfo.DetailedState detailedState, int type, int subType,
     32                                           boolean isAvailable,
     33                                           boolean isConnected) {
     34         NetworkInfo networkInfo = Robolectric.newInstanceOf(NetworkInfo.class);
     35         final ShadowNetworkInfo info = shadowOf(networkInfo);
     36         info.setConnectionType(type);
     37         info.setSubType(subType);
     38         info.setDetailedState(detailedState);
     39         info.setAvailableStatus(isAvailable);
     40         info.setConnectionStatus(isConnected);
     41         return networkInfo;
     42     }
     43 
     44     @Implementation
     45     public boolean isConnected() {
     46         return isConnected;
     47     }
     48 
     49     @Implementation
     50     public boolean isConnectedOrConnecting() {
     51         return isConnected;
     52     }
     53 
     54     @Implementation
     55     public NetworkInfo.State getState() {
     56       return isConnected ? NetworkInfo.State.CONNECTED :
     57           NetworkInfo.State.DISCONNECTED;
     58     }
     59 
     60     @Implementation
     61     public NetworkInfo.DetailedState getDetailedState() {
     62         return detailedState;
     63     }
     64 
     65     @Implementation
     66     public int getType(){
     67     	return connectionType;
     68     }
     69 
     70     @Implementation
     71     public int getSubtype() {
     72         return connectionSubType;
     73     }
     74 
     75     @Implementation
     76     public boolean isAvailable() {
     77         return isAvailable;
     78     }
     79 
     80     /**
     81      * Non-Android accessor
     82      * Sets up the return value of {@link #isAvailable()}.
     83      *
     84      * @param isAvailable the value that {@link #isAvailable()} will return.
     85      */
     86     public void setAvailableStatus(boolean isAvailable) {
     87         this.isAvailable = isAvailable;
     88     }
     89 
     90     /**
     91      * Non-Android accessor
     92      * Sets up the return value of {@link #isConnectedOrConnecting()} and {@link @isConnected()}.
     93      *
     94      * @param isConnected the value that {@link #isConnectedOrConnecting()} and {@link #isConnected()} will return.
     95      */
     96     public void setConnectionStatus(boolean isConnected) {
     97         this.isConnected = isConnected;
     98     }
     99 
    100     /**
    101      * Non-Android accessor
    102      * Sets up the return value of {@link #getType()}.
    103      *
    104      * @param connectionType the value that {@link #getType()} will return.
    105      */
    106     public void setConnectionType(int connectionType){
    107     	this.connectionType = connectionType;
    108     }
    109 
    110     public void setSubType(int subType) {
    111         this.connectionSubType = subType;
    112     }
    113 
    114     public void setDetailedState(NetworkInfo.DetailedState detailedState) {
    115         this.detailedState = detailedState;
    116     }
    117 }
    118