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