| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 MockFileSystem fileSystem; | 88 MockFileSystem fileSystem; |
| 89 fileSystem.contentToRead = "foo"; | 89 fileSystem.contentToRead = "foo"; |
| 90 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 90 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 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) |
| 99 { |
| 100 AdblockPlus::JsEngine jsEngine(0, 0, 0); |
| 101 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read()")); |
| 102 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read('', '')")); |
| 103 } |
| 104 |
| 98 TEST(FileSystemJsObjectTest, ReadError) | 105 TEST(FileSystemJsObjectTest, ReadError) |
| 99 { | 106 { |
| 100 MockFileSystem fileSystem; | 107 MockFileSystem fileSystem; |
| 101 fileSystem.success = false; | 108 fileSystem.success = false; |
| 102 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 109 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 103 std::string content; | 110 std::string content; |
| 104 std::string error; | 111 std::string error; |
| 105 ReadFile(jsEngine, content, error); | 112 ReadFile(jsEngine, content, error); |
| 106 ASSERT_NE("", error); | 113 ASSERT_NE("", error); |
| 107 ASSERT_EQ("", content); | 114 ASSERT_EQ("", content); |
| 108 } | 115 } |
| 109 | 116 |
| 110 TEST(FileSystemJsObjectTest, Write) | 117 TEST(FileSystemJsObjectTest, Write) |
| 111 { | 118 { |
| 112 MockFileSystem fileSystem; | 119 MockFileSystem fileSystem; |
| 113 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 120 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 114 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); | 121 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
| 115 AdblockPlus::Sleep(10); | 122 AdblockPlus::Sleep(10); |
| 116 ASSERT_EQ("foo", fileSystem.lastWrittenPath); | 123 ASSERT_EQ("foo", fileSystem.lastWrittenPath); |
| 117 ASSERT_EQ("bar", fileSystem.lastWrittenContent); | 124 ASSERT_EQ("bar", fileSystem.lastWrittenContent); |
| 118 ASSERT_EQ("", jsEngine.Evaluate("error")); | 125 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 119 } | 126 } |
| 120 | 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 |
| 121 TEST(FileSystemJsObjectTest, WriteError) | 135 TEST(FileSystemJsObjectTest, WriteError) |
| 122 { | 136 { |
| 123 MockFileSystem fileSystem; | 137 MockFileSystem fileSystem; |
| 124 fileSystem.success = false; | 138 fileSystem.success = false; |
| 125 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 139 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 126 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); | 140 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
| 127 AdblockPlus::Sleep(10); | 141 AdblockPlus::Sleep(10); |
| 128 ASSERT_NE("", jsEngine.Evaluate("error")); | 142 ASSERT_NE("", jsEngine.Evaluate("error")); |
| 129 } | 143 } |
| 130 | 144 |
| 131 TEST(FileSystemJsObjectTest, Move) | 145 TEST(FileSystemJsObjectTest, Move) |
| 132 { | 146 { |
| 133 MockFileSystem fileSystem; | 147 MockFileSystem fileSystem; |
| 134 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 148 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 135 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 149 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 136 AdblockPlus::Sleep(10); | 150 AdblockPlus::Sleep(10); |
| 137 ASSERT_EQ("foo", fileSystem.movedFrom); | 151 ASSERT_EQ("foo", fileSystem.movedFrom); |
| 138 ASSERT_EQ("bar", fileSystem.movedTo); | 152 ASSERT_EQ("bar", fileSystem.movedTo); |
| 139 ASSERT_EQ("", jsEngine.Evaluate("error")); | 153 ASSERT_EQ("", jsEngine.Evaluate("error")); |
| 140 } | 154 } |
| 141 | 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 |
| 142 TEST(FileSystemJsObjectTest, MoveError) | 163 TEST(FileSystemJsObjectTest, MoveError) |
| 143 { | 164 { |
| 144 MockFileSystem fileSystem; | 165 MockFileSystem fileSystem; |
| 145 fileSystem.success = false; | 166 fileSystem.success = false; |
| 146 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 167 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 147 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 168 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
| 148 AdblockPlus::Sleep(10); | 169 AdblockPlus::Sleep(10); |
| 149 ASSERT_NE("", jsEngine.Evaluate("error")); | 170 ASSERT_NE("", jsEngine.Evaluate("error")); |
| 150 } | 171 } |
| 151 | 172 |
| 152 TEST(FileSystemJsObjectTest, Remove) | 173 TEST(FileSystemJsObjectTest, Remove) |
| 153 { | 174 { |
| 154 MockFileSystem fileSystem; | 175 MockFileSystem fileSystem; |
| 155 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 176 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 156 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 177 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 157 AdblockPlus::Sleep(10); | 178 AdblockPlus::Sleep(10); |
| 158 ASSERT_EQ("foo", fileSystem.removedPath); | 179 ASSERT_EQ("foo", fileSystem.removedPath); |
| 159 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('', '')")); |
| 160 } | 188 } |
| 161 | 189 |
| 162 TEST(FileSystemJsObjectTest, RemoveError) | 190 TEST(FileSystemJsObjectTest, RemoveError) |
| 163 { | 191 { |
| 164 MockFileSystem fileSystem; | 192 MockFileSystem fileSystem; |
| 165 fileSystem.success = false; | 193 fileSystem.success = false; |
| 166 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 194 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 167 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 195 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
| 168 AdblockPlus::Sleep(10); | 196 AdblockPlus::Sleep(10); |
| 169 ASSERT_NE("", jsEngine.Evaluate("error")); | 197 ASSERT_NE("", jsEngine.Evaluate("error")); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 180 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 208 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 181 AdblockPlus::Sleep(10); | 209 AdblockPlus::Sleep(10); |
| 182 ASSERT_EQ("foo", fileSystem.statPath); | 210 ASSERT_EQ("foo", fileSystem.statPath); |
| 183 ASSERT_EQ("", jsEngine.Evaluate("result.error")); | 211 ASSERT_EQ("", jsEngine.Evaluate("result.error")); |
| 184 ASSERT_EQ("true", jsEngine.Evaluate("result.exists")); | 212 ASSERT_EQ("true", jsEngine.Evaluate("result.exists")); |
| 185 ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory")); | 213 ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory")); |
| 186 ASSERT_EQ("true", jsEngine.Evaluate("result.isFile")); | 214 ASSERT_EQ("true", jsEngine.Evaluate("result.isFile")); |
| 187 ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified")); | 215 ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified")); |
| 188 } | 216 } |
| 189 | 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 |
| 190 TEST(FileSystemJsObjectTest, StatError) | 225 TEST(FileSystemJsObjectTest, StatError) |
| 191 { | 226 { |
| 192 MockFileSystem fileSystem; | 227 MockFileSystem fileSystem; |
| 193 fileSystem.success = false; | 228 fileSystem.success = false; |
| 194 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 229 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); |
| 195 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 230 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
| 196 AdblockPlus::Sleep(10); | 231 AdblockPlus::Sleep(10); |
| 197 ASSERT_NE("", jsEngine.Evaluate("result.error")); | 232 ASSERT_NE("", jsEngine.Evaluate("result.error")); |
| 198 } | 233 } |
| LEFT | RIGHT |