OLD | NEW |
1 #include <AdblockPlus.h> | 1 #include "BaseJsTest.h" |
2 #include <gtest/gtest.h> | |
3 | |
4 #include "../src/Thread.h" | 2 #include "../src/Thread.h" |
5 #include "../src/Utils.h" | 3 #include "../src/Utils.h" |
6 | 4 |
7 namespace | 5 namespace |
8 { | 6 { |
9 class MockFileSystem : public AdblockPlus::FileSystem | 7 class MockFileSystem : public AdblockPlus::FileSystem |
10 { | 8 { |
11 public: | 9 public: |
12 bool success; | 10 bool success; |
13 std::string contentToRead; | 11 std::string contentToRead; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 }; | 72 }; |
75 | 73 |
76 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, | 74 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, |
77 std::string& error) | 75 std::string& error) |
78 { | 76 { |
79 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); | 77 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); |
80 AdblockPlus::Sleep(50); | 78 AdblockPlus::Sleep(50); |
81 content = jsEngine->Evaluate("result.content")->AsString(); | 79 content = jsEngine->Evaluate("result.content")->AsString(); |
82 error = jsEngine->Evaluate("result.error")->AsString(); | 80 error = jsEngine->Evaluate("result.error")->AsString(); |
83 } | 81 } |
| 82 |
| 83 typedef std::tr1::shared_ptr<MockFileSystem> MockFileSystemPtr; |
| 84 |
| 85 class FileSystemJsObjectTest : public BaseJsTest |
| 86 { |
| 87 protected: |
| 88 MockFileSystemPtr mockFileSystem; |
| 89 |
| 90 void SetUp() |
| 91 { |
| 92 BaseJsTest::SetUp(); |
| 93 mockFileSystem = MockFileSystemPtr(new MockFileSystem); |
| 94 jsEngine->SetFileSystem(mockFileSystem); |
| 95 } |
| 96 }; |
84 } | 97 } |
85 | 98 |
86 TEST(FileSystemJsObjectTest, Read) | 99 TEST_F(FileSystemJsObjectTest, Read) |
87 { | 100 { |
88 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 101 mockFileSystem->contentToRead = "foo"; |
89 MockFileSystem* fileSystem = new MockFileSystem(); | |
90 fileSystem->contentToRead = "foo"; | |
91 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
92 std::string content; | 102 std::string content; |
93 std::string error; | 103 std::string error; |
94 ReadFile(jsEngine, content, error); | 104 ReadFile(jsEngine, content, error); |
95 ASSERT_EQ("foo", content); | 105 ASSERT_EQ("foo", content); |
96 ASSERT_EQ("", error); | 106 ASSERT_EQ("", error); |
97 } | 107 } |
98 | 108 |
99 TEST(FileSystemJsObjectTest, ReadIllegalArguments) | 109 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) |
100 { | 110 { |
101 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
102 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); | 111 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); |
103 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); | 112 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); |
104 } | 113 } |
105 | 114 |
106 TEST(FileSystemJsObjectTest, ReadError) | 115 TEST_F(FileSystemJsObjectTest, ReadError) |
107 { | 116 { |
108 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 117 mockFileSystem->success = false; |
109 MockFileSystem* fileSystem = new MockFileSystem(); | |
110 fileSystem->success = false; | |
111 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
112 std::string content; | 118 std::string content; |
113 std::string error; | 119 std::string error; |
114 ReadFile(jsEngine, content, error); | 120 ReadFile(jsEngine, content, error); |
115 ASSERT_NE("", error); | 121 ASSERT_NE("", error); |
116 ASSERT_EQ("", content); | 122 ASSERT_EQ("", content); |
117 } | 123 } |
118 | 124 |
119 TEST(FileSystemJsObjectTest, Write) | 125 TEST_F(FileSystemJsObjectTest, Write) |
120 { | 126 { |
121 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
122 MockFileSystem* fileSystem = new MockFileSystem(); | |
123 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
124 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 127 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
125 AdblockPlus::Sleep(50); | 128 AdblockPlus::Sleep(50); |
126 ASSERT_EQ("foo", fileSystem->lastWrittenPath); | 129 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); |
127 ASSERT_EQ("bar", fileSystem->lastWrittenContent); | 130 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); |
128 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); | 131 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); |
129 } | 132 } |
130 | 133 |
131 TEST(FileSystemJsObjectTest, WriteIllegalArguments) | 134 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
132 { | 135 { |
133 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
134 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); | 136 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
135 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); | 137 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
136 } | 138 } |
137 | 139 |
138 TEST(FileSystemJsObjectTest, WriteError) | 140 TEST_F(FileSystemJsObjectTest, WriteError) |
139 { | 141 { |
140 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 142 mockFileSystem->success = false; |
141 MockFileSystem* fileSystem = new MockFileSystem(); | |
142 fileSystem->success = false; | |
143 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
144 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; | 143 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})")
; |
145 AdblockPlus::Sleep(50); | 144 AdblockPlus::Sleep(50); |
146 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); | 145 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); |
147 } | 146 } |
148 | 147 |
149 TEST(FileSystemJsObjectTest, Move) | 148 TEST_F(FileSystemJsObjectTest, Move) |
150 { | 149 { |
151 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
152 MockFileSystem* fileSystem = new MockFileSystem(); | |
153 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
154 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 150 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
155 AdblockPlus::Sleep(50); | 151 AdblockPlus::Sleep(50); |
156 ASSERT_EQ("foo", fileSystem->movedFrom); | 152 ASSERT_EQ("foo", mockFileSystem->movedFrom); |
157 ASSERT_EQ("bar", fileSystem->movedTo); | 153 ASSERT_EQ("bar", mockFileSystem->movedTo); |
158 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); | 154 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); |
159 } | 155 } |
160 | 156 |
161 TEST(FileSystemJsObjectTest, MoveIllegalArguments) | 157 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) |
162 { | 158 { |
163 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
164 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); | 159 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); |
165 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); | 160 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); |
166 } | 161 } |
167 | 162 |
168 TEST(FileSystemJsObjectTest, MoveError) | 163 TEST_F(FileSystemJsObjectTest, MoveError) |
169 { | 164 { |
170 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 165 mockFileSystem->success = false; |
171 MockFileSystem* fileSystem = new MockFileSystem(); | |
172 fileSystem->success = false; | |
173 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
174 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); | 166 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
175 AdblockPlus::Sleep(50); | 167 AdblockPlus::Sleep(50); |
176 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); | 168 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); |
177 } | 169 } |
178 | 170 |
179 TEST(FileSystemJsObjectTest, Remove) | 171 TEST_F(FileSystemJsObjectTest, Remove) |
180 { | 172 { |
181 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
182 MockFileSystem* fileSystem = new MockFileSystem(); | |
183 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
184 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 173 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
185 AdblockPlus::Sleep(50); | 174 AdblockPlus::Sleep(50); |
186 ASSERT_EQ("foo", fileSystem->removedPath); | 175 ASSERT_EQ("foo", mockFileSystem->removedPath); |
187 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); | 176 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); |
188 } | 177 } |
189 | 178 |
190 TEST(FileSystemJsObjectTest, RemoveIllegalArguments) | 179 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) |
191 { | 180 { |
192 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
193 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); | 181 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); |
194 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); | 182 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); |
195 } | 183 } |
196 | 184 |
197 TEST(FileSystemJsObjectTest, RemoveError) | 185 TEST_F(FileSystemJsObjectTest, RemoveError) |
198 { | 186 { |
199 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 187 mockFileSystem->success = false; |
200 MockFileSystem* fileSystem = new MockFileSystem(); | |
201 fileSystem->success = false; | |
202 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
203 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); | 188 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
204 AdblockPlus::Sleep(50); | 189 AdblockPlus::Sleep(50); |
205 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); | 190 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); |
206 } | 191 } |
207 | 192 |
208 TEST(FileSystemJsObjectTest, Stat) | 193 TEST_F(FileSystemJsObjectTest, Stat) |
209 { | 194 { |
210 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 195 mockFileSystem->statExists = true; |
211 MockFileSystem* fileSystem = new MockFileSystem(); | 196 mockFileSystem->statIsDirectory= false; |
212 fileSystem->statExists = true; | 197 mockFileSystem->statIsFile = true; |
213 fileSystem->statIsDirectory= false; | 198 mockFileSystem->statLastModified = 1337; |
214 fileSystem->statIsFile = true; | |
215 fileSystem->statLastModified = 1337; | |
216 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
217 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 199 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
218 AdblockPlus::Sleep(50); | 200 AdblockPlus::Sleep(50); |
219 ASSERT_EQ("foo", fileSystem->statPath); | 201 ASSERT_EQ("foo", mockFileSystem->statPath); |
220 ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString()); | 202 ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString()); |
221 ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool()); | 203 ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool()); |
222 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory")->AsBool()); | 204 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory")->AsBool()); |
223 ASSERT_TRUE(jsEngine->Evaluate("result.isFile")->AsBool()); | 205 ASSERT_TRUE(jsEngine->Evaluate("result.isFile")->AsBool()); |
224 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified")->AsInt()); | 206 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified")->AsInt()); |
225 } | 207 } |
226 | 208 |
227 TEST(FileSystemJsObjectTest, StatIllegalArguments) | 209 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) |
228 { | 210 { |
229 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
230 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); | 211 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); |
231 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 212 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
232 } | 213 } |
233 | 214 |
234 TEST(FileSystemJsObjectTest, StatError) | 215 TEST_F(FileSystemJsObjectTest, StatError) |
235 { | 216 { |
236 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 217 mockFileSystem->success = false; |
237 MockFileSystem* fileSystem = new MockFileSystem(); | |
238 fileSystem->success = false; | |
239 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(fileSystem));; | |
240 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 218 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
241 AdblockPlus::Sleep(50); | 219 AdblockPlus::Sleep(50); |
242 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString()); | 220 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString()); |
243 } | 221 } |
OLD | NEW |