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

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

Issue 29857561: Fix libadblockplus-android tests, Part 2
Patch Set: Created Aug. 16, 2018, 11:48 a.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
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.libadblockplus.tests; 18 package org.adblockplus.libadblockplus.tests;
19 19
20 import android.os.SystemClock; 20 import org.adblockplus.libadblockplus.BaseFilterEngineTest;
21
22 import org.adblockplus.libadblockplus.HeaderEntry; 21 import org.adblockplus.libadblockplus.HeaderEntry;
23 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; 22 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback;
24 import org.adblockplus.libadblockplus.Platform;
25 import org.adblockplus.libadblockplus.ServerResponse; 23 import org.adblockplus.libadblockplus.ServerResponse;
26 import org.adblockplus.libadblockplus.Subscription; 24 import org.adblockplus.libadblockplus.ThrowingWebRequest;
27 import org.adblockplus.libadblockplus.WebRequest;
28 import org.adblockplus.libadblockplus.android.AndroidWebRequest;
29 import org.junit.Test; 25 import org.junit.Test;
30 26
31 import java.util.LinkedList; 27 import java.util.LinkedList;
32 import java.util.List; 28 import java.util.List;
33 29
34 public class IsAllowedConnectionCallbackTest extends BaseFilterEngineTest 30 public class IsAllowedConnectionCallbackTest extends BaseFilterEngineTest
35 { 31 {
36 private static final int UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS = 5 * 1000; // 5s 32 private static final class TestRequest extends ThrowingWebRequest
33 {
34 private volatile int callCount;
37 35
38 private static final class TestRequest extends AndroidWebRequest
39 {
40 private List<String> urls = new LinkedList<String>(); 36 private List<String> urls = new LinkedList<String>();
41 37
42 public List<String> getUrls() 38 public List<String> getUrls()
43 { 39 {
44 return urls; 40 return urls;
45 } 41 }
46 42
43 public int getCallCount() { return this.callCount; }
44
45 public void reset() { this.callCount = 0; }
46
47 @Override 47 @Override
48 public ServerResponse httpGET(String url, List<HeaderEntry> headers) 48 public ServerResponse httpGET(String url, List<HeaderEntry> headers)
49 { 49 {
50 urls.add(url); 50 urls.add(url);
51 this.callCount++;
51 return super.httpGET(url, headers); 52 return super.httpGET(url, headers);
52 } 53 }
53 } 54 }
54 55
55 private static final class TestCallback implements IsAllowedConnectionCallback 56 private static final class TestCallback implements IsAllowedConnectionCallback
56 { 57 {
58 private volatile int callCount;
57 private boolean result; 59 private boolean result;
58 private boolean invoked; 60 private volatile String connectionType;
59 private String connectionType;
60
61 public boolean isResult()
62 {
63 return result;
64 }
65 61
66 public void setResult(boolean result) 62 public void setResult(boolean result)
67 { 63 {
68 this.result = result; 64 this.result = result;
69 } 65 }
70 66
71 public String getConnectionType() 67 public String getConnectionType()
72 { 68 {
73 return connectionType; 69 return connectionType;
74 } 70 }
75 71
76 public boolean isInvoked() 72 public boolean isInvoked()
77 { 73 {
78 return invoked; 74 return this.callCount > 0;
79 } 75 }
80 76
77 public int getCallCount() { return this.callCount; }
78
79 public void reset() { this.callCount = 0; }
80
81 @Override 81 @Override
82 public boolean isConnectionAllowed(String connectionType) 82 public boolean isConnectionAllowed(String connectionType)
83 { 83 {
84 this.invoked = true;
85 this.connectionType = connectionType; 84 this.connectionType = connectionType;
86 85 this.callCount++;
87 return result; 86 return result;
88 } 87 }
89 } 88 }
90 89
91 private TestRequest request; 90 private TestRequest request;
92 private TestCallback callback; 91 private TestCallback callback;
93 92
94 @Override 93 @Override
95 protected void setUp() throws Exception 94 protected void setUp() throws Exception
96 { 95 {
97 platform = new Platform(createLogSystem(), createWebRequest(),
98 getContext().getFilesDir().getAbsolutePath());
99 callback = new TestCallback(); 96 callback = new TestCallback();
100 platform.setUpFilterEngine(callback); 97 request = new TestRequest();
101 filterEngine = platform.getFilterEngine(); 98 setIsAllowedConnectionCallback(callback);
99 setWebRequest(request);
100 super.setUp();
102 } 101 }
103 102
104 @Override 103 @Override
105 protected WebRequest createWebRequest() 104 protected int getUpdateRequestCount()
106 { 105 {
107 return request = new TestRequest(); 106 return callback.getCallCount() + request.getCallCount();
108 } 107 }
109 108
110 private void updateSubscriptions() 109 private void setResult(boolean result)
111 { 110 {
112 for (final Subscription s : this.filterEngine.getListedSubscriptions()) 111 callback.reset();
113 { 112 request.reset();
114 try 113 callback.setResult(result);
115 {
116 s.updateFilters();
117 }
118 finally
119 {
120 s.dispose();
121 }
122 }
123 } 114 }
124 115
125 @Test 116 @Test
126 public void testAllow() 117 public void testAllow()
127 { 118 {
128 final String allowedConnectionType = "wifi1"; 119 final String allowedConnectionType = "wifi1";
129 filterEngine.setAllowedConnectionType(allowedConnectionType); 120 filterEngine.setAllowedConnectionType(allowedConnectionType);
130 callback.setResult(true);
131 121
122 setResult(true);
132 assertEquals(0, request.getUrls().size()); 123 assertEquals(0, request.getUrls().size());
133 assertFalse(callback.isInvoked());
134 124
135 updateSubscriptions(); 125 updateSubscriptions();
136 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
137 126
138 assertTrue(callback.isInvoked()); 127 assertTrue(callback.isInvoked());
139 assertNotNull(callback.getConnectionType()); 128 assertNotNull(callback.getConnectionType());
140 assertEquals(allowedConnectionType, callback.getConnectionType()); 129 assertEquals(allowedConnectionType, callback.getConnectionType());
141 130
142 assertTrue(request.getUrls().size() > 0); 131 assertTrue(request.getUrls().size() > 0);
143 } 132 }
144 133
145 @Test 134 @Test
146 public void testDeny() 135 public void testDeny()
147 { 136 {
148 final String allowedConnectionType = "wifi2"; 137 final String allowedConnectionType = "wifi2";
149 filterEngine.setAllowedConnectionType(allowedConnectionType); 138 filterEngine.setAllowedConnectionType(allowedConnectionType);
150 139
151 callback.setResult(false); 140 setResult(false);
152 assertEquals(0, request.getUrls().size()); 141 assertEquals(0, request.getUrls().size());
153 142
154 updateSubscriptions(); 143 updateSubscriptions();
155 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
156 144
157 assertTrue(callback.isInvoked()); 145 assertTrue(callback.isInvoked());
158 assertNotNull(callback.getConnectionType()); 146 assertNotNull(callback.getConnectionType());
159 assertEquals(allowedConnectionType, callback.getConnectionType()); 147 assertEquals(allowedConnectionType, callback.getConnectionType());
160 148
161 assertEquals(0, request.getUrls().size()); 149 assertEquals(0, request.getUrls().size());
162 } 150 }
163 } 151 }
OLDNEW

Powered by Google App Engine
This is Rietveld