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

Unified 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, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/chromeos/wallpaper_manager/unit_tests/api_mock.js
diff --git a/chrome/test/data/chromeos/wallpaper_manager/unit_tests/api_mock.js b/chrome/test/data/chromeos/wallpaper_manager/unit_tests/api_mock.js
index f7318bcd47be23af2ca696daf8efa4623672333b..629d3bc893ca280bfe114fccd9a806c1045993ac 100644
--- a/chrome/test/data/chromeos/wallpaper_manager/unit_tests/api_mock.js
+++ b/chrome/test/data/chromeos/wallpaper_manager/unit_tests/api_mock.js
@@ -6,7 +6,169 @@
var TestConstants = {
wallpaperURL: 'https://test.com/test.jpg',
// A dummy string which is used to mock an image.
- image: '*#*@#&'
+ image: '*#*@#&',
+ // A dummy array which is used to mock the file content.
+ fileString: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
+};
+
+function FileReader() {
+ this.result = '';
+ this.onloadend = function() {
+ };
+ this.readAsArrayBuffer = function(mockFile) {
+ this.result = mockFile;
+ this.onloadend();
+ }
+}
+
+// Mock localFS handler
+var mockLocalFS = {
+ root: {
+ dirList: [],
+ rootFileList: [],
+ getDirectory: function(dir, isCreate, success, failure) {
+ for(var i = 0; i < this.dirList.length; i++) {
+ if (this.dirList[i].name == dir) {
+ success(this.dirList[i]);
+ return;
+ }
+ }
+ 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.
+ if (failure) {
+ failure('DIR_NOT_FOUND');
+ }
+ }
+ 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.
+ this.dirList.push(new DirEntry(dir));
+ success(this.dirList[this.dirList.length - 1]);
+ }
+ },
+ getFile: function(fileName, isCreate, success, failure) {
+ if (fileName[0] == '/')
+ fileName = fileName.substr(1);
+ if (fileName.split('/').length == 1) {
+ for(var i = 0; i < this.rootFileList.length; i++) {
+ if (fileName == this.rootFileList[i].name) {
+ success(this.rootFileList[i]);
+ return;
+ }
+ }
+ if (isCreate.create == false) {
bshe 2014/10/27 20:14:51 nit: if (!isCreate.create)
Ran 2014/10/27 22:22:01 Done.
+ if (failure)
+ failure('FILE_NOT_FOUND');
+ }
+ else {
+ this.rootFileList.push(new FileEntry(fileName));
+ success(this.rootFileList[this.rootFileList.length - 1]);
+ }
+ }
+ 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.
+ var realDirName = fileName.split('/')[0];
+ var realFileName = fileName.split('/')[1];
+ var getDirSuccess = function(dirEntry) {
+ dirEntry.getFile(realFileName, isCreate, success, failure);
+ };
+ this.getDirectory(realDirName, {create: false},
+ getDirSuccess, failure);
+ }
+ else {
+ console.error('Only support one-level fileSystem mock')
+ }
+ }
+ },
+ 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.
+ if (fileName[0] == '/')
+ fileName = fileName.substr(1);
+ if (fileName.split('/').length == 1)
+ this.root.rootFileList.push(new FileEntry(fileName));
+ else if (fileName.split('/').length == 2) {
+ var realDirName = fileName.split('/')[0];
+ var realFileName = fileName.split('/')[1];
+ var getDirSuccess = function(dirEntry) {
+ dirEntry.getFile(realFileName, {create: true}, function(){} );
+ };
+ this.root.getDirectory(realDirName, {create: true}, getDirSuccess);
+ }
+ else {
+ console.error('Only support one-level fileSystem mock')
+ }
+ },
+ reset: function() {
+ this.root.dirList = [];
+ this.root.rootFileList = [];
+ }
+};
+
+function DirEntry(dirname) {
+ this.name = dirname;
+ this.fileList = [];
+ this.getFile = function(fileName, isCreate, success, failure) {
+ for(var i = 0; i < this.fileList.length; i++) {
+ if (fileName == this.fileList[i].name) {
+ success(this.fileList[i]);
+ return;
+ }
+ }
+ if (isCreate.create == false) {
+ if (failure) {
+ failure('FILE_NOT_FOUND');
+ }
+ }
+ else {
+ this.fileList.push( new FileEntry(fileName) );
+ success(this.fileList[this.fileList.length - 1]);
+ }
+ }
+}
+
+window.webkitRequestFileSystem = function(type, size, callback) {
+ callback(mockLocalFS);
+}
+
+var mockWriter = {
+ write: function(blobData) {
+ }
+};
+
+function FileEntry(filename) {
+ this.name = filename;
+ this.file = function(callback) {
+ callback(TestConstants.fileString);
+ };
+ this.createWriter = function(callback) {
+ callback(mockWriter);
+ };
+ this.remove = function(success, failure) {
+ };
+}
+
+// Mock chrome syncFS handler
+var mockSyncFS = {
+ root: {
+ fileList: [],
+ getFile: function(fileName, isCreate, success, failure) {
+ for(var i = 0; i < this.fileList.length; i++) {
+ if (fileName == this.fileList[i].name) {
+ success(this.fileList[i]);
+ return;
+ }
+ }
+ if (isCreate.create == false) {
+ if (failure)
+ failure('FILE_NOT_FOUND');
+ }
+ else {
+ this.fileList.push(new FileEntry(fileName));
+ success(this.fileList[this.fileList.length - 1]);
+ }
+ },
+ },
+ addFile: function(fileName) {
+ this.root.fileList.push(new FileEntry(fileName));
+ },
+ reset: function() {
+ this.root.fileList = [];
+ }
};
// Mock a few chrome apis.
@@ -20,7 +182,7 @@ var chrome = {
items[Constants.AccessLocalWallpaperInfoKey] = {
'url': 'dummy',
'layout': 'dummy',
- 'source': 'dummy'
+ 'source': Constants.WallpaperSourceEnum.Custom
};
}
callback(items);
@@ -41,7 +203,8 @@ var chrome = {
}
},
syncFileSystem: {
- requestFileSystem: function(fs) {
+ requestFileSystem: function(callback) {
+ callback(mockSyncFS);
},
onFileStatusChanged: {
addListener: function(listener) {
@@ -66,7 +229,13 @@ var chrome = {
wallpaperPrivate: {
getStrings: function(callback) {
callback({isExperimental: false});
+ },
+ setCustomWallpaper: function(data, layout, isGenerateThumbnail, fileName,
+ callback) {
}
+ },
+ runtime: {
+ lastError: null
}
};

Powered by Google App Engine
This is Rietveld 408576698