| 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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 namespace | 25 namespace |
| 26 { | 26 { |
| 27 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; | 27 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; |
| 28 | 28 |
| 29 class TestFileSystem : public LazyFileSystem | 29 class TestFileSystem : public LazyFileSystem |
| 30 { | 30 { |
| 31 public: | 31 public: |
| 32 IOBuffer prefsContents; | 32 IOBuffer prefsContents; |
| 33 | 33 |
| 34 IOBuffer Read(const std::string& path) const | 34 void Read(const std::string& path, const ReadCallback& callback) const overr
ide |
| 35 { | 35 { |
| 36 if (path == "prefs.json" && !prefsContents.empty()) | 36 scheduler([this, path, callback] |
| 37 return prefsContents; | 37 { |
| 38 if (path == "prefs.json" && !prefsContents.empty()) |
| 39 { |
| 40 callback(IOBuffer(prefsContents.cbegin(), prefsContents.cend()), ""); |
| 41 return; |
| 42 } |
| 38 | 43 |
| 39 return LazyFileSystem::Read(path); | 44 LazyFileSystem::Read(path, callback); |
| 45 }); |
| 40 } | 46 } |
| 41 | 47 |
| 42 void Write(const std::string& path, const IOBuffer& content) | 48 void Write(const std::string& path, const IOBuffer& content, const Callback&
callback) override |
| 43 { | 49 { |
| 44 if (path == "prefs.json") | 50 scheduler([this, path, content, callback] |
| 45 prefsContents = content; | 51 { |
| 46 else | 52 if (path == "prefs.json") |
| 47 LazyFileSystem::Write(path, content); | 53 { |
| 54 prefsContents = content; |
| 55 callback(""); |
| 56 } |
| 57 }); |
| 48 } | 58 } |
| 49 | 59 |
| 50 StatResult Stat(const std::string& path) const | 60 void Stat(const std::string& path, const StatCallback& callback) const overr
ide |
| 51 { | 61 { |
| 52 if (path == "prefs.json") | 62 scheduler([this, path, callback] |
| 53 { | 63 { |
| 54 StatResult result; | 64 if (path == "prefs.json") |
| 55 result.exists = result.isFile = !prefsContents.empty(); | 65 { |
| 56 return result; | 66 StatResult result; |
| 57 } | 67 result.exists = result.isFile = !prefsContents.empty(); |
| 68 callback(result, ""); |
| 69 return; |
| 70 } |
| 58 | 71 |
| 59 return LazyFileSystem::Stat(path); | 72 LazyFileSystem::Stat(path, callback); |
| 73 }); |
| 60 } | 74 } |
| 61 }; | 75 }; |
| 62 | 76 |
| 63 class PrefsTest : public ::testing::Test | 77 class PrefsTest : public ::testing::Test |
| 64 { | 78 { |
| 65 protected: | 79 protected: |
| 66 std::shared_ptr<TestFileSystem> fileSystem; | 80 std::shared_ptr<TestFileSystem> fileSystem; |
| 67 AdblockPlus::JsEnginePtr jsEngine; | 81 AdblockPlus::JsEnginePtr jsEngine; |
| 68 | 82 |
| 69 void SetUp() | 83 void SetUp() |
| (...skipping 10 matching lines...) Expand all Loading... |
| 80 jsEngineParams.logSystem.reset(new LazyLogSystem()); | 94 jsEngineParams.logSystem.reset(new LazyLogSystem()); |
| 81 jsEngineParams.timer.reset(new NoopTimer()); | 95 jsEngineParams.timer.reset(new NoopTimer()); |
| 82 jsEngine = CreateJsEngine(std::move(jsEngineParams)); | 96 jsEngine = CreateJsEngine(std::move(jsEngineParams)); |
| 83 } | 97 } |
| 84 | 98 |
| 85 FilterEnginePtr CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& p
reconfiguredPrefs = | 99 FilterEnginePtr CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& p
reconfiguredPrefs = |
| 86 AdblockPlus::FilterEngine::Prefs()) | 100 AdblockPlus::FilterEngine::Prefs()) |
| 87 { | 101 { |
| 88 AdblockPlus::FilterEngine::CreationParameters createParams; | 102 AdblockPlus::FilterEngine::CreationParameters createParams; |
| 89 createParams.preconfiguredPrefs = preconfiguredPrefs; | 103 createParams.preconfiguredPrefs = preconfiguredPrefs; |
| 90 return AdblockPlus::FilterEngine::Create(jsEngine, createParams); | 104 return ::CreateFilterEngine(*fileSystem, jsEngine, createParams); |
| 91 } | 105 } |
| 92 }; | 106 }; |
| 93 } | 107 } |
| 94 | 108 |
| 95 TEST_F(PrefsTest, PrefsGetSet) | 109 TEST_F(PrefsTest, PrefsGetSet) |
| 96 { | 110 { |
| 97 auto filterEngine = CreateFilterEngine(); | 111 auto filterEngine = CreateFilterEngine(); |
| 98 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); | 112 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); |
| 99 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 113 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); |
| 100 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); | 114 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 117 { | 131 { |
| 118 { | 132 { |
| 119 auto filterEngine = CreateFilterEngine(); | 133 auto filterEngine = CreateFilterEngine(); |
| 120 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); | 134 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); |
| 121 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 135 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); |
| 122 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); | 136 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); |
| 123 | 137 |
| 124 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); | 138 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); |
| 125 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); | 139 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); |
| 126 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false))
; | 140 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false))
; |
| 127 | |
| 128 AdblockPlus::Sleep(100); | |
| 129 } | 141 } |
| 130 ASSERT_FALSE(fileSystem->prefsContents.empty()); | 142 ASSERT_FALSE(fileSystem->prefsContents.empty()); |
| 131 | 143 |
| 132 { | 144 { |
| 133 ResetJsEngine(); | 145 ResetJsEngine(); |
| 134 auto filterEngine = CreateFilterEngine(); | 146 auto filterEngine = CreateFilterEngine(); |
| 135 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString()); | 147 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString()); |
| 136 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt()); | 148 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt()); |
| 137 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); | 149 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); |
| 138 } | 150 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) | 206 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) |
| 195 { | 207 { |
| 196 { | 208 { |
| 197 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 209 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
| 198 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru
e)); | 210 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru
e)); |
| 199 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); | 211 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); |
| 200 | 212 |
| 201 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); | 213 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); |
| 202 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool()); | 214 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool()); |
| 203 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); | 215 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); |
| 204 | |
| 205 AdblockPlus::Sleep(100); | |
| 206 } | 216 } |
| 207 ASSERT_FALSE(fileSystem->prefsContents.empty()); | 217 ASSERT_FALSE(fileSystem->prefsContents.empty()); |
| 208 | 218 |
| 209 { | 219 { |
| 210 ResetJsEngine(); | 220 ResetJsEngine(); |
| 211 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 221 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
| 212 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru
e)); | 222 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru
e)); |
| 213 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); | 223 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); |
| 214 | 224 |
| 215 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); | 225 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); |
| 216 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool()); | 226 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool()); |
| 217 } | 227 } |
| 218 } | 228 } |
| OLD | NEW |