| LEFT | RIGHT |
| 1 #include <iostream> | |
| 2 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
| 3 #include <gtest/gtest.h> | 2 #include <gtest/gtest.h> |
| 4 | 3 |
| 5 #include "../src/Thread.h" | 4 #include "../src/Thread.h" |
| 5 #include "../src/Utils.h" |
| 6 | 6 |
| 7 namespace | 7 namespace |
| 8 { | 8 { |
| 9 class MockFileSystem : public AdblockPlus::FileSystem | 9 class MockFileSystem : public AdblockPlus::FileSystem |
| 10 { | 10 { |
| 11 public: | 11 public: |
| 12 bool success; | 12 bool success; |
| 13 std::string contentToRead; | 13 std::string contentToRead; |
| 14 std::string lastWrittenPath; | 14 std::string lastWrittenPath; |
| 15 std::string lastWrittenContent; | 15 std::string lastWrittenContent; |
| 16 std::string movedFrom; | 16 std::string movedFrom; |
| 17 std::string movedTo; | 17 std::string movedTo; |
| 18 std::string removedPath; | 18 std::string removedPath; |
| 19 mutable std::string statPath; | 19 mutable std::string statPath; |
| 20 bool statExists; | 20 bool statExists; |
| 21 bool statIsDirectory; | 21 bool statIsDirectory; |
| 22 bool statIsFile; | 22 bool statIsFile; |
| 23 int statLastModified; | 23 int statLastModified; |
| 24 | 24 |
| 25 MockFileSystem() : success(true) | 25 MockFileSystem() : success(true) |
| 26 { | 26 { |
| 27 } | 27 } |
| 28 | 28 |
| 29 std::auto_ptr<std::istream> Read(const std::string& path) const | 29 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const |
| 30 { | 30 { |
| 31 if (!success) | 31 if (!success) |
| 32 throw std::runtime_error("Unable to read " + path); | 32 throw std::runtime_error("Unable to read " + path); |
| 33 std::stringstream* const stream = new std::stringstream; | 33 std::stringstream* const stream = new std::stringstream; |
| 34 *stream << contentToRead; | 34 *stream << contentToRead; |
| 35 return std::auto_ptr<std::istream>(stream); | 35 return std::tr1::shared_ptr<std::istream>(stream); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void Write(const std::string& path, const std::string& content) | 38 void Write(const std::string& path, std::tr1::shared_ptr<std::ostream> data) |
| 39 { | 39 { |
| 40 if (!success) | 40 if (!success) |
| 41 throw std::runtime_error("Unable to write to " + path); | 41 throw std::runtime_error("Unable to write to " + path); |
| 42 lastWrittenPath = path; | 42 lastWrittenPath = path; |
| 43 lastWrittenContent = content; | 43 lastWrittenContent = AdblockPlus::Utils::Slurp(*data); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void Move(const std::string& fromPath, const std::string& toPath) | 46 void Move(const std::string& fromPath, const std::string& toPath) |
| 47 { | 47 { |
| 48 if (!success) | 48 if (!success) |
| 49 throw std::runtime_error("Unable to move " + fromPath + " to " | 49 throw std::runtime_error("Unable to move " + fromPath + " to " |
| 50 + toPath); | 50 + toPath); |
| 51 movedFrom = fromPath; | 51 movedFrom = fromPath; |
| 52 movedTo = toPath; | 52 movedTo = toPath; |
| 53 } | 53 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 std::string content; | 91 std::string content; |
| 92 std::string error; | 92 std::string error; |
| 93 ReadFile(jsEngine, content, error); | 93 ReadFile(jsEngine, content, error); |
| 94 ASSERT_EQ("foo", content); | 94 ASSERT_EQ("foo", content); |
| 95 ASSERT_EQ("", error); | 95 ASSERT_EQ("", error); |
| 96 } | 96 } |
| 97 | 97 |
| 98 TEST(FileSystemJsObjectTest, ReadIllegalArguments) | 98 TEST(FileSystemJsObjectTest, ReadIllegalArguments) |
| 99 { | 99 { |
| 100 AdblockPlus::JsEngine jsEngine(0, 0, 0); | 100 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
| 101 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read()")); |
| 101 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read('', '')")); | 102 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read('', '')")); |
| 102 } | 103 } |
| 103 | 104 |
| 104 TEST(FileSystemJsObjectTest, ReadError) | 105 TEST(FileSystemJsObjectTest, ReadError) |
| 105 { | 106 { |
| 106 MockFileSystem fileSystem; | 107 MockFileSystem fileSystem; |
| 107 fileSystem.success = false; | 108 fileSystem.success = false; |
| 108 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 109 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 109 std::string content; | 110 std::string content; |
| 110 std::string error; | 111 std::string error; |
| 111 ReadFile(jsEngine, content, error); | 112 ReadFile(jsEngine, content, error); |
| 112 ASSERT_NE("", error); | 113 ASSERT_NE("", error); |
| 113 ASSERT_EQ("", content); | 114 ASSERT_EQ("", content); |
| 114 } | 115 } |
| 115 | 116 |
| 116 TEST(FileSystemJsObjectTest, Write) | 117 TEST(FileSystemJsObjectTest, Write) |
| 117 { | 118 { |
| 118 MockFileSystem fileSystem; | 119 MockFileSystem fileSystem; |
| 119 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 120 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 120 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); | 121 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
| 121 AdblockPlus::Sleep(10); | 122 AdblockPlus::Sleep(10); |
| 122 ASSERT_EQ("foo", fileSystem.lastWrittenPath); | 123 ASSERT_EQ("foo", fileSystem.lastWrittenPath); |
| 123 ASSERT_EQ("bar", fileSystem.lastWrittenContent); | 124 ASSERT_EQ("bar", fileSystem.lastWrittenContent); |
| 124 ASSERT_EQ("", jsEngine.Evaluate("error")); | 125 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 125 } | 126 } |
| 126 | 127 |
| 128 TEST(FileSystemJsObjectTest, WriteIllegalArguments) |
| 129 { |
| 130 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
| 131 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write()")); |
| 132 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write('', '', '')")); |
| 133 } |
| 134 |
| 127 TEST(FileSystemJsObjectTest, WriteError) | 135 TEST(FileSystemJsObjectTest, WriteError) |
| 128 { | 136 { |
| 129 MockFileSystem fileSystem; | 137 MockFileSystem fileSystem; |
| 130 fileSystem.success = false; | 138 fileSystem.success = false; |
| 131 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 139 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 132 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); | 140 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
| 133 AdblockPlus::Sleep(10); | 141 AdblockPlus::Sleep(10); |
| 134 ASSERT_NE("", jsEngine.Evaluate("error")); | 142 ASSERT_NE("", jsEngine.Evaluate("error")); |
| 135 } | 143 } |
| 136 | 144 |
| 137 TEST(FileSystemJsObjectTest, Move) | 145 TEST(FileSystemJsObjectTest, Move) |
| 138 { | 146 { |
| 139 MockFileSystem fileSystem; | 147 MockFileSystem fileSystem; |
| 140 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 148 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 141 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 149 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 142 AdblockPlus::Sleep(10); | 150 AdblockPlus::Sleep(10); |
| 143 ASSERT_EQ("foo", fileSystem.movedFrom); | 151 ASSERT_EQ("foo", fileSystem.movedFrom); |
| 144 ASSERT_EQ("bar", fileSystem.movedTo); | 152 ASSERT_EQ("bar", fileSystem.movedTo); |
| 145 ASSERT_EQ("", jsEngine.Evaluate("error")); | 153 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 146 } | 154 } |
| 147 | 155 |
| 156 TEST(FileSystemJsObjectTest, MoveIllegalArguments) |
| 157 { |
| 158 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
| 159 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move()")); |
| 160 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move('', '', '')")); |
| 161 } |
| 162 |
| 148 TEST(FileSystemJsObjectTest, MoveError) | 163 TEST(FileSystemJsObjectTest, MoveError) |
| 149 { | 164 { |
| 150 MockFileSystem fileSystem; | 165 MockFileSystem fileSystem; |
| 151 fileSystem.success = false; | 166 fileSystem.success = false; |
| 152 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 167 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 153 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 168 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 154 AdblockPlus::Sleep(10); | 169 AdblockPlus::Sleep(10); |
| 155 ASSERT_NE("", jsEngine.Evaluate("error")); | 170 ASSERT_NE("", jsEngine.Evaluate("error")); |
| 156 } | 171 } |
| 157 | 172 |
| 158 TEST(FileSystemJsObjectTest, Remove) | 173 TEST(FileSystemJsObjectTest, Remove) |
| 159 { | 174 { |
| 160 MockFileSystem fileSystem; | 175 MockFileSystem fileSystem; |
| 161 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 176 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 162 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 177 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 163 AdblockPlus::Sleep(10); | 178 AdblockPlus::Sleep(10); |
| 164 ASSERT_EQ("foo", fileSystem.removedPath); | 179 ASSERT_EQ("foo", fileSystem.removedPath); |
| 165 ASSERT_EQ("", jsEngine.Evaluate("error")); | 180 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 181 } |
| 182 |
| 183 TEST(FileSystemJsObjectTest, RemoveIllegalArguments) |
| 184 { |
| 185 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
| 186 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove()")); |
| 187 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove('', '')")); |
| 166 } | 188 } |
| 167 | 189 |
| 168 TEST(FileSystemJsObjectTest, RemoveError) | 190 TEST(FileSystemJsObjectTest, RemoveError) |
| 169 { | 191 { |
| 170 MockFileSystem fileSystem; | 192 MockFileSystem fileSystem; |
| 171 fileSystem.success = false; | 193 fileSystem.success = false; |
| 172 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 194 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 173 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 195 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 174 AdblockPlus::Sleep(10); | 196 AdblockPlus::Sleep(10); |
| 175 ASSERT_NE("", jsEngine.Evaluate("error")); | 197 ASSERT_NE("", jsEngine.Evaluate("error")); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 186 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 208 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 187 AdblockPlus::Sleep(10); | 209 AdblockPlus::Sleep(10); |
| 188 ASSERT_EQ("foo", fileSystem.statPath); | 210 ASSERT_EQ("foo", fileSystem.statPath); |
| 189 ASSERT_EQ("", jsEngine.Evaluate("result.error")); | 211 ASSERT_EQ("", jsEngine.Evaluate("result.error")); |
| 190 ASSERT_EQ("true", jsEngine.Evaluate("result.exists")); | 212 ASSERT_EQ("true", jsEngine.Evaluate("result.exists")); |
| 191 ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory")); | 213 ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory")); |
| 192 ASSERT_EQ("true", jsEngine.Evaluate("result.isFile")); | 214 ASSERT_EQ("true", jsEngine.Evaluate("result.isFile")); |
| 193 ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified")); | 215 ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified")); |
| 194 } | 216 } |
| 195 | 217 |
| 218 TEST(FileSystemJsObjectTest, StatIllegalArguments) |
| 219 { |
| 220 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
| 221 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat()")); |
| 222 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat('', '')")); |
| 223 } |
| 224 |
| 196 TEST(FileSystemJsObjectTest, StatError) | 225 TEST(FileSystemJsObjectTest, StatError) |
| 197 { | 226 { |
| 198 MockFileSystem fileSystem; | 227 MockFileSystem fileSystem; |
| 199 fileSystem.success = false; | 228 fileSystem.success = false; |
| 200 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 229 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 201 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 230 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 202 AdblockPlus::Sleep(10); | 231 AdblockPlus::Sleep(10); |
| 203 ASSERT_NE("", jsEngine.Evaluate("result.error")); | 232 ASSERT_NE("", jsEngine.Evaluate("result.error")); |
| 204 } | 233 } |
| LEFT | RIGHT |