| Index: test/FileSystemJsObject.cpp |
| =================================================================== |
| --- a/test/FileSystemJsObject.cpp |
| +++ b/test/FileSystemJsObject.cpp |
| @@ -16,17 +16,17 @@ |
| */ |
| #include <sstream> |
| #include "BaseJsTest.h" |
| #include "../src/Thread.h" |
| namespace |
| { |
| - class MockFileSystem : public AdblockPlus::FileSystem |
| + class MockFileSystem : public AdblockPlus::IFileSystem |
| { |
| public: |
| bool success; |
| IOBuffer contentToRead; |
| std::string lastWrittenPath; |
| IOBuffer lastWrittenContent; |
| std::string movedFrom; |
| std::string movedTo; |
| @@ -36,58 +36,80 @@ |
| bool statIsDirectory; |
| bool statIsFile; |
| int statLastModified; |
| MockFileSystem() : success(true) |
| { |
| } |
| - IOBuffer Read(const std::string& path) const |
| + void Read(const std::string& path, const ReadCallback& callback) const |
| { |
| if (!success) |
| - throw std::runtime_error("Unable to read " + path); |
| - return contentToRead; |
| + { |
| + callback(IOBuffer(), "Unable to read " + path); |
| + return; |
| + } |
| + callback(IOBuffer(contentToRead), ""); |
| } |
| - void Write(const std::string& path, const IOBuffer& data) |
| + void Write(const std::string& path, const IOBuffer& data, |
| + const Callback& callback) |
| { |
| if (!success) |
| - throw std::runtime_error("Unable to write to " + path); |
| + { |
| + callback("Unable to write to " + path); |
| + return; |
| + } |
| lastWrittenPath = path; |
| + |
| lastWrittenContent = data; |
| + callback(""); |
| } |
| - void Move(const std::string& fromPath, const std::string& toPath) |
| + void Move(const std::string& fromPath, const std::string& toPath, |
| + const Callback& callback) |
| { |
| if (!success) |
| - throw std::runtime_error("Unable to move " + fromPath + " to " |
| - + toPath); |
| + { |
| + callback("Unable to move " + fromPath + " to " + toPath); |
| + return; |
| + } |
| movedFrom = fromPath; |
| movedTo = toPath; |
| + callback(""); |
| } |
| - void Remove(const std::string& path) |
| + void Remove(const std::string& path, const Callback& callback) |
| { |
| if (!success) |
| - throw std::runtime_error("Unable to remove " + path); |
| + { |
| + callback("Unable to remove " + path); |
| + return; |
| + } |
| removedPath = path; |
| + callback(""); |
| } |
| - StatResult Stat(const std::string& path) const |
| + void Stat(const std::string& path, |
| + const StatCallback& callback) const |
| { |
| - if (!success) |
| - throw std::runtime_error("Unable to stat " + path); |
| - statPath = path; |
| StatResult result; |
| - result.exists = statExists; |
| - result.isDirectory = statIsDirectory; |
| - result.isFile = statIsFile; |
| - result.lastModified = statLastModified; |
| - return result; |
| + std::string error; |
| + if (!success) |
| + error = "Unable to stat " + path; |
| + else |
| + { |
| + statPath = path; |
| + result.exists = statExists; |
| + result.isDirectory = statIsDirectory; |
| + result.isFile = statIsFile; |
| + result.lastModified = statLastModified; |
| + } |
| + callback(result, error); |
| } |
| std::string Resolve(const std::string& path) const |
| { |
| if (!success) |
| throw std::runtime_error("Unable to stat " + path); |
| return path; |
| } |
| @@ -106,32 +128,33 @@ |
| class FileSystemJsObjectTest : public BaseJsTest |
| { |
| protected: |
| MockFileSystemPtr mockFileSystem; |
| void SetUp() |
| { |
| - BaseJsTest::SetUp(); |
| mockFileSystem = MockFileSystemPtr(new MockFileSystem); |
| - jsEngine->SetFileSystem(mockFileSystem); |
| + JsEngineCreationParameters params; |
| + params.fileSystem = mockFileSystem; |
| + jsEngine = CreateJsEngine(std::move(params)); |
| } |
| }; |
| } |
| TEST_F(FileSystemJsObjectTest, Read) |
| { |
| mockFileSystem->contentToRead = |
| - AdblockPlus::FileSystem::IOBuffer{'f', 'o', 'o'}; |
| + AdblockPlus::IFileSystem::IOBuffer{'f', 'o', 'o'}; |
| std::string content; |
| std::string error; |
| ReadFile(jsEngine, content, error); |
| ASSERT_EQ("foo", content); |
| - ASSERT_EQ("", error); |
| + ASSERT_EQ("undefined", error); |
| } |
| TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) |
| { |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); |
| } |
| @@ -145,19 +168,19 @@ |
| ASSERT_EQ("", content); |
| } |
| TEST_F(FileSystemJsObjectTest, Write) |
| { |
| jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
| AdblockPlus::Sleep(50); |
| ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); |
| - ASSERT_EQ((AdblockPlus::FileSystem::IOBuffer{'b', 'a', 'r'}), |
| - mockFileSystem->lastWrittenContent); |
| - ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); |
| + ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), |
| + mockFileSystem->lastWrittenContent); |
| + ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
| } |
| TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
| { |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
| } |
| @@ -170,39 +193,39 @@ |
| } |
| TEST_F(FileSystemJsObjectTest, Move) |
| { |
| jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| AdblockPlus::Sleep(50); |
| ASSERT_EQ("foo", mockFileSystem->movedFrom); |
| ASSERT_EQ("bar", mockFileSystem->movedTo); |
| - ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); |
| + ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
| } |
| TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) |
| { |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); |
| } |
| TEST_F(FileSystemJsObjectTest, MoveError) |
| { |
| mockFileSystem->success = false; |
| jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| AdblockPlus::Sleep(50); |
| - ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
| + ASSERT_FALSE(jsEngine->Evaluate("error").IsUndefined()); |
| } |
| TEST_F(FileSystemJsObjectTest, Remove) |
| { |
| jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| AdblockPlus::Sleep(50); |
| ASSERT_EQ("foo", mockFileSystem->removedPath); |
| - ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); |
| + ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
| } |
| TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) |
| { |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); |
| } |
| @@ -218,17 +241,17 @@ |
| { |
| mockFileSystem->statExists = true; |
| mockFileSystem->statIsDirectory= false; |
| mockFileSystem->statIsFile = true; |
| mockFileSystem->statLastModified = 1337; |
| jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| AdblockPlus::Sleep(50); |
| ASSERT_EQ("foo", mockFileSystem->statPath); |
| - ASSERT_EQ("", jsEngine->Evaluate("result.error").AsString()); |
| + ASSERT_TRUE(jsEngine->Evaluate("result.error").IsUndefined()); |
| ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); |
| ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); |
| ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); |
| ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); |
| } |
| TEST_F(FileSystemJsObjectTest, StatIllegalArguments) |
| { |
| @@ -236,10 +259,10 @@ |
| ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
| } |
| TEST_F(FileSystemJsObjectTest, StatError) |
| { |
| mockFileSystem->success = false; |
| jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| AdblockPlus::Sleep(50); |
| - ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); |
| + ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); |
| } |