1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * TestFixture for EditDictionaryOverlay WebUI testing.
7 * @extends {testing.Test}
10 function EditDictionaryWebUITest() {}
12 EditDictionaryWebUITest
.prototype = {
13 __proto__
: testing
.Test
.prototype,
16 * Browse to the edit dictionary page & call our preLoad().
18 browsePreload
: 'chrome://settings-frame/editDictionary',
21 * Register a mock dictionary handler.
24 this.makeAndRegisterMockHandler(
25 ['refreshDictionaryWords',
27 'removeDictionaryWord',
29 this.mockHandler
.stubs().refreshDictionaryWords().
30 will(callFunction(function() {
31 EditDictionaryOverlay
.setWordList([]);
33 this.mockHandler
.stubs().addDictionaryWord(ANYTHING
);
34 this.mockHandler
.stubs().removeDictionaryWord(ANYTHING
);
38 // Verify that users can add and remove words in the dictionary.
39 TEST_F('EditDictionaryWebUITest', 'testAddRemoveWords', function() {
41 $('language-dictionary-overlay-word-list').querySelector('input').value
=
44 this.mockHandler
.expects(once()).addDictionaryWord([testWord
]).
45 will(callFunction(function() {
46 EditDictionaryOverlay
.setWordList([testWord
]);
48 var addWordItem
= EditDictionaryOverlay
.getWordListForTesting().items
[0];
49 addWordItem
.onEditCommitted_({currentTarget
: addWordItem
});
51 this.mockHandler
.expects(once()).removeDictionaryWord([testWord
]).
52 will(callFunction(function() {
53 EditDictionaryOverlay
.setWordList([]);
55 EditDictionaryOverlay
.getWordListForTesting().deleteItemAtIndex(0);
58 // Verify that users can search words in the dictionary.
59 TEST_F('EditDictionaryWebUITest', 'testSearch', function() {
60 EditDictionaryOverlay
.setWordList(['foo', 'bar']);
61 expectEquals(3, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
64 * @param {Element} el The element to dispatch an event on.
65 * @param {string} value The text of the search event.
67 var fakeSearchEvent = function(el
, value
) {
69 cr
.dispatchSimpleEvent(el
, 'search');
71 var searchField
= $('language-dictionary-overlay-search-field');
72 fakeSearchEvent(searchField
, 'foo');
73 expectEquals(2, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
75 fakeSearchEvent(searchField
, '');
76 expectEquals(3, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
79 TEST_F('EditDictionaryWebUITest', 'testNoCloseOnSearchEnter', function() {
80 var editDictionaryPage
= EditDictionaryOverlay
.getInstance();
81 assertTrue(editDictionaryPage
.visible
);
82 var searchField
= $('language-dictionary-overlay-search-field');
83 searchField
.dispatchEvent(new KeyboardEvent('keydown', {
86 'keyIdentifier': 'Enter'
88 assertTrue(editDictionaryPage
.visible
);
91 // Verify that dictionary shows newly added words that arrived in a
92 // notification, but ignores duplicate add notifications.
93 TEST_F('EditDictionaryWebUITest', 'testAddNotification', function() {
94 // Begin with an empty dictionary.
95 EditDictionaryOverlay
.setWordList([]);
96 expectEquals(1, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
98 // User adds word 'foo'.
99 EditDictionaryOverlay
.getWordListForTesting().addDictionaryWord_('foo');
100 expectEquals(2, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
102 // Backend notifies UI that the word 'foo' has been added. UI ignores this
103 // notification, because the word is displayed immediately after user added
105 EditDictionaryOverlay
.updateWords(['foo'], []);
106 expectEquals(2, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
108 // Backend notifies UI that the words 'bar' and 'baz' were added. UI shows
110 EditDictionaryOverlay
.updateWords(['bar', 'baz'], []);
111 expectEquals(4, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
114 // Verify that dictionary hides newly removed words that arrived in a
115 // notification, but ignores duplicate remove notifications.
116 TEST_F('EditDictionaryWebUITest', 'testRemoveNotification', function() {
117 // Begin with a dictionary with words 'foo', 'bar', 'baz', and 'baz'. The
118 // second instance of 'baz' appears because the user added the word twice.
119 // The backend keeps only one copy of the word.
120 EditDictionaryOverlay
.setWordList(['foo', 'bar', 'baz', 'baz']);
121 expectEquals(5, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
123 // User deletes the second instance of 'baz'.
124 EditDictionaryOverlay
.getWordListForTesting().deleteItemAtIndex(3);
125 expectEquals(4, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
127 // Backend notifies UI that the word 'baz' has been removed. UI ignores this
129 EditDictionaryOverlay
.updateWords([], ['baz']);
130 expectEquals(4, EditDictionaryOverlay
.getWordListForTesting().items
.length
);
132 // Backend notifies UI that words 'foo' and 'bar' have been removed. UI
133 // removes these words.
134 EditDictionaryOverlay
.updateWords([], ['foo', 'bar']);
135 expectEquals(2, EditDictionaryOverlay
.getWordListForTesting().items
.length
);