summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/options/edit_dictionary_browsertest.js
blob: 272cf16c772ddbafa6030bfe0973363e8d6680cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

GEN_INCLUDE(['options_browsertest_base.js']);

/**
 * TestFixture for EditDictionaryOverlay WebUI testing.
 * @extends {testing.Test}
 * @constructor
 */
function EditDictionaryWebUITest() {}

EditDictionaryWebUITest.prototype = {
  __proto__: OptionsBrowsertestBase.prototype,

  /**
   * Browse to the edit dictionary page & call our preLoad().
   */
  browsePreload: 'chrome://settings-frame/editDictionary',

  /**
   * Register a mock dictionary handler.
   */
  preLoad: function() {
    this.makeAndRegisterMockHandler(
        ['refreshDictionaryWords',
         'addDictionaryWord',
         'removeDictionaryWord',
        ]);
    this.mockHandler.stubs().refreshDictionaryWords().
        will(callFunction(function() {
          EditDictionaryOverlay.setWordList([]);
        }));
    this.mockHandler.stubs().addDictionaryWord(ANYTHING);
    this.mockHandler.stubs().removeDictionaryWord(ANYTHING);
  },

  /** @override */
  setUp: function() {
    OptionsBrowsertestBase.prototype.setUp.call(this);

    // Enable when failure is resolved.
    // AX_TEXT_01: http://crbug.com/570556
    this.accessibilityAuditConfig.ignoreSelectors(
        'controlsWithoutLabel',
        '#language-dictionary-overlay-word-list > .deletable-item > *');

    var unsupportedAriaAttributeSelectors = [
      '#language-dictionary-overlay-word-list',
      '#language-options-list',
    ];

    // Enable when failure is resolved.
    // AX_ARIA_10: http://crbug.com/570559
    this.accessibilityAuditConfig.ignoreSelectors(
        'unsupportedAriaAttribute',
        unsupportedAriaAttributeSelectors);
  },
};

// Verify that users can add and remove words in the dictionary.
TEST_F('EditDictionaryWebUITest', 'testAddRemoveWords', function() {
  var testWord = 'foo';
  $('language-dictionary-overlay-word-list').querySelector('input').value =
      testWord;

  this.mockHandler.expects(once()).addDictionaryWord([testWord]).
      will(callFunction(function() {
          EditDictionaryOverlay.setWordList([testWord]);
      }));
  var addWordItem = EditDictionaryOverlay.getWordListForTesting().items[0];
  addWordItem.onEditCommitted_({currentTarget: addWordItem});

  this.mockHandler.expects(once()).removeDictionaryWord([testWord]).
      will(callFunction(function() {
          EditDictionaryOverlay.setWordList([]);
      }));
  EditDictionaryOverlay.getWordListForTesting().deleteItemAtIndex(0);
});

// Verify that users can search words in the dictionary.
TEST_F('EditDictionaryWebUITest', 'testSearch', function() {
  EditDictionaryOverlay.setWordList(['foo', 'bar']);
  expectEquals(3, EditDictionaryOverlay.getWordListForTesting().items.length);

  /**
   * @param {Element} el The element to dispatch an event on.
   * @param {string} value The text of the search event.
   */
  var fakeSearchEvent = function(el, value) {
    el.value = value;
    cr.dispatchSimpleEvent(el, 'search');
  };
  var searchField = $('language-dictionary-overlay-search-field');
  fakeSearchEvent(searchField, 'foo');
  expectEquals(2, EditDictionaryOverlay.getWordListForTesting().items.length);

  fakeSearchEvent(searchField, '');
  expectEquals(3, EditDictionaryOverlay.getWordListForTesting().items.length);
});

TEST_F('EditDictionaryWebUITest', 'testNoCloseOnSearchEnter', function() {
  var editDictionaryPage = EditDictionaryOverlay.getInstance();
  assertTrue(editDictionaryPage.visible);
  var searchField = $('language-dictionary-overlay-search-field');
  searchField.dispatchEvent(new KeyboardEvent('keydown', {
    'bubbles': true,
    'cancelable': true,
    'keyIdentifier': 'Enter'
  }));
  assertTrue(editDictionaryPage.visible);
});

// Verify that dictionary shows newly added words that arrived in a
// notification, but ignores duplicate add notifications.
TEST_F('EditDictionaryWebUITest', 'testAddNotification', function() {
  // Begin with an empty dictionary.
  EditDictionaryOverlay.setWordList([]);
  expectEquals(1, EditDictionaryOverlay.getWordListForTesting().items.length);

  // User adds word 'foo'.
  EditDictionaryOverlay.getWordListForTesting().addDictionaryWord_('foo');
  expectEquals(2, EditDictionaryOverlay.getWordListForTesting().items.length);

  // Backend notifies UI that the word 'foo' has been added. UI ignores this
  // notification, because the word is displayed immediately after user added
  // it.
  EditDictionaryOverlay.updateWords(['foo'], []);
  expectEquals(2, EditDictionaryOverlay.getWordListForTesting().items.length);

  // Backend notifies UI that the words 'bar' and 'baz' were added. UI shows
  // these new words.
  EditDictionaryOverlay.updateWords(['bar', 'baz'], []);
  expectEquals(4, EditDictionaryOverlay.getWordListForTesting().items.length);
});

// Verify that dictionary hides newly removed words that arrived in a
// notification, but ignores duplicate remove notifications.
TEST_F('EditDictionaryWebUITest', 'testRemoveNotification', function() {
  // Begin with a dictionary with words 'foo', 'bar', 'baz', and 'baz'. The
  // second instance of 'baz' appears because the user added the word twice.
  // The backend keeps only one copy of the word.
  EditDictionaryOverlay.setWordList(['foo', 'bar', 'baz', 'baz']);
  expectEquals(5, EditDictionaryOverlay.getWordListForTesting().items.length);

  // User deletes the second instance of 'baz'.
  EditDictionaryOverlay.getWordListForTesting().deleteItemAtIndex(3);
  expectEquals(4, EditDictionaryOverlay.getWordListForTesting().items.length);

  // Backend notifies UI that the word 'baz' has been removed. UI ignores this
  // notification.
  EditDictionaryOverlay.updateWords([], ['baz']);
  expectEquals(4, EditDictionaryOverlay.getWordListForTesting().items.length);

  // Backend notifies UI that words 'foo' and 'bar' have been removed. UI
  // removes these words.
  EditDictionaryOverlay.updateWords([], ['foo', 'bar']);
  expectEquals(2, EditDictionaryOverlay.getWordListForTesting().items.length);
});