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

Delta Between Two Patch Sets: test/FileSystemJsObject.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Left Patch Set: Remove a #include Utils.h from test. Created June 2, 2017, 7:38 p.m.
Right Patch Set: Rebase on master. Last changes. Created July 7, 2017, 1:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « test/DefaultFileSystem.cpp ('k') | test/FilterEngine.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 #include "BaseJsTest.h" 19 #include "BaseJsTest.h"
20 #include "../src/Thread.h" 20 #include "../src/Thread.h"
21 21
22 namespace 22 namespace
23 { 23 {
24 class MockFileSystem : public AdblockPlus::IFileSystem 24 class MockFileSystem : public AdblockPlus::IFileSystem
25 { 25 {
26 public: 26 public:
27 bool success; 27 bool success;
28 std::string contentToRead; 28 IOBuffer contentToRead;
29 std::string lastWrittenPath; 29 std::string lastWrittenPath;
30 std::string lastWrittenContent; 30 IOBuffer lastWrittenContent;
31 std::string movedFrom; 31 std::string movedFrom;
32 std::string movedTo; 32 std::string movedTo;
33 std::string removedPath; 33 std::string removedPath;
34 mutable std::string statPath; 34 mutable std::string statPath;
35 bool statExists; 35 bool statExists;
36 bool statIsDirectory; 36 bool statIsDirectory;
37 bool statIsFile; 37 bool statIsFile;
38 int statLastModified; 38 int statLastModified;
39 39
40 MockFileSystem() : success(true) 40 MockFileSystem() : success(true)
41 { 41 {
42 } 42 }
43 43
44 void Read(const std::string& path, const ReadCallback& callback) const 44 void Read(const std::string& path, const ReadCallback& callback) const
45 { 45 {
46 if (!success) 46 if (!success)
47 { 47 {
48 callback(std::move(std::string()), 48 callback(IOBuffer(), "Unable to read " + path);
sergei 2017/06/16 15:05:56 std::move is not necessary here
hub 2017/06/16 21:52:55 Done.
49 std::string("Unable to read " + path)); 49 return;
sergei 2017/06/16 15:05:56 Actually, here and below one can simply pass "some
hub 2017/06/16 21:52:56 Done.
50 return; 50 }
51 } 51 callback(IOBuffer(contentToRead), "");
52 callback(std::move(std::string(contentToRead)), ""); 52 }
sergei 2017/06/16 15:05:56 std::move is not necessary here
hub 2017/06/16 21:52:55 Done.
53 } 53
54 54 void Write(const std::string& path, const IOBuffer& data,
55 void Write(const std::string& path, std::shared_ptr<std::istream> data,
56 const Callback& callback) 55 const Callback& callback)
57 { 56 {
58 if (!success) 57 if (!success)
59 { 58 {
60 callback(std::string("Unable to write to " + path)); 59 callback("Unable to write to " + path);
61 return; 60 return;
62 } 61 }
63 lastWrittenPath = path; 62 lastWrittenPath = path;
64 63
65 std::stringstream content; 64 lastWrittenContent = data;
66 content << data->rdbuf();
67 lastWrittenContent = content.str();
68 callback(""); 65 callback("");
69 } 66 }
70 67
71 void Move(const std::string& fromPath, const std::string& toPath, 68 void Move(const std::string& fromPath, const std::string& toPath,
72 const Callback& callback) 69 const Callback& callback)
73 { 70 {
74 if (!success) 71 if (!success)
75 { 72 {
76 callback(std::string("Unable to move " + fromPath + " to " 73 callback("Unable to move " + fromPath + " to " + toPath);
77 + toPath));
78 return; 74 return;
79 } 75 }
80 movedFrom = fromPath; 76 movedFrom = fromPath;
81 movedTo = toPath; 77 movedTo = toPath;
82 callback(""); 78 callback("");
83 } 79 }
84 80
85 void Remove(const std::string& path, const Callback& callback) 81 void Remove(const std::string& path, const Callback& callback)
86 { 82 {
87 if (!success) 83 if (!success)
88 { 84 {
89 callback(std::string("Unable to remove " + path)); 85 callback("Unable to remove " + path);
90 return; 86 return;
91 } 87 }
92 removedPath = path; 88 removedPath = path;
93 callback(""); 89 callback("");
94 } 90 }
95 91
96 void Stat(const std::string& path, 92 void Stat(const std::string& path,
97 const StatCallback& callback) const 93 const StatCallback& callback) const
98 { 94 {
99 StatResult result; 95 StatResult result;
100 std::string error; 96 std::string error;
101 if (!success) 97 if (!success)
102 error = std::string("Unable to stat " + path); 98 error = "Unable to stat " + path;
103 else 99 else
104 { 100 {
105 statPath = path; 101 statPath = path;
106 result.exists = statExists; 102 result.exists = statExists;
107 result.isDirectory = statIsDirectory; 103 result.isDirectory = statIsDirectory;
108 result.isFile = statIsFile; 104 result.isFile = statIsFile;
109 result.lastModified = statLastModified; 105 result.lastModified = statLastModified;
110 } 106 }
111 callback(result, error); 107 callback(result, error);
112 } 108 }
(...skipping 27 matching lines...) Expand all
140 mockFileSystem = MockFileSystemPtr(new MockFileSystem); 136 mockFileSystem = MockFileSystemPtr(new MockFileSystem);
141 JsEngineCreationParameters params; 137 JsEngineCreationParameters params;
142 params.fileSystem = mockFileSystem; 138 params.fileSystem = mockFileSystem;
143 jsEngine = CreateJsEngine(std::move(params)); 139 jsEngine = CreateJsEngine(std::move(params));
144 } 140 }
145 }; 141 };
146 } 142 }
147 143
148 TEST_F(FileSystemJsObjectTest, Read) 144 TEST_F(FileSystemJsObjectTest, Read)
149 { 145 {
150 mockFileSystem->contentToRead = "foo"; 146 mockFileSystem->contentToRead =
147 AdblockPlus::IFileSystem::IOBuffer{'f', 'o', 'o'};
151 std::string content; 148 std::string content;
152 std::string error; 149 std::string error;
153 ReadFile(jsEngine, content, error); 150 ReadFile(jsEngine, content, error);
154 ASSERT_EQ("foo", content); 151 ASSERT_EQ("foo", content);
155 ASSERT_EQ("", error); 152 ASSERT_EQ("undefined", error);
156 } 153 }
157 154
158 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) 155 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments)
159 { 156 {
160 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); 157 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()"));
161 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); 158 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')"));
162 } 159 }
163 160
164 TEST_F(FileSystemJsObjectTest, ReadError) 161 TEST_F(FileSystemJsObjectTest, ReadError)
165 { 162 {
166 mockFileSystem->success = false; 163 mockFileSystem->success = false;
167 std::string content; 164 std::string content;
168 std::string error; 165 std::string error;
169 ReadFile(jsEngine, content, error); 166 ReadFile(jsEngine, content, error);
170 ASSERT_NE("", error); 167 ASSERT_NE("", error);
171 ASSERT_EQ("", content); 168 ASSERT_EQ("", content);
172 } 169 }
173 170
174 TEST_F(FileSystemJsObjectTest, Write) 171 TEST_F(FileSystemJsObjectTest, Write)
175 { 172 {
176 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ; 173 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ;
177 AdblockPlus::Sleep(50); 174 AdblockPlus::Sleep(50);
178 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); 175 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath);
179 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); 176 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}),
180 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); 177 mockFileSystem->lastWrittenContent);
178 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined());
181 } 179 }
182 180
183 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) 181 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments)
184 { 182 {
185 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); 183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()"));
186 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); 184 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')"));
187 } 185 }
188 186
189 TEST_F(FileSystemJsObjectTest, WriteError) 187 TEST_F(FileSystemJsObjectTest, WriteError)
190 { 188 {
191 mockFileSystem->success = false; 189 mockFileSystem->success = false;
192 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ; 190 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ;
193 AdblockPlus::Sleep(50); 191 AdblockPlus::Sleep(50);
194 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); 192 ASSERT_NE("", jsEngine->Evaluate("error").AsString());
195 } 193 }
196 194
197 TEST_F(FileSystemJsObjectTest, Move) 195 TEST_F(FileSystemJsObjectTest, Move)
198 { 196 {
199 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 197 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
200 AdblockPlus::Sleep(50); 198 AdblockPlus::Sleep(50);
201 ASSERT_EQ("foo", mockFileSystem->movedFrom); 199 ASSERT_EQ("foo", mockFileSystem->movedFrom);
202 ASSERT_EQ("bar", mockFileSystem->movedTo); 200 ASSERT_EQ("bar", mockFileSystem->movedTo);
203 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); 201 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined());
204 } 202 }
205 203
206 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) 204 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments)
207 { 205 {
208 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); 206 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()"));
209 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); 207 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')"));
210 } 208 }
211 209
212 TEST_F(FileSystemJsObjectTest, MoveError) 210 TEST_F(FileSystemJsObjectTest, MoveError)
213 { 211 {
214 mockFileSystem->success = false; 212 mockFileSystem->success = false;
215 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 213 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
216 AdblockPlus::Sleep(50); 214 AdblockPlus::Sleep(50);
217 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); 215 ASSERT_FALSE(jsEngine->Evaluate("error").IsUndefined());
218 } 216 }
219 217
220 TEST_F(FileSystemJsObjectTest, Remove) 218 TEST_F(FileSystemJsObjectTest, Remove)
221 { 219 {
222 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 220 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
223 AdblockPlus::Sleep(50); 221 AdblockPlus::Sleep(50);
224 ASSERT_EQ("foo", mockFileSystem->removedPath); 222 ASSERT_EQ("foo", mockFileSystem->removedPath);
225 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); 223 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined());
226 } 224 }
227 225
228 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) 226 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments)
229 { 227 {
230 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); 228 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()"));
231 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); 229 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')"));
232 } 230 }
233 231
234 TEST_F(FileSystemJsObjectTest, RemoveError) 232 TEST_F(FileSystemJsObjectTest, RemoveError)
235 { 233 {
236 mockFileSystem->success = false; 234 mockFileSystem->success = false;
237 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 235 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
238 AdblockPlus::Sleep(50); 236 AdblockPlus::Sleep(50);
239 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); 237 ASSERT_NE("", jsEngine->Evaluate("error").AsString());
240 } 238 }
241 239
242 TEST_F(FileSystemJsObjectTest, Stat) 240 TEST_F(FileSystemJsObjectTest, Stat)
243 { 241 {
244 mockFileSystem->statExists = true; 242 mockFileSystem->statExists = true;
245 mockFileSystem->statIsDirectory= false; 243 mockFileSystem->statIsDirectory= false;
246 mockFileSystem->statIsFile = true; 244 mockFileSystem->statIsFile = true;
247 mockFileSystem->statLastModified = 1337; 245 mockFileSystem->statLastModified = 1337;
248 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 246 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
249 AdblockPlus::Sleep(50); 247 AdblockPlus::Sleep(50);
250 ASSERT_EQ("foo", mockFileSystem->statPath); 248 ASSERT_EQ("foo", mockFileSystem->statPath);
251 ASSERT_EQ("", jsEngine->Evaluate("result.error").AsString()); 249 ASSERT_TRUE(jsEngine->Evaluate("result.error").IsUndefined());
252 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); 250 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool());
253 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); 251 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool());
254 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); 252 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool());
255 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); 253 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt());
256 } 254 }
257 255
258 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) 256 TEST_F(FileSystemJsObjectTest, StatIllegalArguments)
259 { 257 {
260 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); 258 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()"));
261 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); 259 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')"));
262 } 260 }
263 261
264 TEST_F(FileSystemJsObjectTest, StatError) 262 TEST_F(FileSystemJsObjectTest, StatError)
265 { 263 {
266 mockFileSystem->success = false; 264 mockFileSystem->success = false;
267 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 265 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
268 AdblockPlus::Sleep(50); 266 AdblockPlus::Sleep(50);
269 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); 267 ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined());
270 } 268 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld