| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 #include <AdblockPlus.h> | 
|  | 2 #include <gtest/gtest.h> | 
|  | 3 | 
|  | 4 #include "../src/Thread.h" | 
|  | 5 #include "../src/Utils.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::tr1::shared_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::tr1::shared_ptr<std::istream>(stream); | 
|  | 36     } | 
|  | 37 | 
|  | 38     void Write(const std::string& path, std::tr1::shared_ptr<std::ostream> data) | 
|  | 39     { | 
|  | 40       if (!success) | 
|  | 41         throw std::runtime_error("Unable to write to " + path); | 
|  | 42       lastWrittenPath = path; | 
|  | 43       lastWrittenContent = AdblockPlus::Utils::Slurp(*data); | 
|  | 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   ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.read('', '')")); | 
|  | 103 } | 
|  | 104 | 
|  | 105 TEST(FileSystemJsObjectTest, ReadError) | 
|  | 106 { | 
|  | 107   MockFileSystem fileSystem; | 
|  | 108   fileSystem.success = false; | 
|  | 109   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 110   std::string content; | 
|  | 111   std::string error; | 
|  | 112   ReadFile(jsEngine, content, error); | 
|  | 113   ASSERT_NE("", error); | 
|  | 114   ASSERT_EQ("", content); | 
|  | 115 } | 
|  | 116 | 
|  | 117 TEST(FileSystemJsObjectTest, Write) | 
|  | 118 { | 
|  | 119   MockFileSystem fileSystem; | 
|  | 120   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 121   jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); | 
|  | 122   AdblockPlus::Sleep(10); | 
|  | 123   ASSERT_EQ("foo", fileSystem.lastWrittenPath); | 
|  | 124   ASSERT_EQ("bar", fileSystem.lastWrittenContent); | 
|  | 125   ASSERT_EQ("", jsEngine.Evaluate("error")); | 
|  | 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 | 
|  | 135 TEST(FileSystemJsObjectTest, WriteError) | 
|  | 136 { | 
|  | 137   MockFileSystem fileSystem; | 
|  | 138   fileSystem.success = false; | 
|  | 139   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 140   jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); | 
|  | 141   AdblockPlus::Sleep(10); | 
|  | 142   ASSERT_NE("", jsEngine.Evaluate("error")); | 
|  | 143 } | 
|  | 144 | 
|  | 145 TEST(FileSystemJsObjectTest, Move) | 
|  | 146 { | 
|  | 147   MockFileSystem fileSystem; | 
|  | 148   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 149   jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 
|  | 150   AdblockPlus::Sleep(10); | 
|  | 151   ASSERT_EQ("foo", fileSystem.movedFrom); | 
|  | 152   ASSERT_EQ("bar", fileSystem.movedTo); | 
|  | 153   ASSERT_EQ("", jsEngine.Evaluate("error")); | 
|  | 154 } | 
|  | 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 | 
|  | 163 TEST(FileSystemJsObjectTest, MoveError) | 
|  | 164 { | 
|  | 165   MockFileSystem fileSystem; | 
|  | 166   fileSystem.success = false; | 
|  | 167   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 168   jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 
|  | 169   AdblockPlus::Sleep(10); | 
|  | 170   ASSERT_NE("", jsEngine.Evaluate("error")); | 
|  | 171 } | 
|  | 172 | 
|  | 173 TEST(FileSystemJsObjectTest, Remove) | 
|  | 174 { | 
|  | 175   MockFileSystem fileSystem; | 
|  | 176   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 177   jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 
|  | 178   AdblockPlus::Sleep(10); | 
|  | 179   ASSERT_EQ("foo", fileSystem.removedPath); | 
|  | 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('', '')")); | 
|  | 188 } | 
|  | 189 | 
|  | 190 TEST(FileSystemJsObjectTest, RemoveError) | 
|  | 191 { | 
|  | 192   MockFileSystem fileSystem; | 
|  | 193   fileSystem.success = false; | 
|  | 194   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 195   jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 
|  | 196   AdblockPlus::Sleep(10); | 
|  | 197   ASSERT_NE("", jsEngine.Evaluate("error")); | 
|  | 198 } | 
|  | 199 | 
|  | 200 TEST(FileSystemJsObjectTest, Stat) | 
|  | 201 { | 
|  | 202   MockFileSystem fileSystem; | 
|  | 203   fileSystem.statExists = true; | 
|  | 204   fileSystem.statIsDirectory= false; | 
|  | 205   fileSystem.statIsFile = true; | 
|  | 206   fileSystem.statLastModified = 1337; | 
|  | 207   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 208   jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 
|  | 209   AdblockPlus::Sleep(10); | 
|  | 210   ASSERT_EQ("foo", fileSystem.statPath); | 
|  | 211   ASSERT_EQ("", jsEngine.Evaluate("result.error")); | 
|  | 212   ASSERT_EQ("true", jsEngine.Evaluate("result.exists")); | 
|  | 213   ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory")); | 
|  | 214   ASSERT_EQ("true", jsEngine.Evaluate("result.isFile")); | 
|  | 215   ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified")); | 
|  | 216 } | 
|  | 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 | 
|  | 225 TEST(FileSystemJsObjectTest, StatError) | 
|  | 226 { | 
|  | 227   MockFileSystem fileSystem; | 
|  | 228   fileSystem.success = false; | 
|  | 229   AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); | 
|  | 230   jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 
|  | 231   AdblockPlus::Sleep(10); | 
|  | 232   ASSERT_NE("", jsEngine.Evaluate("result.error")); | 
|  | 233 } | 
| OLD | NEW | 
|---|