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

Unified Diff: test/FilterEngine.cpp

Issue 29363607: Issue 4612 - enable AA on first run and make automatic adding of any subscription optional (Closed)
Patch Set: address comments Created Nov. 21, 2016, 2:01 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/prefs.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/FilterEngine.cpp
diff --git a/test/FilterEngine.cpp b/test/FilterEngine.cpp
index 05cecfa71a4a2422204edf3dfeb0e0a12de905b6..b5fed9b6eeb986da3c365b973636414e90fc0393 100644
--- a/test/FilterEngine.cpp
+++ b/test/FilterEngine.cpp
@@ -16,7 +16,9 @@
*/
#include "BaseJsTest.h"
+#include <thread>
+using namespace AdblockPlus;
namespace
{
typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
@@ -110,6 +112,50 @@ namespace
// Workaround for https://issues.adblockplus.org/ticket/1397.
void NoOpUpdaterCallback(const std::string&) {}
+
+ class FilterEngineWithFreshFolder : public ::testing::Test
+ {
+ protected:
+ FileSystemPtr m_fileSystem;
+ std::weak_ptr<JsEngine> m_jsEngine;
+
+ void SetUp() override
+ {
+ m_fileSystem.reset(new DefaultFileSystem());
+ // Since there are neither in memory FS nor functionality to work with
+ // directories use the hack: manually clean the directory.
+ removeFileIfExists("patterns.ini");
+ removeFileIfExists("prefs.json");
+ }
+ JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo())
+ {
+ auto jsEngine = JsEngine::New(appInfo);
+ m_jsEngine = jsEngine;
+ jsEngine->SetFileSystem(m_fileSystem);
+ jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest()));
+ jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
+ return jsEngine;
+ }
+ void TearDown() override
+ {
+ removeFileIfExists("patterns.ini");
+ removeFileIfExists("prefs.json");
+ m_fileSystem.reset();
+ }
+ void removeFileIfExists(const std::string& path)
+ {
+ {
+ // Hack: allow IO to finish currently running operations, in particular
+ // writing into files. Otherwise we get Permission denied.
+ int i = 100;
+ while (i-- > 0 && m_jsEngine.lock()) {
+ std::this_thread::sleep_for(std::chrono::seconds(2));
+ }
+ }
+ if (m_fileSystem->Stat(path).exists)
+ m_fileSystem->Remove(path);
+ }
+ };
}
TEST_F(FilterEngineTest, FilterCreation)
@@ -546,3 +592,67 @@ TEST(NewFilterEngineTest, MemoryLeak_NoCircularReferences)
}
EXPECT_FALSE(weakJsEngine.lock());
}
+
+TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
+{
+ AppInfo appInfo;
+ appInfo.locale = "zh";
+ const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplus.org/easylistchina+easylist.txt";
+ auto jsEngine = createJsEngine(appInfo);
+ auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine));
+ const auto subscriptions = filterEngine->GetListedSubscriptions();
+ ASSERT_EQ(2u, subscriptions.size());
+ const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsString();
+ SubscriptionPtr aaSubscription;
+ SubscriptionPtr langSubscription;
+ if (subscriptions[0]->GetProperty("url")->AsString() == aaUrl)
+ {
+ aaSubscription = subscriptions[0];
+ langSubscription = subscriptions[1];
+ } else if (subscriptions[1]->GetProperty("url")->AsString() == aaUrl)
+ {
+ aaSubscription = subscriptions[1];
+ langSubscription = subscriptions[0];
+ }
+ ASSERT_NE(nullptr, aaSubscription);
+ ASSERT_NE(nullptr, langSubscription);
+ EXPECT_EQ(aaUrl, aaSubscription->GetProperty("url")->AsString());
+ EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url")->AsString());
+}
+
+TEST_F(FilterEngineWithFreshFolder, DisableAASubscriptionChoosingOnFirstRun)
+{
+ AppInfo appInfo;
+ appInfo.locale = "sk";
+ const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplus.org/easylistczechslovak+easylist.txt";
+ auto jsEngine = createJsEngine(appInfo);
+ FilterEngine::Prefs preSettings;
+ preSettings["first_run_enable_acceptable_ads"] = jsEngine->NewValue(false);
+ auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, preSettings));
+ const auto subscriptions = filterEngine->GetListedSubscriptions();
+ ASSERT_EQ(1u, subscriptions.size());
+ EXPECT_EQ(langSubscriptionUrl, subscriptions[0]->GetProperty("url")->AsString());
+}
+
+TEST_F(FilterEngineWithFreshFolder, DisableLangSubscriptionChoosingOnFirstRun)
+{
+ auto jsEngine = createJsEngine();
+ FilterEngine::Prefs preSettings;
+ preSettings["first_run_enable_current_locale_subscription"] = jsEngine->NewValue(false);
+ auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, preSettings));
+ const auto subscriptions = filterEngine->GetListedSubscriptions();
+ ASSERT_EQ(1u, subscriptions.size());
+ const auto aaUrl = filterEngine->GetPref("subscriptions_exceptionsurl")->AsString();
+ EXPECT_EQ(aaUrl, subscriptions[0]->GetProperty("url")->AsString());
+}
+
+TEST_F(FilterEngineWithFreshFolder, DisableLangAndAASubscriptionsChoosingOnFirstRun)
+{
+ auto jsEngine = createJsEngine();
+ FilterEngine::Prefs preSettings;
+ preSettings["first_run_enable_acceptable_ads"] = jsEngine->NewValue(false);
+ preSettings["first_run_enable_current_locale_subscription"] = jsEngine->NewValue(false);
+ auto filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine, preSettings));
+ const auto subscriptions = filterEngine->GetListedSubscriptions();
+ EXPECT_EQ(0u, subscriptions.size());
+}
« no previous file with comments | « lib/prefs.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld