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

Side by Side Diff: test/FilterEngine.cpp

Issue 10802049: Functional prefs implementation (Closed)
Patch Set: Created June 5, 2013, 9:03 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/BaseJsTest.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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>
19
20 #include "../src/Thread.h"
18 #include "BaseJsTest.h" 21 #include "BaseJsTest.h"
19 22
20 namespace 23 namespace
21 { 24 {
22 typedef std::tr1::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; 25 typedef std::tr1::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
23 26
24 class VeryLazyFileSystem : public LazyFileSystem 27 class VeryLazyFileSystem : public LazyFileSystem
25 { 28 {
29 public:
26 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const 30 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const
27 { 31 {
28 std::string dummyData("# Adblock Plus preferences"); 32 std::string dummyData("# Adblock Plus preferences");
29 return std::tr1::shared_ptr<std::istream>(new std::istringstream(dummyData )); 33 return std::tr1::shared_ptr<std::istream>(new std::istringstream(dummyData ));
30 } 34 }
31 }; 35 };
32 36
37 class PrefTestFileSystem : public LazyFileSystem
38 {
39 public:
40 std::string prefsContents;
41
42 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const
43 {
44 if (path == "prefs.json" && !prefsContents.empty())
45 return std::tr1::shared_ptr<std::istream>(new std::istringstream(prefsCo ntents));
46 else
47 return LazyFileSystem::Read(path);
48 }
49
50 void Write(const std::string& path, std::tr1::shared_ptr<std::istream> conte nt)
51 {
52 if (path == "prefs.json")
53 {
54 std::stringstream ss;
55 ss << content->rdbuf();
56 prefsContents = ss.str();
57 }
58 else
59 LazyFileSystem::Write(path, content);
60 }
61
62 StatResult Stat(const std::string& path) const
63 {
64 if (path == "prefs.json")
65 {
66 StatResult result;
67 result.exists = result.isFile = !prefsContents.empty();
68 return result;
69 }
70 else
71 return LazyFileSystem::Stat(path);
72 }
73 };
74
33 template<class FileSystem, class LogSystem> 75 template<class FileSystem, class LogSystem>
34 class FilterEngineTestGeneric : public BaseJsTest 76 class FilterEngineTestGeneric : public BaseJsTest
35 { 77 {
36 protected: 78 protected:
37 FilterEnginePtr filterEngine; 79 FilterEnginePtr filterEngine;
38 80
39 void SetUp() 81 void SetUp()
40 { 82 {
41 BaseJsTest::SetUp(); 83 BaseJsTest::SetUp();
42 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem)); 84 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem));
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 229
188 TEST_F(FilterEngineTest, FirstRunFlag) 230 TEST_F(FilterEngineTest, FirstRunFlag)
189 { 231 {
190 ASSERT_FALSE(filterEngine->IsFirstRun()); 232 ASSERT_FALSE(filterEngine->IsFirstRun());
191 } 233 }
192 234
193 TEST_F(FilterEngineTestNoData, FirstRunFlag) 235 TEST_F(FilterEngineTestNoData, FirstRunFlag)
194 { 236 {
195 ASSERT_FALSE(filterEngine->IsFirstRun()); 237 ASSERT_FALSE(filterEngine->IsFirstRun());
196 } 238 }
239
240 TEST_F(FilterEngineTest, PrefsGetSet)
241 {
242 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString());
243 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt());
244 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool());
245 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined());
246
247 ASSERT_ANY_THROW(filterEngine->SetPref("patternsfile", jsEngine->NewValue(0))) ;
248 ASSERT_ANY_THROW(filterEngine->SetPref("patternsbackupinterval", jsEngine->New Value(true)));
249 ASSERT_ANY_THROW(filterEngine->SetPref("subscriptions_autoupdate", jsEngine->N ewValue("foo")));
250
251 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini"));
252 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48));
253 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false));
254
255 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString());
256 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt());
257 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool());
258 }
259
260 TEST(FilterEngineTestCustom, PrefsReadWrite)
261 {
262 PrefTestFileSystem* fs = new PrefTestFileSystem;
263 AdblockPlus::FileSystemPtr fileSystem(fs);
264
265 // Verify that settings persist
266 {
267 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
268 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new ThrowingLogSystem));
269 jsEngine->SetFileSystem(fileSystem);
270 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest));
271 FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine));
272
273 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile")->AsString()) ;
274 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt());
275 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool());
276
277 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini"));
278 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48));
279 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)) ;
280
281 AdblockPlus::Sleep(100);
282
283 ASSERT_FALSE(fs->prefsContents.empty());
284 }
285
286 {
287 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New();
288 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new ThrowingLogSystem));
289 jsEngine->SetFileSystem(fileSystem);
290 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest));
291 FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine));
292
293 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile")->AsString());
294 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval")->AsInt());
295 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate")->AsBool());
296 }
297
298 // Verify that unknown prefs are ignored while others are respected
299 {
300 fs->prefsContents = "{\"foobar\":2, \"patternsbackupinterval\": 12}";
301
302 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New();
303 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new ThrowingLogSystem));
304 jsEngine->SetFileSystem(fileSystem);
305 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest));
306 FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine));
307
308 ASSERT_TRUE(filterEngine->GetPref("foobar")->IsUndefined());
309 ASSERT_EQ(12, filterEngine->GetPref("patternsbackupinterval")->AsInt());
310 }
311
312 // Verify wrong syntax causes a fallback to defaults
313 {
314 fs->prefsContents = "{\"patternsbackupinterval\": 6, \"foo\"}";
315
316 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New();
317 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem));
318 jsEngine->SetFileSystem(fileSystem);
319 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest));
320 FilterEnginePtr filterEngine(new AdblockPlus::FilterEngine(jsEngine));
321
322 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval")->AsInt());
323 }
324 }
OLDNEW
« no previous file with comments | « test/BaseJsTest.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld