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

Side by Side Diff: test/Prefs.cpp

Issue 29535572: Issue 5183 - make FileSystemPtr std::unique_ptr (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Sept. 4, 2017, 9:59 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/FileSystemJsObject.cpp ('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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present 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 "../src/Thread.h" 20 #include "../src/Thread.h"
21 #include "BaseJsTest.h" 21 #include "BaseJsTest.h"
22 22
23 using namespace AdblockPlus; 23 using namespace AdblockPlus;
24 24
25 namespace 25 namespace
26 { 26 {
27 class TestFileSystem : public LazyFileSystem 27 class TestFileSystem : public LazyFileSystem
28 { 28 {
29 IOBuffer& prefsContents;
29 public: 30 public:
30 IOBuffer prefsContents; 31 explicit TestFileSystem(IOBuffer& prefsContent)
31 32 : prefsContents(prefsContent)
33 {
34 }
32 void Read(const std::string& fileName, const ReadCallback& callback) const o verride 35 void Read(const std::string& fileName, const ReadCallback& callback) const o verride
33 { 36 {
34 scheduler([this, fileName, callback] 37 scheduler([this, fileName, callback]
35 { 38 {
36 if (fileName == "prefs.json" && !prefsContents.empty()) 39 if (fileName == "prefs.json" && !prefsContents.empty())
37 { 40 {
38 callback(IOBuffer(prefsContents.cbegin(), prefsContents.cend()), ""); 41 callback(IOBuffer(prefsContents.cbegin(), prefsContents.cend()), "");
39 return; 42 return;
40 } 43 }
41 44
(...skipping 25 matching lines...) Expand all
67 return; 70 return;
68 } 71 }
69 72
70 LazyFileSystem::Stat(fileName, callback); 73 LazyFileSystem::Stat(fileName, callback);
71 }); 74 });
72 } 75 }
73 }; 76 };
74 77
75 class PrefsTest : public BaseJsTest 78 class PrefsTest : public BaseJsTest
76 { 79 {
80 LazyFileSystem* fileSystem;
77 protected: 81 protected:
78 std::shared_ptr<TestFileSystem> fileSystem; 82 IFileSystem::IOBuffer prefsContent;
79 83
80 void SetUp() 84 void SetUp()
81 { 85 {
82 fileSystem = std::make_shared<TestFileSystem>();
83 ResetPlatform(); 86 ResetPlatform();
84 } 87 }
85 88
86 void ResetPlatform() 89 void ResetPlatform()
87 { 90 {
88 ThrowingPlatformCreationParameters platformParams; 91 ThrowingPlatformCreationParameters platformParams;
89 platformParams.fileSystem = fileSystem; 92 platformParams.fileSystem.reset(fileSystem = new TestFileSystem(prefsConte nt));
90 platformParams.webRequest.reset(new NoopWebRequest()); 93 platformParams.webRequest.reset(new NoopWebRequest());
91 platformParams.logSystem.reset(new LazyLogSystem()); 94 platformParams.logSystem.reset(new LazyLogSystem());
92 platformParams.timer.reset(new NoopTimer()); 95 platformParams.timer.reset(new NoopTimer());
93 platform.reset(new Platform(std::move(platformParams))); 96 platform.reset(new Platform(std::move(platformParams)));
94 } 97 }
95 98
96 FilterEngine& CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& pre configuredPrefs = 99 FilterEngine& CreateFilterEngine(const AdblockPlus::FilterEngine::Prefs& pre configuredPrefs =
97 AdblockPlus::FilterEngine::Prefs()) 100 AdblockPlus::FilterEngine::Prefs())
98 { 101 {
99 AdblockPlus::FilterEngine::CreationParameters createParams; 102 AdblockPlus::FilterEngine::CreationParameters createParams;
(...skipping 23 matching lines...) Expand all
123 TEST_F(PrefsTest, PrefsPersist) 126 TEST_F(PrefsTest, PrefsPersist)
124 { 127 {
125 { 128 {
126 auto& filterEngine = CreateFilterEngine(); 129 auto& filterEngine = CreateFilterEngine();
127 ASSERT_EQ(24, filterEngine.GetPref("patternsbackupinterval").AsInt()); 130 ASSERT_EQ(24, filterEngine.GetPref("patternsbackupinterval").AsInt());
128 ASSERT_TRUE(filterEngine.GetPref("subscriptions_autoupdate").AsBool()); 131 ASSERT_TRUE(filterEngine.GetPref("subscriptions_autoupdate").AsBool());
129 132
130 filterEngine.SetPref("patternsbackupinterval", GetJsEngine().NewValue(48)); 133 filterEngine.SetPref("patternsbackupinterval", GetJsEngine().NewValue(48));
131 filterEngine.SetPref("subscriptions_autoupdate", GetJsEngine().NewValue(fals e)); 134 filterEngine.SetPref("subscriptions_autoupdate", GetJsEngine().NewValue(fals e));
132 } 135 }
133 ASSERT_FALSE(fileSystem->prefsContents.empty()); 136 ASSERT_FALSE(prefsContent.empty());
134 137
135 { 138 {
136 ResetPlatform(); 139 ResetPlatform();
137 auto& filterEngine = CreateFilterEngine(); 140 auto& filterEngine = CreateFilterEngine();
138 ASSERT_EQ(48, filterEngine.GetPref("patternsbackupinterval").AsInt()); 141 ASSERT_EQ(48, filterEngine.GetPref("patternsbackupinterval").AsInt());
139 ASSERT_FALSE(filterEngine.GetPref("subscriptions_autoupdate").AsBool()); 142 ASSERT_FALSE(filterEngine.GetPref("subscriptions_autoupdate").AsBool());
140 } 143 }
141 } 144 }
142 145
143 TEST_F(PrefsTest, UnknownPrefs) 146 TEST_F(PrefsTest, UnknownPrefs)
144 { 147 {
145 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer; 148 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer;
146 std::string content = "{\"foobar\":2, \"patternsbackupinterval\": 12}"; 149 std::string content = "{\"foobar\":2, \"patternsbackupinterval\": 12}";
147 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend()); 150 prefsContent = IOBuffer(content.cbegin(), content.cend());
148 auto& filterEngine = CreateFilterEngine(); 151 auto& filterEngine = CreateFilterEngine();
149 ASSERT_TRUE(filterEngine.GetPref("foobar").IsUndefined()); 152 ASSERT_TRUE(filterEngine.GetPref("foobar").IsUndefined());
150 ASSERT_EQ(12, filterEngine.GetPref("patternsbackupinterval").AsInt()); 153 ASSERT_EQ(12, filterEngine.GetPref("patternsbackupinterval").AsInt());
151 } 154 }
152 155
153 TEST_F(PrefsTest, SyntaxFailure) 156 TEST_F(PrefsTest, SyntaxFailure)
154 { 157 {
155 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer; 158 using IOBuffer = AdblockPlus::IFileSystem::IOBuffer;
156 std::string content = "{\"patternsbackupinterval\": 6, \"foo\"}"; 159 std::string content = "{\"patternsbackupinterval\": 6, \"foo\"}";
157 fileSystem->prefsContents = IOBuffer(content.cbegin(), content.cend()); 160 prefsContent = IOBuffer(content.cbegin(), content.cend());
158 ASSERT_EQ(24, CreateFilterEngine().GetPref("patternsbackupinterval").AsInt()); 161 ASSERT_EQ(24, CreateFilterEngine().GetPref("patternsbackupinterval").AsInt());
159 } 162 }
160 163
161 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured) 164 TEST_F(PrefsTest, PreconfiguredPrefsPreconfigured)
162 { 165 {
163 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 166 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
164 preconfiguredPrefs.emplace("disable_auto_updates", GetJsEngine().NewValue(fals e)); 167 preconfiguredPrefs.emplace("disable_auto_updates", GetJsEngine().NewValue(fals e));
165 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue(t rue)); 168 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue(t rue));
166 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); 169 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs);
167 170
(...skipping 27 matching lines...) Expand all
195 { 198 {
196 { 199 {
197 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 200 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
198 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue (true)); 201 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue (true));
199 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); 202 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs);
200 203
201 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool()); 204 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool());
202 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").AsBool()); 205 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").AsBool());
203 filterEngine.SetPref("suppress_first_run_page", GetJsEngine().NewValue(false )); 206 filterEngine.SetPref("suppress_first_run_page", GetJsEngine().NewValue(false ));
204 } 207 }
205 ASSERT_FALSE(fileSystem->prefsContents.empty()); 208 ASSERT_FALSE(prefsContent.empty());
206 209
207 { 210 {
208 ResetPlatform(); 211 ResetPlatform();
209 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs; 212 AdblockPlus::FilterEngine::Prefs preconfiguredPrefs;
210 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue (true)); 213 preconfiguredPrefs.emplace("suppress_first_run_page", GetJsEngine().NewValue (true));
211 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs); 214 auto& filterEngine = CreateFilterEngine(preconfiguredPrefs);
212 215
213 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool()); 216 ASSERT_TRUE(filterEngine.GetPref("suppress_first_run_page").IsBool());
214 ASSERT_FALSE(filterEngine.GetPref("suppress_first_run_page").AsBool()); 217 ASSERT_FALSE(filterEngine.GetPref("suppress_first_run_page").AsBool());
215 } 218 }
216 } 219 }
OLDNEW
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld