summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/quick-filter-bar/browser_keyboardInterface.js
blob: 7deced3391df164927aa9466cb249dda50a4c049 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests keyboard stuff that doesn't fall under some other test's heading.
 * Namely, control-shift-k toggling the bar into existence happens in
 * test-toggle-bar.js, but we test that repeatedly hitting control-shift-k
 * selects the text entered in the quick filter bar.
 */

"use strict";

var {
  be_in_folder,
  create_folder,
  get_about_3pane,
  make_message_sets_in_folders,
  mc,
  select_click_row,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
var {
  assert_constraints_expressed,
  assert_filter_text,
  assert_quick_filter_bar_visible,
  clear_constraints,
  set_filter_text,
  toggle_boolean_constraints,
  toggle_quick_filter_bar,
  cleanup_qfb_button,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/QuickFilterBarHelpers.jsm"
);

var folder;

add_setup(async function () {
  folder = await create_folder("QuickFilterBarKeyboardInterface");
  // We need a message so we can select it so we can find in message.
  await make_message_sets_in_folders([folder], [{ count: 1 }]);
  await be_in_folder(folder);
  await ensure_table_view();

  // Quick filter bar is hidden by default, need to toggle it on.
  await toggle_quick_filter_bar();

  registerCleanupFunction(async () => {
    await ensure_cards_view();
    await cleanup_qfb_button();
    // Quick filter bar is hidden by default, need to toggle it off.
    await toggle_quick_filter_bar();
  });
});

/**
 * The rules for pressing escape:
 * - If there are any applied constraints:
 *   - If there is a 'most recent' constraint, it is relaxed and the 'most
 *     recent' field gets cleared, so that if escape gets hit again...
 *   - If there is no 'most recent' constraint, all constraints are cleared.
 * - If there are no applied constraints, we close the filter bar.
 *
 * We test these rules two ways:
 * 1) With the focus in the thread pane.
 * 2) With our focus in our text-box.
 */
add_task(async function test_escape_rules() {
  assert_quick_filter_bar_visible(true); // (precondition)

  // the common logic for each bit...
  async function legwork() {
    // apply two...
    toggle_boolean_constraints("unread", "starred", "addrbook");
    assert_constraints_expressed({
      unread: true,
      starred: true,
      addrbook: true,
    });
    assert_quick_filter_bar_visible(true);

    // hit escape, should clear addrbook
    EventUtils.synthesizeKey("VK_ESCAPE", {});
    assert_quick_filter_bar_visible(true);
    assert_constraints_expressed({ unread: true, starred: true });

    // hit escape, should clear both remaining ones
    EventUtils.synthesizeKey("VK_ESCAPE", {});
    assert_quick_filter_bar_visible(true);
    assert_constraints_expressed({});

    // hit escape, bar should disappear
    EventUtils.synthesizeKey("VK_ESCAPE", {});
    assert_quick_filter_bar_visible(false);

    // bring the bar back for the next dude
    await toggle_quick_filter_bar();
  }

  let about3Pane = get_about_3pane();

  // 1) focus in the thread pane
  about3Pane.document.getElementById("threadTree").focus();
  await legwork();

  // 2) focus in the text box
  about3Pane.document.getElementById("qfb-qs-textbox").focus();
  await legwork();

  // 3) focus in the text box and pretend to type stuff...
  about3Pane.document.getElementById("qfb-qs-textbox").focus();
  set_filter_text("qxqxqxqx");

  // Escape should clear the text constraint but the bar should still be
  //  visible.  The trick here is that escape is clearing the text widget
  //  and is not falling through to the cmd_popQuickFilterBarStack case so we
  //  end up with a situation where the _lastFilterAttr is the textbox but the
  //  textbox does not actually have any active filter.
  EventUtils.synthesizeKey("VK_ESCAPE", {});
  assert_quick_filter_bar_visible(true);
  assert_constraints_expressed({});
  assert_filter_text("");

  // Next escape should close the box
  EventUtils.synthesizeKey("VK_ESCAPE", {});
  assert_quick_filter_bar_visible(false);
  teardownTest();
});

/**
 * Control-shift-k expands the quick filter bar when it's collapsed. When
 * already expanded, it focuses the text box and selects its text.
 */
add_task(function test_control_shift_k_shows_quick_filter_bar() {
  let about3Pane = get_about_3pane();

  let dispatcha = mc.window.document.commandDispatcher;
  let qfbTextbox = about3Pane.document.getElementById("qfb-qs-textbox");

  // focus explicitly on the thread pane so we know where the focus is.
  about3Pane.document.getElementById("threadTree").focus();
  // select a message so we can find in message
  select_click_row(0);

  // hit control-shift-k to get in the quick filter box
  EventUtils.synthesizeKey("k", { accelKey: true, shiftKey: true });
  if (dispatcha.focusedElement != qfbTextbox.inputField) {
    throw new Error("control-shift-k did not focus quick filter textbox");
  }

  set_filter_text("search string");

  // hit control-shift-k to select the text in the quick filter box
  EventUtils.synthesizeKey("k", { accelKey: true, shiftKey: true });
  if (dispatcha.focusedElement != qfbTextbox.inputField) {
    throw new Error(
      "second control-shift-k did not keep focus on filter textbox"
    );
  }
  if (
    qfbTextbox.inputField.selectionStart != 0 ||
    qfbTextbox.inputField.selectionEnd != qfbTextbox.inputField.textLength
  ) {
    throw new Error(
      "second control-shift-k did not select text in filter textbox"
    );
  }

  // hit escape and make sure the text is cleared, but the quick filter bar is
  // still open.
  EventUtils.synthesizeKey("VK_ESCAPE", {});
  assert_quick_filter_bar_visible(true);
  assert_filter_text("");

  // hit escape one more time and make sure we finally collapsed the quick
  // filter bar.
  EventUtils.synthesizeKey("VK_ESCAPE", {});
  assert_quick_filter_bar_visible(false);
  teardownTest();
});

function teardownTest() {
  clear_constraints();
}