| OLD | NEW |
| 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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 import org.adblockplus.libadblockplus.HeaderEntry; | 23 import org.adblockplus.libadblockplus.HeaderEntry; |
| 24 import org.adblockplus.libadblockplus.JsEngine; | 24 import org.adblockplus.libadblockplus.JsEngine; |
| 25 import org.adblockplus.libadblockplus.JsValue; | 25 import org.adblockplus.libadblockplus.JsValue; |
| 26 import org.adblockplus.libadblockplus.LazyLogSystem; | 26 import org.adblockplus.libadblockplus.LazyLogSystem; |
| 27 import org.adblockplus.libadblockplus.LazyWebRequest; | 27 import org.adblockplus.libadblockplus.LazyWebRequest; |
| 28 import org.adblockplus.libadblockplus.ServerResponse; | 28 import org.adblockplus.libadblockplus.ServerResponse; |
| 29 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; | 29 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; |
| 30 | 30 |
| 31 import org.junit.Test; | 31 import org.junit.Test; |
| 32 | 32 |
| 33 import java.io.File; |
| 34 import java.io.IOException; |
| 33 import java.util.List; | 35 import java.util.List; |
| 34 | 36 |
| 35 public class UpdateCheckTest extends BaseJsTest | 37 public class UpdateCheckTest extends BaseJsTest |
| 36 { | 38 { |
| 37 protected String previousRequestUrl; | 39 protected String previousRequestUrl; |
| 38 | 40 |
| 39 public class TestWebRequest extends LazyWebRequest | 41 public class TestWebRequest extends LazyWebRequest |
| 40 { | 42 { |
| 41 public ServerResponse response = new ServerResponse(); | 43 public ServerResponse response = new ServerResponse(); |
| 42 | 44 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback
() | 76 private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback
() |
| 75 { | 77 { |
| 76 @Override | 78 @Override |
| 77 public void updateCheckDoneCallback(String error) | 79 public void updateCheckDoneCallback(String error) |
| 78 { | 80 { |
| 79 updateCallbackCalled = true; | 81 updateCallbackCalled = true; |
| 80 updateError = error; | 82 updateError = error; |
| 81 } | 83 } |
| 82 }; | 84 }; |
| 83 | 85 |
| 84 public void reset() | 86 public void reset() throws IOException |
| 85 { | 87 { |
| 86 jsEngine = new JsEngine(appInfo); | 88 jsEngine = new JsEngine(appInfo); |
| 87 jsEngine.setLogSystem(new LazyLogSystem()); | 89 jsEngine.setLogSystem(new LazyLogSystem()); |
| 88 jsEngine.setDefaultFileSystem(getContext().getFilesDir().getAbsolutePath
()); | 90 |
| 91 cleanupFileSystem(); |
| 92 tmpFileSystemPath = buildTmpFileSystemPath(); |
| 93 jsEngine.setDefaultFileSystem(tmpFileSystemPath.getAbsolutePath()); |
| 94 |
| 89 jsEngine.setWebRequest(webRequest); | 95 jsEngine.setWebRequest(webRequest); |
| 90 jsEngine.setEventCallback("updateAvailable", eventCallback); | 96 jsEngine.setEventCallback("updateAvailable", eventCallback); |
| 91 | 97 |
| 92 filterEngine = new FilterEngine(jsEngine); | 98 filterEngine = new FilterEngine(jsEngine); |
| 93 } | 99 } |
| 94 | 100 |
| 95 @Override | 101 @Override |
| 96 protected void setUp() throws Exception | 102 protected void setUp() throws Exception |
| 97 { | 103 { |
| 98 super.setUp(); | 104 super.setUp(); |
| 99 | 105 |
| 100 appInfo = AppInfo.builder().build(); | 106 appInfo = AppInfo.builder().build(); |
| 101 webRequest = new TestWebRequest(); | 107 webRequest = new TestWebRequest(); |
| 102 eventCallbackCalled = false; | 108 eventCallbackCalled = false; |
| 103 updateCallbackCalled = false; | 109 updateCallbackCalled = false; |
| 104 reset(); | 110 reset(); |
| 105 } | 111 } |
| 106 | 112 |
| 107 public void forceUpdateCheck() | 113 public void forceUpdateCheck() |
| 108 { | 114 { |
| 109 filterEngine.forceUpdateCheck(updateCallback); | 115 filterEngine.forceUpdateCheck(updateCallback); |
| 110 } | 116 } |
| 111 | 117 |
| 112 @Test | 118 @Test |
| 113 public void testRequestFailure() throws InterruptedException | 119 public void testRequestFailure() throws InterruptedException, IOException |
| 114 { | 120 { |
| 115 webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); | 121 webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); |
| 116 | 122 |
| 117 appInfo = AppInfo | 123 appInfo = AppInfo |
| 118 .builder() | 124 .builder() |
| 119 .setName("1") | 125 .setName("1") |
| 120 .setVersion("3") | 126 .setVersion("3") |
| 121 .setApplication("4") | 127 .setApplication("4") |
| 122 .setApplicationVersion("2") | 128 .setApplicationVersion("2") |
| 123 .setDevelopmentBuild(false) | 129 .setDevelopmentBuild(false) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 146 "&application=" + appInfo.application + | 152 "&application=" + appInfo.application + |
| 147 "&applicationVersion=" + appInfo.applicationVersion + | 153 "&applicationVersion=" + appInfo.applicationVersion + |
| 148 "&platform=" + platform + | 154 "&platform=" + platform + |
| 149 "&platformVersion=" + platformVersion + | 155 "&platformVersion=" + platformVersion + |
| 150 "&lastVersion=0&downloadCount=0"; | 156 "&lastVersion=0&downloadCount=0"; |
| 151 | 157 |
| 152 assertEquals(expectedUrl, previousRequestUrl); | 158 assertEquals(expectedUrl, previousRequestUrl); |
| 153 } | 159 } |
| 154 | 160 |
| 155 @Test | 161 @Test |
| 156 public void testApplicationUpdateAvailable() throws InterruptedException | 162 public void testApplicationUpdateAvailable() throws InterruptedException, IO
Exception |
| 157 { | 163 { |
| 158 webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 164 webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
| 159 webRequest.response.setResponseStatus(200); | 165 webRequest.response.setResponseStatus(200); |
| 160 webRequest.response.setResponse( | 166 webRequest.response.setResponse( |
| 161 "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); | 167 "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |
| 162 | 168 |
| 163 appInfo = AppInfo | 169 appInfo = AppInfo |
| 164 .builder() | 170 .builder() |
| 165 .setName("1") | 171 .setName("1") |
| 166 .setVersion("3") | 172 .setVersion("3") |
| 167 .setApplication("4") | 173 .setApplication("4") |
| 168 .setApplicationVersion("2") | 174 .setApplicationVersion("2") |
| 169 .setDevelopmentBuild(true) | 175 .setDevelopmentBuild(true) |
| 170 .build(); | 176 .build(); |
| 171 | 177 |
| 172 reset(); | 178 reset(); |
| 173 forceUpdateCheck(); | 179 forceUpdateCheck(); |
| 174 | 180 |
| 175 Thread.sleep(1000); | 181 Thread.sleep(1000); |
| 176 | 182 |
| 177 assertTrue(eventCallbackCalled); | 183 assertTrue(eventCallbackCalled); |
| 178 assertNotNull(eventCallbackParams); | 184 assertNotNull(eventCallbackParams); |
| 179 assertEquals(1l, eventCallbackParams.size()); | 185 assertEquals(1l, eventCallbackParams.size()); |
| 180 assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); | 186 assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); |
| 181 assertTrue(updateCallbackCalled); | 187 assertTrue(updateCallbackCalled); |
| 182 assertEquals("", updateError); | 188 assertEquals("", updateError); |
| 183 } | 189 } |
| 184 | 190 |
| 185 @Test | 191 @Test |
| 186 public void testWrongApplication() throws InterruptedException | 192 public void testWrongApplication() throws InterruptedException, IOException |
| 187 { | 193 { |
| 188 webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 194 webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
| 189 webRequest.response.setResponseStatus(200); | 195 webRequest.response.setResponseStatus(200); |
| 190 webRequest.response.setResponse( | 196 webRequest.response.setResponse( |
| 191 "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); | 197 "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |
| 192 | 198 |
| 193 appInfo = AppInfo | 199 appInfo = AppInfo |
| 194 .builder() | 200 .builder() |
| 195 .setName("1") | 201 .setName("1") |
| 196 .setVersion("3") | 202 .setVersion("3") |
| 197 .setApplication("4") | 203 .setApplication("4") |
| 198 .setApplicationVersion("2") | 204 .setApplicationVersion("2") |
| 199 .setDevelopmentBuild(true) | 205 .setDevelopmentBuild(true) |
| 200 .build(); | 206 .build(); |
| 201 | 207 |
| 202 reset(); | 208 reset(); |
| 203 forceUpdateCheck(); | 209 forceUpdateCheck(); |
| 204 | 210 |
| 205 Thread.sleep(1000); | 211 Thread.sleep(1000); |
| 206 | 212 |
| 207 assertFalse(eventCallbackCalled); | 213 assertFalse(eventCallbackCalled); |
| 208 assertTrue(updateCallbackCalled); | 214 assertTrue(updateCallbackCalled); |
| 209 assertEquals("", updateError); | 215 assertEquals("", updateError); |
| 210 } | 216 } |
| 211 | 217 |
| 212 @Test | 218 @Test |
| 213 public void testWrongVersion() throws InterruptedException | 219 public void testWrongVersion() throws InterruptedException, IOException |
| 214 { | 220 { |
| 215 webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 221 webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
| 216 webRequest.response.setResponseStatus(200); | 222 webRequest.response.setResponseStatus(200); |
| 217 webRequest.response.setResponse( | 223 webRequest.response.setResponse( |
| 218 "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); | 224 "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); |
| 219 | 225 |
| 220 appInfo = AppInfo | 226 appInfo = AppInfo |
| 221 .builder() | 227 .builder() |
| 222 .setName("1") | 228 .setName("1") |
| 223 .setVersion("3") | 229 .setVersion("3") |
| 224 .setApplication("4") | 230 .setApplication("4") |
| 225 .setApplicationVersion("2") | 231 .setApplicationVersion("2") |
| 226 .setDevelopmentBuild(true) | 232 .setDevelopmentBuild(true) |
| 227 .build(); | 233 .build(); |
| 228 | 234 |
| 229 reset(); | 235 reset(); |
| 230 forceUpdateCheck(); | 236 forceUpdateCheck(); |
| 231 | 237 |
| 232 Thread.sleep(1000); | 238 Thread.sleep(1000); |
| 233 | 239 |
| 234 assertFalse(eventCallbackCalled); | 240 assertFalse(eventCallbackCalled); |
| 235 assertTrue(updateCallbackCalled); | 241 assertTrue(updateCallbackCalled); |
| 236 assertEquals("", updateError); | 242 assertEquals("", updateError); |
| 237 } | 243 } |
| 238 | 244 |
| 239 @Test | 245 @Test |
| 240 public void testWrongURL() throws InterruptedException | 246 public void testWrongURL() throws InterruptedException, IOException |
| 241 { | 247 { |
| 242 webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 248 webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
| 243 webRequest.response.setResponseStatus(200); | 249 webRequest.response.setResponseStatus(200); |
| 244 webRequest.response.setResponse( | 250 webRequest.response.setResponse( |
| 245 "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); | 251 "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); |
| 246 | 252 |
| 247 appInfo = AppInfo | 253 appInfo = AppInfo |
| 248 .builder() | 254 .builder() |
| 249 .setName("1") | 255 .setName("1") |
| 250 .setVersion("3") | 256 .setVersion("3") |
| 251 .setApplication("4") | 257 .setApplication("4") |
| 252 .setApplicationVersion("2") | 258 .setApplicationVersion("2") |
| 253 .setDevelopmentBuild(true) | 259 .setDevelopmentBuild(true) |
| 254 .build(); | 260 .build(); |
| 255 | 261 |
| 256 reset(); | 262 reset(); |
| 257 forceUpdateCheck(); | 263 forceUpdateCheck(); |
| 258 | 264 |
| 259 Thread.sleep(1000); | 265 Thread.sleep(1000); |
| 260 | 266 |
| 261 assertFalse(eventCallbackCalled); | 267 assertFalse(eventCallbackCalled); |
| 262 assertTrue(updateCallbackCalled); | 268 assertTrue(updateCallbackCalled); |
| 263 assertTrue(updateError.length() > 0); | 269 assertTrue(updateError.length() > 0); |
| 264 } | 270 } |
| 265 } | 271 } |
| OLD | NEW |