Commit fa9a7584 authored by Moe Ahmadi's avatar Moe Ahmadi Committed by Commit Bot

[WebUI][NTP] browser tests for realbox

Tests cover the functionality added in:
-crrev.com/c/2194472
-crrev.com/c/2199361

Also fixes bugs and issues that came up while writing the tests:
-Restores previous selection, if any, and if no match is selected by
 default.
-Remove and Toggle buttons can be triggered by pressing 'Enter' and
 'Space'.
-Removes isTrusted check for mouse and keyboard events. This is not
 necessary and not commonly done in WebUI and makes testing complicated.

Bug: 10846603
Change-Id: Id0274fa9a420a6e097a8f63538571498fd3e1d9c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2194275
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarTibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770790}
parent f0f922f7
...@@ -15,4 +15,5 @@ import './app.js'; ...@@ -15,4 +15,5 @@ import './app.js';
export {BackgroundManager} from './background_manager.js'; export {BackgroundManager} from './background_manager.js';
export {BrowserProxy} from './browser_proxy.js'; export {BrowserProxy} from './browser_proxy.js';
export {BackgroundSelectionType} from './customize_dialog.js'; export {BackgroundSelectionType} from './customize_dialog.js';
export {NO_SUGGESTION_GROUP_ID} from './realbox_dropdown.js';
export {$$, createScrollBorders, decodeString16, hexColorToSkColor, mojoString16, skColorToRgba} from './utils.js'; export {$$, createScrollBorders, decodeString16, hexColorToSkColor, mojoString16, skColorToRgba} from './utils.js';
...@@ -122,7 +122,7 @@ ...@@ -122,7 +122,7 @@
on-focus="onInputFocus_" on-input="onInputInput_" on-focus="onInputFocus_" on-input="onInputInput_"
on-keydown="onInputKeydown_" on-keyup="onInputKeyup_" on-keydown="onInputKeydown_" on-keyup="onInputKeyup_"
on-mousedown="onInputMouseDown_" on-paste="onInputPaste_"> on-mousedown="onInputMouseDown_" on-paste="onInputPaste_">
<ntp-realbox-icon match="[[selectedMatch_]]" <ntp-realbox-icon id="icon" match="[[selectedMatch_]]"
favicon-data-url="[[selectedMatch_.faviconDataUrl]]" favicon-data-url="[[selectedMatch_.faviconDataUrl]]"
default-icon="[[realboxIcon_]]" in-searchbox> default-icon="[[realboxIcon_]]" in-searchbox>
</ntp-realbox-icon> </ntp-realbox-icon>
......
...@@ -278,6 +278,16 @@ class RealboxElement extends PolymerElement { ...@@ -278,6 +278,16 @@ class RealboxElement extends PolymerElement {
this.navigateToMatch_(0, this.lastIgnoredEnterEvent_); this.navigateToMatch_(0, this.lastIgnoredEnterEvent_);
this.lastIgnoredEnterEvent_ = null; this.lastIgnoredEnterEvent_ = null;
} }
} else if (
hasMatches && this.selectedMatchIndex_ !== -1 &&
this.selectedMatchIndex_ < this.result_.matches.length) {
// Restore the selection, if any.
this.$.matches.selectIndex(this.selectedMatchIndex_);
this.updateInput_({
text: decodeString16(this.selectedMatch_.fillIntoEdit),
inline: '',
moveCursorToEnd: true
});
} else { } else {
this.$.matches.unselect(); this.$.matches.unselect();
this.updateInput_({ this.updateInput_({
...@@ -418,9 +428,8 @@ class RealboxElement extends PolymerElement { ...@@ -418,9 +428,8 @@ class RealboxElement extends PolymerElement {
* @private * @private
*/ */
onInputMouseDown_(e) { onInputMouseDown_(e) {
if (!e.isTrusted || e.button !== 0) { if (e.button !== 0) {
// Only handle main (generally left) button presses generated by a user // Only handle main (generally left) button presses.
// action.
return; return;
} }
if (!this.$.input.value) { if (!this.$.input.value) {
...@@ -480,6 +489,11 @@ class RealboxElement extends PolymerElement { ...@@ -480,6 +489,11 @@ class RealboxElement extends PolymerElement {
return; return;
} }
if (e.defaultPrevented) {
// Ignore previousely handled events.
return;
}
// ArrowUp/ArrowDown query autocomplete when matches are not visible. // ArrowUp/ArrowDown query autocomplete when matches are not visible.
if (!this.matchesAreVisible) { if (!this.matchesAreVisible) {
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
...@@ -634,11 +648,12 @@ class RealboxElement extends PolymerElement { ...@@ -634,11 +648,12 @@ class RealboxElement extends PolymerElement {
} }
/** /**
* Clears the autocomplete results on * Clears the autocomplete result on the page and on the autocomplete backend.
* @private * @private
*/ */
clearAutocompleteMatches_() { clearAutocompleteMatches_() {
this.result_ = null; this.result_ = null;
this.$.matches.unselect();
this.pageHandler_.stopAutocomplete(/*clearResult=*/ true); this.pageHandler_.stopAutocomplete(/*clearResult=*/ true);
// Autocomplete sends updates once it is stopped. Invalidate those results // Autocomplete sends updates once it is stopped. Invalidate those results
// by setting the |this.lastQueriedInput_| to its default value. // by setting the |this.lastQueriedInput_| to its default value.
......
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
group-is-hidden$="[[groupIsHidden_(hiddenGroupIds_.*, groupId)]]"> group-is-hidden$="[[groupIsHidden_(hiddenGroupIds_.*, groupId)]]">
[[headerForGroup_(groupId)]] [[headerForGroup_(groupId)]]
<ntp-realbox-button data-id$="[[groupId]]" <ntp-realbox-button data-id$="[[groupId]]"
on-click="onToggleButtonClick_"> on-click="onToggleButtonClick_" on-keydown="onToggleButtonKeydown_">
</ntp-realbox-button> </ntp-realbox-button>
</div> </div>
</template> </template>
......
...@@ -257,6 +257,20 @@ class RealboxDropdownElement extends PolymerElement { ...@@ -257,6 +257,20 @@ class RealboxDropdownElement extends PolymerElement {
} }
} }
/**
* @param {!Event} e
* @private
*/
onToggleButtonKeydown_(e) {
if (e.key !== 'Enter' && e.key !== ' ') {
return;
}
// Simulate a click so that it gets handled by |onToggleButtonClick_|.
e.target.click();
e.preventDefault(); // Prevents default browser action.
}
//============================================================================ //============================================================================
// Helpers // Helpers
//============================================================================ //============================================================================
......
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
</style> </style>
<a id="link" href$="[[match.destinationUrl.url]]"> <a id="link" href$="[[match.destinationUrl.url]]">
<ntp-realbox-icon match="[[match]]" <ntp-realbox-icon id="icon" match="[[match]]"
favicon-data-url="[[faviconDataUrl]]" image-data-url="[[imageDataUrl]]"> favicon-data-url="[[faviconDataUrl]]" image-data-url="[[imageDataUrl]]">
</ntp-realbox-icon> </ntp-realbox-icon>
<div id="container"> <div id="container">
...@@ -92,6 +92,7 @@ ...@@ -92,6 +92,7 @@
<span id="description" inner-h-t-m-l="[[descriptionHtml_]]"></span> <span id="description" inner-h-t-m-l="[[descriptionHtml_]]"></span>
</div> </div>
<ntp-realbox-button id="remove" on-click="onRemoveButtonClick_" <ntp-realbox-button id="remove" on-click="onRemoveButtonClick_"
on-keydown="onRemoveButtonKeydown_"
title$="[[removeButtonTitle_]]" hidden$="[[!removeButtonIsVisible_]]"> title$="[[removeButtonTitle_]]" hidden$="[[!removeButtonIsVisible_]]">
</ntp-realbox-button> </ntp-realbox-button>
</a> </a>
...@@ -260,10 +260,8 @@ class RealboxMatchElement extends PolymerElement { ...@@ -260,10 +260,8 @@ class RealboxMatchElement extends PolymerElement {
* @private * @private
*/ */
onMatchClick_(e) { onMatchClick_(e) {
if (!e.isTrusted || e.defaultPrevented || e.button > 1) { if (e.button > 1) {
// Only handle main (generally left) and middle button presses generated // Only handle main (generally left) and middle button presses.
// by a user action. Ignore previously handled events (i.e. remove
// button).
return; return;
} }
...@@ -294,9 +292,8 @@ class RealboxMatchElement extends PolymerElement { ...@@ -294,9 +292,8 @@ class RealboxMatchElement extends PolymerElement {
* @private * @private
*/ */
onRemoveButtonClick_(e) { onRemoveButtonClick_(e) {
if (!e.isTrusted || e.button !== 0) { if (e.button !== 0) {
// Only handle main (generally left) button presses generated by a user // Only handle main (generally left) button presses.
// action.
return; return;
} }
this.dispatchEvent(new CustomEvent('match-remove', { this.dispatchEvent(new CustomEvent('match-remove', {
...@@ -306,6 +303,21 @@ class RealboxMatchElement extends PolymerElement { ...@@ -306,6 +303,21 @@ class RealboxMatchElement extends PolymerElement {
})); }));
e.preventDefault(); // Prevents default browser action (navigation). e.preventDefault(); // Prevents default browser action (navigation).
e.stopPropagation(); // Prevents <iron-selector> from selecting the match.
}
/**
* @param {!Event} e
* @private
*/
onRemoveButtonKeydown_(e) {
if (e.key !== 'Enter' && e.key !== ' ') {
return;
}
// Simulate a click so that it gets handled by |onRemoveButtonClick_|.
e.target.click();
e.preventDefault(); // Prevents default browser action.
} }
//============================================================================ //============================================================================
......
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