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: Created May 26, 2017, 12:43 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
« include/AdblockPlus/FileSystem.h ('K') | « test/BaseJsTest.h ('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-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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 std::shared_ptr<std::istream> Read(const std::string& path) const 44 std::shared_ptr<std::istream> Read(const std::string& path) const
45 { 45 {
46 if (!success) 46 if (!success)
47 throw std::runtime_error("Unable to read " + path); 47 throw std::runtime_error("Unable to read " + path);
48 std::stringstream* const stream = new std::stringstream; 48 std::stringstream* const stream = new std::stringstream;
49 *stream << contentToRead; 49 *stream << contentToRead;
50 return std::shared_ptr<std::istream>(stream); 50 return std::shared_ptr<std::istream>(stream);
51 } 51 }
52 52
53 void Read(const std::string& path,
54 const ReadCallback& callback) const
55 {
56 try
57 {
58 auto result = Read(path);
59 callback(result);
60 }
61 catch (...)
62 {
63 }
64 }
65
53 void Write(const std::string& path, std::istream& data) 66 void Write(const std::string& path, std::istream& data)
54 { 67 {
55 if (!success) 68 if (!success)
56 throw std::runtime_error("Unable to write to " + path); 69 throw std::runtime_error("Unable to write to " + path);
57 lastWrittenPath = path; 70 lastWrittenPath = path;
58 71
59 std::stringstream content; 72 std::stringstream content;
60 content << data.rdbuf(); 73 content << data.rdbuf();
61 lastWrittenContent = content.str(); 74 lastWrittenContent = content.str();
62 } 75 }
63 76
77 void Write(const std::string& path,
78 std::istream& data,
79 const Callback& callback)
80 {
81 try
82 {
83 Write(path, data);
84 callback();
85 }
86 catch (...)
87 {
88 }
89 }
90
64 void Move(const std::string& fromPath, const std::string& toPath) 91 void Move(const std::string& fromPath, const std::string& toPath)
65 { 92 {
66 if (!success) 93 if (!success)
67 throw std::runtime_error("Unable to move " + fromPath + " to " 94 throw std::runtime_error("Unable to move " + fromPath + " to "
68 + toPath); 95 + toPath);
69 movedFrom = fromPath; 96 movedFrom = fromPath;
70 movedTo = toPath; 97 movedTo = toPath;
71 } 98 }
72 99
100 void Move(const std::string& fromPath,
101 const std::string& toPath,
102 const Callback& callback)
103 {
104 try
105 {
106 Move(fromPath, toPath);
107 callback();
108 }
109 catch (...)
110 {
111 }
112 }
113
73 void Remove(const std::string& path) 114 void Remove(const std::string& path)
74 { 115 {
75 if (!success) 116 if (!success)
76 throw std::runtime_error("Unable to remove " + path); 117 throw std::runtime_error("Unable to remove " + path);
77 removedPath = path; 118 removedPath = path;
78 } 119 }
79 120
121 void Remove(const std::string& path, const Callback& callback)
122 {
123 try
124 {
125 Remove(path);
126 callback();
127 }
128 catch (...)
129 {
130 }
131 }
132
80 StatResult Stat(const std::string& path) const 133 StatResult Stat(const std::string& path) const
81 { 134 {
82 if (!success) 135 if (!success)
83 throw std::runtime_error("Unable to stat " + path); 136 throw std::runtime_error("Unable to stat " + path);
84 statPath = path; 137 statPath = path;
85 StatResult result; 138 StatResult result;
86 result.exists = statExists; 139 result.exists = statExists;
87 result.isDirectory = statIsDirectory; 140 result.isDirectory = statIsDirectory;
88 result.isFile = statIsFile; 141 result.isFile = statIsFile;
89 result.lastModified = statLastModified; 142 result.lastModified = statLastModified;
90 return result; 143 return result;
91 } 144 }
92 145
146 void Stat(const std::string& path,
147 const StatCallback& callback) const
148 {
149 try
150 {
151 auto result = Stat(path);
152 callback(result);
153 }
154 catch (...)
155 {
156 }
157 }
158
93 std::string Resolve(const std::string& path) const 159 std::string Resolve(const std::string& path) const
94 { 160 {
95 if (!success) 161 if (!success)
96 throw std::runtime_error("Unable to stat " + path); 162 throw std::runtime_error("Unable to stat " + path);
97 return path; 163 return path;
98 } 164 }
165
166 void Resolve(const std::string& path,
167 const ResolveCallback& callback) const
168 {
169 try
170 {
171 auto result = Resolve(path);
172 callback(result);
173 }
174 catch (...)
175 {
176 }
177 }
99 }; 178 };
100 179
101 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, 180 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content,
102 std::string& error) 181 std::string& error)
103 { 182 {
104 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); 183 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})");
105 AdblockPlus::Sleep(50); 184 AdblockPlus::Sleep(50);
106 content = jsEngine->Evaluate("result.content").AsString(); 185 content = jsEngine->Evaluate("result.content").AsString();
107 error = jsEngine->Evaluate("result.error").AsString(); 186 error = jsEngine->Evaluate("result.error").AsString();
108 } 187 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); 318 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')"));
240 } 319 }
241 320
242 TEST_F(FileSystemJsObjectTest, StatError) 321 TEST_F(FileSystemJsObjectTest, StatError)
243 { 322 {
244 mockFileSystem->success = false; 323 mockFileSystem->success = false;
245 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 324 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
246 AdblockPlus::Sleep(50); 325 AdblockPlus::Sleep(50);
247 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); 326 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString());
248 } 327 }
OLDNEW
« include/AdblockPlus/FileSystem.h ('K') | « test/BaseJsTest.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld