aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/shadows/ShadowNetworkInfo.java
blob: 7446b970e0b33e4d60cd8a457f6aec5b3c2512d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.xtremelabs.robolectric.shadows;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

/**
 * Shadow of {@code NetworkInfo} which is used by ShadowConnectivityManager.
 */

@Implements(NetworkInfo.class)
public class ShadowNetworkInfo {
    private boolean isAvailable;
    private boolean isConnected;
    private int connectionType;
    private int connectionSubType;
    private NetworkInfo.DetailedState detailedState;

    public static NetworkInfo newInstance() {
        return newInstance(null);
    }

    public static NetworkInfo newInstance(NetworkInfo.DetailedState detailedState) {
        return newInstance(detailedState, ConnectivityManager.TYPE_MOBILE, 0, true, true);
    }

    public static NetworkInfo newInstance(NetworkInfo.DetailedState detailedState, int type, int subType,
                                          boolean isAvailable,
                                          boolean isConnected) {
        NetworkInfo networkInfo = Robolectric.newInstanceOf(NetworkInfo.class);
        final ShadowNetworkInfo info = shadowOf(networkInfo);
        info.setConnectionType(type);
        info.setSubType(subType);
        info.setDetailedState(detailedState);
        info.setAvailableStatus(isAvailable);
        info.setConnectionStatus(isConnected);
        return networkInfo;
    }

    @Implementation
    public boolean isConnected() {
        return isConnected;
    }

    @Implementation
    public boolean isConnectedOrConnecting() {
        return isConnected;
    }

    @Implementation
    public NetworkInfo.State getState() {
      return isConnected ? NetworkInfo.State.CONNECTED :
          NetworkInfo.State.DISCONNECTED;
    }

    @Implementation
    public NetworkInfo.DetailedState getDetailedState() {
        return detailedState;
    }

    @Implementation
    public int getType(){
    	return connectionType;
    }

    @Implementation
    public int getSubtype() {
        return connectionSubType;
    }

    @Implementation
    public boolean isAvailable() {
        return isAvailable;
    }

    /**
     * Non-Android accessor
     * Sets up the return value of {@link #isAvailable()}.
     *
     * @param isAvailable the value that {@link #isAvailable()} will return.
     */
    public void setAvailableStatus(boolean isAvailable) {
        this.isAvailable = isAvailable;
    }

    /**
     * Non-Android accessor
     * Sets up the return value of {@link #isConnectedOrConnecting()} and {@link @isConnected()}.
     *
     * @param isConnected the value that {@link #isConnectedOrConnecting()} and {@link #isConnected()} will return.
     */
    public void setConnectionStatus(boolean isConnected) {
        this.isConnected = isConnected;
    }

    /**
     * Non-Android accessor
     * Sets up the return value of {@link #getType()}.
     *
     * @param connectionType the value that {@link #getType()} will return.
     */
    public void setConnectionType(int connectionType){
    	this.connectionType = connectionType;
    }

    public void setSubType(int subType) {
        this.connectionSubType = subType;
    }

    public void setDetailedState(NetworkInfo.DetailedState detailedState) {
        this.detailedState = detailedState;
    }
}