LEFT | RIGHT |
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 19 matching lines...) Expand all Loading... |
30 std::string prefsContents; | 30 std::string prefsContents; |
31 | 31 |
32 std::shared_ptr<std::istream> Read(const std::string& path) const | 32 std::shared_ptr<std::istream> Read(const std::string& path) const |
33 { | 33 { |
34 if (path == "prefs.json" && !prefsContents.empty()) | 34 if (path == "prefs.json" && !prefsContents.empty()) |
35 return std::shared_ptr<std::istream>(new std::istringstream(prefsContent
s)); | 35 return std::shared_ptr<std::istream>(new std::istringstream(prefsContent
s)); |
36 | 36 |
37 return LazyFileSystem::Read(path); | 37 return LazyFileSystem::Read(path); |
38 } | 38 } |
39 | 39 |
40 void Write(const std::string& path, const std::istream& content) | 40 void Write(const std::string& path, std::istream& content) |
41 { | 41 { |
42 if (path == "prefs.json") | 42 if (path == "prefs.json") |
43 { | 43 { |
44 std::stringstream ss; | 44 std::stringstream ss; |
45 ss << content.rdbuf(); | 45 ss << content.rdbuf(); |
46 prefsContents = ss.str(); | 46 prefsContents = ss.str(); |
47 } | 47 } |
48 else | 48 else |
49 LazyFileSystem::Write(path, content); | 49 LazyFileSystem::Write(path, content); |
50 } | 50 } |
(...skipping 10 matching lines...) Expand all Loading... |
61 return LazyFileSystem::Stat(path); | 61 return LazyFileSystem::Stat(path); |
62 } | 62 } |
63 }; | 63 }; |
64 | 64 |
65 class PrefsTest : public ::testing::Test | 65 class PrefsTest : public ::testing::Test |
66 { | 66 { |
67 protected: | 67 protected: |
68 TestFileSystem* fileSystem; | 68 TestFileSystem* fileSystem; |
69 AdblockPlus::FileSystemPtr fileSystemPtr; | 69 AdblockPlus::FileSystemPtr fileSystemPtr; |
70 AdblockPlus::JsEnginePtr jsEngine; | 70 AdblockPlus::JsEnginePtr jsEngine; |
71 FilterEnginePtr filterEngine; | |
72 | 71 |
73 void SetUp() | 72 void SetUp() |
74 { | 73 { |
75 fileSystem = new TestFileSystem(); | 74 fileSystem = new TestFileSystem(); |
76 fileSystemPtr.reset(fileSystem); | 75 fileSystemPtr.reset(fileSystem); |
| 76 |
| 77 ResetJsEngine(); |
| 78 } |
| 79 |
| 80 void ResetJsEngine() |
| 81 { |
77 jsEngine = CreateJsEngine(); | 82 jsEngine = CreateJsEngine(); |
78 | |
79 Reset(); | |
80 } | |
81 | |
82 void Reset(const AdblockPlus::FilterEngine::Prefs& preconfiguredPrefs = | |
83 AdblockPlus::FilterEngine::Prefs()) | |
84 { | |
85 if (!jsEngine) | |
86 jsEngine = CreateJsEngine(); | |
87 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem)); | 83 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem)); |
88 jsEngine->SetFileSystem(fileSystemPtr); | 84 jsEngine->SetFileSystem(fileSystemPtr); |
89 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); | 85 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); |
| 86 } |
| 87 |
| 88 FilterEnginePtr CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& p
reconfiguredPrefs = |
| 89 AdblockPlus::FilterEngine::Prefs()) |
| 90 { |
90 AdblockPlus::FilterEngine::CreationParameters createParams; | 91 AdblockPlus::FilterEngine::CreationParameters createParams; |
91 createParams.preconfiguredPrefs = preconfiguredPrefs; | 92 createParams.preconfiguredPrefs = preconfiguredPrefs; |
92 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams); | 93 return AdblockPlus::FilterEngine::Create(jsEngine, createParams); |
93 } | 94 } |
94 }; | 95 }; |
95 } | 96 } |
96 | 97 |
97 TEST_F(PrefsTest, PrefsGetSet) | 98 TEST_F(PrefsTest, PrefsGetSet) |
98 { | 99 { |
| 100 auto filterEngine = CreateFilterEngine(); |
99 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()); | 101 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()); |
100 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 102 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
101 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); | 103 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
102 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); | 104 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); |
103 | 105 |
104 ASSERT_ANY_THROW(filterEngine->SetPref("patternsfile", jsEngine->NewValue(0)))
; | 106 ASSERT_ANY_THROW(filterEngine->SetPref("patternsfile", jsEngine->NewValue(0)))
; |
105 ASSERT_ANY_THROW(filterEngine->SetPref("patternsbackupinterval", jsEngine->New
Value(true))); | 107 ASSERT_ANY_THROW(filterEngine->SetPref("patternsbackupinterval", jsEngine->New
Value(true))); |
106 ASSERT_ANY_THROW(filterEngine->SetPref("subscriptions_autoupdate", jsEngine->N
ewValue("foo"))); | 108 ASSERT_ANY_THROW(filterEngine->SetPref("subscriptions_autoupdate", jsEngine->N
ewValue("foo"))); |
107 | 109 |
108 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); | 110 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); |
109 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); | 111 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); |
110 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)); | 112 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)); |
111 | 113 |
112 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString()); | 114 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString()); |
113 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 115 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
114 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); | 116 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
115 } | 117 } |
116 | 118 |
117 TEST_F(PrefsTest, PrefsPersist) | 119 TEST_F(PrefsTest, PrefsPersist) |
118 { | 120 { |
119 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()); | 121 { |
120 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 122 auto filterEngine = CreateFilterEngine(); |
121 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); | 123 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString())
; |
122 | 124 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
123 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); | 125 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
124 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); | 126 |
125 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)); | 127 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); |
126 | 128 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); |
127 AdblockPlus::Sleep(100); | 129 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false))
; |
128 | 130 |
| 131 AdblockPlus::Sleep(100); |
| 132 } |
129 ASSERT_FALSE(fileSystem->prefsContents.empty()); | 133 ASSERT_FALSE(fileSystem->prefsContents.empty()); |
130 | 134 |
131 Reset(); | 135 { |
132 | 136 ResetJsEngine(); |
133 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString()); | 137 auto filterEngine = CreateFilterEngine(); |
134 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 138 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString()); |
135 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); | 139 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
| 140 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool()); |
| 141 } |
136 } | 142 } |
137 | 143 |
138 TEST_F(PrefsTest, UnknownPrefs) | 144 TEST_F(PrefsTest, UnknownPrefs) |
139 { | 145 { |
140 fileSystem->prefsContents = "{\"foobar\":2, \"patternsbackupinterval\": 12}"; | 146 fileSystem->prefsContents = "{\"foobar\":2, \"patternsbackupinterval\": 12}"; |
141 Reset(); | 147 auto filterEngine = CreateFilterEngine(); |
142 | |
143 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); | 148 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined()); |
144 ASSERT_EQ(12, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 149 ASSERT_EQ(12, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
145 } | 150 } |
146 | 151 |
147 TEST_F(PrefsTest, SyntaxFailure) | 152 TEST_F(PrefsTest, SyntaxFailure) |
148 { | 153 { |
149 fileSystem->prefsContents = "{\"patternsbackupinterval\": 6, \"foo\"}"; | 154 fileSystem->prefsContents = "{\"patternsbackupinterval\": 6, \"foo\"}"; |
150 Reset(); | 155 auto filterEngine = CreateFilterEngine(); |
151 | 156 |
152 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); | 157 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt()); |
153 } | 158 } |
154 | 159 |
155 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) | 160 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) |
156 { | 161 { |
157 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 162 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
158 preconfiguredPrefs["disable_auto_updates"] = jsEngine->NewValue(false); | 163 preconfiguredPrefs["disable_auto_updates"] = jsEngine->NewValue(false); |
159 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); | 164 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); |
160 Reset(preconfiguredPrefs); | 165 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); |
161 | 166 |
162 ASSERT_TRUE(filterEngine->GetPref("disable_auto_updates")->IsBool()); | 167 ASSERT_TRUE(filterEngine->GetPref("disable_auto_updates")->IsBool()); |
163 ASSERT_FALSE(filterEngine->GetPref("disable_auto_updates")->AsBool()); | 168 ASSERT_FALSE(filterEngine->GetPref("disable_auto_updates")->AsBool()); |
164 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 169 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
165 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 170 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
166 } | 171 } |
167 | 172 |
168 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported) | 173 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported) |
169 { | 174 { |
170 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 175 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
171 preconfiguredPrefs["unsupported_preconfig"] = jsEngine->NewValue(true); | 176 preconfiguredPrefs["unsupported_preconfig"] = jsEngine->NewValue(true); |
172 Reset(preconfiguredPrefs); | 177 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); |
173 | 178 |
174 ASSERT_TRUE(filterEngine->GetPref("unsupported_preconfig")->IsUndefined()); | 179 ASSERT_TRUE(filterEngine->GetPref("unsupported_preconfig")->IsUndefined()); |
175 } | 180 } |
176 | 181 |
177 TEST_F(PrefsTest, PreconfiguredPrefsOverride) | 182 TEST_F(PrefsTest, PreconfiguredPrefsOverride) |
178 { | 183 { |
179 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 184 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
180 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); | 185 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); |
181 Reset(preconfiguredPrefs); | 186 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); |
182 | 187 |
183 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); | 188 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); |
184 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 189 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
185 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 190 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
186 } | 191 } |
187 | 192 |
188 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) | 193 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) |
189 { | 194 { |
190 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; | 195 { |
191 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); | 196 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
192 Reset(preconfiguredPrefs); | 197 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); |
193 | 198 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); |
194 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 199 |
195 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 200 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
196 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); | 201 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
197 | 202 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); |
198 AdblockPlus::Sleep(100); | 203 |
199 | 204 AdblockPlus::Sleep(100); |
| 205 } |
200 ASSERT_FALSE(fileSystem->prefsContents.empty()); | 206 ASSERT_FALSE(fileSystem->prefsContents.empty()); |
201 | 207 |
202 jsEngine.reset(); | 208 { |
203 Reset(preconfiguredPrefs); | 209 ResetJsEngine(); |
204 | 210 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; |
205 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); | 211 preconfiguredPrefs["suppress_first_run_page"] = jsEngine->NewValue(true); |
206 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); | 212 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); |
207 } | 213 |
| 214 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page")->IsBool()); |
| 215 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page")->AsBool()); |
| 216 } |
| 217 } |
LEFT | RIGHT |