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

Side by Side Diff: test/FilterEngine.cpp

Issue 29371607: Issue #3593 - Make isolate a fully internal member of the engine
Patch Set: improve unit tests to go with isolate change Created Jan. 16, 2017, 3:53 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
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | test/JsEngine.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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-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 "BaseJsTest.h" 18 #include "BaseJsTest.h"
19 #include <thread> 19 #include <thread>
20 #include "../src/JsEngineTransition.h"
20 21
21 using namespace AdblockPlus; 22 using namespace AdblockPlus;
22 23
23 namespace 24 namespace
24 { 25 {
25 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; 26 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
26 27
27 class VeryLazyFileSystem : public LazyFileSystem 28 class VeryLazyFileSystem : public LazyFileSystem
28 { 29 {
29 public: 30 public:
30 StatResult Stat(const std::string& path) const 31 StatResult Stat(const std::string& path) const
31 { 32 {
32 return StatResult(); 33 return StatResult();
33 } 34 }
34 }; 35 };
35 36
36 template<class FileSystem, class LogSystem> 37 template<class FileSystem, class LogSystem>
37 class FilterEngineTestGeneric : public BaseJsTest 38 class FilterEngineTestGeneric : public BaseJsTest
38 { 39 {
39 40
40 protected: 41 protected:
41 FilterEnginePtr filterEngine; 42 FilterEnginePtr filterEngine;
42 std::shared_ptr<LazyWebRequest> webRequest; 43 std::shared_ptr<LazyWebRequest> webRequest;
43 44
44 void SetUp() 45 void SetUp()
45 { 46 {
46 BaseJsTest::SetUp(); 47 BaseJsTest::SetUp();
47 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem)); 48 jsEngine->SetFileSystem(std::make_shared<FileSystem>());
48 webRequest = std::make_shared<LazyWebRequest>(); 49 webRequest = std::make_shared<LazyWebRequest>();
49 jsEngine->SetWebRequest(webRequest); 50 jsEngine->SetWebRequest(webRequest);
50 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem)); 51 jsEngine->SetLogSystem(std::make_shared<LogSystem>());
51 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); 52 filterEngine = std::make_shared<AdblockPlus::FilterEngine>(jsEngine);
52 } 53 }
53 54
54 void TearDown() 55 void TearDown()
55 { 56 {
56 if (webRequest) 57 if (webRequest)
57 { 58 {
58 webRequest->Cancel(); 59 webRequest->Cancel();
59 } 60 }
60 webRequest.reset(); 61 webRequest.reset();
61 filterEngine.reset(); 62 filterEngine.reset();
(...skipping 25 matching lines...) Expand all
87 public: 88 public:
88 AdblockPlus::ServerResponse response; 89 AdblockPlus::ServerResponse response;
89 90
90 AdblockPlus::ServerResponse GET(const std::string& url, 91 AdblockPlus::ServerResponse GET(const std::string& url,
91 const AdblockPlus::HeaderList& requestHeaders) const 92 const AdblockPlus::HeaderList& requestHeaders) const
92 { 93 {
93 return response; 94 return response;
94 } 95 }
95 }; 96 };
96 97
97 MockWebRequest* mockWebRequest; 98 AdblockPlus::JsEnginePtr jsEngine;
99 std::shared_ptr<MockWebRequest> mockWebRequest;
98 FilterEnginePtr filterEngine; 100 FilterEnginePtr filterEngine;
99 101
100 void SetUp() 102 void SetUp()
101 { 103 {
102 AdblockPlus::AppInfo appInfo; 104 AdblockPlus::AppInfo appInfo;
103 appInfo.name = "test"; 105 appInfo.name = "test";
104 appInfo.version = "1.0.1"; 106 appInfo.version = "1.0.1";
105 AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(appInfo); 107 jsEngine = CreateJsEngine(appInfo);
106 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); 108 jsEngine->SetFileSystem(std::make_shared<LazyFileSystem>());
107 mockWebRequest = new MockWebRequest; 109 mockWebRequest = std::make_shared<MockWebRequest>();
108 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(mockWebRequest)); 110 jsEngine->SetWebRequest(mockWebRequest);
109 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); 111 filterEngine = std::make_shared<FilterEngine>(jsEngine);
112 }
113
114 void TearDown()
115 {
116 filterEngine.reset();
117 mockWebRequest.reset();
118 ToInternal(jsEngine)->WaitForQuietScheduler();
119 ASSERT_EQ(1, jsEngine.use_count());
120 jsEngine.reset();
110 } 121 }
111 }; 122 };
112 123
113 struct MockUpdateAvailableCallback 124 struct MockUpdateAvailableCallback
114 { 125 {
115 MockUpdateAvailableCallback(int& timesCalled) : timesCalled(timesCalled) {} 126 MockUpdateAvailableCallback(int& timesCalled) : timesCalled(timesCalled) {}
116 127
117 void operator()(const std::string&) 128 void operator()(const std::string&)
118 { 129 {
119 timesCalled++; 130 timesCalled++;
120 } 131 }
121 132
122 private: 133 private:
123 // We currently cannot store timesCalled in the functor, see: 134 // We currently cannot store timesCalled in the functor, see:
124 // https://issues.adblockplus.org/ticket/1378. 135 // https://issues.adblockplus.org/ticket/1378.
125 int& timesCalled; 136 int& timesCalled;
126 }; 137 };
127 138
128 // Workaround for https://issues.adblockplus.org/ticket/1397. 139 // Workaround for https://issues.adblockplus.org/ticket/1397.
129 void NoOpUpdaterCallback(const std::string&) {} 140 void NoOpUpdaterCallback(const std::string&) {}
130 141
131 class FilterEngineWithFreshFolder : public ::testing::Test 142 class FilterEngineWithFreshFolder : public ::testing::Test
132 { 143 {
133 protected: 144 protected:
134 FileSystemPtr fileSystem; 145 FileSystemPtr fileSystem;
135 std::weak_ptr<JsEngine> weakJsEngine; 146 std::shared_ptr<LazyWebRequest> webRequest;
147 AdblockPlus::JsEnginePtr jsEngine;
136 148
137 void SetUp() override 149 void SetUp() override
138 { 150 {
139 fileSystem.reset(new DefaultFileSystem()); 151 fileSystem = std::make_shared<DefaultFileSystem>();
152 webRequest = std::make_shared<LazyWebRequest>();
140 // Since there is neither in memory FS nor functionality to work with 153 // Since there is neither in memory FS nor functionality to work with
141 // directories use the hack: manually clean the directory. 154 // directories use the hack: manually clean the directory.
142 removeFileIfExists("patterns.ini"); 155 removeFileIfExists("patterns.ini");
143 removeFileIfExists("prefs.json"); 156 removeFileIfExists("prefs.json");
144 } 157 }
145 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) 158
159 void createJsEngine(const AppInfo& appInfo = AppInfo())
146 { 160 {
147 auto jsEngine = JsEngine::New(appInfo); 161 jsEngine = JsEngine::New(appInfo);
148 weakJsEngine = jsEngine;
149 jsEngine->SetFileSystem(fileSystem); 162 jsEngine->SetFileSystem(fileSystem);
150 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); 163 jsEngine->SetWebRequest(webRequest);
151 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); 164 jsEngine->SetLogSystem(std::make_shared<LazyLogSystem>());
152 return jsEngine;
153 } 165 }
166
154 void TearDown() override 167 void TearDown() override
155 { 168 {
169 if (webRequest)
170 {
171 webRequest->Cancel();
172 }
156 removeFileIfExists("patterns.ini"); 173 removeFileIfExists("patterns.ini");
157 removeFileIfExists("prefs.json"); 174 removeFileIfExists("prefs.json");
158 fileSystem.reset(); 175 fileSystem.reset();
176 ToInternal(jsEngine)->WaitForQuietScheduler();
177 ASSERT_EQ(1, jsEngine.use_count());
178 jsEngine.reset();
159 } 179 }
180
160 void removeFileIfExists(const std::string& path) 181 void removeFileIfExists(const std::string& path)
161 { 182 {
162 // Hack: allow IO to finish currently running operations, in particular 183 // Hack: allow IO to finish currently running operations, in particular
163 // writing into files. Otherwise we get "Permission denied". 184 // writing into files. Otherwise we get "Permission denied".
164 auto safeRemove = [this, &path]()->bool 185 auto safeRemove = [this, &path]()->bool
165 { 186 {
166 try 187 try
167 { 188 {
168 if (fileSystem->Stat(path).exists) 189 if (fileSystem->Stat(path).exists)
169 fileSystem->Remove(path); 190 fileSystem->Remove(path);
170 return true; 191 return true;
171 } 192 }
172 catch (...) 193 catch (...)
173 { 194 {
174 return false; 195 return false;
175 } 196 }
176 }; 197 };
177 int i = 5; 198 int i = 5;
178 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove()) 199 while ((i-- > 0) || !safeRemove())
179 std::this_thread::sleep_for(std::chrono::seconds(2)); 200 std::this_thread::sleep_for(std::chrono::seconds(2));
180 } 201 }
181 }; 202 };
182 } 203 }
183 204
184 TEST_F(FilterEngineTest, FilterCreation) 205 TEST_F(FilterEngineTest, FilterCreation)
185 { 206 {
186 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); 207 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo");
187 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); 208 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType());
188 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); 209 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo");
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( 622 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted(
602 "http://example.co.uk", 623 "http://example.co.uk",
603 documentUrls1)); 624 documentUrls1));
604 } 625 }
605 626
606 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) 627 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
607 { 628 {
608 AppInfo appInfo; 629 AppInfo appInfo;
609 appInfo.locale = "zh"; 630 appInfo.locale = "zh";
610 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt"; 631 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt";
611 auto jsEngine = createJsEngine(appInfo); 632 createJsEngine(appInfo);
612 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); 633 auto filterEngine = std::make_shared<AdblockPlus::FilterEngine>(jsEngine);
613 const auto subscriptions = filterEngine->GetListedSubscriptions(); 634 auto subscriptions = filterEngine->GetListedSubscriptions();
614 ASSERT_EQ(2u, subscriptions.size()); 635 ASSERT_EQ(2u, subscriptions.size());
615 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing(); 636 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing();
616 SubscriptionPtr aaSubscription; 637 SubscriptionPtr aaSubscription;
617 SubscriptionPtr langSubscription; 638 SubscriptionPtr langSubscription;
618 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) 639 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl)
619 { 640 {
620 aaSubscription = subscriptions[0]; 641 aaSubscription = subscriptions[0];
621 langSubscription = subscriptions[1]; 642 langSubscription = subscriptions[1];
622 } 643 }
623 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl) 644 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl)
624 { 645 {
625 aaSubscription = subscriptions[1]; 646 aaSubscription = subscriptions[1];
626 langSubscription = subscriptions[0]; 647 langSubscription = subscriptions[0];
627 } 648 }
628 ASSERT_NE(nullptr, aaSubscription); 649 ASSERT_NE(nullptr, aaSubscription);
629 ASSERT_NE(nullptr, langSubscription); 650 ASSERT_NE(nullptr, langSubscription);
630 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString()); 651 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString());
631 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( )); 652 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( ));
632 } 653 }
633 654
634 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 655 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
635 { 656 {
636 auto jsEngine = createJsEngine(); 657 createJsEngine();
637 FilterEngine::Prefs preSettings; 658 FilterEngine::Prefs preSettings;
638 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(false); 659 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(false);
639 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr eSettings)); 660 auto filterEngine = std::make_shared<AdblockPlus::FilterEngine>(jsEngine, preS ettings);
640 const auto subscriptions = filterEngine->GetListedSubscriptions(); 661 const auto subscriptions = filterEngine->GetListedSubscriptions();
641 EXPECT_EQ(0u, subscriptions.size()); 662 EXPECT_EQ(0u, subscriptions.size());
642 } 663 }
OLDNEW
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | test/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld