OLD | NEW |
(Empty) | |
| 1 #include <iostream> |
| 2 #include <AdblockPlus.h> |
| 3 #include <gtest/gtest.h> |
| 4 |
| 5 #include "../src/Thread.h" |
| 6 |
| 7 namespace |
| 8 { |
| 9 class MockFileSystem : public AdblockPlus::FileSystem |
| 10 { |
| 11 public: |
| 12 bool success; |
| 13 std::string contentToRead; |
| 14 std::string lastWrittenPath; |
| 15 std::string lastWrittenContent; |
| 16 std::string movedFrom; |
| 17 std::string movedTo; |
| 18 std::string removedPath; |
| 19 mutable std::string statPath; |
| 20 bool statExists; |
| 21 bool statIsDirectory; |
| 22 bool statIsFile; |
| 23 int statLastModified; |
| 24 |
| 25 MockFileSystem() : success(true) |
| 26 { |
| 27 } |
| 28 |
| 29 std::auto_ptr<std::istream> Read(const std::string& path) const |
| 30 { |
| 31 if (!success) |
| 32 throw std::runtime_error("Unable to read " + path); |
| 33 std::stringstream* const stream = new std::stringstream; |
| 34 *stream << contentToRead; |
| 35 return std::auto_ptr<std::istream>(stream); |
| 36 } |
| 37 |
| 38 void Write(const std::string& path, const std::string& content) |
| 39 { |
| 40 if (!success) |
| 41 throw std::runtime_error("Unable to write to " + path); |
| 42 lastWrittenPath = path; |
| 43 lastWrittenContent = content; |
| 44 } |
| 45 |
| 46 void Move(const std::string& fromPath, const std::string& toPath) |
| 47 { |
| 48 if (!success) |
| 49 throw std::runtime_error("Unable to move " + fromPath + " to " |
| 50 + toPath); |
| 51 movedFrom = fromPath; |
| 52 movedTo = toPath; |
| 53 } |
| 54 |
| 55 void Remove(const std::string& path) |
| 56 { |
| 57 if (!success) |
| 58 throw std::runtime_error("Unable to remove " + path); |
| 59 removedPath = path; |
| 60 } |
| 61 |
| 62 StatResult Stat(const std::string& path) const |
| 63 { |
| 64 if (!success) |
| 65 throw std::runtime_error("Unable to stat " + path); |
| 66 statPath = path; |
| 67 StatResult result; |
| 68 result.exists = statExists; |
| 69 result.isDirectory = statIsDirectory; |
| 70 result.isFile = statIsFile; |
| 71 result.lastModified = statLastModified; |
| 72 return result; |
| 73 } |
| 74 }; |
| 75 |
| 76 void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content, |
| 77 std::string& error) |
| 78 { |
| 79 jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})"); |
| 80 AdblockPlus::Sleep(10); |
| 81 content = jsEngine.Evaluate("result.content"); |
| 82 error = jsEngine.Evaluate("result.error"); |
| 83 } |
| 84 } |
| 85 |
| 86 TEST(FileSystemJsObjectTest, Read) |
| 87 { |
| 88 MockFileSystem fileSystem; |
| 89 fileSystem.contentToRead = "foo"; |
| 90 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 91 std::string content; |
| 92 std::string error; |
| 93 ReadFile(jsEngine, content, error); |
| 94 ASSERT_EQ("foo", content); |
| 95 ASSERT_EQ("", error); |
| 96 } |
| 97 |
| 98 TEST(FileSystemJsObjectTest, ReadIllegalArguments) |
| 99 { |
| 100 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
| 101 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read('', '')")); |
| 102 } |
| 103 |
| 104 TEST(FileSystemJsObjectTest, ReadError) |
| 105 { |
| 106 MockFileSystem fileSystem; |
| 107 fileSystem.success = false; |
| 108 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 109 std::string content; |
| 110 std::string error; |
| 111 ReadFile(jsEngine, content, error); |
| 112 ASSERT_NE("", error); |
| 113 ASSERT_EQ("", content); |
| 114 } |
| 115 |
| 116 TEST(FileSystemJsObjectTest, Write) |
| 117 { |
| 118 MockFileSystem fileSystem; |
| 119 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 120 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
| 121 AdblockPlus::Sleep(10); |
| 122 ASSERT_EQ("foo", fileSystem.lastWrittenPath); |
| 123 ASSERT_EQ("bar", fileSystem.lastWrittenContent); |
| 124 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 125 } |
| 126 |
| 127 TEST(FileSystemJsObjectTest, WriteError) |
| 128 { |
| 129 MockFileSystem fileSystem; |
| 130 fileSystem.success = false; |
| 131 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 132 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
| 133 AdblockPlus::Sleep(10); |
| 134 ASSERT_NE("", jsEngine.Evaluate("error")); |
| 135 } |
| 136 |
| 137 TEST(FileSystemJsObjectTest, Move) |
| 138 { |
| 139 MockFileSystem fileSystem; |
| 140 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 141 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 142 AdblockPlus::Sleep(10); |
| 143 ASSERT_EQ("foo", fileSystem.movedFrom); |
| 144 ASSERT_EQ("bar", fileSystem.movedTo); |
| 145 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 146 } |
| 147 |
| 148 TEST(FileSystemJsObjectTest, MoveError) |
| 149 { |
| 150 MockFileSystem fileSystem; |
| 151 fileSystem.success = false; |
| 152 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 153 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 154 AdblockPlus::Sleep(10); |
| 155 ASSERT_NE("", jsEngine.Evaluate("error")); |
| 156 } |
| 157 |
| 158 TEST(FileSystemJsObjectTest, Remove) |
| 159 { |
| 160 MockFileSystem fileSystem; |
| 161 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 162 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 163 AdblockPlus::Sleep(10); |
| 164 ASSERT_EQ("foo", fileSystem.removedPath); |
| 165 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 166 } |
| 167 |
| 168 TEST(FileSystemJsObjectTest, RemoveError) |
| 169 { |
| 170 MockFileSystem fileSystem; |
| 171 fileSystem.success = false; |
| 172 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 173 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 174 AdblockPlus::Sleep(10); |
| 175 ASSERT_NE("", jsEngine.Evaluate("error")); |
| 176 } |
| 177 |
| 178 TEST(FileSystemJsObjectTest, Stat) |
| 179 { |
| 180 MockFileSystem fileSystem; |
| 181 fileSystem.statExists = true; |
| 182 fileSystem.statIsDirectory= false; |
| 183 fileSystem.statIsFile = true; |
| 184 fileSystem.statLastModified = 1337; |
| 185 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 186 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 187 AdblockPlus::Sleep(10); |
| 188 ASSERT_EQ("foo", fileSystem.statPath); |
| 189 ASSERT_EQ("", jsEngine.Evaluate("result.error")); |
| 190 ASSERT_EQ("true", jsEngine.Evaluate("result.exists")); |
| 191 ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory")); |
| 192 ASSERT_EQ("true", jsEngine.Evaluate("result.isFile")); |
| 193 ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified")); |
| 194 } |
| 195 |
| 196 TEST(FileSystemJsObjectTest, StatError) |
| 197 { |
| 198 MockFileSystem fileSystem; |
| 199 fileSystem.success = false; |
| 200 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 201 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 202 AdblockPlus::Sleep(10); |
| 203 ASSERT_NE("", jsEngine.Evaluate("result.error")); |
| 204 } |
OLD | NEW |