OLD | NEW |
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 |
(...skipping 23 matching lines...) Expand all Loading... |
34 std::string movedTo; | 34 std::string movedTo; |
35 std::string removedFile; | 35 std::string removedFile; |
36 mutable std::string statFile; | 36 mutable std::string statFile; |
37 bool statExists; | 37 bool statExists; |
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& fileName, const ReadCallback& callback) const o
verride | 44 void Read(const std::string& fileName, const ReadCallback& callback, const C
allback& errorCallback) const override |
45 { | 45 { |
46 if (!success) | 46 if (success) |
47 { | 47 try |
48 callback(IOBuffer(), "Unable to read " + fileName); | 48 { |
49 return; | 49 callback(IOBuffer(contentToRead)); |
50 } | 50 } |
51 callback(IOBuffer(contentToRead), ""); | 51 catch (const std::exception& ex) |
| 52 { |
| 53 errorCallback(ex.what()); |
| 54 } |
| 55 else |
| 56 errorCallback("Unable to read " + fileName); |
52 } | 57 } |
53 | 58 |
54 void Write(const std::string& fileName, const IOBuffer& data, | 59 void Write(const std::string& fileName, const IOBuffer& data, |
55 const Callback& callback) override | 60 const Callback& callback) override |
56 { | 61 { |
57 if (!success) | 62 if (!success) |
58 { | 63 { |
59 callback("Unable to write to " + fileName); | 64 callback("Unable to write to " + fileName); |
60 return; | 65 return; |
61 } | 66 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 result.exists = statExists; | 106 result.exists = statExists; |
102 result.lastModified = statLastModified; | 107 result.lastModified = statLastModified; |
103 } | 108 } |
104 callback(result, error); | 109 callback(result, error); |
105 } | 110 } |
106 }; | 111 }; |
107 | 112 |
108 void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content, | 113 void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content, |
109 std::string& error) | 114 std::string& error) |
110 { | 115 { |
111 jsEngine.Evaluate("let result; _fileSystem.read('', function(r) {result = r}
)"); | 116 jsEngine.Evaluate("let result = {}; _fileSystem.read('', function(r) {result
.content = r.content;}, function(r) {result.error = r.error;})"); |
112 content = jsEngine.Evaluate("result.content").AsString(); | 117 content = jsEngine.Evaluate("result.content").AsString(); |
113 error = jsEngine.Evaluate("result.error").AsString(); | 118 error = jsEngine.Evaluate("result.error").AsString(); |
114 } | 119 } |
115 | 120 |
116 class FileSystemJsObjectTest : public BaseJsTest | 121 class FileSystemJsObjectTest : public BaseJsTest |
117 { | 122 { |
118 protected: | 123 protected: |
119 MockFileSystem* mockFileSystem; | 124 MockFileSystem* mockFileSystem; |
120 | 125 |
121 void SetUp() | 126 void SetUp() |
(...skipping 22 matching lines...) Expand all Loading... |
144 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.read('', '')")); | 149 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.read('', '')")); |
145 } | 150 } |
146 | 151 |
147 TEST_F(FileSystemJsObjectTest, ReadError) | 152 TEST_F(FileSystemJsObjectTest, ReadError) |
148 { | 153 { |
149 mockFileSystem->success = false; | 154 mockFileSystem->success = false; |
150 std::string content; | 155 std::string content; |
151 std::string error; | 156 std::string error; |
152 ReadFile(GetJsEngine(), content, error); | 157 ReadFile(GetJsEngine(), content, error); |
153 ASSERT_NE("", error); | 158 ASSERT_NE("", error); |
154 ASSERT_EQ("", content); | 159 ASSERT_EQ("undefined", content); |
155 } | 160 } |
156 | 161 |
157 TEST_F(FileSystemJsObjectTest, Write) | 162 TEST_F(FileSystemJsObjectTest, Write) |
158 { | 163 { |
159 GetJsEngine().Evaluate("let error = true; _fileSystem.write('foo', 'bar', func
tion(e) {error = e})"); | 164 GetJsEngine().Evaluate("let error = true; _fileSystem.write('foo', 'bar', func
tion(e) {error = e})"); |
160 ASSERT_EQ("foo", mockFileSystem->lastWrittenFile); | 165 ASSERT_EQ("foo", mockFileSystem->lastWrittenFile); |
161 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), | 166 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), |
162 mockFileSystem->lastWrittenContent); | 167 mockFileSystem->lastWrittenContent); |
163 ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined()); | 168 ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined()); |
164 } | 169 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 } | 274 } |
270 else | 275 else |
271 { | 276 { |
272 ASSERT_EQ(1u, jsArgs.size()); | 277 ASSERT_EQ(1u, jsArgs.size()); |
273 EXPECT_TRUE(jsArgs[0].IsString()); | 278 EXPECT_TRUE(jsArgs[0].IsString()); |
274 EXPECT_FALSE(jsArgs[0].AsString().empty()); | 279 EXPECT_FALSE(jsArgs[0].AsString().empty()); |
275 } | 280 } |
276 }); | 281 }); |
277 jsEngine.Evaluate(R"js(_fileSystem.readFromFile("foo", | 282 jsEngine.Evaluate(R"js(_fileSystem.readFromFile("foo", |
278 (line) => _triggerEvent("onLine", line), | 283 (line) => _triggerEvent("onLine", line), |
279 (error) => | 284 () =>_triggerEvent("onDone"), |
280 { | 285 (error) => _triggerEvent("onDone", error)); |
281 if (error) | 286 )js"); |
282 _triggerEvent("onDone", error); | |
283 else | |
284 _triggerEvent("onDone"); | |
285 });)js"); | |
286 EXPECT_TRUE(isOnDoneCalled); | 287 EXPECT_TRUE(isOnDoneCalled); |
287 } | 288 } |
288 | 289 |
289 void readFromFile_Lines(const std::string& content, const Lines& expected) | 290 void readFromFile_Lines(const std::string& content, const Lines& expected) |
290 { | 291 { |
291 mockFileSystem->contentToRead.assign(content.begin(), content.end()); | 292 mockFileSystem->contentToRead.assign(content.begin(), content.end()); |
292 std::vector<std::string> readLines; | 293 std::vector<std::string> readLines; |
293 readFromFile([&readLines](const std::string& line) | 294 readFromFile([&readLines](const std::string& line) |
294 { | 295 { |
295 readLines.emplace_back(line); | 296 readLines.emplace_back(line); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 let onLineCounter = 0; | 386 let onLineCounter = 0; |
386 _fileSystem.readFromFile("foo", | 387 _fileSystem.readFromFile("foo", |
387 (line) => | 388 (line) => |
388 { | 389 { |
389 if (onLineCounter++ == 2) | 390 if (onLineCounter++ == 2) |
390 { | 391 { |
391 throw new Error("my-error"); | 392 throw new Error("my-error"); |
392 } | 393 } |
393 _triggerEvent("onLine", line); | 394 _triggerEvent("onLine", line); |
394 }, | 395 }, |
395 (error) => | 396 () => _triggerEvent("onDone"), |
396 { | 397 (error) => _triggerEvent("onDone", error)); |
397 if (error) | 398 )js"); |
398 _triggerEvent("onDone", error); | |
399 else | |
400 _triggerEvent("onDone"); | |
401 });)js"); | |
402 EXPECT_EQ(2u, readLines.size()); | 399 EXPECT_EQ(2u, readLines.size()); |
403 EXPECT_EQ("Error: my-error at undefined:8", error); | 400 EXPECT_EQ("Error: my-error at undefined:8", error); |
404 } | 401 } |
OLD | NEW |