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-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 30 matching lines...) Expand all Loading... |
41 void Stat(const std::string& path, const StatCallback& callback) const overr
ide | 41 void Stat(const std::string& path, const StatCallback& callback) const overr
ide |
42 { | 42 { |
43 scheduler([callback] | 43 scheduler([callback] |
44 { | 44 { |
45 callback(StatResult(), ""); | 45 callback(StatResult(), ""); |
46 }); | 46 }); |
47 } | 47 } |
48 }; | 48 }; |
49 | 49 |
50 template<class LazyFileSystemT, class LogSystem> | 50 template<class LazyFileSystemT, class LogSystem> |
51 class FilterEngineTestGeneric : public ::testing::Test | 51 class FilterEngineTestGeneric : public BaseJsTest |
52 { | 52 { |
53 protected: | 53 protected: |
54 std::unique_ptr<Platform> platform; | |
55 FilterEnginePtr filterEngine; | 54 FilterEnginePtr filterEngine; |
56 | 55 |
57 void SetUp() override | 56 void SetUp() override |
58 { | 57 { |
59 LazyFileSystemT* fileSystem; | 58 LazyFileSystemT* fileSystem; |
60 ThrowingPlatformCreationParameters platformParams; | 59 ThrowingPlatformCreationParameters platformParams; |
61 platformParams.logSystem.reset(new LogSystem()); | 60 platformParams.logSystem.reset(new LogSystem()); |
62 platformParams.timer.reset(new NoopTimer()); | 61 platformParams.timer.reset(new NoopTimer()); |
63 platformParams.fileSystem.reset(fileSystem = new LazyFileSystemT()); | 62 platformParams.fileSystem.reset(fileSystem = new LazyFileSystemT()); |
64 platformParams.webRequest.reset(new NoopWebRequest()); | 63 platformParams.webRequest.reset(new NoopWebRequest()); |
65 platform.reset(new Platform(std::move(platformParams))); | 64 platform.reset(new Platform(std::move(platformParams))); |
66 filterEngine = CreateFilterEngine(*fileSystem, *platform); | 65 filterEngine = CreateFilterEngine(*fileSystem, *platform); |
67 } | 66 } |
68 }; | 67 }; |
69 | 68 |
70 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem>
FilterEngineTest; | 69 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem>
FilterEngineTest; |
71 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine
TestNoData; | 70 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine
TestNoData; |
72 | 71 |
73 class FilterEngineWithFreshFolder : public ::testing::Test | 72 class FilterEngineWithFreshFolder : public BaseJsTest |
74 { | 73 { |
75 protected: | 74 protected: |
76 std::unique_ptr<Platform> platform; | |
77 FileSystemPtr fileSystem; | 75 FileSystemPtr fileSystem; |
78 std::list<SchedulerTask> fileSystemTasks; | 76 std::list<SchedulerTask> fileSystemTasks; |
79 | 77 |
80 void SetUp() override | 78 void SetUp() override |
81 { | 79 { |
82 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task) | 80 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task) |
83 { | 81 { |
84 fileSystemTasks.emplace_back(task); | 82 fileSystemTasks.emplace_back(task); |
85 }); | 83 }); |
86 // Since there is neither in memory FS nor functionality to work with | 84 // Since there is neither in memory FS nor functionality to work with |
(...skipping 25 matching lines...) Expand all Loading... |
112 fileSystemTasks.pop_front(); | 110 fileSystemTasks.pop_front(); |
113 } | 111 } |
114 return retValue; | 112 return retValue; |
115 } | 113 } |
116 | 114 |
117 void TearDown() override | 115 void TearDown() override |
118 { | 116 { |
119 removeFileIfExists("patterns.ini"); | 117 removeFileIfExists("patterns.ini"); |
120 removeFileIfExists("prefs.json"); | 118 removeFileIfExists("prefs.json"); |
121 fileSystem.reset(); | 119 fileSystem.reset(); |
| 120 BaseJsTest::TearDown(); |
122 } | 121 } |
123 void removeFileIfExists(const std::string& path) | 122 void removeFileIfExists(const std::string& path) |
124 { | 123 { |
125 bool hasStatRun = false; | 124 bool hasStatRun = false; |
126 bool doesFileExists; | 125 bool doesFileExists; |
127 fileSystem->Stat(path, [&hasStatRun, &doesFileExists](const IFileSystem::S
tatResult& stats, const std::string& error) | 126 fileSystem->Stat(path, [&hasStatRun, &doesFileExists](const IFileSystem::S
tatResult& stats, const std::string& error) |
128 { | 127 { |
129 EXPECT_TRUE(error.empty()) << error; | 128 EXPECT_TRUE(error.empty()) << error; |
130 doesFileExists = stats.exists; | 129 doesFileExists = stats.exists; |
131 hasStatRun = true; | 130 hasStatRun = true; |
(...skipping 14 matching lines...) Expand all Loading... |
146 hasRemoveRun = true; | 145 hasRemoveRun = true; |
147 }); | 146 }); |
148 while (!hasStatRun && !fileSystemTasks.empty()) | 147 while (!hasStatRun && !fileSystemTasks.empty()) |
149 { | 148 { |
150 (*fileSystemTasks.begin())(); | 149 (*fileSystemTasks.begin())(); |
151 fileSystemTasks.pop_front(); | 150 fileSystemTasks.pop_front(); |
152 } | 151 } |
153 } | 152 } |
154 }; | 153 }; |
155 | 154 |
156 class FilterEngineIsSubscriptionDownloadAllowedTest : public ::testing::Test | 155 class FilterEngineIsSubscriptionDownloadAllowedTest : public BaseJsTest |
157 { | 156 { |
158 protected: | 157 protected: |
159 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; | 158 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; |
160 DelayedWebRequest::SharedTasks webRequestTasks; | 159 DelayedWebRequest::SharedTasks webRequestTasks; |
161 DelayedTimer::SharedTasks timerTasks; | 160 DelayedTimer::SharedTasks timerTasks; |
162 FilterEngine::CreationParameters createParams; | 161 FilterEngine::CreationParameters createParams; |
163 ConnectionTypes capturedConnectionTypes; | 162 ConnectionTypes capturedConnectionTypes; |
164 bool isConnectionAllowed; | 163 bool isConnectionAllowed; |
165 std::vector<std::function<void(bool)>> isSubscriptionDownloadAllowedCallback
s; | 164 std::vector<std::function<void(bool)>> isSubscriptionDownloadAllowedCallback
s; |
166 std::unique_ptr<Platform> platform; | |
167 FilterEnginePtr filterEngine; | 165 FilterEnginePtr filterEngine; |
168 LazyFileSystem* fileSystem; | 166 LazyFileSystem* fileSystem; |
169 | 167 |
170 void SetUp() | 168 void SetUp() |
171 { | 169 { |
172 isConnectionAllowed = true; | 170 isConnectionAllowed = true; |
173 | 171 |
174 ThrowingPlatformCreationParameters platformParams; | 172 ThrowingPlatformCreationParameters platformParams; |
175 platformParams.logSystem.reset(new LazyLogSystem()); | 173 platformParams.logSystem.reset(new LazyLogSystem()); |
176 platformParams.timer = DelayedTimer::New(timerTasks); | 174 platformParams.timer = DelayedTimer::New(timerTasks); |
177 platformParams.fileSystem.reset(fileSystem = new LazyFileSystem()); | 175 platformParams.fileSystem.reset(fileSystem = new LazyFileSystem()); |
178 platformParams.webRequest = DelayedWebRequest::New(webRequestTasks); | 176 platformParams.webRequest = DelayedWebRequest::New(webRequestTasks); |
179 platform.reset(new Platform(std::move(platformParams))); | 177 platform.reset(new Platform(std::move(platformParams))); |
180 | 178 |
181 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec
t", platform->GetJsEngine()->NewValue(false)); | 179 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec
t", GetJsEngine().NewValue(false)); |
182 | 180 |
183 createParams.isSubscriptionDownloadAllowedCallback = [this](const std::str
ing* allowedConnectionType, | 181 createParams.isSubscriptionDownloadAllowedCallback = [this](const std::str
ing* allowedConnectionType, |
184 const std::function<void(bool)>& isSubscriptionDownloadAllowedCallback){ | 182 const std::function<void(bool)>& isSubscriptionDownloadAllowedCallback){ |
185 capturedConnectionTypes.emplace_back(!!allowedConnectionType, allowedCon
nectionType ? *allowedConnectionType : std::string()); | 183 capturedConnectionTypes.emplace_back(!!allowedConnectionType, allowedCon
nectionType ? *allowedConnectionType : std::string()); |
186 isSubscriptionDownloadAllowedCallbacks.emplace_back(isSubscriptionDownlo
adAllowedCallback); | 184 isSubscriptionDownloadAllowedCallbacks.emplace_back(isSubscriptionDownlo
adAllowedCallback); |
187 }; | 185 }; |
188 } | 186 } |
189 | 187 |
190 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp
endToUrl = std::string()) | 188 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp
endToUrl = std::string()) |
191 { | 189 { |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 ASSERT_NE(nullptr, aaSubscription); | 700 ASSERT_NE(nullptr, aaSubscription); |
703 ASSERT_NE(nullptr, langSubscription); | 701 ASSERT_NE(nullptr, langSubscription); |
704 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString()
); | 702 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString()
); |
705 EXPECT_TRUE(filterEngine->IsAAEnabled()); | 703 EXPECT_TRUE(filterEngine->IsAAEnabled()); |
706 } | 704 } |
707 | 705 |
708 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) | 706 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) |
709 { | 707 { |
710 InitPlatformAndAppInfo(); | 708 InitPlatformAndAppInfo(); |
711 FilterEngine::CreationParameters createParams; | 709 FilterEngine::CreationParameters createParams; |
712 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select",
platform->GetJsEngine()->NewValue(false)); | 710 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select",
GetJsEngine().NewValue(false)); |
713 auto filterEngine = CreateFilterEngine(createParams); | 711 auto filterEngine = CreateFilterEngine(createParams); |
714 const auto subscriptions = filterEngine->GetListedSubscriptions(); | 712 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
715 EXPECT_EQ(0u, subscriptions.size()); | 713 EXPECT_EQ(0u, subscriptions.size()); |
716 EXPECT_FALSE(filterEngine->IsAAEnabled()); | 714 EXPECT_FALSE(filterEngine->IsAAEnabled()); |
717 } | 715 } |
718 | 716 |
719 namespace AA_ApiTest | 717 namespace AA_ApiTest |
720 { | 718 { |
721 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t
xt"; | 719 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t
xt"; |
722 enum class AAStatus | 720 enum class AAStatus |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); | 989 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); |
992 EXPECT_EQ("synchronize_connection_error", subscription.GetProperty("downloadSt
atus").AsString()); | 990 EXPECT_EQ("synchronize_connection_error", subscription.GetProperty("downloadSt
atus").AsString()); |
993 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()); | 991 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()); |
994 EXPECT_EQ(1u, capturedConnectionTypes.size()); | 992 EXPECT_EQ(1u, capturedConnectionTypes.size()); |
995 } | 993 } |
996 | 994 |
997 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, PredefinedAllowedConnectio
nTypeIsPassedToCallback) | 995 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, PredefinedAllowedConnectio
nTypeIsPassedToCallback) |
998 { | 996 { |
999 std::string predefinedAllowedConnectionType = "non-metered"; | 997 std::string predefinedAllowedConnectionType = "non-metered"; |
1000 createParams.preconfiguredPrefs.insert(std::make_pair("allowed_connection_type
", | 998 createParams.preconfiguredPrefs.insert(std::make_pair("allowed_connection_type
", |
1001 platform->GetJsEngine()->NewValue(predefinedAllowedConnectionType))); | 999 GetJsEngine().NewValue(predefinedAllowedConnectionType))); |
1002 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); | 1000 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); |
1003 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin
g()); | 1001 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin
g()); |
1004 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 1002 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); |
1005 ASSERT_EQ(1u, capturedConnectionTypes.size()); | 1003 ASSERT_EQ(1u, capturedConnectionTypes.size()); |
1006 EXPECT_TRUE(capturedConnectionTypes[0].first); | 1004 EXPECT_TRUE(capturedConnectionTypes[0].first); |
1007 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second); | 1005 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second); |
1008 } | 1006 } |
1009 | 1007 |
1010 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, ConfiguredConnectionTypeIs
PassedToCallback) | 1008 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, ConfiguredConnectionTypeIs
PassedToCallback) |
1011 { | 1009 { |
1012 // FilterEngine->RemoveSubscription is not usable here because subscriptions | 1010 // FilterEngine->RemoveSubscription is not usable here because subscriptions |
1013 // are cached internally by URL. So, different URLs are used in diffirent | 1011 // are cached internally by URL. So, different URLs are used in diffirent |
1014 // checks. | 1012 // checks. |
1015 { | 1013 { |
1016 std::string predefinedAllowedConnectionType = "non-metered"; | 1014 std::string predefinedAllowedConnectionType = "non-metered"; |
1017 createParams.preconfiguredPrefs.insert(std::make_pair( | 1015 createParams.preconfiguredPrefs.insert(std::make_pair( |
1018 "allowed_connection_type", platform->GetJsEngine()->NewValue(predefinedAll
owedConnectionType))); | 1016 "allowed_connection_type", GetJsEngine().NewValue(predefinedAllowedConnect
ionType))); |
1019 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); | 1017 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); |
1020 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); | 1018 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); |
1021 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 1019 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); |
1022 ASSERT_EQ(1u, capturedConnectionTypes.size()); | 1020 ASSERT_EQ(1u, capturedConnectionTypes.size()); |
1023 EXPECT_TRUE(capturedConnectionTypes[0].first); | 1021 EXPECT_TRUE(capturedConnectionTypes[0].first); |
1024 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second
); | 1022 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second
); |
1025 } | 1023 } |
1026 capturedConnectionTypes.clear(); | 1024 capturedConnectionTypes.clear(); |
1027 { | 1025 { |
1028 // set no value | 1026 // set no value |
(...skipping 11 matching lines...) Expand all Loading... |
1040 std::string testConnection = "test connection"; | 1038 std::string testConnection = "test connection"; |
1041 filterEngine->SetAllowedConnectionType(&testConnection); | 1039 filterEngine->SetAllowedConnectionType(&testConnection); |
1042 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); | 1040 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); |
1043 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); | 1041 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); |
1044 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 1042 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); |
1045 ASSERT_EQ(1u, capturedConnectionTypes.size()); | 1043 ASSERT_EQ(1u, capturedConnectionTypes.size()); |
1046 EXPECT_TRUE(capturedConnectionTypes[0].first); | 1044 EXPECT_TRUE(capturedConnectionTypes[0].first); |
1047 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); | 1045 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); |
1048 } | 1046 } |
1049 } | 1047 } |
OLD | NEW |