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

Side by Side Diff: test/FileSystemJsObject.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Updated implementation. Created June 2, 2017, 3:49 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
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::FileSystem 24 class MockFileSystem : public AdblockPlus::IFileSystem
25 { 25 {
26 public: 26 public:
27 bool success; 27 bool success;
28 std::string contentToRead; 28 std::string contentToRead;
29 std::string lastWrittenPath; 29 std::string lastWrittenPath;
30 std::string lastWrittenContent; 30 std::string 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 std::shared_ptr<std::istream> Read(const std::string& path) const 44 void Read(const std::string& path, const ReadCallback& callback) const
45 { 45 {
46 if (!success) 46 if (!success)
47 throw std::runtime_error("Unable to read " + path); 47 {
48 std::stringstream* const stream = new std::stringstream; 48 callback(std::move(std::string()),
49 *stream << contentToRead; 49 std::string("Unable to read " + path));
50 return std::shared_ptr<std::istream>(stream); 50 return;
51 }
52 callback(std::move(std::string(contentToRead)), "");
51 } 53 }
52 54
53 void Write(const std::string& path, std::istream& data) 55 void Write(const std::string& path, std::shared_ptr<std::istream> data,
56 const Callback& callback)
54 { 57 {
55 if (!success) 58 if (!success)
56 throw std::runtime_error("Unable to write to " + path); 59 {
60 callback(std::string("Unable to write to " + path));
61 return;
62 }
57 lastWrittenPath = path; 63 lastWrittenPath = path;
58 64
59 std::stringstream content; 65 std::stringstream content;
60 content << data.rdbuf(); 66 content << data->rdbuf();
61 lastWrittenContent = content.str(); 67 lastWrittenContent = content.str();
68 callback("");
62 } 69 }
63 70
64 void Move(const std::string& fromPath, const std::string& toPath) 71 void Move(const std::string& fromPath, const std::string& toPath,
72 const Callback& callback)
65 { 73 {
66 if (!success) 74 if (!success)
67 throw std::runtime_error("Unable to move " + fromPath + " to " 75 {
68 + toPath); 76 callback(std::string("Unable to move " + fromPath + " to "
77 + toPath));
78 return;
79 }
69 movedFrom = fromPath; 80 movedFrom = fromPath;
70 movedTo = toPath; 81 movedTo = toPath;
82 callback("");
71 } 83 }
72 84
73 void Remove(const std::string& path) 85 void Remove(const std::string& path, const Callback& callback)
74 { 86 {
75 if (!success) 87 if (!success)
76 throw std::runtime_error("Unable to remove " + path); 88 {
89 callback(std::string("Unable to remove " + path));
90 return;
91 }
77 removedPath = path; 92 removedPath = path;
93 callback("");
78 } 94 }
79 95
80 StatResult Stat(const std::string& path) const 96 void Stat(const std::string& path,
97 const StatCallback& callback) const
81 { 98 {
99 StatResult result;
100 std::string error;
82 if (!success) 101 if (!success)
83 throw std::runtime_error("Unable to stat " + path); 102 error = std::string("Unable to stat " + path);
84 statPath = path; 103 else
85 StatResult result; 104 {
86 result.exists = statExists; 105 statPath = path;
87 result.isDirectory = statIsDirectory; 106 result.exists = statExists;
88 result.isFile = statIsFile; 107 result.isDirectory = statIsDirectory;
89 result.lastModified = statLastModified; 108 result.isFile = statIsFile;
90 return result; 109 result.lastModified = statLastModified;
110 }
111 callback(result, error);
91 } 112 }
92 113
93 std::string Resolve(const std::string& path) const 114 std::string Resolve(const std::string& path) const
94 { 115 {
95 if (!success) 116 if (!success)
96 throw std::runtime_error("Unable to stat " + path); 117 throw std::runtime_error("Unable to stat " + path);
97 return path; 118 return path;
98 } 119 }
99 }; 120 };
100 121
101 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, 122 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content,
102 std::string& error) 123 std::string& error)
103 { 124 {
104 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); 125 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})");
105 AdblockPlus::Sleep(50); 126 AdblockPlus::Sleep(50);
106 content = jsEngine->Evaluate("result.content").AsString(); 127 content = jsEngine->Evaluate("result.content").AsString();
107 error = jsEngine->Evaluate("result.error").AsString(); 128 error = jsEngine->Evaluate("result.error").AsString();
108 } 129 }
109 130
110 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; 131 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr;
111 132
112 class FileSystemJsObjectTest : public BaseJsTest 133 class FileSystemJsObjectTest : public BaseJsTest
113 { 134 {
114 protected: 135 protected:
115 MockFileSystemPtr mockFileSystem; 136 MockFileSystemPtr mockFileSystem;
116 137
117 void SetUp() 138 void SetUp()
118 { 139 {
119 BaseJsTest::SetUp();
120 mockFileSystem = MockFileSystemPtr(new MockFileSystem); 140 mockFileSystem = MockFileSystemPtr(new MockFileSystem);
121 jsEngine->SetFileSystem(mockFileSystem); 141 JsEngineCreationParameters params;
142 params.fileSystem = mockFileSystem;
143 jsEngine = CreateJsEngine(std::move(params));
122 } 144 }
123 }; 145 };
124 } 146 }
125 147
126 TEST_F(FileSystemJsObjectTest, Read) 148 TEST_F(FileSystemJsObjectTest, Read)
127 { 149 {
128 mockFileSystem->contentToRead = "foo"; 150 mockFileSystem->contentToRead = "foo";
129 std::string content; 151 std::string content;
130 std::string error; 152 std::string error;
131 ReadFile(jsEngine, content, error); 153 ReadFile(jsEngine, content, error);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); 261 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')"));
240 } 262 }
241 263
242 TEST_F(FileSystemJsObjectTest, StatError) 264 TEST_F(FileSystemJsObjectTest, StatError)
243 { 265 {
244 mockFileSystem->success = false; 266 mockFileSystem->success = false;
245 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 267 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
246 AdblockPlus::Sleep(50); 268 AdblockPlus::Sleep(50);
247 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); 269 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString());
248 } 270 }
OLDNEW
« lib/init.js ('K') | « test/DefaultFileSystem.cpp ('k') | test/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld