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

Side by Side Diff: test/FilterEngine.cpp

Issue 29508555: Issue 5450 - move FilterEngine into Platform (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Aug. 7, 2017, 8:28 a.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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 56
57 void SetUp() override 57 void SetUp() override
58 { 58 {
59 LazyFileSystemT* fileSystem; 59 LazyFileSystemT* fileSystem;
60 ThrowingPlatformCreationParameters platformParams; 60 ThrowingPlatformCreationParameters platformParams;
61 platformParams.logSystem.reset(new LogSystem()); 61 platformParams.logSystem.reset(new LogSystem());
62 platformParams.timer.reset(new NoopTimer()); 62 platformParams.timer.reset(new NoopTimer());
63 platformParams.fileSystem.reset(fileSystem = new LazyFileSystemT()); 63 platformParams.fileSystem.reset(fileSystem = new LazyFileSystemT());
64 platformParams.webRequest.reset(new NoopWebRequest()); 64 platformParams.webRequest.reset(new NoopWebRequest());
65 platform.reset(new Platform(std::move(platformParams))); 65 platform.reset(new Platform(std::move(platformParams)));
66 filterEngine = CreateFilterEngine(*fileSystem, platform->GetJsEngine()); 66 filterEngine = CreateFilterEngine(*fileSystem, *platform);
67 } 67 }
68 }; 68 };
69 69
70 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; 70 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest;
71 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine TestNoData; 71 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine TestNoData;
72 72
73 class FilterEngineWithFreshFolder : public ::testing::Test 73 class FilterEngineWithFreshFolder : public ::testing::Test
74 { 74 {
75 protected: 75 protected:
76 std::unique_ptr<Platform> platform; 76 std::unique_ptr<Platform> platform;
77 FileSystemPtr fileSystem; 77 FileSystemPtr fileSystem;
78 std::list<SchedulerTask> fileSystemTasks; 78 std::list<SchedulerTask> fileSystemTasks;
79 std::weak_ptr<JsEngine> weakJsEngine;
80 79
81 void SetUp() override 80 void SetUp() override
82 { 81 {
83 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task) 82 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task)
84 { 83 {
85 fileSystemTasks.emplace_back(task); 84 fileSystemTasks.emplace_back(task);
86 }); 85 });
87 // Since there is neither in memory FS nor functionality to work with 86 // Since there is neither in memory FS nor functionality to work with
88 // directories use the hack: manually clean the directory. 87 // directories use the hack: manually clean the directory.
89 removeFileIfExists("patterns.ini"); 88 removeFileIfExists("patterns.ini");
90 removeFileIfExists("prefs.json"); 89 removeFileIfExists("prefs.json");
91 } 90 }
92 JsEnginePtr CreateJsEngine(const AppInfo& appInfo = AppInfo()) 91 void InitPlatformAndAppInfo(const AppInfo& appInfo = AppInfo())
93 { 92 {
94 ThrowingPlatformCreationParameters platformParams; 93 ThrowingPlatformCreationParameters platformParams;
95 platformParams.logSystem.reset(new LazyLogSystem()); 94 platformParams.logSystem.reset(new LazyLogSystem());
96 platformParams.timer.reset(new NoopTimer()); 95 platformParams.timer.reset(new NoopTimer());
97 platformParams.fileSystem = fileSystem; 96 platformParams.fileSystem = fileSystem;
98 platformParams.webRequest.reset(new NoopWebRequest()); 97 platformParams.webRequest.reset(new NoopWebRequest());
99 platform.reset(new Platform(std::move(platformParams))); 98 platform.reset(new Platform(std::move(platformParams)));
100 platform->SetUpJsEngine(appInfo); 99 platform->SetUpJsEngine(appInfo);
101 auto jsEngine = platform->GetJsEngine();
102 weakJsEngine = jsEngine;
103 return jsEngine;
104 } 100 }
105 101
106 FilterEnginePtr CreateFilterEngine(const JsEnginePtr& jsEngine, 102 FilterEnginePtr CreateFilterEngine(const FilterEngine::CreationParameters& c reationParams = FilterEngine::CreationParameters())
107 const FilterEngine::CreationParameters& creationParams = FilterEngine::Cre ationParameters())
108 { 103 {
109 FilterEnginePtr retValue; 104 FilterEnginePtr retValue;
110 FilterEngine::CreateAsync(jsEngine, [&retValue](const FilterEnginePtr& fil terEngine) 105 platform->CreateFilterEngineAsync(creationParams, [&retValue](const Filter EnginePtr& filterEngine)
111 { 106 {
112 retValue = filterEngine; 107 retValue = filterEngine;
113 }, creationParams); 108 });
114 while (!retValue && !fileSystemTasks.empty()) 109 while (!retValue && !fileSystemTasks.empty())
115 { 110 {
116 (*fileSystemTasks.begin())(); 111 (*fileSystemTasks.begin())();
117 fileSystemTasks.pop_front(); 112 fileSystemTasks.pop_front();
118 } 113 }
119 return retValue; 114 return retValue;
120 } 115 }
121 116
122 void TearDown() override 117 void TearDown() override
123 { 118 {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 isSubscriptionDownloadAllowedCallbacks.emplace_back(isSubscriptionDownlo adAllowedCallback); 186 isSubscriptionDownloadAllowedCallbacks.emplace_back(isSubscriptionDownlo adAllowedCallback);
192 }; 187 };
193 } 188 }
194 189
195 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp endToUrl = std::string()) 190 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp endToUrl = std::string())
196 { 191 {
197 auto subscriptionUrl = "http://example" + apppendToUrl; 192 auto subscriptionUrl = "http://example" + apppendToUrl;
198 bool isSubscriptionDownloadStatusReceived = false; 193 bool isSubscriptionDownloadStatusReceived = false;
199 if (!filterEngine) 194 if (!filterEngine)
200 { 195 {
201 filterEngine = CreateFilterEngine(*fileSystem, platform->GetJsEngine(), createParams); 196 filterEngine = CreateFilterEngine(*fileSystem, *platform, createParams);
202 filterEngine->SetFilterChangeCallback([&isSubscriptionDownloadStatusRece ived, &subscriptionUrl](const std::string& action, JsValue&& item) 197 filterEngine->SetFilterChangeCallback([&isSubscriptionDownloadStatusRece ived, &subscriptionUrl](const std::string& action, JsValue&& item)
203 { 198 {
204 if (action == "subscription.downloadStatus" && item.GetProperty("url") .AsString() == subscriptionUrl) 199 if (action == "subscription.downloadStatus" && item.GetProperty("url") .AsString() == subscriptionUrl)
205 isSubscriptionDownloadStatusReceived = true; 200 isSubscriptionDownloadStatusReceived = true;
206 }); 201 });
207 } 202 }
208 auto subscription = filterEngine->GetSubscription(subscriptionUrl); 203 auto subscription = filterEngine->GetSubscription(subscriptionUrl);
209 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs criptionUrl; 204 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs criptionUrl;
210 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr iptionUrl; 205 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr iptionUrl;
211 subscription.UpdateFilters(); 206 subscription.UpdateFilters();
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( 676 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted(
682 "http://example.co.uk", 677 "http://example.co.uk",
683 documentUrls1)); 678 documentUrls1));
684 } 679 }
685 680
686 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) 681 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
687 { 682 {
688 AppInfo appInfo; 683 AppInfo appInfo;
689 appInfo.locale = "zh"; 684 appInfo.locale = "zh";
690 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt"; 685 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt";
691 auto jsEngine = CreateJsEngine(appInfo); 686 InitPlatformAndAppInfo(appInfo);
692 auto filterEngine = CreateFilterEngine(jsEngine); 687 auto filterEngine = CreateFilterEngine();
693 const auto subscriptions = filterEngine->GetListedSubscriptions(); 688 const auto subscriptions = filterEngine->GetListedSubscriptions();
694 ASSERT_EQ(2u, subscriptions.size()); 689 ASSERT_EQ(2u, subscriptions.size());
695 std::unique_ptr<Subscription> aaSubscription; 690 std::unique_ptr<Subscription> aaSubscription;
696 std::unique_ptr<Subscription> langSubscription; 691 std::unique_ptr<Subscription> langSubscription;
697 if (subscriptions[0].IsAA()) 692 if (subscriptions[0].IsAA())
698 { 693 {
699 aaSubscription.reset(new Subscription(subscriptions[0])); 694 aaSubscription.reset(new Subscription(subscriptions[0]));
700 langSubscription.reset(new Subscription(subscriptions[1])); 695 langSubscription.reset(new Subscription(subscriptions[1]));
701 } 696 }
702 else if (subscriptions[1].IsAA()) 697 else if (subscriptions[1].IsAA())
703 { 698 {
704 aaSubscription.reset(new Subscription(subscriptions[1])); 699 aaSubscription.reset(new Subscription(subscriptions[1]));
705 langSubscription.reset(new Subscription(subscriptions[0])); 700 langSubscription.reset(new Subscription(subscriptions[0]));
706 } 701 }
707 ASSERT_NE(nullptr, aaSubscription); 702 ASSERT_NE(nullptr, aaSubscription);
708 ASSERT_NE(nullptr, langSubscription); 703 ASSERT_NE(nullptr, langSubscription);
709 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString() ); 704 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString() );
710 EXPECT_TRUE(filterEngine->IsAAEnabled()); 705 EXPECT_TRUE(filterEngine->IsAAEnabled());
711 } 706 }
712 707
713 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 708 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
714 { 709 {
715 auto jsEngine = CreateJsEngine(); 710 InitPlatformAndAppInfo();
716 FilterEngine::CreationParameters createParams; 711 FilterEngine::CreationParameters createParams;
717 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select", jsEngine->NewValue(false)); 712 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select", platform->GetJsEngine()->NewValue(false));
718 auto filterEngine = CreateFilterEngine(jsEngine, createParams); 713 auto filterEngine = CreateFilterEngine(createParams);
719 const auto subscriptions = filterEngine->GetListedSubscriptions(); 714 const auto subscriptions = filterEngine->GetListedSubscriptions();
720 EXPECT_EQ(0u, subscriptions.size()); 715 EXPECT_EQ(0u, subscriptions.size());
721 EXPECT_FALSE(filterEngine->IsAAEnabled()); 716 EXPECT_FALSE(filterEngine->IsAAEnabled());
722 } 717 }
723 718
724 namespace AA_ApiTest 719 namespace AA_ApiTest
725 { 720 {
726 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t xt"; 721 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t xt";
727 enum class AAStatus 722 enum class AAStatus
728 { 723 {
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 std::string testConnection = "test connection"; 1040 std::string testConnection = "test connection";
1046 filterEngine->SetAllowedConnectionType(&testConnection); 1041 filterEngine->SetAllowedConnectionType(&testConnection);
1047 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1042 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
1048 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1043 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1049 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1044 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1050 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1045 ASSERT_EQ(1u, capturedConnectionTypes.size());
1051 EXPECT_TRUE(capturedConnectionTypes[0].first); 1046 EXPECT_TRUE(capturedConnectionTypes[0].first);
1052 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1047 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
1053 } 1048 }
1054 } 1049 }
OLDNEW
« test/BaseJsTest.cpp ('K') | « test/BaseJsTest.cpp ('k') | test/Notification.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld