| 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-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; | 
 |   21  | 
|   20 import org.adblockplus.libadblockplus.AppInfo; |   22 import org.adblockplus.libadblockplus.AppInfo; | 
 |   23 import org.adblockplus.libadblockplus.BaseFilterEngineTest; | 
|   21 import org.adblockplus.libadblockplus.EventCallback; |   24 import org.adblockplus.libadblockplus.EventCallback; | 
|   22 import org.adblockplus.libadblockplus.HeaderEntry; |   25 import org.adblockplus.libadblockplus.HeaderEntry; | 
|   23 import org.adblockplus.libadblockplus.JsValue; |   26 import org.adblockplus.libadblockplus.JsValue; | 
|   24 import org.adblockplus.libadblockplus.LazyLogSystem; |  | 
|   25 import org.adblockplus.libadblockplus.LazyWebRequest; |  | 
|   26 import org.adblockplus.libadblockplus.Platform; |  | 
|   27 import org.adblockplus.libadblockplus.ServerResponse; |   27 import org.adblockplus.libadblockplus.ServerResponse; | 
 |   28 import org.adblockplus.libadblockplus.ThrowingWebRequest; | 
|   28 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; |   29 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; | 
|   29  |   30  | 
|   30 import org.junit.Test; |   31 import org.junit.Test; | 
|   31  |   32  | 
|   32 import java.util.List; |   33 import java.util.List; | 
|   33  |   34  | 
|   34 public class UpdateCheckTest extends BaseFilterEngineTest |   35 public class UpdateCheckTest extends BaseFilterEngineTest | 
|   35 { |   36 { | 
|   36   protected String previousRequestUrl; |   37   protected String previousRequestUrl; | 
|   37  |   38  | 
|   38   public class TestWebRequest extends LazyWebRequest |   39   public class TestWebRequest extends ThrowingWebRequest | 
|   39   { |   40   { | 
|   40     public ServerResponse response = new ServerResponse(); |   41     public ServerResponse response = new ServerResponse(); | 
|   41  |   42  | 
|   42     @Override |   43     @Override | 
|   43     public ServerResponse httpGET(String url, List<HeaderEntry> headers) |   44     public ServerResponse httpGET(String url, List<HeaderEntry> headers) | 
|   44     { |   45     { | 
|   45       if (url.indexOf("easylist") >= 0) |   46       if (url.indexOf("easylist") >= 0) | 
|   46       { |   47       { | 
|   47         return super.httpGET(url, headers); |   48         return super.httpGET(url, headers); | 
|   48       } |   49       } | 
|   49  |   50  | 
|   50       previousRequestUrl = url; |   51       previousRequestUrl = url; | 
|   51       return response; |   52       return response; | 
|   52     } |   53     } | 
|   53   } |   54   } | 
|   54  |   55  | 
|   55   protected AppInfo appInfo; |  | 
|   56   protected TestWebRequest webRequest; |   56   protected TestWebRequest webRequest; | 
|   57  |   57  | 
|   58   protected boolean eventCallbackCalled; |   58   protected boolean eventCallbackCalled; | 
|   59   protected List<JsValue> eventCallbackParams; |   59   protected List<JsValue> eventCallbackParams; | 
|   60   protected boolean updateCallbackCalled; |   60   protected boolean updateCallbackCalled; | 
|   61   protected String updateError; |   61   protected String updateError; | 
|   62  |   62  | 
|   63   private EventCallback eventCallback = new EventCallback() |   63   private EventCallback eventCallback = new EventCallback() | 
|   64   { |   64   { | 
|   65     @Override |   65     @Override | 
|   66     public void eventCallback(List<JsValue> params) |   66     public void eventCallback(List<JsValue> params) | 
|   67     { |   67     { | 
|   68       eventCallbackCalled = true; |   68       eventCallbackCalled = true; | 
|   69       eventCallbackParams = params; |   69       eventCallbackParams = params; | 
|   70     } |   70     } | 
|   71   }; |   71   }; | 
|   72  |   72  | 
|   73   private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback() |   73   private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback() | 
|   74   { |   74   { | 
|   75     @Override |   75     @Override | 
|   76     public void updateCheckDoneCallback(String error) |   76     public void updateCheckDoneCallback(String error) | 
|   77     { |   77     { | 
|   78       updateCallbackCalled = true; |   78       updateCallbackCalled = true; | 
|   79       updateError = error; |   79       updateError = error; | 
|   80     } |   80     } | 
|   81   }; |   81   }; | 
|   82  |   82  | 
|   83   public void reset() throws InterruptedException |   83   public void reset() | 
|   84   { |   84   { | 
|   85     disposeFilterEngine(); |   85     disposeEngines(); | 
|   86     if (platform != null) |   86     setupJsEngine(); | 
|   87     { |   87     jsEngine.setEventCallback("updateAvailable", eventCallback); | 
|   88       platform.dispose(); |   88     setupFilterEngine(); | 
|   89     } |  | 
|   90     platform = new Platform(new LazyLogSystem(), webRequest, |  | 
|   91         getContext().getFilesDir().getAbsolutePath()); |  | 
|   92     platform.setUpJsEngine(appInfo); |  | 
|   93     platform.getJsEngine().setEventCallback("updateAvailable", eventCallback); |  | 
|   94     filterEngine = platform.getFilterEngine(); |  | 
|   95   } |   89   } | 
|   96  |   90  | 
|   97   @Override |   91   @Override | 
|   98   protected void setUp() throws Exception |   92   protected void setUp() throws Exception | 
|   99   { |   93   { | 
|  100     appInfo = AppInfo.builder().build(); |  | 
|  101     webRequest = new TestWebRequest(); |   94     webRequest = new TestWebRequest(); | 
 |   95     setWebRequest(webRequest); | 
|  102     eventCallbackCalled = false; |   96     eventCallbackCalled = false; | 
|  103     updateCallbackCalled = false; |   97     updateCallbackCalled = false; | 
|  104     reset(); |   98     super.setUp(); | 
|  105   } |   99   } | 
|  106  |  100  | 
|  107   public void forceUpdateCheck() |  101   public void forceUpdateCheck() | 
|  108   { |  102   { | 
|  109     filterEngine.forceUpdateCheck(updateCallback); |  103     filterEngine.forceUpdateCheck(updateCallback); | 
 |  104     for (int i = 0; i < UPDATE_SUBSCRIPTIONS_WAIT_CHUNKS; i++) | 
 |  105     { | 
 |  106       if (eventCallbackCalled || updateCallbackCalled) | 
 |  107       { | 
 |  108         break; | 
 |  109       } | 
 |  110       SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS / UPDATE_SUBSCRIPTION
     S_WAIT_CHUNKS); | 
 |  111     } | 
|  110   } |  112   } | 
|  111  |  113  | 
|  112   @Test |  114   @Test | 
|  113   public void testRequestFailure() throws InterruptedException |  115   public void testRequestFailure() | 
|  114   { |  116   { | 
|  115     webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); |  117     webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); | 
|  116  |  118  | 
|  117     appInfo = AppInfo |  119     AppInfo appInfo = AppInfo | 
|  118       .builder() |  120       .builder() | 
|  119       .setName("1") |  121       .setName("1") | 
|  120       .setVersion("3") |  122       .setVersion("3") | 
|  121       .setApplication("4") |  123       .setApplication("4") | 
|  122       .setApplicationVersion("2") |  124       .setApplicationVersion("2") | 
|  123       .setDevelopmentBuild(false) |  125       .setDevelopmentBuild(false) | 
|  124       .build(); |  126       .build(); | 
 |  127     setAppInfo(appInfo); | 
|  125  |  128  | 
|  126     reset(); |  129     reset(); | 
|  127     forceUpdateCheck(); |  130     forceUpdateCheck(); | 
|  128  |  131  | 
|  129     Thread.sleep(100); |  | 
|  130  |  | 
|  131     assertFalse(eventCallbackCalled); |  132     assertFalse(eventCallbackCalled); | 
|  132     assertTrue(updateCallbackCalled); |  133     assertTrue(updateCallbackCalled); | 
|  133     assertNotNull(updateError); |  134     assertNotNull(updateError); | 
|  134  |  135  | 
|  135     String expectedUrl = filterEngine.getPref("update_url_release").asString(); |  136     String expectedUrl = filterEngine.getPref("update_url_release").asString(); | 
|  136     String platform = "libadblockplus"; |  137     String platform = "libadblockplus"; | 
|  137     String platformVersion = "1.0"; |  138     String platformVersion = "1.0"; | 
|  138  |  139  | 
|  139     expectedUrl = expectedUrl |  140     expectedUrl = expectedUrl | 
|  140       .replaceAll("%NAME%", appInfo.name) |  141       .replaceAll("%NAME%", appInfo.name) | 
|  141       .replaceAll("%TYPE%", "1"); // manual update |  142       .replaceAll("%TYPE%", "1"); // manual update | 
|  142  |  143  | 
|  143     expectedUrl += |  144     expectedUrl += | 
|  144       "&addonName=" + appInfo.name + |  145       "&addonName=" + appInfo.name + | 
|  145       "&addonVersion=" + appInfo.version + |  146       "&addonVersion=" + appInfo.version + | 
|  146       "&application=" + appInfo.application + |  147       "&application=" + appInfo.application + | 
|  147       "&applicationVersion=" + appInfo.applicationVersion + |  148       "&applicationVersion=" + appInfo.applicationVersion + | 
|  148       "&platform=" + platform + |  149       "&platform=" + platform + | 
|  149       "&platformVersion=" + platformVersion + |  150       "&platformVersion=" + platformVersion + | 
|  150       "&lastVersion=0&downloadCount=0"; |  151       "&lastVersion=0&downloadCount=0"; | 
|  151  |  152  | 
|  152     assertEquals(expectedUrl, previousRequestUrl); |  153     assertEquals(expectedUrl, previousRequestUrl); | 
|  153   } |  154   } | 
|  154  |  155  | 
|  155   @Test |  156   @Test | 
|  156   public void testApplicationUpdateAvailable() throws InterruptedException |  157   public void testApplicationUpdateAvailable() | 
|  157   { |  158   { | 
|  158     webRequest.response.setStatus(ServerResponse.NsStatus.OK); |  159     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  159     webRequest.response.setResponseStatus(200); |  160     webRequest.response.setResponseStatus(200); | 
|  160     webRequest.response.setResponse( |  161     webRequest.response.setResponse( | 
|  161       "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |  162       "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); | 
|  162  |  163  | 
|  163     appInfo = AppInfo |  164     AppInfo appInfo = AppInfo | 
|  164       .builder() |  165       .builder() | 
|  165       .setName("1") |  166       .setName("1") | 
|  166       .setVersion("3") |  167       .setVersion("3") | 
|  167       .setApplication("4") |  168       .setApplication("4") | 
|  168       .setApplicationVersion("2") |  169       .setApplicationVersion("2") | 
|  169       .setDevelopmentBuild(true) |  170       .setDevelopmentBuild(true) | 
|  170       .build(); |  171       .build(); | 
 |  172     setAppInfo(appInfo); | 
|  171  |  173  | 
|  172     reset(); |  174     reset(); | 
|  173     forceUpdateCheck(); |  175     forceUpdateCheck(); | 
|  174  |  176  | 
|  175     Thread.sleep(1000); |  | 
|  176  |  | 
|  177     assertTrue(eventCallbackCalled); |  177     assertTrue(eventCallbackCalled); | 
|  178     assertNotNull(eventCallbackParams); |  178     assertNotNull(eventCallbackParams); | 
|  179     assertEquals(1l, eventCallbackParams.size()); |  179     assertEquals(1l, eventCallbackParams.size()); | 
|  180     assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); |  180     assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); | 
|  181     assertTrue(updateCallbackCalled); |  181     assertTrue(updateCallbackCalled); | 
|  182     assertEquals("", updateError); |  182     assertEquals("", updateError); | 
|  183   } |  183   } | 
|  184  |  184  | 
|  185   @Test |  185   @Test | 
|  186   public void testWrongApplication() throws InterruptedException |  186   public void testWrongApplication() | 
|  187   { |  187   { | 
|  188     webRequest.response.setStatus(ServerResponse.NsStatus.OK); |  188     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  189     webRequest.response.setResponseStatus(200); |  189     webRequest.response.setResponseStatus(200); | 
|  190     webRequest.response.setResponse( |  190     webRequest.response.setResponse( | 
|  191       "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |  191       "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); | 
|  192  |  192  | 
|  193     appInfo = AppInfo |  193     AppInfo appInfo = AppInfo | 
|  194       .builder() |  194       .builder() | 
|  195       .setName("1") |  195       .setName("1") | 
|  196       .setVersion("3") |  196       .setVersion("3") | 
|  197       .setApplication("4") |  197       .setApplication("4") | 
|  198       .setApplicationVersion("2") |  198       .setApplicationVersion("2") | 
|  199       .setDevelopmentBuild(true) |  199       .setDevelopmentBuild(true) | 
|  200       .build(); |  200       .build(); | 
 |  201     setAppInfo(appInfo); | 
|  201  |  202  | 
|  202     reset(); |  203     reset(); | 
|  203     forceUpdateCheck(); |  204     forceUpdateCheck(); | 
|  204  |  205  | 
|  205     Thread.sleep(1000); |  | 
|  206  |  | 
|  207     assertFalse(eventCallbackCalled); |  206     assertFalse(eventCallbackCalled); | 
|  208     assertTrue(updateCallbackCalled); |  207     assertTrue(updateCallbackCalled); | 
|  209     assertEquals("", updateError); |  208     assertEquals("", updateError); | 
|  210   } |  209   } | 
|  211  |  210  | 
|  212   @Test |  211   @Test | 
|  213   public void testWrongVersion() throws InterruptedException |  212   public void testWrongVersion() | 
|  214   { |  213   { | 
|  215     webRequest.response.setStatus(ServerResponse.NsStatus.OK); |  214     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  216     webRequest.response.setResponseStatus(200); |  215     webRequest.response.setResponseStatus(200); | 
|  217     webRequest.response.setResponse( |  216     webRequest.response.setResponse( | 
|  218       "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); |  217       "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); | 
|  219  |  218  | 
|  220     appInfo = AppInfo |  219     AppInfo appInfo = AppInfo | 
|  221       .builder() |  220       .builder() | 
|  222       .setName("1") |  221       .setName("1") | 
|  223       .setVersion("3") |  222       .setVersion("3") | 
|  224       .setApplication("4") |  223       .setApplication("4") | 
|  225       .setApplicationVersion("2") |  224       .setApplicationVersion("2") | 
|  226       .setDevelopmentBuild(true) |  225       .setDevelopmentBuild(true) | 
|  227       .build(); |  226       .build(); | 
 |  227     setAppInfo(appInfo); | 
|  228  |  228  | 
|  229     reset(); |  229     reset(); | 
|  230     forceUpdateCheck(); |  230     forceUpdateCheck(); | 
|  231  |  231  | 
|  232     Thread.sleep(1000); |  | 
|  233  |  | 
|  234     assertFalse(eventCallbackCalled); |  232     assertFalse(eventCallbackCalled); | 
|  235     assertTrue(updateCallbackCalled); |  233     assertTrue(updateCallbackCalled); | 
|  236     assertEquals("", updateError); |  234     assertEquals("", updateError); | 
|  237   } |  235   } | 
|  238  |  236  | 
|  239   @Test |  237   @Test | 
|  240   public void testWrongURL() throws InterruptedException |  238   public void testWrongURL() | 
|  241   { |  239   { | 
|  242     webRequest.response.setStatus(ServerResponse.NsStatus.OK); |  240     webRequest.response.setStatus(ServerResponse.NsStatus.OK); | 
|  243     webRequest.response.setResponseStatus(200); |  241     webRequest.response.setResponseStatus(200); | 
|  244     webRequest.response.setResponse( |  242     webRequest.response.setResponse( | 
|  245       "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); |  243       "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); | 
|  246  |  244  | 
|  247     appInfo = AppInfo |  245     AppInfo appInfo = AppInfo | 
|  248       .builder() |  246       .builder() | 
|  249       .setName("1") |  247       .setName("1") | 
|  250       .setVersion("3") |  248       .setVersion("3") | 
|  251       .setApplication("4") |  249       .setApplication("4") | 
|  252       .setApplicationVersion("2") |  250       .setApplicationVersion("2") | 
|  253       .setDevelopmentBuild(true) |  251       .setDevelopmentBuild(true) | 
|  254       .build(); |  252       .build(); | 
 |  253     setAppInfo(appInfo); | 
|  255  |  254  | 
|  256     reset(); |  255     reset(); | 
|  257     forceUpdateCheck(); |  256     forceUpdateCheck(); | 
|  258  |  257  | 
|  259     Thread.sleep(1000); |  | 
|  260  |  | 
|  261     assertFalse(eventCallbackCalled); |  258     assertFalse(eventCallbackCalled); | 
|  262     assertTrue(updateCallbackCalled); |  259     assertTrue(updateCallbackCalled); | 
|  263     assertTrue(updateError.length() > 0); |  260     assertTrue(updateError.length() > 0); | 
|  264   } |  261   } | 
|  265 } |  262 } | 
| OLD | NEW |