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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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" | |
21 | 20 |
22 namespace | 21 namespace |
23 { | 22 { |
24 class MockFileSystem : public AdblockPlus::FileSystem | 23 class MockFileSystem : public AdblockPlus::FileSystem |
25 { | 24 { |
26 public: | 25 public: |
27 bool success; | 26 bool success; |
28 std::string contentToRead; | 27 std::string contentToRead; |
29 std::string lastWrittenPath; | 28 std::string lastWrittenPath; |
30 std::string lastWrittenContent; | 29 std::string lastWrittenContent; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 if (!success) | 94 if (!success) |
96 throw std::runtime_error("Unable to stat " + path); | 95 throw std::runtime_error("Unable to stat " + path); |
97 return path; | 96 return path; |
98 } | 97 } |
99 }; | 98 }; |
100 | 99 |
101 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, | 100 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, |
102 std::string& error) | 101 std::string& error) |
103 { | 102 { |
104 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); | 103 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); |
105 AdblockPlus::Sleep(50); | 104 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
106 content = jsEngine->Evaluate("result.content")->AsString(); | 105 content = jsEngine->Evaluate("result.content")->AsString(); |
107 error = jsEngine->Evaluate("result.error")->AsString(); | 106 error = jsEngine->Evaluate("result.error")->AsString(); |
108 } | 107 } |
109 | 108 |
110 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; | 109 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; |
111 | 110 |
112 class FileSystemJsObjectTest : public BaseJsTest | 111 class FileSystemJsObjectTest : public BaseJsTest |
113 { | 112 { |
114 protected: | 113 protected: |
115 MockFileSystemPtr mockFileSystem; | 114 MockFileSystemPtr mockFileSystem; |
(...skipping 29 matching lines...) Expand all Loading... |
145 std::string content; | 144 std::string content; |
146 std::string error; | 145 std::string error; |
147 ReadFile(jsEngine, content, error); | 146 ReadFile(jsEngine, content, error); |
148 ASSERT_NE("", error); | 147 ASSERT_NE("", error); |
149 ASSERT_EQ("", content); | 148 ASSERT_EQ("", content); |
150 } | 149 } |
151 | 150 |
152 TEST_F(FileSystemJsObjectTest, Write) | 151 TEST_F(FileSystemJsObjectTest, Write) |
153 { | 152 { |
154 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 153 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
155 AdblockPlus::Sleep(50); | 154 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
156 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); | 155 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); |
157 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); | 156 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); |
158 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); | 157 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); |
159 } | 158 } |
160 | 159 |
161 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) | 160 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
162 { | 161 { |
163 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); | 162 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
164 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); | 163 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
165 } | 164 } |
166 | 165 |
167 TEST_F(FileSystemJsObjectTest, WriteError) | 166 TEST_F(FileSystemJsObjectTest, WriteError) |
168 { | 167 { |
169 mockFileSystem->success = false; | 168 mockFileSystem->success = false; |
170 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 169 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
171 AdblockPlus::Sleep(50); | 170 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
172 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); | 171 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); |
173 } | 172 } |
174 | 173 |
175 TEST_F(FileSystemJsObjectTest, Move) | 174 TEST_F(FileSystemJsObjectTest, Move) |
176 { | 175 { |
177 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 176 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
178 AdblockPlus::Sleep(50); | 177 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
179 ASSERT_EQ("foo", mockFileSystem->movedFrom); | 178 ASSERT_EQ("foo", mockFileSystem->movedFrom); |
180 ASSERT_EQ("bar", mockFileSystem->movedTo); | 179 ASSERT_EQ("bar", mockFileSystem->movedTo); |
181 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); | 180 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); |
182 } | 181 } |
183 | 182 |
184 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) | 183 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) |
185 { | 184 { |
186 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); | 185 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); |
187 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); | 186 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); |
188 } | 187 } |
189 | 188 |
190 TEST_F(FileSystemJsObjectTest, MoveError) | 189 TEST_F(FileSystemJsObjectTest, MoveError) |
191 { | 190 { |
192 mockFileSystem->success = false; | 191 mockFileSystem->success = false; |
193 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 192 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
194 AdblockPlus::Sleep(50); | 193 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
195 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); | 194 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); |
196 } | 195 } |
197 | 196 |
198 TEST_F(FileSystemJsObjectTest, Remove) | 197 TEST_F(FileSystemJsObjectTest, Remove) |
199 { | 198 { |
200 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 199 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
201 AdblockPlus::Sleep(50); | 200 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
202 ASSERT_EQ("foo", mockFileSystem->removedPath); | 201 ASSERT_EQ("foo", mockFileSystem->removedPath); |
203 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); | 202 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); |
204 } | 203 } |
205 | 204 |
206 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) | 205 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) |
207 { | 206 { |
208 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); | 207 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); |
209 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); | 208 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); |
210 } | 209 } |
211 | 210 |
212 TEST_F(FileSystemJsObjectTest, RemoveError) | 211 TEST_F(FileSystemJsObjectTest, RemoveError) |
213 { | 212 { |
214 mockFileSystem->success = false; | 213 mockFileSystem->success = false; |
215 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 214 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
216 AdblockPlus::Sleep(50); | 215 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
217 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); | 216 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); |
218 } | 217 } |
219 | 218 |
220 TEST_F(FileSystemJsObjectTest, Stat) | 219 TEST_F(FileSystemJsObjectTest, Stat) |
221 { | 220 { |
222 mockFileSystem->statExists = true; | 221 mockFileSystem->statExists = true; |
223 mockFileSystem->statIsDirectory= false; | 222 mockFileSystem->statIsDirectory= false; |
224 mockFileSystem->statIsFile = true; | 223 mockFileSystem->statIsFile = true; |
225 mockFileSystem->statLastModified = 1337; | 224 mockFileSystem->statLastModified = 1337; |
226 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 225 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
227 AdblockPlus::Sleep(50); | 226 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
228 ASSERT_EQ("foo", mockFileSystem->statPath); | 227 ASSERT_EQ("foo", mockFileSystem->statPath); |
229 ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString()); | 228 ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString()); |
230 ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool()); | 229 ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool()); |
231 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory")->AsBool()); | 230 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory")->AsBool()); |
232 ASSERT_TRUE(jsEngine->Evaluate("result.isFile")->AsBool()); | 231 ASSERT_TRUE(jsEngine->Evaluate("result.isFile")->AsBool()); |
233 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified")->AsInt()); | 232 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified")->AsInt()); |
234 } | 233 } |
235 | 234 |
236 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) | 235 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) |
237 { | 236 { |
238 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); | 237 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); |
239 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 238 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
240 } | 239 } |
241 | 240 |
242 TEST_F(FileSystemJsObjectTest, StatError) | 241 TEST_F(FileSystemJsObjectTest, StatError) |
243 { | 242 { |
244 mockFileSystem->success = false; | 243 mockFileSystem->success = false; |
245 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 244 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
246 AdblockPlus::Sleep(50); | 245 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
247 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString()); | 246 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString()); |
248 } | 247 } |
OLD | NEW |