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> |
19 | 20 |
| 21 using namespace AdblockPlus; |
20 namespace | 22 namespace |
21 { | 23 { |
22 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; | 24 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; |
23 | 25 |
24 class VeryLazyFileSystem : public LazyFileSystem | 26 class VeryLazyFileSystem : public LazyFileSystem |
25 { | 27 { |
26 public: | 28 public: |
27 StatResult Stat(const std::string& path) const | 29 StatResult Stat(const std::string& path) const |
28 { | 30 { |
29 return StatResult(); | 31 return StatResult(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 } | 105 } |
104 | 106 |
105 private: | 107 private: |
106 // We currently cannot store timesCalled in the functor, see: | 108 // We currently cannot store timesCalled in the functor, see: |
107 // https://issues.adblockplus.org/ticket/1378. | 109 // https://issues.adblockplus.org/ticket/1378. |
108 int& timesCalled; | 110 int& timesCalled; |
109 }; | 111 }; |
110 | 112 |
111 // Workaround for https://issues.adblockplus.org/ticket/1397. | 113 // Workaround for https://issues.adblockplus.org/ticket/1397. |
112 void NoOpUpdaterCallback(const std::string&) {} | 114 void NoOpUpdaterCallback(const std::string&) {} |
| 115 |
| 116 class FilterEngineWithFreshFolder : public ::testing::Test |
| 117 { |
| 118 protected: |
| 119 FileSystemPtr m_fileSystem; |
| 120 std::weak_ptr<JsEngine> m_jsEngine; |
| 121 |
| 122 void SetUp() override |
| 123 { |
| 124 m_fileSystem.reset(new DefaultFileSystem()); |
| 125 // Since there are neither in memory FS nor functionality to work with |
| 126 // directories use the hack: manually clean the directory. |
| 127 removeFileIfExists("patterns.ini"); |
| 128 removeFileIfExists("prefs.json"); |
| 129 } |
| 130 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) |
| 131 { |
| 132 auto jsEngine = JsEngine::New(appInfo); |
| 133 m_jsEngine = jsEngine; |
| 134 jsEngine->SetFileSystem(m_fileSystem); |
| 135 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); |
| 136 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); |
| 137 return jsEngine; |
| 138 } |
| 139 void TearDown() override |
| 140 { |
| 141 removeFileIfExists("patterns.ini"); |
| 142 removeFileIfExists("prefs.json"); |
| 143 m_fileSystem.reset(); |
| 144 } |
| 145 void removeFileIfExists(const std::string& path) |
| 146 { |
| 147 { |
| 148 // Hack: allow IO to finish currently running operations, in particular |
| 149 // writing into files. Otherwise we get Permission denied. |
| 150 int i = 100; |
| 151 while (i-- > 0 && m_jsEngine.lock()) { |
| 152 std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 153 } |
| 154 } |
| 155 if (m_fileSystem->Stat(path).exists) |
| 156 m_fileSystem->Remove(path); |
| 157 } |
| 158 }; |
113 } | 159 } |
114 | 160 |
115 TEST_F(FilterEngineTest, FilterCreation) | 161 TEST_F(FilterEngineTest, FilterCreation) |
116 { | 162 { |
117 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); | 163 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); |
118 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); | 164 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); |
119 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); | 165 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); |
120 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); | 166 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); |
121 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo"); | 167 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo"); |
122 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType()); | 168 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType()); |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 std::weak_ptr<AdblockPlus::JsEngine> weakJsEngine; | 585 std::weak_ptr<AdblockPlus::JsEngine> weakJsEngine; |
540 { | 586 { |
541 auto jsEngine = AdblockPlus::JsEngine::New(); | 587 auto jsEngine = AdblockPlus::JsEngine::New(); |
542 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); | 588 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); |
543 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); | 589 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); |
544 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); | 590 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); |
545 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine))
; | 591 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine))
; |
546 } | 592 } |
547 EXPECT_FALSE(weakJsEngine.lock()); | 593 EXPECT_FALSE(weakJsEngine.lock()); |
548 } | 594 } |
| 595 |
| 596 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) |
| 597 { |
| 598 AppInfo appInfo; |
| 599 appInfo.locale = "zh"; |
| 600 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu
s.org/easylistchina+easylist.txt"; |
| 601 auto jsEngine = createJsEngine(appInfo); |
| 602 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); |
| 603 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 604 ASSERT_EQ(2u, subscriptions.size()); |
| 605 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr
ing(); |
| 606 SubscriptionPtr aaSubscription; |
| 607 SubscriptionPtr langSubscription; |
| 608 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) |
| 609 { |
| 610 aaSubscription = subscriptions[0]; |
| 611 langSubscription = subscriptions[1]; |
| 612 } else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl) |
| 613 { |
| 614 aaSubscription = subscriptions[1]; |
| 615 langSubscription = subscriptions[0]; |
| 616 } |
| 617 ASSERT_NE(nullptr, aaSubscription); |
| 618 ASSERT_NE(nullptr, langSubscription); |
| 619 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString()); |
| 620 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString(
)); |
| 621 } |
| 622 |
| 623 TEST_F(FilterEngineWithFreshFolder, DisableAASubscriptionChoosingOnFirstRun) |
| 624 { |
| 625 AppInfo appInfo; |
| 626 appInfo.locale = "sk"; |
| 627 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu
s.org/easylistczechslovak+easylist.txt"; |
| 628 auto jsEngine = createJsEngine(appInfo); |
| 629 FilterEngine::Prefs preSettings; |
| 630 preSettings["first_run_enable_acceptable_ads"] = jsEngine->NewValue(false); |
| 631 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr
eSettings)); |
| 632 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 633 ASSERT_EQ(1u, subscriptions.size()); |
| 634 EXPECT_EQ(langSubscriptionUrl, subscriptions[0]->GetProperty("url")->AsString(
)); |
| 635 } |
| 636 |
| 637 TEST_F(FilterEngineWithFreshFolder, DisableLangSubscriptionChoosingOnFirstRun) |
| 638 { |
| 639 auto jsEngine = createJsEngine(); |
| 640 FilterEngine::Prefs preSettings; |
| 641 preSettings["first_run_enable_current_locale_subscription"] = jsEngine->NewVal
ue(false); |
| 642 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr
eSettings)); |
| 643 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 644 ASSERT_EQ(1u, subscriptions.size()); |
| 645 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr
ing(); |
| 646 EXPECT_EQ(aaUrl, subscriptions[0]->GetProperty("url")->AsString()); |
| 647 } |
| 648 |
| 649 TEST_F(FilterEngineWithFreshFolder, DisableLangAndAASubscriptionsChoosingOnFirst
Run) |
| 650 { |
| 651 auto jsEngine = createJsEngine(); |
| 652 FilterEngine::Prefs preSettings; |
| 653 preSettings["first_run_enable_acceptable_ads"] = jsEngine->NewValue(false); |
| 654 preSettings["first_run_enable_current_locale_subscription"] = jsEngine->NewVal
ue(false); |
| 655 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr
eSettings)); |
| 656 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 657 EXPECT_EQ(0u, subscriptions.size()); |
| 658 } |
OLD | NEW |