Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/IsAllowedConnectionCallbackImplTest.java

Issue 29413555: Issue 5145 - Add test for IsAllowedConnectionCallbackImpl (Closed)
Patch Set: Created April 15, 2017, 1:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17 package org.adblockplus.libadblockplus.tests;
18
19 import android.net.ConnectivityManager;
20 import android.net.NetworkInfo;
21
22 import org.adblockplus.libadblockplus.android.settings.ConnectionType;
23 import org.adblockplus.libadblockplus.android.settings.IsAllowedConnectionCallba ckImpl;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mockito;
27 import org.mockito.internal.stubbing.answers.Returns;
28 import org.mockito.runners.MockitoJUnitRunner;
29
30 @RunWith(MockitoJUnitRunner.class)
31 public class IsAllowedConnectionCallbackImplTest extends BaseJsTest
32 {
33 private final int[] connectionTypes = new int[]
34 {
35 ConnectivityManager.TYPE_WIFI,
36 ConnectivityManager.TYPE_MOBILE,
37 ConnectivityManager.TYPE_ETHERNET,
38 };
39
40 @Override
41 protected void setUp() throws Exception
42 {
43 super.setUp();
44
45 System.setProperty(
46 "dexmaker.dexcache",
47 getInstrumentation().getTargetContext().getCacheDir().getPath());
48 }
49
50 public static ConnectivityManager buildNoActiveConnectionCM()
51 {
52 final ConnectivityManager connectivityManager = Mockito.mock(ConnectivityMan ager.class);
53
54 Mockito
55 .when(connectivityManager.getActiveNetworkInfo())
56 .thenReturn(null);
57
58 return connectivityManager;
59 }
60
61 public static ConnectivityManager buildConnectivityManager(boolean connected, int type, boolean metered)
62 {
63 final ConnectivityManager connectivityManager = Mockito.mock(ConnectivityMan ager.class);
64 final NetworkInfo networkInfo = Mockito.mock(NetworkInfo.class);
65
66 Mockito
67 .when(connectivityManager.getActiveNetworkInfo())
68 .thenReturn(networkInfo);
69
70 Mockito
71 .when(connectivityManager.isActiveNetworkMetered())
72 .thenReturn(metered);
73
74 Mockito
75 .when(networkInfo.isConnected())
76 .thenReturn(connected);
77
78 Mockito
79 .when(networkInfo.getType())
80 .then(new Returns(type));
81
82 return connectivityManager;
83 }
84
85 @Test
86 public void testNullAllowedConnection()
87 {
88 final IsAllowedConnectionCallbackImpl callback = new IsAllowedConnectionCall backImpl(null);
89 assertTrue(callback.isConnectionAllowed(null));
90 }
91
92 @Test
93 public void testNoActiveConnection()
94 {
95 ConnectivityManager connectivityManager = buildNoActiveConnectionCM();
96 final IsAllowedConnectionCallbackImpl callback =
97 new IsAllowedConnectionCallbackImpl(connectivityManager);
98
99 for (ConnectionType eachConnectionType : ConnectionType.values())
100 {
101 assertFalse(callback.isConnectionAllowed(eachConnectionType.getValue()));
102 }
103 }
104
105 @Test
106 public void testFalseForAnyTypeIfNotConnected()
107 {
108 for (int currentConnectionType : connectionTypes)
109 {
110 ConnectivityManager cm = buildConnectivityManager(false, currentConnection Type, false);
111 final IsAllowedConnectionCallbackImpl callback = new IsAllowedConnectionCa llbackImpl(cm);
112
113 for (ConnectionType eachConnectionType : ConnectionType.values())
114 {
115 assertFalse(callback.isConnectionAllowed(eachConnectionType.getValue())) ;
116 }
117 }
118 }
119
120 @Test
121 public void testFalseForUnknownConnectionType()
122 {
123 final String allowedConnectionType = "unknown";
124
125 for (int currentConnectionType : connectionTypes)
126 {
127 ConnectivityManager cm = buildConnectivityManager(true, currentConnectionT ype, false);
128 final IsAllowedConnectionCallbackImpl callback = new IsAllowedConnectionCa llbackImpl(cm);
129
130 assertFalse(callback.isConnectionAllowed(allowedConnectionType));
131 }
132 }
133
134 @Test
135 public void testWifiNonMetered()
136 {
137 // current connection is wifi non metered
138 final IsAllowedConnectionCallbackImpl callback =
139 new IsAllowedConnectionCallbackImpl(
140 buildConnectivityManager(true, ConnectivityManager.TYPE_WIFI, false));
141
142 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue()));
143 assertTrue(callback.isConnectionAllowed(ConnectionType.WIFI.getValue()));
144 assertTrue(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.getV alue()));
145 }
146
147 @Test
148 public void testWifiMetered()
149 {
150 // current connection is wifi metered
151 final IsAllowedConnectionCallbackImpl callback =
152 new IsAllowedConnectionCallbackImpl(
153 buildConnectivityManager(true, ConnectivityManager.TYPE_WIFI, true));
154
155 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue()));
156 assertTrue(callback.isConnectionAllowed(ConnectionType.WIFI.getValue()));
157 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.get Value()));
158 }
159
160 @Test
161 public void testMobile()
162 {
163 // current connection is mobile
164 final IsAllowedConnectionCallbackImpl callback =
165 new IsAllowedConnectionCallbackImpl(
166 buildConnectivityManager(true, ConnectivityManager.TYPE_MOBILE, false));
167
168 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue()));
169 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI.getValue()));
170 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.get Value()));
171 }
172
173 @Test
174 public void testEthernet()
175 {
176 // current connection is ethernet
177 final IsAllowedConnectionCallbackImpl callback =
178 new IsAllowedConnectionCallbackImpl(
179 buildConnectivityManager(true, ConnectivityManager.TYPE_ETHERNET, false) );
180
181 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue()));
182 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI.getValue()));
183 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.get Value()));
184 }
185 }
OLDNEW

Powered by Google App Engine
This is Rietveld