OLD | NEW |
---|---|
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 'use strict'; | 4 'use strict'; |
5 | 5 |
6 var mockController; | 6 var mockController; |
7 | 7 |
8 var fileString = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; | |
9 var mockWriter = {}; | |
10 mockWriter.write = function(blobData) { | |
11 }; | |
12 | |
13 var currentWallpaperName = 'currentwallpapername'; | |
bshe
2014/10/20 21:01:00
nit: all the variables are defined in global names
Ran
2014/10/23 15:58:59
Done.
| |
14 var currentWallpaperLayout = 'currentwallpaperlayout'; | |
15 var newWallpaperName = 'newwallpapername'; | |
16 var existWallpaperName = 'existwallpapername'; | |
17 var existWallpaperThumbnailNameInSync = existWallpaperName + | |
18 Constants.CustomWallpaperThumbnailSuffix; | |
19 var existOriginalWallpaperPath = Constants.WallpaperDirNameEnum.ORIGINAL + '/' + | |
20 existWallpaperName; | |
21 var existThumbnailWallpaperPath = Constants.WallpaperDirNameEnum.THUMBNAIL + | |
22 '/'+ existWallpaperName; | |
23 var mockCurrentFileEntry = new FileEntry(currentWallpaperName); | |
24 var mockNewFileEntry = new FileEntry(newWallpaperName); | |
25 var mockExistFileEntry = new FileEntry(existWallpaperName); | |
26 var mockExistFileEntryThumbnailInSync = new FileEntry( | |
27 existWallpaperThumbnailNameInSync); | |
28 var mockExistFileEntryOriginalPath = new FileEntry(existOriginalWallpaperPath); | |
29 var mockExistFileEntryThumbnailPath = new FileEntry( | |
30 existThumbnailWallpaperPath); | |
31 | |
32 var mockSyncFS = {}; | |
33 var mockLocalFS = {}; | |
34 var mockLocalFSDir = {}; | |
35 | |
36 mockSyncFS.fileList = [mockCurrentFileEntry, mockNewFileEntry, | |
37 mockExistFileEntry, mockExistFileEntryThumbnailInSync]; | |
38 mockSyncFS.root = { | |
39 getFile: function(fileName, isCreate, success, failure) { | |
40 for(var i = 0; i < mockSyncFS.fileList.length; i++) { | |
41 if (fileName == mockSyncFS.fileList[i].name) { | |
42 success(mockSyncFS.fileList[i]); | |
43 return; | |
44 } | |
45 } | |
46 if (isCreate.create == false) { | |
47 if (failure) | |
48 failure('FILE_NOT_FOUND'); | |
49 } | |
50 else { | |
51 mockSyncFS.fileList.push(new FileEntry(fileName)); | |
52 success(mockSyncFS.fileList[mockSyncFS.fileList.length - 1]); | |
53 } | |
54 } | |
55 }; | |
56 | |
57 var selectedFileEntry = null; | |
58 mockLocalFSDir.fileList = [mockCurrentFileEntry, mockExistFileEntry, | |
59 mockExistFileEntryOriginalPath, | |
60 mockExistFileEntryThumbnailPath]; | |
61 mockLocalFSDir.getFile = function(fileName, isCreate, success, failure) { | |
62 for(var i = 0; i < mockLocalFSDir.fileList.length; i++) { | |
63 if (fileName == mockLocalFSDir.fileList[i].name) { | |
64 selectedFileEntry = mockLocalFSDir.fileList[i]; | |
65 success(selectedFileEntry); | |
66 return; | |
67 } | |
68 } | |
69 if (isCreate.create == false) { | |
70 if (failure) { | |
71 failure('FILE_NOT_FOUND'); | |
72 } | |
73 } | |
74 else { | |
75 mockLocalFSDir.fileList.push( new FileEntry(fileName) ); | |
76 selectedFileEntry = mockLocalFSDir.fileList[mockLocalFSDir.fileList.length - | |
77 1]; | |
78 success(selectedFileEntry ); | |
79 } | |
80 } | |
81 | |
82 mockLocalFS.root = { | |
83 getDirectory: function(dir, isCreate, success, fail) { | |
84 success(mockLocalFSDir); | |
85 }, | |
86 getFile: function(fileName, isCreate, success, failure) { | |
87 mockLocalFSDir.getFile(fileName, isCreate, success, failure); | |
88 } | |
89 } | |
90 | |
91 window.webkitRequestFileSystem = function(dontcare1, dontcare2, callback) { | |
92 callback(mockLocalFS); | |
93 } | |
94 | |
95 chrome.syncFileSystem.requestFileSystem = function(callback) { | |
96 callback(mockSyncFS); | |
97 }; | |
98 | |
99 WallpaperUtil.enabledExperimentalFeatureCallback = function(callback) { | |
100 callback(); | |
101 }; | |
102 | |
103 chrome.storage.local.get = function(key, callback) { | |
bshe
2014/10/20 21:00:59
this overrides the globe stub implementation of ch
Ran
2014/10/23 15:58:58
Done.
| |
104 var items = {}; | |
105 switch (key) { | |
106 case Constants.AccessLocalWallpaperInfoKey: | |
107 items[Constants.AccessLocalWallpaperInfoKey] = { | |
108 'url': currentWallpaperName, | |
109 'layout': currentWallpaperLayout, | |
110 'source': Constants.WallpaperSourceEnum.Custom | |
111 }; | |
112 } | |
113 callback(items); | |
114 }; | |
115 | |
bshe
2014/10/20 21:00:59
I think FileEntry and FileReader could move to api
Ran
2014/10/23 15:58:58
Done.
| |
116 function FileEntry(filename) { | |
117 this.name = filename; | |
118 this.file = function(callback) { | |
119 callback(fileString); | |
120 }; | |
121 this.createWriter = function(callback) { | |
122 callback(mockWriter); | |
123 }; | |
124 this.remove = function(success, failure) { | |
125 }; | |
126 } | |
127 | |
128 function FileReader() { | |
129 this.result = ''; | |
130 this.onloadend = function() { | |
131 }; | |
132 this.readAsArrayBuffer = function(mockFile) { | |
133 this.result = mockFile; | |
134 this.onloadend(); | |
135 } | |
136 } | |
137 | |
8 function setUp() { | 138 function setUp() { |
9 mockController = new MockController(); | 139 mockController = new MockController(); |
10 installMockXMLHttpRequest(); | 140 installMockXMLHttpRequest(); |
11 } | 141 } |
12 | 142 |
13 function tearDown() { | 143 function tearDown() { |
14 mockController.verifyMocks(); | 144 mockController.verifyMocks(); |
15 mockController.reset(); | 145 mockController.reset(); |
16 uninstallMockXMLHttpRequest(); | 146 uninstallMockXMLHttpRequest(); |
147 selectedFileEntry = null; | |
148 } | |
149 | |
150 // Test sync Custom wallpaper. When the synced wallpaper info is not the same as | |
151 // the local wallpaper info, wallpaper should set to the synced one. | |
152 function testSyncCustomWallpaperSet() { | |
153 var mockSetCustomWallpaper = mockController.createFunctionMock( | |
154 chrome.wallpaperPrivate, 'setCustomWallpaper'); | |
155 mockSetCustomWallpaper.addExpectation(fileString, | |
156 currentWallpaperLayout, | |
157 true, | |
158 currentWallpaperName); | |
159 var syncFSChanges = {}; | |
160 syncFSChanges.status = 'synced'; | |
161 syncFSChanges.direction = 'remote_to_local'; | |
162 syncFSChanges.action = 'added'; | |
163 syncFSChanges.fileEntry = mockCurrentFileEntry; | |
164 chrome.syncFileSystem.onFileStatusChanged.dispatch(syncFSChanges); | |
165 } | |
166 | |
167 // Test sync Custom wallpaper. When the synced wallpaper info is not the same as | |
168 // the local wallpaper info, wallpaper should set to the synced one. | |
169 function testSyncCustoWallpapermStore() { | |
170 var syncFSChanges = {}; | |
171 syncFSChanges.status = 'synced'; | |
172 syncFSChanges.direction = 'remote_to_local'; | |
173 syncFSChanges.action = 'added'; | |
174 syncFSChanges.fileEntry = mockNewFileEntry; | |
175 | |
176 // TODO(ranj): support two callbacks with success and failure? | |
177 var mockWrite = mockController.createFunctionMock(mockWriter, 'write'); | |
178 mockWrite.addExpectation(new Blob([new Int8Array(fileString)])); | |
bshe
2014/10/20 21:00:59
Is it possible to move this to previous test? This
Ran
2014/10/23 15:58:59
I think it might be clear to separate them since o
| |
179 chrome.syncFileSystem.onFileStatusChanged.dispatch(syncFSChanges); | |
180 } | |
181 | |
182 // Test sync Custom wallpaper. When the synced wallpaper info is not the same as | |
183 // the local wallpaper info, wallpaper should set to the synced one. | |
bshe
2014/10/20 21:00:59
nit: update the comment.
Ran
2014/10/23 15:58:58
Done.
| |
184 function testSyncCustomWallpaperDelete() { | |
185 var syncFSChanges = {}; | |
186 syncFSChanges.status = 'synced'; | |
187 syncFSChanges.direction = 'remote_to_local'; | |
188 syncFSChanges.action = 'deleted'; | |
189 syncFSChanges.fileEntry = mockExistFileEntry; | |
190 | |
191 var mockRemoveThumbnail = mockController.createFunctionMock( | |
192 mockExistFileEntryThumbnailPath, 'remove'); | |
193 mockRemoveThumbnail.addExpectation(function() {}, null); | |
194 var mockRemoveOriginal = mockController.createFunctionMock( | |
195 mockExistFileEntryOriginalPath, 'remove'); | |
196 mockRemoveOriginal.addExpectation(function() {}, null); | |
197 chrome.syncFileSystem.onFileStatusChanged.dispatch(syncFSChanges); | |
17 } | 198 } |
18 | 199 |
19 // Test sync online wallpaper. When the synced wallpaper info is not the same as | 200 // Test sync online wallpaper. When the synced wallpaper info is not the same as |
20 // the local wallpaper info, wallpaper should set to the synced one. | 201 // the local wallpaper info, wallpaper should set to the synced one. |
21 function testSyncOnlineWallpaper() { | 202 function testSyncOnlineWallpaper() { |
22 // Setup sync value. | 203 // Setup sync value. |
23 var changes = {}; | 204 var changes = {}; |
24 changes[Constants.AccessSyncWallpaperInfoKey] = {}; | 205 changes[Constants.AccessSyncWallpaperInfoKey] = {}; |
25 changes[Constants.AccessSyncWallpaperInfoKey].newValue = { | 206 changes[Constants.AccessSyncWallpaperInfoKey].newValue = { |
26 'url': TestConstants.wallpaperURL, | 207 'url': TestConstants.wallpaperURL, |
(...skipping 10 matching lines...) Expand all Loading... | |
37 | 218 |
38 var mockSetWallpaper = mockController.createFunctionMock( | 219 var mockSetWallpaper = mockController.createFunctionMock( |
39 chrome.wallpaperPrivate, 'setWallpaper'); | 220 chrome.wallpaperPrivate, 'setWallpaper'); |
40 mockSetWallpaper.addExpectation( | 221 mockSetWallpaper.addExpectation( |
41 TestConstants.image, | 222 TestConstants.image, |
42 changes[Constants.AccessSyncWallpaperInfoKey].newValue.layout, | 223 changes[Constants.AccessSyncWallpaperInfoKey].newValue.layout, |
43 changes[Constants.AccessSyncWallpaperInfoKey].newValue.url); | 224 changes[Constants.AccessSyncWallpaperInfoKey].newValue.url); |
44 | 225 |
45 chrome.storage.onChanged.dispatch(changes); | 226 chrome.storage.onChanged.dispatch(changes); |
46 } | 227 } |
OLD | NEW |