Commit 6fb93242 authored by satorux@chromium.org's avatar satorux@chromium.org

file_manager: Make test.util.fakeKeyDown more flexible

Previously, the target element (the file list) was hard coded.
Instead, we should be able to specify the target element.

BUG=none
TEST=none; just a test util change. the existing tests should pass as before

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194735 0039d316-1c4b-4281-b951-d872f2087c98
parent ab56e7fc
...@@ -1144,6 +1144,7 @@ DialogType.isModal = function(type) { ...@@ -1144,6 +1144,7 @@ DialogType.isModal = function(type) {
// on ChromeOS. // on ChromeOS.
list.setAttribute('role', 'listbox'); list.setAttribute('role', 'listbox');
list.addEventListener('click', this.onDetailClick_.bind(this)); list.addEventListener('click', this.onDetailClick_.bind(this));
list.id = 'file-list';
}; };
/** /**
......
...@@ -191,7 +191,7 @@ test.util.selectFile = function(contentWindow, filename) { ...@@ -191,7 +191,7 @@ test.util.selectFile = function(contentWindow, filename) {
var table = contentWindow.document.querySelector('#detail-table'); var table = contentWindow.document.querySelector('#detail-table');
var rows = table.querySelectorAll('li'); var rows = table.querySelectorAll('li');
for (var index = 0; index < rows.length; ++index) { for (var index = 0; index < rows.length; ++index) {
test.util.fakeKeyDown(contentWindow, 'Down', false); test.util.fakeKeyDown(contentWindow, '#file-list', 'Down', false);
var selection = test.util.getSelectedFiles(contentWindow); var selection = test.util.getSelectedFiles(contentWindow);
if (selection.length === 1 && selection[0] === filename) if (selection.length === 1 && selection[0] === filename)
return true; return true;
...@@ -201,19 +201,40 @@ test.util.selectFile = function(contentWindow, filename) { ...@@ -201,19 +201,40 @@ test.util.selectFile = function(contentWindow, filename) {
}; };
/** /**
* Sends a fake key event with the given |keyIdentifier| and optional |ctrl| * Sends an event to the element specified by |targetQuery|.
* modifier to the file manager.
* *
* @param {Window} contentWindow Window to be tested. * @param {Window} contentWindow Window to be tested.
* @param {string} targetQuery Query to specify the element.
* @param {Event} event Event to be sent.
* @return {boolean} True if the event is sent to the target, false otherwise.
*/
test.util.sendEvent = function(contentWindow, targetQuery, event) {
var target = contentWindow.document.querySelector(targetQuery);
if (target) {
target.dispatchEvent(event);
return true;
} else {
console.error('Target element for ' + targetQuery + ' not found.');
return false;
}
};
/**
* Sends a fake key event to the element specified by |targetQuery| with the
* given |keyIdentifier| and optional |ctrl| modifier to the file manager.
*
* @param {Window} contentWindow Window to be tested.
* @param {string} targetQuery Query to specify the element.
* @param {string} keyIdentifier Identifier of the emulated key. * @param {string} keyIdentifier Identifier of the emulated key.
* @param {boolean} ctrl Whether CTRL should be pressed, or not. * @param {boolean} ctrl Whether CTRL should be pressed, or not.
* @return {boolean} True if the event is sent to the target, false otherwise.
*/ */
test.util.fakeKeyDown = function(contentWindow, keyIdentifier, ctrl) { test.util.fakeKeyDown = function(
contentWindow, targetQuery, keyIdentifier, ctrl) {
var event = new KeyboardEvent( var event = new KeyboardEvent(
'keydown', 'keydown',
{ bubbles: true, keyIdentifier: keyIdentifier, ctrlKey: ctrl }); { bubbles: true, keyIdentifier: keyIdentifier, ctrlKey: ctrl });
var detailTable = contentWindow.document.querySelector('#detail-table'); return test.util.sendEvent(contentWindow, targetQuery, event);
detailTable.querySelector('list').dispatchEvent(event);
}; };
/** /**
...@@ -221,18 +242,11 @@ test.util.fakeKeyDown = function(contentWindow, keyIdentifier, ctrl) { ...@@ -221,18 +242,11 @@ test.util.fakeKeyDown = function(contentWindow, keyIdentifier, ctrl) {
* *
* @param {Window} contentWindow Window to be tested. * @param {Window} contentWindow Window to be tested.
* @param {string} targetQuery Query to specify the element. * @param {string} targetQuery Query to specify the element.
* @return {boolean} True if file got selected, false otherwise. * @return {boolean} True if the event is sent to the target, false otherwise.
*/ */
test.util.fakeMouseClick = function(contentWindow, targetQuery) { test.util.fakeMouseClick = function(contentWindow, targetQuery) {
var event = new MouseEvent('click', { bubbles: true }); var event = new MouseEvent('click', { bubbles: true });
var target = contentWindow.document.querySelector(targetQuery); return test.util.sendEvent(contentWindow, targetQuery, event);
if (target) {
target.dispatchEvent(event);
return true;
} else {
console.error('Target element for ' + targetQuery + ' not found.');
return false;
}
}; };
/** /**
...@@ -246,8 +260,9 @@ test.util.fakeMouseClick = function(contentWindow, targetQuery) { ...@@ -246,8 +260,9 @@ test.util.fakeMouseClick = function(contentWindow, targetQuery) {
test.util.copyFile = function(contentWindow, filename) { test.util.copyFile = function(contentWindow, filename) {
if (!test.util.selectFile(contentWindow, filename)) if (!test.util.selectFile(contentWindow, filename))
return false; return false;
test.util.fakeKeyDown(contentWindow, 'U+0043', true); // Ctrl+C // Ctrl+C and Ctrl+V
test.util.fakeKeyDown(contentWindow, 'U+0056', true); // Ctrl+V test.util.fakeKeyDown(contentWindow, '#file-list', 'U+0043', true);
test.util.fakeKeyDown(contentWindow, '#file-list', 'U+0056', true);
return true; return true;
}; };
...@@ -262,7 +277,8 @@ test.util.copyFile = function(contentWindow, filename) { ...@@ -262,7 +277,8 @@ test.util.copyFile = function(contentWindow, filename) {
test.util.deleteFile = function(contentWindow, filename) { test.util.deleteFile = function(contentWindow, filename) {
if (!test.util.selectFile(contentWindow, filename)) if (!test.util.selectFile(contentWindow, filename))
return false; return false;
test.util.fakeKeyDown(contentWindow, 'U+007F', false); // Delete // Delete
test.util.fakeKeyDown(contentWindow, '#file-list', 'U+007F', false);
return true; return true;
}; };
...@@ -318,8 +334,10 @@ test.util.registerRemoteTestUtils = function() { ...@@ -318,8 +334,10 @@ test.util.registerRemoteTestUtils = function() {
test.util.sendResponse(selectFile(contentWindow, request.args[0])); test.util.sendResponse(selectFile(contentWindow, request.args[0]));
return false; return false;
case 'fakeKeyDown': case 'fakeKeyDown':
test.util.fakeKeyDown( sendResponse(test.util.fakeKeyDown(contentWindow,
contentWindow, request.args[0], request.request[1]); request.args[0],
request.args[1],
request.args[2]));
return false; return false;
case 'fakeMouseClick': case 'fakeMouseClick':
sendResponse(test.util.fakeMouseClick( sendResponse(test.util.fakeMouseClick(
......
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