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

Side by Side Diff: test/FilterEngine.cpp

Issue 29377064: Issue 4944 - Make creation of FilterEngine asynchronous. (Closed)
Patch Set: Created Feb. 28, 2017, 2:21 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
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 20
21 using namespace AdblockPlus; 21 using namespace AdblockPlus;
22 22
23 namespace 23 namespace
24 { 24 {
25 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
26
27 class VeryLazyFileSystem : public LazyFileSystem 25 class VeryLazyFileSystem : public LazyFileSystem
28 { 26 {
29 public: 27 public:
30 StatResult Stat(const std::string& path) const 28 StatResult Stat(const std::string& path) const
31 { 29 {
32 return StatResult(); 30 return StatResult();
33 } 31 }
34 }; 32 };
35 33
36 template<class FileSystem, class LogSystem> 34 template<class FileSystem, class LogSystem>
37 class FilterEngineTestGeneric : public BaseJsTest 35 class FilterEngineTestGeneric : public BaseJsTest
38 { 36 {
39 protected: 37 protected:
40 FilterEnginePtr filterEngine; 38 FilterEnginePtr filterEngine;
41 39
42 void SetUp() 40 void SetUp()
43 { 41 {
44 BaseJsTest::SetUp(); 42 BaseJsTest::SetUp();
45 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem)); 43 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem));
46 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); 44 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest));
47 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem)); 45 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem));
48 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); 46 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
49 } 47 }
50 }; 48 };
51 49
52 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; 50 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest;
53 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData; 51 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData;
54 52
55 struct MockFilterChangeCallback 53 struct MockFilterChangeCallback
56 { 54 {
57 MockFilterChangeCallback(int& timesCalled) : timesCalled(timesCalled) {} 55 MockFilterChangeCallback(int& timesCalled) : timesCalled(timesCalled) {}
58 56
(...skipping 26 matching lines...) Expand all
85 83
86 void SetUp() 84 void SetUp()
87 { 85 {
88 AdblockPlus::AppInfo appInfo; 86 AdblockPlus::AppInfo appInfo;
89 appInfo.name = "test"; 87 appInfo.name = "test";
90 appInfo.version = "1.0.1"; 88 appInfo.version = "1.0.1";
91 AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(appInfo); 89 AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(appInfo);
92 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); 90 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem));
93 mockWebRequest = new MockWebRequest; 91 mockWebRequest = new MockWebRequest;
94 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(mockWebRequest)); 92 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(mockWebRequest));
95 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); 93 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
96 } 94 }
97 }; 95 };
98 96
99 struct MockUpdateAvailableCallback 97 struct MockUpdateAvailableCallback
100 { 98 {
101 MockUpdateAvailableCallback(int& timesCalled) : timesCalled(timesCalled) {} 99 MockUpdateAvailableCallback(int& timesCalled) : timesCalled(timesCalled) {}
102 100
103 void operator()(const std::string&) 101 void operator()(const std::string&)
104 { 102 {
105 timesCalled++; 103 timesCalled++;
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 486
489 TEST_F(FilterEngineTestNoData, FirstRunFlag) 487 TEST_F(FilterEngineTestNoData, FirstRunFlag)
490 { 488 {
491 ASSERT_TRUE(filterEngine->IsFirstRun()); 489 ASSERT_TRUE(filterEngine->IsFirstRun());
492 } 490 }
493 491
494 TEST_F(FilterEngineTest, SetRemoveFilterChangeCallback) 492 TEST_F(FilterEngineTest, SetRemoveFilterChangeCallback)
495 { 493 {
496 int timesCalled = 0; 494 int timesCalled = 0;
497 MockFilterChangeCallback mockFilterChangeCallback(timesCalled); 495 MockFilterChangeCallback mockFilterChangeCallback(timesCalled);
498 496 std::this_thread::sleep_for(std::chrono::milliseconds(200));
499 filterEngine->SetFilterChangeCallback(mockFilterChangeCallback); 497 filterEngine->SetFilterChangeCallback(mockFilterChangeCallback);
500 filterEngine->GetFilter("foo")->AddToList(); 498 filterEngine->GetFilter("foo")->AddToList();
501 EXPECT_EQ(1, timesCalled); 499 EXPECT_EQ(1, timesCalled);
502 500
503 filterEngine->RemoveFilterChangeCallback(); 501 filterEngine->RemoveFilterChangeCallback();
504 filterEngine->GetFilter("foo")->RemoveFromList(); 502 filterEngine->GetFilter("foo")->RemoveFromList();
505 EXPECT_EQ(1, timesCalled); 503 EXPECT_EQ(1, timesCalled);
506 } 504 }
507 505
508 TEST_F(UpdaterTest, SetRemoveUpdateAvailableCallback) 506 TEST_F(UpdaterTest, SetRemoveUpdateAvailableCallback)
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 "http://example.co.uk", 586 "http://example.co.uk",
589 documentUrls1)); 587 documentUrls1));
590 } 588 }
591 589
592 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) 590 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
593 { 591 {
594 AppInfo appInfo; 592 AppInfo appInfo;
595 appInfo.locale = "zh"; 593 appInfo.locale = "zh";
596 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt"; 594 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt";
597 auto jsEngine = createJsEngine(appInfo); 595 auto jsEngine = createJsEngine(appInfo);
598 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); 596 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
599 const auto subscriptions = filterEngine->GetListedSubscriptions(); 597 const auto subscriptions = filterEngine->GetListedSubscriptions();
600 ASSERT_EQ(2u, subscriptions.size()); 598 ASSERT_EQ(2u, subscriptions.size());
601 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing(); 599 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing();
602 SubscriptionPtr aaSubscription; 600 SubscriptionPtr aaSubscription;
603 SubscriptionPtr langSubscription; 601 SubscriptionPtr langSubscription;
604 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) 602 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl)
605 { 603 {
606 aaSubscription = subscriptions[0]; 604 aaSubscription = subscriptions[0];
607 langSubscription = subscriptions[1]; 605 langSubscription = subscriptions[1];
608 } 606 }
609 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl) 607 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl)
610 { 608 {
611 aaSubscription = subscriptions[1]; 609 aaSubscription = subscriptions[1];
612 langSubscription = subscriptions[0]; 610 langSubscription = subscriptions[0];
613 } 611 }
614 ASSERT_NE(nullptr, aaSubscription); 612 ASSERT_NE(nullptr, aaSubscription);
615 ASSERT_NE(nullptr, langSubscription); 613 ASSERT_NE(nullptr, langSubscription);
616 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString()); 614 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString());
617 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( )); 615 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( ));
618 } 616 }
619 617
620 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 618 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
621 { 619 {
622 auto jsEngine = createJsEngine(); 620 auto jsEngine = createJsEngine();
623 FilterEngine::Prefs preSettings; 621 FilterEngine::CreateParameters createParams;
624 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(false); 622 createParams.preconfiguredPrefs["first_run_subscription_auto_select"] = jsEngi ne->NewValue(false);
625 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr eSettings)); 623 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams);
626 const auto subscriptions = filterEngine->GetListedSubscriptions(); 624 const auto subscriptions = filterEngine->GetListedSubscriptions();
627 EXPECT_EQ(0u, subscriptions.size()); 625 EXPECT_EQ(0u, subscriptions.size());
628 } 626 }
OLDNEW

Powered by Google App Engine
This is Rietveld