Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: chrome/test/data/chromeos/wallpaper_manager/unit_tests/api_mock.js

Issue 642943002: Add tests for sync custom wallpaper feature (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix issues from 2nd code review Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 5
6 var TestConstants = { 6 var TestConstants = {
7 wallpaperURL: 'https://test.com/test.jpg', 7 wallpaperURL: 'https://test.com/test.jpg',
8 // A dummy string which is used to mock an image. 8 // A dummy string which is used to mock an image.
9 image: '*#*@#&' 9 image: '*#*@#&',
10 // A dummy array which is used to mock the file content.
11 fileString: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
12 };
13
14 function FileReader() {
15 this.result = '';
16 this.onloadend = function() {
17 };
18 this.readAsArrayBuffer = function(mockFile) {
19 this.result = mockFile;
20 this.onloadend();
21 }
22 }
23
24 // Mock localFS handler
25 var mockLocalFS = {
26 root: {
27 dirList: [],
28 rootFileList: [],
29 getDirectory: function(dir, isCreate, success, failure) {
30 for(var i = 0; i < this.dirList.length; i++) {
31 if (this.dirList[i].name == dir) {
32 success(this.dirList[i]);
33 return;
34 }
35 }
36 if (!isCreate.create) {
37 if (failure) {
bshe 2014/10/28 22:34:57 nit: perhaps easier to read this way: if (!isCreat
Ran 2014/10/29 19:13:13 I don't think they are equivalent. As if we pass i
bshe 2014/10/30 12:51:36 dooh. My mistake. you are right.
38 failure('DIR_NOT_FOUND');
39 }
40 } else {
41 this.dirList.push(new DirEntry(dir));
42 success(this.dirList[this.dirList.length - 1]);
43 }
44 },
45 getFile: function(fileName, isCreate, success, failure) {
46 if (fileName[0] == '/')
47 fileName = fileName.substr(1);
48 if (fileName.split('/').length == 1) {
49 for(var i = 0; i < this.rootFileList.length; i++) {
50 if (fileName == this.rootFileList[i].name) {
51 success(this.rootFileList[i]);
52 return;
53 }
54 }
55 if (!isCreate.create) {
56 if (failure)
57 failure('FILE_NOT_FOUND');
58 } else {
59 this.rootFileList.push(new FileEntry(fileName));
60 success(this.rootFileList[this.rootFileList.length - 1]);
61 }
62 } else if (fileName.split('/').length == 2) {
63 var realDirName = fileName.split('/')[0];
64 var realFileName = fileName.split('/')[1];
65 var getDirSuccess = function(dirEntry) {
66 dirEntry.getFile(realFileName, isCreate, success, failure);
67 };
68 this.getDirectory(realDirName, {create: false},
69 getDirSuccess, failure);
70 } else {
71 console.error('Only support one-level fileSystem mock')
bshe 2014/10/28 22:34:57 nit: Only support one level deep subdirectory.
Ran 2014/10/29 19:13:13 Done.
72 }
73 }
74 },
75 mockTestFile: function(fileName) {
76 var ret;
bshe 2014/10/28 22:34:57 nit: ret/mockFile
Ran 2014/10/29 19:13:13 Done.
77 if (fileName[0] == '/')
78 fileName = fileName.substr(1);
79 if (fileName.split('/').length == 1) {
80 ret = new FileEntry(fileName);
81 this.root.rootFileList.push(ret);
82 } else if (fileName.split('/').length == 2) {
83 var realDirName = fileName.split('/')[0];
84 var realFileName = fileName.split('/')[1];
85 var getDirSuccess = function(dirEntry) {
86 dirEntry.getFile(realFileName, {create: true},
87 function(fe) {ret = fe;} );
88 };
89 this.root.getDirectory(realDirName, {create: true}, getDirSuccess);
90 } else {
91 console.error('Only support one-level fileSystem mock')
92 }
93 return ret;
94 },
95 reset: function() {
96 this.root.dirList = [];
97 this.root.rootFileList = [];
98 }
99 };
100
101 function DirEntry(dirname) {
102 this.name = dirname;
103 this.fileList = [];
104 this.getFile = function(fileName, isCreate, success, failure) {
105 for(var i = 0; i < this.fileList.length; i++) {
106 if (fileName == this.fileList[i].name) {
107 success(this.fileList[i]);
108 return;
109 }
110 }
111 if (!isCreate.create) {
112 if (failure) {
113 failure('FILE_NOT_FOUND');
114 }
115 } else {
116 this.fileList.push( new FileEntry(fileName) );
117 success(this.fileList[this.fileList.length - 1]);
118 }
119 }
120 }
121
122 window.webkitRequestFileSystem = function(type, size, callback) {
123 callback(mockLocalFS);
124 }
125
126 function Blob(arg) {
127 var data = arg[0];
128 this.content = '';
129 if (typeof data == 'string') {
130 this.content = data;
131 } else {
132 this.content = Array.prototype.join.call(data);
133 }
134 }
135
136 var mockWriter = {
137 write: function(blobData) {
138 }
139 };
140
141 function FileEntry(filename) {
142 this.name = filename;
143 this.file = function(callback) {
144 callback(TestConstants.fileString);
145 };
146 this.createWriter = function(callback) {
147 callback(mockWriter);
148 };
149 this.remove = function(success, failure) {
150 };
151 }
152
153 // Mock chrome syncFS handler
154 var mockSyncFS = {
155 root: {
156 fileList: [],
157 getFile: function(fileName, isCreate, success, failure) {
158 for(var i = 0; i < this.fileList.length; i++) {
159 if (fileName == this.fileList[i].name) {
160 success(this.fileList[i]);
161 return;
162 }
163 }
164 if (!isCreate.create) {
165 if (failure)
166 failure('FILE_NOT_FOUND');
167 } else {
168 this.fileList.push(new FileEntry(fileName));
169 success(this.fileList[this.fileList.length - 1]);
170 }
171 },
172 },
173 mockTestFile: function(fileName) {
174 var ret = new FileEntry(fileName);
175 this.root.fileList.push(ret);
176 return ret;
177 },
178 reset: function() {
179 this.root.fileList = [];
180 }
10 }; 181 };
11 182
12 // Mock a few chrome apis. 183 // Mock a few chrome apis.
13 var chrome = { 184 var chrome = {
14 storage: { 185 storage: {
15 local: { 186 local: {
16 get: function(key, callback) { 187 get: function(key, callback) {
17 var items = {}; 188 var items = {};
18 switch (key) { 189 switch (key) {
19 case Constants.AccessLocalWallpaperInfoKey: 190 case Constants.AccessLocalWallpaperInfoKey:
20 items[Constants.AccessLocalWallpaperInfoKey] = { 191 items[Constants.AccessLocalWallpaperInfoKey] = {
21 'url': 'dummy', 192 'url': 'dummy',
22 'layout': 'dummy', 193 'layout': 'dummy',
23 'source': 'dummy' 194 'source': Constants.WallpaperSourceEnum.Custom
24 }; 195 };
25 } 196 }
26 callback(items); 197 callback(items);
27 }, 198 },
28 set: function(items, callback) { 199 set: function(items, callback) {
29 } 200 }
30 }, 201 },
31 sync: { 202 sync: {
32 get: function(key, callback) { 203 get: function(key, callback) {
33 }, 204 },
34 set: function(items, callback) { 205 set: function(items, callback) {
35 } 206 }
36 }, 207 },
37 onChanged: { 208 onChanged: {
38 addListener: function(listener) { 209 addListener: function(listener) {
39 this.dispatch = listener; 210 this.dispatch = listener;
40 } 211 }
41 } 212 }
42 }, 213 },
43 syncFileSystem: { 214 syncFileSystem: {
44 requestFileSystem: function(fs) { 215 requestFileSystem: function(callback) {
216 callback(mockSyncFS);
45 }, 217 },
46 onFileStatusChanged: { 218 onFileStatusChanged: {
47 addListener: function(listener) { 219 addListener: function(listener) {
48 this.dispatch = listener; 220 this.dispatch = listener;
49 } 221 }
50 } 222 }
51 }, 223 },
52 app: { 224 app: {
53 runtime: { 225 runtime: {
54 onLaunched: { 226 onLaunched: {
55 addListener: function(listener) { 227 addListener: function(listener) {
56 } 228 }
57 } 229 }
58 } 230 }
59 }, 231 },
60 alarms: { 232 alarms: {
61 onAlarm: { 233 onAlarm: {
62 addListener: function(listener) { 234 addListener: function(listener) {
63 } 235 }
64 } 236 }
65 }, 237 },
66 wallpaperPrivate: { 238 wallpaperPrivate: {
67 getStrings: function(callback) { 239 getStrings: function(callback) {
68 callback({isExperimental: false}); 240 callback({isExperimental: false});
241 },
242 setCustomWallpaper: function(data, layout, isGenerateThumbnail, fileName,
243 callback) {
69 } 244 }
245 },
246 runtime: {
247 lastError: null
70 } 248 }
71 }; 249 };
72 250
73 (function (exports) { 251 (function (exports) {
74 var originalXMLHttpRequest = window.XMLHttpRequest; 252 var originalXMLHttpRequest = window.XMLHttpRequest;
75 253
76 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status 254 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status
77 // equals to 200 in function |send|. 255 // equals to 200 in function |send|.
78 function MockXMLHttpRequest() { 256 function MockXMLHttpRequest() {
79 } 257 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 }; 304 };
127 305
128 function uninstallMockXMLHttpRequest() { 306 function uninstallMockXMLHttpRequest() {
129 window['XMLHttpRequest'] = originalXMLHttpRequest; 307 window['XMLHttpRequest'] = originalXMLHttpRequest;
130 }; 308 };
131 309
132 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; 310 exports.installMockXMLHttpRequest = installMockXMLHttpRequest;
133 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; 311 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest;
134 312
135 })(window); 313 })(window);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698