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-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> |
| 20 |
| 21 using namespace AdblockPlus; |
19 | 22 |
20 namespace | 23 namespace |
21 { | 24 { |
22 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; | 25 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; |
23 | 26 |
24 class VeryLazyFileSystem : public LazyFileSystem | 27 class VeryLazyFileSystem : public LazyFileSystem |
25 { | 28 { |
26 public: | 29 public: |
27 StatResult Stat(const std::string& path) const | 30 StatResult Stat(const std::string& path) const |
28 { | 31 { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 } | 106 } |
104 | 107 |
105 private: | 108 private: |
106 // We currently cannot store timesCalled in the functor, see: | 109 // We currently cannot store timesCalled in the functor, see: |
107 // https://issues.adblockplus.org/ticket/1378. | 110 // https://issues.adblockplus.org/ticket/1378. |
108 int& timesCalled; | 111 int& timesCalled; |
109 }; | 112 }; |
110 | 113 |
111 // Workaround for https://issues.adblockplus.org/ticket/1397. | 114 // Workaround for https://issues.adblockplus.org/ticket/1397. |
112 void NoOpUpdaterCallback(const std::string&) {} | 115 void NoOpUpdaterCallback(const std::string&) {} |
| 116 |
| 117 class FilterEngineWithFreshFolder : public ::testing::Test |
| 118 { |
| 119 protected: |
| 120 FileSystemPtr fileSystem; |
| 121 std::weak_ptr<JsEngine> weakJsEngine; |
| 122 |
| 123 void SetUp() override |
| 124 { |
| 125 fileSystem.reset(new DefaultFileSystem()); |
| 126 // Since there is neither in memory FS nor functionality to work with |
| 127 // directories use the hack: manually clean the directory. |
| 128 removeFileIfExists("patterns.ini"); |
| 129 removeFileIfExists("prefs.json"); |
| 130 } |
| 131 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) |
| 132 { |
| 133 auto jsEngine = JsEngine::New(appInfo); |
| 134 weakJsEngine = jsEngine; |
| 135 jsEngine->SetFileSystem(fileSystem); |
| 136 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); |
| 137 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); |
| 138 return jsEngine; |
| 139 } |
| 140 void TearDown() override |
| 141 { |
| 142 removeFileIfExists("patterns.ini"); |
| 143 removeFileIfExists("prefs.json"); |
| 144 fileSystem.reset(); |
| 145 } |
| 146 void removeFileIfExists(const std::string& path) |
| 147 { |
| 148 // Hack: allow IO to finish currently running operations, in particular |
| 149 // writing into files. Otherwise we get "Permission denied". |
| 150 auto safeRemove = [this, &path]()->bool |
| 151 { |
| 152 try |
| 153 { |
| 154 if (fileSystem->Stat(path).exists) |
| 155 fileSystem->Remove(path); |
| 156 return true; |
| 157 } |
| 158 catch (...) |
| 159 { |
| 160 return false; |
| 161 } |
| 162 }; |
| 163 int i = 5; |
| 164 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove()) |
| 165 std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 166 } |
| 167 }; |
113 } | 168 } |
114 | 169 |
115 TEST_F(FilterEngineTest, FilterCreation) | 170 TEST_F(FilterEngineTest, FilterCreation) |
116 { | 171 { |
117 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); | 172 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); |
118 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); | 173 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); |
119 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); | 174 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); |
120 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); | 175 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); |
121 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo"); | 176 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo"); |
122 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType()); | 177 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType()); |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 documentUrls1.push_back("http://example.de"); | 581 documentUrls1.push_back("http://example.de"); |
527 | 582 |
528 ASSERT_TRUE(filterEngine->IsElemhideWhitelisted( | 583 ASSERT_TRUE(filterEngine->IsElemhideWhitelisted( |
529 "http://example.com", | 584 "http://example.com", |
530 documentUrls1)); | 585 documentUrls1)); |
531 | 586 |
532 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( | 587 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( |
533 "http://example.co.uk", | 588 "http://example.co.uk", |
534 documentUrls1)); | 589 documentUrls1)); |
535 } | 590 } |
| 591 |
| 592 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) |
| 593 { |
| 594 AppInfo appInfo; |
| 595 appInfo.locale = "zh"; |
| 596 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu
s.org/easylistchina+easylist.txt"; |
| 597 auto jsEngine = createJsEngine(appInfo); |
| 598 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); |
| 599 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 600 ASSERT_EQ(2u, subscriptions.size()); |
| 601 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr
ing(); |
| 602 SubscriptionPtr aaSubscription; |
| 603 SubscriptionPtr langSubscription; |
| 604 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) |
| 605 { |
| 606 aaSubscription = subscriptions[0]; |
| 607 langSubscription = subscriptions[1]; |
| 608 } |
| 609 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl) |
| 610 { |
| 611 aaSubscription = subscriptions[1]; |
| 612 langSubscription = subscriptions[0]; |
| 613 } |
| 614 ASSERT_NE(nullptr, aaSubscription); |
| 615 ASSERT_NE(nullptr, langSubscription); |
| 616 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString()); |
| 617 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString(
)); |
| 618 } |
| 619 |
| 620 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) |
| 621 { |
| 622 auto jsEngine = createJsEngine(); |
| 623 FilterEngine::Prefs preSettings; |
| 624 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(false); |
| 625 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr
eSettings)); |
| 626 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 627 EXPECT_EQ(0u, subscriptions.size()); |
| 628 } |
OLD | NEW |