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.
4
5/**
6 * TestFixture for EditDictionaryOverlay WebUI testing.
7 * @extends {testing.Test}
8 * @constructor
9 */
10function EditDictionaryWebUITest() {}
11
12EditDictionaryWebUITest.prototype = {
13  __proto__: testing.Test.prototype,
14
15  /**
16   * Browse to the edit dictionary page & call our preLoad().
17   */
18  browsePreload: 'chrome://settings-frame/editDictionary',
19
20  /**
21   * Register a mock dictionary handler.
22   */
23  preLoad: function() {
24    this.makeAndRegisterMockHandler(
25        ['refreshDictionaryWords',
26         'addDictionaryWord',
27         'removeDictionaryWord',
28        ]);
29    this.mockHandler.stubs().refreshDictionaryWords().
30        will(callFunction(function() {
31          EditDictionaryOverlay.setWordList([]);
32        }));
33    this.mockHandler.stubs().addDictionaryWord(ANYTHING);
34    this.mockHandler.stubs().removeDictionaryWord(ANYTHING);
35  },
36};
37
38// Verify that users can add and remove words in the dictionary.
39TEST_F('EditDictionaryWebUITest', 'testAddRemoveWords', function() {
40  var testWord = 'foo';
41  $('language-dictionary-overlay-word-list').querySelector('input').value =
42      testWord;
43
44  this.mockHandler.expects(once()).addDictionaryWord([testWord]).
45      will(callFunction(function() {
46          EditDictionaryOverlay.setWordList([testWord]);
47      }));
48  var addWordItem = EditDictionaryOverlay.getWordListForTesting().items[0];
49  addWordItem.onEditCommitted_({currentTarget: addWordItem});
50
51  this.mockHandler.expects(once()).removeDictionaryWord([testWord]).
52      will(callFunction(function() {
53          EditDictionaryOverlay.setWordList([]);
54      }));
55  EditDictionaryOverlay.getWordListForTesting().deleteItemAtIndex(0);
56});
57
58// Verify that users can search words in the dictionary.
59TEST_F('EditDictionaryWebUITest', 'testSearch', function() {
60  EditDictionaryOverlay.setWordList(['foo', 'bar']);
61  expectEquals(3, EditDictionaryOverlay.getWordListForTesting().items.length);
62
63  /**
64   * @param {Element} el The element to dispatch an event on.
65   * @param {string} value The text of the search event.
66   */
67  var fakeSearchEvent = function(el, value) {
68    el.value = value;
69    cr.dispatchSimpleEvent(el, 'search');
70  };
71  var searchField = $('language-dictionary-overlay-search-field');
72  fakeSearchEvent(searchField, 'foo');
73  expectEquals(2, EditDictionaryOverlay.getWordListForTesting().items.length);
74
75  fakeSearchEvent(searchField, '');
76  expectEquals(3, EditDictionaryOverlay.getWordListForTesting().items.length);
77});
78
79TEST_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', {
84    'bubbles': true,
85    'cancelable': true,
86    'keyIdentifier': 'Enter'
87  }));
88  assertTrue(editDictionaryPage.visible);
89});
90
91// Verify that dictionary shows newly added words that arrived in a
92// notification, but ignores duplicate add notifications.
93TEST_F('EditDictionaryWebUITest', 'testAddNotification', function() {
94  // Begin with an empty dictionary.
95  EditDictionaryOverlay.setWordList([]);
96  expectEquals(1, EditDictionaryOverlay.getWordListForTesting().items.length);
97
98  // User adds word 'foo'.
99  EditDictionaryOverlay.getWordListForTesting().addDictionaryWord_('foo');
100  expectEquals(2, EditDictionaryOverlay.getWordListForTesting().items.length);
101
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
104  // it.
105  EditDictionaryOverlay.updateWords(['foo'], []);
106  expectEquals(2, EditDictionaryOverlay.getWordListForTesting().items.length);
107
108  // Backend notifies UI that the words 'bar' and 'baz' were added. UI shows
109  // these new words.
110  EditDictionaryOverlay.updateWords(['bar', 'baz'], []);
111  expectEquals(4, EditDictionaryOverlay.getWordListForTesting().items.length);
112});
113
114// Verify that dictionary hides newly removed words that arrived in a
115// notification, but ignores duplicate remove notifications.
116TEST_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);
122
123  // User deletes the second instance of 'baz'.
124  EditDictionaryOverlay.getWordListForTesting().deleteItemAtIndex(3);
125  expectEquals(4, EditDictionaryOverlay.getWordListForTesting().items.length);
126
127  // Backend notifies UI that the word 'baz' has been removed. UI ignores this
128  // notification.
129  EditDictionaryOverlay.updateWords([], ['baz']);
130  expectEquals(4, EditDictionaryOverlay.getWordListForTesting().items.length);
131
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);
136});
137