| 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 <sstream> | 18 #include <sstream> |
| 19 | 19 |
| 20 #include "BaseJsTest.h" | 20 #include "BaseJsTest.h" |
| 21 #include "../src/JsEngineTransition.h" |
| 21 | 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 TestFileSystem : public LazyFileSystem | 27 class TestFileSystem : public LazyFileSystem |
| 27 { | 28 { |
| 28 public: | 29 public: |
| 29 std::string prefsContents; | 30 std::string prefsContents; |
| 30 | 31 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 class PrefsTest : public ::testing::Test | 65 class PrefsTest : public ::testing::Test |
| 65 { | 66 { |
| 66 protected: | 67 protected: |
| 67 std::shared_ptr<TestFileSystem> fileSystem; | 68 std::shared_ptr<TestFileSystem> fileSystem; |
| 68 AdblockPlus::JsEnginePtr jsEngine; | 69 AdblockPlus::JsEnginePtr jsEngine; |
| 69 FilterEnginePtr filterEngine; | 70 FilterEnginePtr filterEngine; |
| 70 | 71 |
| 71 void SetUp() | 72 void SetUp() |
| 72 { | 73 { |
| 73 fileSystem = std::make_shared<TestFileSystem>(); | 74 fileSystem = std::make_shared<TestFileSystem>(); |
| 74 Reset(); | 75 Construct(boolPrefsInitializer()); |
| 75 } | 76 } |
| 76 | 77 |
| 77 void Reset(const AdblockPlus::FilterEngine::Prefs& preconfiguredPrefs = | 78 void TearDown() |
| 78 AdblockPlus::FilterEngine::Prefs()) | |
| 79 { | 79 { |
| 80 Destroy(); |
| 81 fileSystem.reset(); |
| 82 } |
| 83 |
| 84 // Type of initializer values for C++ boolean preferences |
| 85 typedef std::map<std::string, bool> boolPrefsInitializer; |
| 86 |
| 87 void Reset(const boolPrefsInitializer& boolPrefs = boolPrefsInitializer()) |
| 88 { |
| 89 Destroy(); |
| 90 Construct(boolPrefs); |
| 91 } |
| 92 |
| 93 private: |
| 94 void Construct(const boolPrefsInitializer& boolPrefs) |
| 95 { |
| 96 // New JS engine and its resources |
| 80 jsEngine = CreateJsEngine(); | 97 jsEngine = CreateJsEngine(); |
| 81 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem)); | 98 jsEngine->SetLogSystem(std::make_shared<LazyLogSystem>()); |
| 82 jsEngine->SetFileSystem(fileSystem); | 99 jsEngine->SetFileSystem(fileSystem); |
| 83 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); | 100 jsEngine->SetWebRequest(std::make_shared<LazyWebRequest>()); |
| 101 /* |
| 102 * Create preference values under the new JS engine. |
| 103 */ |
| 104 AdblockPlus::FilterEngine::Prefs jsValuePrefs; |
| 105 for (boolPrefsInitializer::const_iterator entry = boolPrefs.begin(); |
| 106 entry != boolPrefs.end(); ++entry) |
| 107 { |
| 108 jsValuePrefs[entry->first] = jsEngine->NewValue(entry->second); |
| 109 } |
| 110 // Use a new filter engine |
| 111 filterEngine = std::make_shared<AdblockPlus::FilterEngine>(jsEngine, jsVal
uePrefs); |
| 112 } |
| 84 | 113 |
| 85 filterEngine.reset( | 114 void Destroy() |
| 86 new AdblockPlus::FilterEngine(jsEngine, preconfiguredPrefs)); | 115 { |
| 116 filterEngine.reset(); |
| 117 ToInternal(jsEngine)->WaitForQuietScheduler(); |
| 118 jsEngine.reset(); |
| 119 EXPECT_EQ(0, jsEngine.use_count()); |
| 87 } | 120 } |
| 88 }; | 121 }; |
| 89 } | 122 } |
| 90 | 123 |
| 91 TEST_F(PrefsTest, PrefsGetSet) | 124 TEST_F(PrefsTest, PrefsGetSet) |
| 92 { | 125 { |
| 93 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()); | 126 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()); |
| 94 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 127 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
| 95 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); | 128 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
| 96 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); | 129 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 TEST_F(PrefsTest, SyntaxFailure) | 174 TEST_F(PrefsTest, SyntaxFailure) |
| 142 { | 175 { |
| 143 fileSystem->prefsContents = "{\"patternsbackupinterval\": 6, \"foo\"}"; | 176 fileSystem->prefsContents = "{\"patternsbackupinterval\": 6, \"foo\"}"; |
| 144 Reset(); | 177 Reset(); |
| 145 | 178 |
| 146 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 179 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
| 147 } | 180 } |
| 148 | 181 |
| 149 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) | 182 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) |
| 150 { | 183 { |
| 151 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 184 boolPrefsInitializer preconfiguredPrefs; |
| 152 preconfiguredPrefs["disable_auto_updates"] = jsEngine->NewValue(false); | 185 preconfiguredPrefs["disable_auto_updates"] = false; |
| 153 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); | 186 preconfiguredPrefs["suppress_first_run_page"] = true; |
| 154 Reset(preconfiguredPrefs); | 187 Reset(preconfiguredPrefs); |
| 155 | 188 |
| 156 ASSERT_TRUE(filterEngine->GetPref("disable_auto_updates")->IsBool()); | 189 ASSERT_TRUE(filterEngine->GetPref("disable_auto_updates")->IsBool()); |
| 157 ASSERT_FALSE(filterEngine->GetPref("disable_auto_updates")->AsBool()); | 190 ASSERT_FALSE(filterEngine->GetPref("disable_auto_updates")->AsBool()); |
| 158 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 191 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
| 159 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 192 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
| 160 } | 193 } |
| 161 | 194 |
| 162 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported) | 195 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported) |
| 163 { | 196 { |
| 164 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 197 boolPrefsInitializer preconfiguredPrefs; |
| 165 preconfiguredPrefs["unsupported_preconfig"] = jsEngine->NewValue(true); | 198 preconfiguredPrefs["unsupported_preconfig"] = true; |
| 166 Reset(preconfiguredPrefs); | 199 Reset(preconfiguredPrefs); |
| 167 | 200 |
| 168 ASSERT_TRUE(filterEngine->GetPref("unsupported_preconfig")->IsUndefined()); | 201 ASSERT_TRUE(filterEngine->GetPref("unsupported_preconfig")->IsUndefined()); |
| 169 } | 202 } |
| 170 | 203 |
| 171 TEST_F(PrefsTest, PreconfiguredPrefsOverride) | 204 TEST_F(PrefsTest, PreconfiguredPrefsOverride) |
| 172 { | 205 { |
| 173 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 206 boolPrefsInitializer preconfiguredPrefs; |
| 174 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); | 207 preconfiguredPrefs["suppress_first_run_page"] = true; |
| 175 Reset(preconfiguredPrefs); | 208 Reset(preconfiguredPrefs); |
| 176 | 209 |
| 177 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); | 210 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); |
| 178 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 211 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
| 179 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 212 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
| 180 } | 213 } |
| 181 | 214 |
| 182 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) | 215 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) |
| 183 { | 216 { |
| 184 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 217 boolPrefsInitializer preconfiguredPrefs; |
| 185 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); | 218 preconfiguredPrefs["suppress_first_run_page"] = true; |
| 186 Reset(preconfiguredPrefs); | 219 Reset(preconfiguredPrefs); |
| 187 | 220 |
| 188 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 221 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
| 189 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 222 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
| 190 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); | 223 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); |
| 191 | 224 |
| 192 std::this_thread::sleep_for(std::chrono::milliseconds(100)); | 225 std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 193 | 226 |
| 194 ASSERT_FALSE(fileSystem->prefsContents.empty()); | 227 ASSERT_FALSE(fileSystem->prefsContents.empty()); |
| 195 | 228 |
| 196 Reset(preconfiguredPrefs); | 229 Reset(preconfiguredPrefs); |
| 197 | 230 |
| 198 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 231 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
| 199 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 232 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
| 200 } | 233 } |
| OLD | NEW |