| 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 | 
|   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 #include <functional> |   18 #include <functional> | 
|   19  |   19  | 
|   20 #include "BaseJsTest.h" |   20 #include "BaseJsTest.h" | 
 |   21 #include "../src/JsEngineTransition.h" | 
|   21  |   22  | 
|   22 namespace |   23 namespace | 
|   23 { |   24 { | 
|   24   typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; |   25   typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; | 
|   25  |   26  | 
|   26   void FindAndReplace(std::string& source, const std::string& find, const std::s
     tring& replace) |   27   void FindAndReplace(std::string& source, const std::string& find, const std::s
     tring& replace) | 
|   27   { |   28   { | 
|   28     for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po
     s += replace.size()) |   29     for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po
     s += replace.size()) | 
|   29       source.replace(pos, find.size(), replace); |   30       source.replace(pos, find.size(), replace); | 
|   30   } |   31   } | 
|   31  |   32  | 
|   32   std::string previousRequestUrl; |   33   std::string previousRequestUrl; | 
|   33   class TestWebRequest : public LazyWebRequest |   34   class TestWebRequest : public LazyWebRequest | 
|   34   { |   35   { | 
|   35   public: |   36   public: | 
|   36     AdblockPlus::ServerResponse response; |   37     AdblockPlus::ServerResponse response; | 
|   37     AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H
     eaderList& requestHeaders) const |   38     AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H
     eaderList& requestHeaders) const | 
|   38     { |   39     { | 
|   39       if (url.find("easylist") != std::string::npos) |   40       if (url.find("easylist") != std::string::npos) | 
|   40         return LazyWebRequest::GET(url, requestHeaders); |   41         return LazyWebRequest::GET(url, requestHeaders); | 
|   41  |   42  | 
|   42       previousRequestUrl = url; |   43       previousRequestUrl = url; | 
|   43       return response; |   44       return response; | 
|   44     } |   45     } | 
|   45   }; |   46   }; | 
|   46  |   47  | 
|   47   class UpdateCheckTest : public ::testing::Test |   48   class UpdateCheckTest : public ::testing::Test | 
|   48   { |   49   { | 
 |   50     void Construct() | 
 |   51     { | 
 |   52       jsEngine = CreateJsEngine(appInfo); | 
 |   53       jsEngine->SetLogSystem(std::make_shared<LazyLogSystem>()); | 
 |   54       jsEngine->SetFileSystem(std::make_shared<LazyFileSystem>()); | 
 |   55       jsEngine->SetWebRequest(webRequest); | 
 |   56       jsEngine->SetEventCallback("updateAvailable", | 
 |   57         std::bind(&UpdateCheckTest::EventCallback, this, std::placeholders::_1))
     ; | 
 |   58  | 
 |   59       filterEngine = std::make_shared<AdblockPlus::FilterEngine>(jsEngine); | 
 |   60     } | 
 |   61  | 
 |   62     void Destroy() | 
 |   63     { | 
 |   64       webRequest->Cancel(); | 
 |   65       filterEngine.reset(); | 
 |   66       ToInternal(jsEngine)->WaitForQuietScheduler(); | 
 |   67       jsEngine.reset(); | 
 |   68       ASSERT_EQ(0, jsEngine.use_count()); | 
 |   69     } | 
 |   70  | 
|   49   protected: |   71   protected: | 
|   50     AdblockPlus::AppInfo appInfo; |   72     AdblockPlus::AppInfo appInfo; | 
|   51     TestWebRequest* webRequest; |   73     std::shared_ptr<TestWebRequest> webRequest; | 
|   52     AdblockPlus::WebRequestPtr webRequestPtr; |  | 
|   53     AdblockPlus::JsEnginePtr jsEngine; |   74     AdblockPlus::JsEnginePtr jsEngine; | 
|   54     FilterEnginePtr filterEngine; |   75     FilterEnginePtr filterEngine; | 
|   55  |   76  | 
|   56     bool eventCallbackCalled; |   77     bool eventCallbackCalled; | 
|   57     AdblockPlus::JsValueList eventCallbackParams; |   78     AdblockPlus::JsValueList eventCallbackParams; | 
|   58     bool updateCallbackCalled; |   79     bool updateCallbackCalled; | 
|   59     std::string updateError; |   80     std::string updateError; | 
|   60  |   81  | 
|   61     void SetUp() |   82     void SetUp() | 
|   62     { |   83     { | 
|   63       webRequest = new TestWebRequest(); |   84       webRequest = std::make_shared<TestWebRequest>(); | 
|   64       webRequestPtr.reset(webRequest); |  | 
|   65  |  | 
|   66       eventCallbackCalled = false; |   85       eventCallbackCalled = false; | 
|   67       updateCallbackCalled = false; |   86       updateCallbackCalled = false; | 
|   68       Reset(); |   87       Construct(); | 
|   69     } |   88     } | 
|   70  |   89  | 
|   71     void TearDown() |   90     void TearDown() | 
|   72     { |   91     { | 
|   73       webRequest->Cancel(); |   92       Destroy(); | 
 |   93       webRequest.reset(); | 
|   74     } |   94     } | 
|   75  |   95  | 
|   76     void Reset() |   96     void Reset() | 
|   77     { |   97     { | 
|   78       jsEngine = CreateJsEngine(appInfo); |   98       Destroy(); | 
|   79       jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem)); |   99       Construct(); | 
|   80       jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); |  | 
|   81       jsEngine->SetWebRequest(webRequestPtr); |  | 
|   82       jsEngine->SetEventCallback("updateAvailable", |  | 
|   83           std::bind(&UpdateCheckTest::EventCallback, this, std::placeholders::_1
     )); |  | 
|   84  |  | 
|   85       filterEngine.reset(new AdblockPlus::FilterEngine(jsEngine)); |  | 
|   86     } |  100     } | 
|   87  |  101  | 
|   88     void ForceUpdateCheck() |  102     void ForceUpdateCheck() | 
|   89     { |  103     { | 
|   90       filterEngine->ForceUpdateCheck( |  104       filterEngine->ForceUpdateCheck( | 
|   91           std::bind(&UpdateCheckTest::UpdateCallback, this, std::placeholders::_
     1)); |  105           std::bind(&UpdateCheckTest::UpdateCallback, this, std::placeholders::_
     1)); | 
|   92     } |  106     } | 
|   93  |  107  | 
|   94     void EventCallback(AdblockPlus::JsValueList& params) |  108     void EventCallback(AdblockPlus::JsValueList& params) | 
|   95     { |  109     { | 
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  261  |  275  | 
|  262   Reset(); |  276   Reset(); | 
|  263   ForceUpdateCheck(); |  277   ForceUpdateCheck(); | 
|  264  |  278  | 
|  265   std::this_thread::sleep_for(std::chrono::milliseconds(100)); |  279   std::this_thread::sleep_for(std::chrono::milliseconds(100)); | 
|  266  |  280  | 
|  267   ASSERT_FALSE(eventCallbackCalled); |  281   ASSERT_FALSE(eventCallbackCalled); | 
|  268   ASSERT_TRUE(updateCallbackCalled); |  282   ASSERT_TRUE(updateCallbackCalled); | 
|  269   ASSERT_FALSE(updateError.empty()); |  283   ASSERT_FALSE(updateError.empty()); | 
|  270 } |  284 } | 
| OLD | NEW |