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

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: Move mock functions to api_mock.js 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 == false) {
bshe 2014/10/27 20:14:51 nit: it is probably more robust to use: if (!isCre
Ran 2014/10/27 22:22:01 Done.
37 if (failure) {
38 failure('DIR_NOT_FOUND');
39 }
40 }
41 else {
bshe 2014/10/27 20:14:51 nit: move 'else' to previous line. The same for al
Ran 2014/10/27 22:22:01 Done.
42 this.dirList.push(new DirEntry(dir));
43 success(this.dirList[this.dirList.length - 1]);
44 }
45 },
46 getFile: function(fileName, isCreate, success, failure) {
47 if (fileName[0] == '/')
48 fileName = fileName.substr(1);
49 if (fileName.split('/').length == 1) {
50 for(var i = 0; i < this.rootFileList.length; i++) {
51 if (fileName == this.rootFileList[i].name) {
52 success(this.rootFileList[i]);
53 return;
54 }
55 }
56 if (isCreate.create == false) {
bshe 2014/10/27 20:14:51 nit: if (!isCreate.create)
Ran 2014/10/27 22:22:01 Done.
57 if (failure)
58 failure('FILE_NOT_FOUND');
59 }
60 else {
61 this.rootFileList.push(new FileEntry(fileName));
62 success(this.rootFileList[this.rootFileList.length - 1]);
63 }
64 }
65 else if (fileName.split('/').length == 2) {
bshe 2014/10/27 20:14:51 nit: move else if to previous line
Ran 2014/10/27 22:22:01 Done.
66 var realDirName = fileName.split('/')[0];
67 var realFileName = fileName.split('/')[1];
68 var getDirSuccess = function(dirEntry) {
69 dirEntry.getFile(realFileName, isCreate, success, failure);
70 };
71 this.getDirectory(realDirName, {create: false},
72 getDirSuccess, failure);
73 }
74 else {
75 console.error('Only support one-level fileSystem mock')
76 }
77 }
78 },
79 addFile: function(fileName) {
bshe 2014/10/27 20:14:51 nit: perhaps rename it to mockTestFile to be more
Ran 2014/10/27 22:22:01 Done.
80 if (fileName[0] == '/')
81 fileName = fileName.substr(1);
82 if (fileName.split('/').length == 1)
83 this.root.rootFileList.push(new FileEntry(fileName));
84 else if (fileName.split('/').length == 2) {
85 var realDirName = fileName.split('/')[0];
86 var realFileName = fileName.split('/')[1];
87 var getDirSuccess = function(dirEntry) {
88 dirEntry.getFile(realFileName, {create: true}, function(){} );
89 };
90 this.root.getDirectory(realDirName, {create: true}, getDirSuccess);
91 }
92 else {
93 console.error('Only support one-level fileSystem mock')
94 }
95 },
96 reset: function() {
97 this.root.dirList = [];
98 this.root.rootFileList = [];
99 }
100 };
101
102 function DirEntry(dirname) {
103 this.name = dirname;
104 this.fileList = [];
105 this.getFile = function(fileName, isCreate, success, failure) {
106 for(var i = 0; i < this.fileList.length; i++) {
107 if (fileName == this.fileList[i].name) {
108 success(this.fileList[i]);
109 return;
110 }
111 }
112 if (isCreate.create == false) {
113 if (failure) {
114 failure('FILE_NOT_FOUND');
115 }
116 }
117 else {
118 this.fileList.push( new FileEntry(fileName) );
119 success(this.fileList[this.fileList.length - 1]);
120 }
121 }
122 }
123
124 window.webkitRequestFileSystem = function(type, size, callback) {
125 callback(mockLocalFS);
126 }
127
128 var mockWriter = {
129 write: function(blobData) {
130 }
131 };
132
133 function FileEntry(filename) {
134 this.name = filename;
135 this.file = function(callback) {
136 callback(TestConstants.fileString);
137 };
138 this.createWriter = function(callback) {
139 callback(mockWriter);
140 };
141 this.remove = function(success, failure) {
142 };
143 }
144
145 // Mock chrome syncFS handler
146 var mockSyncFS = {
147 root: {
148 fileList: [],
149 getFile: function(fileName, isCreate, success, failure) {
150 for(var i = 0; i < this.fileList.length; i++) {
151 if (fileName == this.fileList[i].name) {
152 success(this.fileList[i]);
153 return;
154 }
155 }
156 if (isCreate.create == false) {
157 if (failure)
158 failure('FILE_NOT_FOUND');
159 }
160 else {
161 this.fileList.push(new FileEntry(fileName));
162 success(this.fileList[this.fileList.length - 1]);
163 }
164 },
165 },
166 addFile: function(fileName) {
167 this.root.fileList.push(new FileEntry(fileName));
168 },
169 reset: function() {
170 this.root.fileList = [];
171 }
10 }; 172 };
11 173
12 // Mock a few chrome apis. 174 // Mock a few chrome apis.
13 var chrome = { 175 var chrome = {
14 storage: { 176 storage: {
15 local: { 177 local: {
16 get: function(key, callback) { 178 get: function(key, callback) {
17 var items = {}; 179 var items = {};
18 switch (key) { 180 switch (key) {
19 case Constants.AccessLocalWallpaperInfoKey: 181 case Constants.AccessLocalWallpaperInfoKey:
20 items[Constants.AccessLocalWallpaperInfoKey] = { 182 items[Constants.AccessLocalWallpaperInfoKey] = {
21 'url': 'dummy', 183 'url': 'dummy',
22 'layout': 'dummy', 184 'layout': 'dummy',
23 'source': 'dummy' 185 'source': Constants.WallpaperSourceEnum.Custom
24 }; 186 };
25 } 187 }
26 callback(items); 188 callback(items);
27 }, 189 },
28 set: function(items, callback) { 190 set: function(items, callback) {
29 } 191 }
30 }, 192 },
31 sync: { 193 sync: {
32 get: function(key, callback) { 194 get: function(key, callback) {
33 }, 195 },
34 set: function(items, callback) { 196 set: function(items, callback) {
35 } 197 }
36 }, 198 },
37 onChanged: { 199 onChanged: {
38 addListener: function(listener) { 200 addListener: function(listener) {
39 this.dispatch = listener; 201 this.dispatch = listener;
40 } 202 }
41 } 203 }
42 }, 204 },
43 syncFileSystem: { 205 syncFileSystem: {
44 requestFileSystem: function(fs) { 206 requestFileSystem: function(callback) {
207 callback(mockSyncFS);
45 }, 208 },
46 onFileStatusChanged: { 209 onFileStatusChanged: {
47 addListener: function(listener) { 210 addListener: function(listener) {
48 this.dispatch = listener; 211 this.dispatch = listener;
49 } 212 }
50 } 213 }
51 }, 214 },
52 app: { 215 app: {
53 runtime: { 216 runtime: {
54 onLaunched: { 217 onLaunched: {
55 addListener: function(listener) { 218 addListener: function(listener) {
56 } 219 }
57 } 220 }
58 } 221 }
59 }, 222 },
60 alarms: { 223 alarms: {
61 onAlarm: { 224 onAlarm: {
62 addListener: function(listener) { 225 addListener: function(listener) {
63 } 226 }
64 } 227 }
65 }, 228 },
66 wallpaperPrivate: { 229 wallpaperPrivate: {
67 getStrings: function(callback) { 230 getStrings: function(callback) {
68 callback({isExperimental: false}); 231 callback({isExperimental: false});
232 },
233 setCustomWallpaper: function(data, layout, isGenerateThumbnail, fileName,
234 callback) {
69 } 235 }
236 },
237 runtime: {
238 lastError: null
70 } 239 }
71 }; 240 };
72 241
73 (function (exports) { 242 (function (exports) {
74 var originalXMLHttpRequest = window.XMLHttpRequest; 243 var originalXMLHttpRequest = window.XMLHttpRequest;
75 244
76 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status 245 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status
77 // equals to 200 in function |send|. 246 // equals to 200 in function |send|.
78 function MockXMLHttpRequest() { 247 function MockXMLHttpRequest() {
79 } 248 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 }; 295 };
127 296
128 function uninstallMockXMLHttpRequest() { 297 function uninstallMockXMLHttpRequest() {
129 window['XMLHttpRequest'] = originalXMLHttpRequest; 298 window['XMLHttpRequest'] = originalXMLHttpRequest;
130 }; 299 };
131 300
132 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; 301 exports.installMockXMLHttpRequest = installMockXMLHttpRequest;
133 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; 302 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest;
134 303
135 })(window); 304 })(window);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698