Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/FileSystemJsObject.cpp

Issue 10296001: Implement File API (Closed)
Patch Set: Created April 12, 2013, 10:10 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/Utils.cpp ('k') | test/JsEngine.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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, ReadError)
99 {
100 MockFileSystem fileSystem;
101 fileSystem.success = false;
102 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
103 std::string content;
104 std::string error;
105 ReadFile(jsEngine, content, error);
106 ASSERT_NE("", error);
107 ASSERT_EQ("", content);
108 }
109
110 TEST(FileSystemJsObjectTest, Write)
111 {
112 MockFileSystem fileSystem;
113 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
114 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
115 AdblockPlus::Sleep(10);
116 ASSERT_EQ("foo", fileSystem.lastWrittenPath);
117 ASSERT_EQ("bar", fileSystem.lastWrittenContent);
118 ASSERT_EQ("", jsEngine.Evaluate("error"));
119 }
120
121 TEST(FileSystemJsObjectTest, WriteError)
122 {
123 MockFileSystem fileSystem;
124 fileSystem.success = false;
125 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
126 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
127 AdblockPlus::Sleep(10);
128 ASSERT_NE("", jsEngine.Evaluate("error"));
129 }
130
131 TEST(FileSystemJsObjectTest, Move)
132 {
133 MockFileSystem fileSystem;
134 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
135 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
136 AdblockPlus::Sleep(10);
137 ASSERT_EQ("foo", fileSystem.movedFrom);
138 ASSERT_EQ("bar", fileSystem.movedTo);
139 ASSERT_EQ("", jsEngine.Evaluate("error"));
140 }
141
142 TEST(FileSystemJsObjectTest, MoveError)
143 {
144 MockFileSystem fileSystem;
145 fileSystem.success = false;
146 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
147 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
148 AdblockPlus::Sleep(10);
149 ASSERT_NE("", jsEngine.Evaluate("error"));
150 }
151
152 TEST(FileSystemJsObjectTest, Remove)
153 {
154 MockFileSystem fileSystem;
155 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
156 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
157 AdblockPlus::Sleep(10);
158 ASSERT_EQ("foo", fileSystem.removedPath);
159 ASSERT_EQ("", jsEngine.Evaluate("error"));
160 }
161
162 TEST(FileSystemJsObjectTest, RemoveError)
163 {
164 MockFileSystem fileSystem;
165 fileSystem.success = false;
166 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
167 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
168 AdblockPlus::Sleep(10);
169 ASSERT_NE("", jsEngine.Evaluate("error"));
170 }
171
172 TEST(FileSystemJsObjectTest, Stat)
173 {
174 MockFileSystem fileSystem;
175 fileSystem.statExists = true;
176 fileSystem.statIsDirectory= false;
177 fileSystem.statIsFile = true;
178 fileSystem.statLastModified = 1337;
179 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
180 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
181 AdblockPlus::Sleep(10);
182 ASSERT_EQ("foo", fileSystem.statPath);
183 ASSERT_EQ("", jsEngine.Evaluate("result.error"));
184 ASSERT_EQ("true", jsEngine.Evaluate("result.exists"));
185 ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory"));
186 ASSERT_EQ("true", jsEngine.Evaluate("result.isFile"));
187 ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified"));
188 }
189
190 TEST(FileSystemJsObjectTest, StatError)
191 {
192 MockFileSystem fileSystem;
193 fileSystem.success = false;
194 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
195 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
196 AdblockPlus::Sleep(10);
197 ASSERT_NE("", jsEngine.Evaluate("result.error"));
198 }
OLDNEW
« no previous file with comments | « src/Utils.cpp ('k') | test/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld