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 = 100; |
| 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 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 std::weak_ptr<AdblockPlus::JsEngine> weakJsEngine; | 594 std::weak_ptr<AdblockPlus::JsEngine> weakJsEngine; |
540 { | 595 { |
541 auto jsEngine = AdblockPlus::JsEngine::New(); | 596 auto jsEngine = AdblockPlus::JsEngine::New(); |
542 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); | 597 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); |
543 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); | 598 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); |
544 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); | 599 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); |
545 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine))
; | 600 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine))
; |
546 } | 601 } |
547 EXPECT_FALSE(weakJsEngine.lock()); | 602 EXPECT_FALSE(weakJsEngine.lock()); |
548 } | 603 } |
| 604 |
| 605 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) |
| 606 { |
| 607 AppInfo appInfo; |
| 608 appInfo.locale = "zh"; |
| 609 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu
s.org/easylistchina+easylist.txt"; |
| 610 auto jsEngine = createJsEngine(appInfo); |
| 611 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); |
| 612 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 613 ASSERT_EQ(2u, subscriptions.size()); |
| 614 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr
ing(); |
| 615 SubscriptionPtr aaSubscription; |
| 616 SubscriptionPtr langSubscription; |
| 617 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) |
| 618 { |
| 619 aaSubscription = subscriptions[0]; |
| 620 langSubscription = subscriptions[1]; |
| 621 } |
| 622 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl) |
| 623 { |
| 624 aaSubscription = subscriptions[1]; |
| 625 langSubscription = subscriptions[0]; |
| 626 } |
| 627 ASSERT_NE(nullptr, aaSubscription); |
| 628 ASSERT_NE(nullptr, langSubscription); |
| 629 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString()); |
| 630 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString(
)); |
| 631 } |
| 632 |
| 633 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) |
| 634 { |
| 635 auto jsEngine = createJsEngine(); |
| 636 FilterEngine::Prefs preSettings; |
| 637 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(false); |
| 638 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr
eSettings)); |
| 639 const auto subscriptions = filterEngine->GetListedSubscriptions(); |
| 640 EXPECT_EQ(0u, subscriptions.size()); |
| 641 } |
OLD | NEW |