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 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 FilterEngine& GetFilterEngine() | 66 FilterEngine& GetFilterEngine() |
67 { | 67 { |
68 return platform->GetFilterEngine(); | 68 return platform->GetFilterEngine(); |
69 } | 69 } |
70 }; | 70 }; |
71 | 71 |
72 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem>
FilterEngineTest; | 72 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem>
FilterEngineTest; |
73 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine
TestNoData; | 73 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine
TestNoData; |
74 | 74 |
75 class FilterEngineWithFreshFolder : public BaseJsTest | 75 class FilterEngineWithInMemoryFS : public BaseJsTest |
76 { | 76 { |
| 77 LazyFileSystem* fileSystem; |
77 protected: | 78 protected: |
78 FileSystemPtr fileSystem; | |
79 std::list<SchedulerTask> fileSystemTasks; | |
80 | |
81 void SetUp() override | |
82 { | |
83 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task) | |
84 { | |
85 fileSystemTasks.emplace_back(task); | |
86 }); | |
87 // Since there is neither in memory FS nor functionality to work with | |
88 // directories use the hack: manually clean the directory. | |
89 removeFileIfExists("patterns.ini"); | |
90 removeFileIfExists("prefs.json"); | |
91 } | |
92 void InitPlatformAndAppInfo(const AppInfo& appInfo = AppInfo()) | 79 void InitPlatformAndAppInfo(const AppInfo& appInfo = AppInfo()) |
93 { | 80 { |
94 ThrowingPlatformCreationParameters platformParams; | 81 ThrowingPlatformCreationParameters platformParams; |
95 platformParams.logSystem.reset(new LazyLogSystem()); | 82 platformParams.logSystem.reset(new LazyLogSystem()); |
96 platformParams.timer.reset(new NoopTimer()); | 83 platformParams.timer.reset(new NoopTimer()); |
97 platformParams.fileSystem = fileSystem; | 84 platformParams.fileSystem.reset(fileSystem = new InMemoryFileSystem()); |
98 platformParams.webRequest.reset(new NoopWebRequest()); | 85 platformParams.webRequest.reset(new NoopWebRequest()); |
99 platform.reset(new Platform(std::move(platformParams))); | 86 platform.reset(new Platform(std::move(platformParams))); |
100 platform->SetUpJsEngine(appInfo); | 87 platform->SetUpJsEngine(appInfo); |
101 } | 88 } |
102 | 89 |
103 FilterEngine& CreateFilterEngine(const FilterEngine::CreationParameters& cre
ationParams = FilterEngine::CreationParameters()) | 90 FilterEngine& CreateFilterEngine(const FilterEngine::CreationParameters& cre
ationParams = FilterEngine::CreationParameters()) |
104 { | 91 { |
105 bool isFilterEngineReady = false; | 92 ::CreateFilterEngine(*fileSystem, *platform, creationParams); |
106 platform->CreateFilterEngineAsync(creationParams, [&isFilterEngineReady](c
onst FilterEngine& filterEngine) | |
107 { | |
108 isFilterEngineReady = true; | |
109 }); | |
110 while (!isFilterEngineReady && !fileSystemTasks.empty()) | |
111 { | |
112 (*fileSystemTasks.begin())(); | |
113 fileSystemTasks.pop_front(); | |
114 } | |
115 return platform->GetFilterEngine(); | 93 return platform->GetFilterEngine(); |
116 } | 94 } |
117 | |
118 void TearDown() override | |
119 { | |
120 removeFileIfExists("patterns.ini"); | |
121 removeFileIfExists("prefs.json"); | |
122 fileSystem.reset(); | |
123 BaseJsTest::TearDown(); | |
124 } | |
125 void removeFileIfExists(const std::string& fileName) | |
126 { | |
127 bool hasStatRun = false; | |
128 bool doesFileExists; | |
129 fileSystem->Stat(fileName, [&hasStatRun, &doesFileExists](const IFileSyste
m::StatResult& stats, const std::string& error) | |
130 { | |
131 EXPECT_TRUE(error.empty()) << error; | |
132 doesFileExists = stats.exists; | |
133 hasStatRun = true; | |
134 }); | |
135 while (!hasStatRun && !fileSystemTasks.empty()) | |
136 { | |
137 (*fileSystemTasks.begin())(); | |
138 fileSystemTasks.pop_front(); | |
139 } | |
140 | |
141 if (!doesFileExists) | |
142 return; | |
143 | |
144 bool hasRemoveRun = false; | |
145 fileSystem->Remove(fileName, [&hasRemoveRun](const std::string& error) | |
146 { | |
147 EXPECT_TRUE(error.empty()) << error; | |
148 hasRemoveRun = true; | |
149 }); | |
150 while (!hasStatRun && !fileSystemTasks.empty()) | |
151 { | |
152 (*fileSystemTasks.begin())(); | |
153 fileSystemTasks.pop_front(); | |
154 } | |
155 } | |
156 }; | 95 }; |
157 | 96 |
158 class FilterEngineIsSubscriptionDownloadAllowedTest : public BaseJsTest | 97 class FilterEngineIsSubscriptionDownloadAllowedTest : public BaseJsTest |
159 { | 98 { |
160 protected: | 99 protected: |
161 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; | 100 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; |
162 DelayedWebRequest::SharedTasks webRequestTasks; | 101 DelayedWebRequest::SharedTasks webRequestTasks; |
163 DelayedTimer::SharedTasks timerTasks; | 102 DelayedTimer::SharedTasks timerTasks; |
164 FilterEngine::CreationParameters createParams; | 103 FilterEngine::CreationParameters createParams; |
165 ConnectionTypes capturedConnectionTypes; | 104 ConnectionTypes capturedConnectionTypes; |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 | 631 |
693 ASSERT_TRUE(filterEngine.IsElemhideWhitelisted( | 632 ASSERT_TRUE(filterEngine.IsElemhideWhitelisted( |
694 "http://example.com", | 633 "http://example.com", |
695 documentUrls1)); | 634 documentUrls1)); |
696 | 635 |
697 ASSERT_FALSE(filterEngine.IsElemhideWhitelisted( | 636 ASSERT_FALSE(filterEngine.IsElemhideWhitelisted( |
698 "http://example.co.uk", | 637 "http://example.co.uk", |
699 documentUrls1)); | 638 documentUrls1)); |
700 } | 639 } |
701 | 640 |
702 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) | 641 TEST_F(FilterEngineWithInMemoryFS, LangAndAASubscriptionsAreChosenOnFirstRun) |
703 { | 642 { |
704 AppInfo appInfo; | 643 AppInfo appInfo; |
705 appInfo.locale = "zh"; | 644 appInfo.locale = "zh"; |
706 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu
s.org/easylistchina+easylist.txt"; | 645 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu
s.org/easylistchina+easylist.txt"; |
707 InitPlatformAndAppInfo(appInfo); | 646 InitPlatformAndAppInfo(appInfo); |
708 auto& filterEngine = CreateFilterEngine(); | 647 auto& filterEngine = CreateFilterEngine(); |
709 const auto subscriptions = filterEngine.GetListedSubscriptions(); | 648 const auto subscriptions = filterEngine.GetListedSubscriptions(); |
710 ASSERT_EQ(2u, subscriptions.size()); | 649 ASSERT_EQ(2u, subscriptions.size()); |
711 std::unique_ptr<Subscription> aaSubscription; | 650 std::unique_ptr<Subscription> aaSubscription; |
712 std::unique_ptr<Subscription> langSubscription; | 651 std::unique_ptr<Subscription> langSubscription; |
713 if (subscriptions[0].IsAA()) | 652 if (subscriptions[0].IsAA()) |
714 { | 653 { |
715 aaSubscription.reset(new Subscription(subscriptions[0])); | 654 aaSubscription.reset(new Subscription(subscriptions[0])); |
716 langSubscription.reset(new Subscription(subscriptions[1])); | 655 langSubscription.reset(new Subscription(subscriptions[1])); |
717 } | 656 } |
718 else if (subscriptions[1].IsAA()) | 657 else if (subscriptions[1].IsAA()) |
719 { | 658 { |
720 aaSubscription.reset(new Subscription(subscriptions[1])); | 659 aaSubscription.reset(new Subscription(subscriptions[1])); |
721 langSubscription.reset(new Subscription(subscriptions[0])); | 660 langSubscription.reset(new Subscription(subscriptions[0])); |
722 } | 661 } |
723 ASSERT_NE(nullptr, aaSubscription); | 662 ASSERT_NE(nullptr, aaSubscription); |
724 ASSERT_NE(nullptr, langSubscription); | 663 ASSERT_NE(nullptr, langSubscription); |
725 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString()
); | 664 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString()
); |
726 EXPECT_TRUE(filterEngine.IsAAEnabled()); | 665 EXPECT_TRUE(filterEngine.IsAAEnabled()); |
727 } | 666 } |
728 | 667 |
729 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) | 668 TEST_F(FilterEngineWithInMemoryFS, DisableSubscriptionsAutoSelectOnFirstRun) |
730 { | 669 { |
731 InitPlatformAndAppInfo(); | 670 InitPlatformAndAppInfo(); |
732 FilterEngine::CreationParameters createParams; | 671 FilterEngine::CreationParameters createParams; |
733 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select",
GetJsEngine().NewValue(false)); | 672 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select",
GetJsEngine().NewValue(false)); |
734 auto& filterEngine = CreateFilterEngine(createParams); | 673 auto& filterEngine = CreateFilterEngine(createParams); |
735 const auto subscriptions = filterEngine.GetListedSubscriptions(); | 674 const auto subscriptions = filterEngine.GetListedSubscriptions(); |
736 EXPECT_EQ(0u, subscriptions.size()); | 675 EXPECT_EQ(0u, subscriptions.size()); |
737 EXPECT_FALSE(filterEngine.IsAAEnabled()); | 676 EXPECT_FALSE(filterEngine.IsAAEnabled()); |
738 } | 677 } |
739 | 678 |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1064 std::string testConnection = "test connection"; | 1003 std::string testConnection = "test connection"; |
1065 GetFilterEngine().SetAllowedConnectionType(&testConnection); | 1004 GetFilterEngine().SetAllowedConnectionType(&testConnection); |
1066 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); | 1005 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); |
1067 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); | 1006 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); |
1068 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 1007 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); |
1069 ASSERT_EQ(1u, capturedConnectionTypes.size()); | 1008 ASSERT_EQ(1u, capturedConnectionTypes.size()); |
1070 EXPECT_TRUE(capturedConnectionTypes[0].first); | 1009 EXPECT_TRUE(capturedConnectionTypes[0].first); |
1071 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); | 1010 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); |
1072 } | 1011 } |
1073 } | 1012 } |
OLD | NEW |