Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/FilterEngine.cpp

Issue 29363607: Issue 4612 - enable AA on first run and make automatic adding of any subscription optional (Closed)
Left Patch Set: address comments Created Nov. 21, 2016, 2:01 p.m.
Right Patch Set: reduce number of attempts to remove test files Created Dec. 2, 2016, 2:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/prefs.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 #include <thread>
20 20
21 using namespace AdblockPlus; 21 using namespace AdblockPlus;
22
22 namespace 23 namespace
23 { 24 {
24 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; 25 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
25 26
26 class VeryLazyFileSystem : public LazyFileSystem 27 class VeryLazyFileSystem : public LazyFileSystem
27 { 28 {
28 public: 29 public:
29 StatResult Stat(const std::string& path) const 30 StatResult Stat(const std::string& path) const
30 { 31 {
31 return StatResult(); 32 return StatResult();
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 // https://issues.adblockplus.org/ticket/1378. 110 // https://issues.adblockplus.org/ticket/1378.
110 int& timesCalled; 111 int& timesCalled;
111 }; 112 };
112 113
113 // Workaround for https://issues.adblockplus.org/ticket/1397. 114 // Workaround for https://issues.adblockplus.org/ticket/1397.
114 void NoOpUpdaterCallback(const std::string&) {} 115 void NoOpUpdaterCallback(const std::string&) {}
115 116
116 class FilterEngineWithFreshFolder : public ::testing::Test 117 class FilterEngineWithFreshFolder : public ::testing::Test
117 { 118 {
118 protected: 119 protected:
119 FileSystemPtr m_fileSystem; 120 FileSystemPtr fileSystem;
120 std::weak_ptr<JsEngine> m_jsEngine; 121 std::weak_ptr<JsEngine> weakJsEngine;
121 122
122 void SetUp() override 123 void SetUp() override
123 { 124 {
124 m_fileSystem.reset(new DefaultFileSystem()); 125 fileSystem.reset(new DefaultFileSystem());
125 // Since there are neither in memory FS nor functionality to work with 126 // Since there is neither in memory FS nor functionality to work with
126 // directories use the hack: manually clean the directory. 127 // directories use the hack: manually clean the directory.
127 removeFileIfExists("patterns.ini"); 128 removeFileIfExists("patterns.ini");
128 removeFileIfExists("prefs.json"); 129 removeFileIfExists("prefs.json");
129 } 130 }
130 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) 131 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo())
131 { 132 {
132 auto jsEngine = JsEngine::New(appInfo); 133 auto jsEngine = JsEngine::New(appInfo);
133 m_jsEngine = jsEngine; 134 weakJsEngine = jsEngine;
134 jsEngine->SetFileSystem(m_fileSystem); 135 jsEngine->SetFileSystem(fileSystem);
135 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); 136 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest()));
136 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); 137 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
137 return jsEngine; 138 return jsEngine;
138 } 139 }
139 void TearDown() override 140 void TearDown() override
140 { 141 {
141 removeFileIfExists("patterns.ini"); 142 removeFileIfExists("patterns.ini");
142 removeFileIfExists("prefs.json"); 143 removeFileIfExists("prefs.json");
143 m_fileSystem.reset(); 144 fileSystem.reset();
144 } 145 }
145 void removeFileIfExists(const std::string& path) 146 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 auto safeRemove = [this, &path]()->bool
147 { 151 {
148 // Hack: allow IO to finish currently running operations, in particular 152 try
149 // writing into files. Otherwise we get Permission denied. 153 {
150 int i = 100; 154 if (fileSystem->Stat(path).exists)
151 while (i-- > 0 && m_jsEngine.lock()) { 155 fileSystem->Remove(path);
152 std::this_thread::sleep_for(std::chrono::seconds(2)); 156 return true;
153 } 157 }
154 } 158 catch (...)
155 if (m_fileSystem->Stat(path).exists) 159 {
156 m_fileSystem->Remove(path); 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));
157 } 166 }
158 }; 167 };
159 } 168 }
160 169
161 TEST_F(FilterEngineTest, FilterCreation) 170 TEST_F(FilterEngineTest, FilterCreation)
162 { 171 {
163 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); 172 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo");
164 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); 173 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType());
165 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); 174 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo");
166 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); 175 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType());
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 582
574 ASSERT_TRUE(filterEngine->IsElemhideWhitelisted( 583 ASSERT_TRUE(filterEngine->IsElemhideWhitelisted(
575 "http://example.com", 584 "http://example.com",
576 documentUrls1)); 585 documentUrls1));
577 586
578 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( 587 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted(
579 "http://example.co.uk", 588 "http://example.co.uk",
580 documentUrls1)); 589 documentUrls1));
581 } 590 }
582 591
583 TEST(NewFilterEngineTest, MemoryLeak_NoCircularReferences)
584 {
585 std::weak_ptr<AdblockPlus::JsEngine> weakJsEngine;
586 {
587 auto jsEngine = AdblockPlus::JsEngine::New();
588 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem()));
589 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest()));
590 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
591 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)) ;
592 }
593 EXPECT_FALSE(weakJsEngine.lock());
594 }
595
596 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) 592 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
597 { 593 {
598 AppInfo appInfo; 594 AppInfo appInfo;
599 appInfo.locale = "zh"; 595 appInfo.locale = "zh";
600 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt"; 596 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt";
601 auto jsEngine = createJsEngine(appInfo); 597 auto jsEngine = createJsEngine(appInfo);
602 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); 598 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine));
603 const auto subscriptions = filterEngine->GetListedSubscriptions(); 599 const auto subscriptions = filterEngine->GetListedSubscriptions();
604 ASSERT_EQ(2u, subscriptions.size()); 600 ASSERT_EQ(2u, subscriptions.size());
605 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing(); 601 const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsStr ing();
606 SubscriptionPtr aaSubscription; 602 SubscriptionPtr aaSubscription;
607 SubscriptionPtr langSubscription; 603 SubscriptionPtr langSubscription;
608 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl) 604 if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl)
609 { 605 {
610 aaSubscription = subscriptions[0]; 606 aaSubscription = subscriptions[0];
611 langSubscription = subscriptions[1]; 607 langSubscription = subscriptions[1];
612 } else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl) 608 }
609 else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl)
613 { 610 {
614 aaSubscription = subscriptions[1]; 611 aaSubscription = subscriptions[1];
615 langSubscription = subscriptions[0]; 612 langSubscription = subscriptions[0];
616 } 613 }
617 ASSERT_NE(nullptr, aaSubscription); 614 ASSERT_NE(nullptr, aaSubscription);
618 ASSERT_NE(nullptr, langSubscription); 615 ASSERT_NE(nullptr, langSubscription);
619 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString()); 616 EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString());
620 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( )); 617 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString( ));
621 } 618 }
622 619
623 TEST_F(FilterEngineWithFreshFolder, DisableAASubscriptionChoosingOnFirstRun) 620 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
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 { 621 {
639 auto jsEngine = createJsEngine(); 622 auto jsEngine = createJsEngine();
640 FilterEngine::Prefs preSettings; 623 FilterEngine::Prefs preSettings;
641 preSettings["first_run_enable_current_locale_subscription"] = jsEngine->NewVal ue(false); 624 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(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)); 625 auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, pr eSettings));
656 const auto subscriptions = filterEngine->GetListedSubscriptions(); 626 const auto subscriptions = filterEngine->GetListedSubscriptions();
657 EXPECT_EQ(0u, subscriptions.size()); 627 EXPECT_EQ(0u, subscriptions.size());
658 } 628 }
LEFTRIGHT
« lib/prefs.js ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld