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

Side by Side Diff: test/Prefs.cpp

Issue 29500602: Issue 5450 - introduce the Platform class (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created July 28, 2017, 12:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 } 70 }
71 71
72 LazyFileSystem::Stat(path, callback); 72 LazyFileSystem::Stat(path, callback);
73 }); 73 });
74 } 74 }
75 }; 75 };
76 76
77 class PrefsTest : public ::testing::Test 77 class PrefsTest : public ::testing::Test
78 { 78 {
79 protected: 79 protected:
80 std::unique_ptr<Platform> platform;
80 std::shared_ptr<TestFileSystem> fileSystem; 81 std::shared_ptr<TestFileSystem> fileSystem;
81 AdblockPlus::JsEnginePtr jsEngine;
82 82
83 void SetUp() 83 void SetUp()
84 { 84 {
85 fileSystem = std::make_shared<TestFileSystem>(); 85 fileSystem = std::make_shared<TestFileSystem>();
86 ResetJsEngine(); 86 ResetPlatform();
87 } 87 }
88 88
89 void ResetJsEngine() 89 void ResetPlatform()
90 { 90 {
91 JsEngineCreationParameters jsEngineParams; 91 ThrowingPlatformCreationParameters platformParams;
92 jsEngineParams.fileSystem = fileSystem; 92 platformParams.fileSystem = fileSystem;
93 jsEngineParams.webRequest.reset(new NoopWebRequest()); 93 platformParams.webRequest.reset(new NoopWebRequest());
94 jsEngineParams.logSystem.reset(new LazyLogSystem()); 94 platformParams.logSystem.reset(new LazyLogSystem());
95 jsEngineParams.timer.reset(new NoopTimer()); 95 platformParams.timer.reset(new NoopTimer());
96 jsEngine = CreateJsEngine(std::move(jsEngineParams)); 96 platform.reset(new Platform(std::move(platformParams)));
97 } 97 }
98 98
99 FilterEnginePtr CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& p reconfiguredPrefs = 99 FilterEnginePtr CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& p reconfiguredPrefs =
100 AdblockPlus::FilterEngine::Prefs()) 100 AdblockPlus::FilterEngine::Prefs())
101 { 101 {
102 AdblockPlus::FilterEngine::CreationParameters createParams; 102 AdblockPlus::FilterEngine::CreationParameters createParams;
103 createParams.preconfiguredPrefs = preconfiguredPrefs; 103 createParams.preconfiguredPrefs = preconfiguredPrefs;
104 return ::CreateFilterEngine(*fileSystem, jsEngine, createParams); 104 return ::CreateFilterEngine(*fileSystem, platform->GetJsEngine(), createPa rams);
105 } 105 }
106 }; 106 };
107 } 107 }
108 108
109 TEST_F(PrefsTest, PrefsGetSet) 109 TEST_F(PrefsTest, PrefsGetSet)
110 { 110 {
111 auto filterEngine = CreateFilterEngine(); 111 auto filterEngine = CreateFilterEngine();
112 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); 112 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString());
113 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); 113 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt());
114 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); 114 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool());
115 ASSERT_TRUE(filterEngine->GetPref("foobar").IsUndefined()); 115 ASSERT_TRUE(filterEngine->GetPref("foobar").IsUndefined());
116 116
117 ASSERT_ANY_THROW(filterEngine->SetPref("patternsfile", jsEngine->NewValue(0))) ; 117 ASSERT_ANY_THROW(filterEngine->SetPref("patternsfile", platform->GetJsEngine() ->NewValue(0)));
118 ASSERT_ANY_THROW(filterEngine->SetPref("patternsbackupinterval", jsEngine->New Value(true))); 118 ASSERT_ANY_THROW(filterEngine->SetPref("patternsbackupinterval", platform->Get JsEngine()->NewValue(true)));
119 ASSERT_ANY_THROW(filterEngine->SetPref("subscriptions_autoupdate", jsEngine->N ewValue("foo"))); 119 ASSERT_ANY_THROW(filterEngine->SetPref("subscriptions_autoupdate", platform->G etJsEngine()->NewValue("foo")));
120 120
121 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); 121 filterEngine->SetPref("patternsfile", platform->GetJsEngine()->NewValue("filte rs.ini"));
122 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); 122 filterEngine->SetPref("patternsbackupinterval", platform->GetJsEngine()->NewVa lue(48));
123 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)); 123 filterEngine->SetPref("subscriptions_autoupdate", platform->GetJsEngine()->New Value(false));
124 124
125 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString()); 125 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString());
126 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt()); 126 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt());
127 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); 127 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool());
128 } 128 }
129 129
130 TEST_F(PrefsTest, PrefsPersist) 130 TEST_F(PrefsTest, PrefsPersist)
131 { 131 {
132 { 132 {
133 auto filterEngine = CreateFilterEngine(); 133 auto filterEngine = CreateFilterEngine();
134 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString()); 134 ASSERT_EQ("patterns.ini", filterEngine->GetPref("patternsfile").AsString());
135 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); 135 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt());
136 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); 136 ASSERT_TRUE(filterEngine->GetPref("subscriptions_autoupdate").AsBool());
137 137
138 filterEngine->SetPref("patternsfile", jsEngine->NewValue("filters.ini")); 138 filterEngine->SetPref("patternsfile", platform->GetJsEngine()->NewValue("fil ters.ini"));
139 filterEngine->SetPref("patternsbackupinterval", jsEngine->NewValue(48)); 139 filterEngine->SetPref("patternsbackupinterval", platform->GetJsEngine()->New Value(48));
140 filterEngine->SetPref("subscriptions_autoupdate", jsEngine->NewValue(false)) ; 140 filterEngine->SetPref("subscriptions_autoupdate", platform->GetJsEngine()->N ewValue(false));
141 } 141 }
142 ASSERT_FALSE(fileSystem->prefsContents.empty()); 142 ASSERT_FALSE(fileSystem->prefsContents.empty());
143 143
144 { 144 {
145 ResetJsEngine(); 145 ResetPlatform();
146 auto filterEngine = CreateFilterEngine(); 146 auto filterEngine = CreateFilterEngine();
147 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString()); 147 ASSERT_EQ("filters.ini", filterEngine->GetPref("patternsfile").AsString());
148 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt()); 148 ASSERT_EQ(48, filterEngine->GetPref("patternsbackupinterval").AsInt());
149 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool()); 149 ASSERT_FALSE(filterEngine->GetPref("subscriptions_autoupdate").AsBool());
150 } 150 }
151 } 151 }
152 152
153 TEST_F(PrefsTest, UnknownPrefs) 153 TEST_F(PrefsTest, UnknownPrefs)
154 { 154 {
155 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer; 155 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer;
(...skipping 10 matching lines...) Expand all
166 std::string content = "{\"patternsbackupinterval\": 6, \"foo\"}"; 166 std::string content = "{\"patternsbackupinterval\": 6, \"foo\"}";
167 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend()); 167 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend());
168 auto filterEngine = CreateFilterEngine(); 168 auto filterEngine = CreateFilterEngine();
169 169
170 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt()); 170 ASSERT_EQ(24, filterEngine->GetPref("patternsbackupinterval").AsInt());
171 } 171 }
172 172
173 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) 173 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured)
174 { 174 {
175 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 175 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
176 preconfiguredPrefs.emplace("disable_auto_updates", jsEngine->NewValue(false)); 176 preconfiguredPrefs.emplace("disable_auto_updates", platform->GetJsEngine()->Ne wValue(false));
177 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(true) ); 177 preconfiguredPrefs.emplace("suppress_first_run_page", platform->GetJsEngine()- >NewValue(true));
178 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); 178 auto filterEngine = CreateFilterEngine(preconfiguredPrefs);
179 179
180 ASSERT_TRUE(filterEngine->GetPref("disable_auto_updates").IsBool()); 180 ASSERT_TRUE(filterEngine->GetPref("disable_auto_updates").IsBool());
181 ASSERT_FALSE(filterEngine->GetPref("disable_auto_updates").AsBool()); 181 ASSERT_FALSE(filterEngine->GetPref("disable_auto_updates").AsBool());
182 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); 182 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool());
183 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool()); 183 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool());
184 } 184 }
185 185
186 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported) 186 TEST_F(PrefsTest, PreconfiguredPrefsUnsupported)
187 { 187 {
188 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 188 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
189 preconfiguredPrefs.emplace("unsupported_preconfig", jsEngine->NewValue(true)); 189 preconfiguredPrefs.emplace("unsupported_preconfig", platform->GetJsEngine()->N ewValue(true));
190 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); 190 auto filterEngine = CreateFilterEngine(preconfiguredPrefs);
191 191
192 ASSERT_TRUE(filterEngine->GetPref("unsupported_preconfig").IsUndefined()); 192 ASSERT_TRUE(filterEngine->GetPref("unsupported_preconfig").IsUndefined());
193 } 193 }
194 194
195 TEST_F(PrefsTest, PreconfiguredPrefsOverride) 195 TEST_F(PrefsTest, PreconfiguredPrefsOverride)
196 { 196 {
197 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 197 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
198 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(true) ); 198 preconfiguredPrefs.emplace("suppress_first_run_page", platform->GetJsEngine()- >NewValue(true));
199 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); 199 auto filterEngine = CreateFilterEngine(preconfiguredPrefs);
200 200
201 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); 201 filterEngine->SetPref("suppress_first_run_page", platform->GetJsEngine()->NewV alue(false));
202 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); 202 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool());
203 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool()); 203 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool());
204 } 204 }
205 205
206 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured) 206 TEST_F(PrefsTest, PrefsPersistWhenPreconfigured)
207 { 207 {
208 { 208 {
209 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 209 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
210 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru e)); 210 preconfiguredPrefs.emplace("suppress_first_run_page", platform->GetJsEngine( )->NewValue(true));
211 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); 211 auto filterEngine = CreateFilterEngine(preconfiguredPrefs);
212 212
213 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); 213 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool());
214 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool()); 214 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").AsBool());
215 filterEngine->SetPref("suppress_first_run_page", jsEngine->NewValue(false)); 215 filterEngine->SetPref("suppress_first_run_page", platform->GetJsEngine()->Ne wValue(false));
216 } 216 }
217 ASSERT_FALSE(fileSystem->prefsContents.empty()); 217 ASSERT_FALSE(fileSystem->prefsContents.empty());
218 218
219 { 219 {
220 ResetJsEngine(); 220 ResetPlatform();
221 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 221 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
222 preconfiguredPrefs.emplace("suppress_first_run_page", jsEngine->NewValue(tru e)); 222 preconfiguredPrefs.emplace("suppress_first_run_page", platform->GetJsEngine( )->NewValue(true));
223 auto filterEngine = CreateFilterEngine(preconfiguredPrefs); 223 auto filterEngine = CreateFilterEngine(preconfiguredPrefs);
224 224
225 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool()); 225 ASSERT_TRUE(filterEngine->GetPref("suppress_first_run_page").IsBool());
226 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool()); 226 ASSERT_FALSE(filterEngine->GetPref("suppress_first_run_page").AsBool());
227 } 227 }
228 } 228 }
OLDNEW

Powered by Google App Engine
This is Rietveld