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

Unified Diff: test/FileSystemJsObject.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Rebased. Corrected most of the review issues. Created July 4, 2017, 7:57 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
std::string contentToRead;
std::string lastWrittenPath;
std::string lastWrittenContent;
std::string movedFrom;
std::string movedTo;
@@ -36,63 +36,80 @@
bool statIsDirectory;
bool statIsFile;
int statLastModified;
MockFileSystem() : success(true)
{
}
- std::shared_ptr<std::istream> 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);
- std::stringstream* const stream = new std::stringstream;
- *stream << contentToRead;
- return std::shared_ptr<std::istream>(stream);
+ {
+ callback("", "Unable to read " + path);
+ return;
+ }
+ callback(std::string(contentToRead), "");
}
- void Write(const std::string& path, std::istream& data)
+ void Write(const std::string& path, const std::string& data,
+ const Callback& callback)
{
if (!success)
- throw std::runtime_error("Unable to write to " + path);
+ {
+ callback("Unable to write to " + path);
+ return;
+ }
lastWrittenPath = path;
- std::stringstream content;
- content << data.rdbuf();
- lastWrittenContent = content.str();
+ 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;
}
@@ -111,19 +128,20 @@
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 = "foo";
std::string content;
@@ -150,17 +168,17 @@
}
TEST_F(FileSystemJsObjectTest, Write)
{
jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
AdblockPlus::Sleep(50);
ASSERT_EQ("foo", mockFileSystem->lastWrittenPath);
ASSERT_EQ("bar", mockFileSystem->lastWrittenContent);
- ASSERT_EQ("", jsEngine->Evaluate("error").AsString());
+ ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined());
}
TEST_F(FileSystemJsObjectTest, WriteIllegalArguments)
{
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()"));
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')"));
}
@@ -173,39 +191,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('', '')"));
}
@@ -221,17 +239,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)
{
@@ -239,10 +257,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());
}

Powered by Google App Engine
This is Rietveld