Commit 41aa8842 authored by ranj's avatar ranj Committed by Commit bot

Add tests for sync custom wallpaper feature

BUG=421864

Review URL: https://codereview.chromium.org/642943002

Cr-Commit-Position: refs/heads/master@{#302659}
parent 46e4793e
...@@ -54,6 +54,7 @@ WallpaperUtil.storeWallpaperFromSyncFSToLocalFS = function(wallpaperFileEntry) { ...@@ -54,6 +54,7 @@ WallpaperUtil.storeWallpaperFromSyncFSToLocalFS = function(wallpaperFileEntry) {
var storeDir = Constants.WallpaperDirNameEnum.ORIGINAL; var storeDir = Constants.WallpaperDirNameEnum.ORIGINAL;
if (filenName.indexOf(Constants.CustomWallpaperThumbnailSuffix) != -1) if (filenName.indexOf(Constants.CustomWallpaperThumbnailSuffix) != -1)
storeDir = Constants.WallpaperDirNameEnum.THUMBNAIL; storeDir = Constants.WallpaperDirNameEnum.THUMBNAIL;
filenName = filenName.replace(Constants.CustomWallpaperThumbnailSuffix, '');
wallpaperFileEntry.file(function(file) { wallpaperFileEntry.file(function(file) {
var reader = new FileReader(); var reader = new FileReader();
reader.onloadend = function() { reader.onloadend = function() {
......
...@@ -6,7 +6,192 @@ ...@@ -6,7 +6,192 @@
var TestConstants = { var TestConstants = {
wallpaperURL: 'https://test.com/test.jpg', wallpaperURL: 'https://test.com/test.jpg',
// A dummy string which is used to mock an image. // 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]
};
// mock FileReader object in HTML5 File System
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) {
if (failure)
failure('DIR_NOT_FOUND');
} else {
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) {
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) {
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 deep subdirectory')
}
}
},
/**
* Create a new file in mockLocalFS.
* @param {string} fileName File name that to be created.
* @return {FileEntry} Handle of the new file
*/
mockTestFile: function(fileName) {
var mockFile;
if (fileName[0] == '/')
fileName = fileName.substr(1);
if (fileName.split('/').length == 1) {
mockFile = new FileEntry(fileName);
this.root.rootFileList.push(mockFile);
} 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(fe) {mockFile = fe;} );
};
this.root.getDirectory(realDirName, {create: true}, getDirSuccess);
} else {
console.error('Only support one-level fileSystem mock')
}
return mockFile;
},
/**
* Delete all files and directories in mockLocalFS.
*/
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) {
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);
}
function Blob(arg) {
var data = arg[0];
this.content = '';
if (typeof data == 'string')
this.content = data;
else
this.content = Array.prototype.join.call(data);
}
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) {
if (failure)
failure('FILE_NOT_FOUND');
} else {
this.fileList.push(new FileEntry(fileName));
success(this.fileList[this.fileList.length - 1]);
}
},
},
/**
* Create a new file in mockSyncFS.
* @param {string} fileName File name that to be created.
* @return {FileEntry} Handle of the new file
*/
mockTestFile: function(fileName) {
var mockFile = new FileEntry(fileName);
this.root.fileList.push(mockFile);
return mockFile;
},
/**
* Delete all files in mockSyncFS.
*/
reset: function() {
this.root.fileList = [];
}
}; };
// Mock a few chrome apis. // Mock a few chrome apis.
...@@ -20,7 +205,7 @@ var chrome = { ...@@ -20,7 +205,7 @@ var chrome = {
items[Constants.AccessLocalWallpaperInfoKey] = { items[Constants.AccessLocalWallpaperInfoKey] = {
'url': 'dummy', 'url': 'dummy',
'layout': 'dummy', 'layout': 'dummy',
'source': 'dummy' 'source': Constants.WallpaperSourceEnum.Custom
}; };
} }
callback(items); callback(items);
...@@ -41,7 +226,8 @@ var chrome = { ...@@ -41,7 +226,8 @@ var chrome = {
} }
}, },
syncFileSystem: { syncFileSystem: {
requestFileSystem: function(fs) { requestFileSystem: function(callback) {
callback(mockSyncFS);
}, },
onFileStatusChanged: { onFileStatusChanged: {
addListener: function(listener) { addListener: function(listener) {
...@@ -67,9 +253,15 @@ var chrome = { ...@@ -67,9 +253,15 @@ var chrome = {
getStrings: function(callback) { getStrings: function(callback) {
callback({isExperimental: false}); callback({isExperimental: false});
}, },
setCustomWallpaper: function(data, layout, isGenerateThumbnail, fileName,
callback) {
},
getSyncSetting: function(callback) { getSyncSetting: function(callback) {
callback({syncThemes: true}); callback({syncThemes: true});
} }
},
runtime: {
lastError: null
} }
}; };
...@@ -100,20 +292,18 @@ var chrome = { ...@@ -100,20 +292,18 @@ var chrome = {
var listeners = this.eventListeners && this.eventListeners[type] || []; var listeners = this.eventListeners && this.eventListeners[type] || [];
for (var i = 0; i < listeners.length; ++i) { for (var i = 0; i < listeners.length; ++i) {
if (listeners[i] == listener) { if (listeners[i] == listener)
return listeners.splice(i, 1); return listeners.splice(i, 1);
}
} }
}, },
dispatchEvent: function(type) { dispatchEvent: function(type) {
var listeners = this.eventListeners && this.eventListeners[type] || []; var listeners = this.eventListeners && this.eventListeners[type] || [];
if (/test.jpg$/g.test(this.url)) { if (/test.jpg$/g.test(this.url))
this.response = TestConstants.image; this.response = TestConstants.IMAGE;
} else { else
this.response = ''; this.response = '';
}
for (var i = 0; i < listeners.length; ++i) for (var i = 0; i < listeners.length; ++i)
listeners[i].call(this, new Event(type)); listeners[i].call(this, new Event(type));
......
...@@ -5,6 +5,14 @@ ...@@ -5,6 +5,14 @@
var mockController; var mockController;
WallpaperUtil.enabledExperimentalFeatureCallback = function(callback) {
callback();
};
WallpaperUtil.enabledSyncThemesCallback = function(callback) {
callback();
};
function setUp() { function setUp() {
mockController = new MockController(); mockController = new MockController();
installMockXMLHttpRequest(); installMockXMLHttpRequest();
...@@ -14,6 +22,63 @@ function tearDown() { ...@@ -14,6 +22,63 @@ function tearDown() {
mockController.verifyMocks(); mockController.verifyMocks();
mockController.reset(); mockController.reset();
uninstallMockXMLHttpRequest(); uninstallMockXMLHttpRequest();
mockSyncFS.reset();
mockLocalFS.reset();
}
// Test set custom wallpaper from syncFS. When local wallpaper name is different
// with the name in server, wallpaper should use the one in server.
function testSyncCustomWallpaperSet() {
var mockSetCustomWallpaper = mockController.createFunctionMock(
chrome.wallpaperPrivate, 'setCustomWallpaper');
mockSetCustomWallpaper.addExpectation(TestConstants.FILESTRING,
'dummy',
true,
'dummy');
var syncFSChanges = {};
syncFSChanges.status = 'synced';
syncFSChanges.direction = 'remote_to_local';
syncFSChanges.action = 'added';
syncFSChanges.fileEntry = mockSyncFS.mockTestFile('dummy');
chrome.syncFileSystem.onFileStatusChanged.dispatch(syncFSChanges);
}
// Test store historical custom wallpaper. When receive a historical wallpaper
// from syncFS, we store it to local.
function testSyncCustoWallpapermStore() {
var syncFSChanges = {};
syncFSChanges.status = 'synced';
syncFSChanges.direction = 'remote_to_local';
syncFSChanges.action = 'added';
syncFSChanges.fileEntry = mockSyncFS.mockTestFile('historicalwallpaper');
// TODO(ranj): support two callbacks with success and failure?
var mockWrite = mockController.createFunctionMock(mockWriter, 'write');
mockWrite.addExpectation(new Blob([new Int8Array(TestConstants.FILESTRING)]));
chrome.syncFileSystem.onFileStatusChanged.dispatch(syncFSChanges);
}
// Test delete custom wallpaper from local. When receive a syncFS delete file
// event, delete the file in localFS as well.
function testSyncCustomWallpaperDelete() {
var localOriginalPath = Constants.WallpaperDirNameEnum.ORIGINAL + '/' +
'deletewallpapername';
var localThumbnailPath = Constants.WallpaperDirNameEnum.THUMBNAIL + '/' +
'deletewallpapername';
var originalFE = mockLocalFS.mockTestFile(localOriginalPath);
var thumbnailFE = mockLocalFS.mockTestFile(localThumbnailPath);
var syncFSChanges = {};
syncFSChanges.status = 'synced';
syncFSChanges.direction = 'remote_to_local';
syncFSChanges.action = 'deleted';
syncFSChanges.fileEntry = new FileEntry('deletewallpapername');
var mockRemoveOriginal = mockController.createFunctionMock(originalFE,
'remove');
mockRemoveOriginal.addExpectation(function() {}, null);
var mockRemoveThumbnail = mockController.createFunctionMock(thumbnailFE,
'remove');
mockRemoveThumbnail.addExpectation(function() {}, null);
chrome.syncFileSystem.onFileStatusChanged.dispatch(syncFSChanges);
} }
// Test sync online wallpaper. When the synced wallpaper info is not the same as // Test sync online wallpaper. When the synced wallpaper info is not the same as
...@@ -38,7 +103,7 @@ function testSyncOnlineWallpaper() { ...@@ -38,7 +103,7 @@ function testSyncOnlineWallpaper() {
var mockSetWallpaper = mockController.createFunctionMock( var mockSetWallpaper = mockController.createFunctionMock(
chrome.wallpaperPrivate, 'setWallpaper'); chrome.wallpaperPrivate, 'setWallpaper');
mockSetWallpaper.addExpectation( mockSetWallpaper.addExpectation(
TestConstants.image, TestConstants.IMAGE,
changes[Constants.AccessSyncWallpaperInfoKey].newValue.layout, changes[Constants.AccessSyncWallpaperInfoKey].newValue.layout,
changes[Constants.AccessSyncWallpaperInfoKey].newValue.url); changes[Constants.AccessSyncWallpaperInfoKey].newValue.url);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment