summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/folder-display/browser_folderPaneVisibility.js
blob: 0c74031457d18333f9f7bb7e2d616a345881897d (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* 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/. */

/*
 * Test that the folder pane collapses properly, stays collapsed amongst tab
 * changes, and that persistence works (to a first approximation).
 */

"use strict";

var {
  be_in_folder,
  close_tab,
  create_folder,
  get_about_3pane,
  make_message_sets_in_folders,
  mc,
  open_folder_in_new_tab,
  open_selected_message_in_new_tab,
  select_click_row,
  switch_tab,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

var folder;

add_setup(async function () {
  folder = await create_folder("FolderPaneVisibility");
  await make_message_sets_in_folders([folder], [{ count: 3 }]);
});

/**
 * When displaying a folder, assert that the folder pane is visible and all the
 * menus, splitters, etc. are set up right.
 */
function assert_folder_pane_visible() {
  let win = get_about_3pane();

  Assert.equal(
    win.paneLayout.folderPaneVisible,
    true,
    "The tab does not think that the folder pane is visible, but it should!"
  );
  Assert.ok(
    BrowserTestUtils.is_visible(
      win.window.document.getElementById("folderTree")
    ),
    "The folder tree should not be collapsed!"
  );
  Assert.equal(
    win.folderPaneSplitter.isCollapsed,
    false,
    "The folder tree splitter should not be collapsed!"
  );

  mc.window.view_init(); // Force the view menu to update.
  let paneMenuItem = mc.window.document.getElementById("menu_showFolderPane");
  Assert.equal(
    paneMenuItem.getAttribute("checked"),
    "true",
    "The Folder Pane menu item should be checked."
  );
}

/**
 * When displaying a folder, assert that the folder pane is hidden and all the
 * menus, splitters, etc. are set up right.
 */
function assert_folder_pane_hidden() {
  let win = get_about_3pane();

  Assert.equal(
    win.paneLayout.folderPaneVisible,
    false,
    "The tab thinks that the folder pane is visible, but it shouldn't!"
  );
  Assert.ok(
    BrowserTestUtils.is_hidden(
      win.window.document.getElementById("folderTree")
    ),
    "The folder tree should be collapsed!"
  );
  Assert.equal(
    win.folderPaneSplitter.isCollapsed,
    true,
    "The folder tree splitter should be collapsed!"
  );

  mc.window.view_init(); // Force the view menu to update.
  let paneMenuItem = mc.window.document.getElementById("menu_showFolderPane");
  Assert.notEqual(
    paneMenuItem.getAttribute("checked"),
    "true",
    "The Folder Pane menu item should not be checked."
  );
}

function toggle_folder_pane() {
  // Since we don't have a shortcut to toggle the folder pane, we're going to
  // have to collapse it ourselves
  get_about_3pane().commandController.doCommand("cmd_toggleFolderPane");
}

/**
 * By default, the folder pane should be visible.
 */
add_task(async function test_folder_pane_visible_state_is_right() {
  await be_in_folder(folder);
  assert_folder_pane_visible();
});

/**
 * Toggle the folder pane off.
 */
add_task(function test_toggle_folder_pane_off() {
  toggle_folder_pane();
  assert_folder_pane_hidden();
});

/**
 * Toggle the folder pane on.
 */
add_task(function test_toggle_folder_pane_on() {
  toggle_folder_pane();
  assert_folder_pane_visible();
});

/**
 * Make sure that switching to message tabs of folder tabs with a different
 * folder pane state does not break. This test should cover all transition
 * states.
 */
add_task(async function test_folder_pane_is_sticky() {
  Assert.equal(document.getElementById("tabmail").tabInfo.length, 1);
  let tabFolderA = await be_in_folder(folder);
  assert_folder_pane_visible();

  // [folder+ => (new) message]
  select_click_row(0);
  let tabMessage = await open_selected_message_in_new_tab();

  // [message => folder+]
  await switch_tab(tabFolderA);
  assert_folder_pane_visible();

  // [folder+ => (new) folder+]
  let tabFolderB = await open_folder_in_new_tab(folder);
  assert_folder_pane_visible();

  // [folder pane toggle + => -]
  toggle_folder_pane();
  assert_folder_pane_hidden();

  // [folder- => folder+]
  await switch_tab(tabFolderA);
  assert_folder_pane_visible();

  // (redundant) [ folder pane toggle + => -]
  toggle_folder_pane();
  assert_folder_pane_hidden();

  // [folder- => message]
  await switch_tab(tabMessage);

  // [message => folder-]
  close_tab(tabMessage);
  assert_folder_pane_hidden();

  // the tab we are on now doesn't matter, so we don't care
  assert_folder_pane_hidden();
  await switch_tab(tabFolderB);

  // [ folder pane toggle - => + ]
  toggle_folder_pane();
  assert_folder_pane_visible();

  // [folder+ => folder-]
  close_tab(tabFolderB);
  assert_folder_pane_hidden();

  // (redundant) [ folder pane toggle - => + ]
  toggle_folder_pane();
  assert_folder_pane_visible();
});

/**
 * Test that if we serialize and restore the tabs then the folder pane is in the
 * expected collapsed/non-collapsed state. Because of the special "first tab"
 * situation, we need to do this twice to test each case for the first tab.  For
 * additional thoroughness we also flip the state we have the other tabs be in.
 */
add_task(async function test_folder_pane_persistence_generally_works() {
  await be_in_folder(folder);

  let tabmail = mc.window.document.getElementById("tabmail");

  // helper to open tabs with the folder pane in the desired states (1 for
  //  visible, 0 for hidden)
  async function openTabs(aConfig) {
    for (let [iTab, folderPaneVisible] of aConfig.entries()) {
      if (iTab != 0) {
        await open_folder_in_new_tab(folder);
      }
      if (
        tabmail.currentAbout3Pane.paneLayout.folderPaneVisible !=
        folderPaneVisible
      ) {
        toggle_folder_pane();
      }
    }
  }

  // close everything but the first tab.
  function closeTabs() {
    while (tabmail.tabInfo.length > 1) {
      tabmail.closeTab(1);
    }
  }

  async function verifyTabs(aConfig) {
    for (let [iTab, folderPaneVisible] of aConfig.entries()) {
      info("tab " + iTab);

      await switch_tab(iTab);
      if (tabmail.currentAbout3Pane.document.readyState != "complete") {
        await BrowserTestUtils.waitForEvent(tabmail.currentAbout3Pane, "load");
        await new Promise(resolve =>
          tabmail.currentAbout3Pane.setTimeout(resolve)
        );
      }

      if (folderPaneVisible) {
        assert_folder_pane_visible();
      } else {
        assert_folder_pane_hidden();
      }
    }
  }

  let configs = [
    // 1st time: [+ - - + +]
    [1, 0, 0, 1, 1],
    // 2nd time: [- + + - -]
    [0, 1, 1, 0, 0],
  ];

  for (let config of configs) {
    await openTabs(config);
    await verifyTabs(config); // make sure openTabs did its job right
    let state = tabmail.persistTabs();
    closeTabs();

    Assert.equal(state.tabs[0].state.folderPaneVisible, config[0]);
    Assert.equal(state.tabs[1].state.folderPaneVisible, config[1]);
    Assert.equal(state.tabs[2].state.folderPaneVisible, config[2]);
    Assert.equal(state.tabs[3].state.folderPaneVisible, config[3]);
    Assert.equal(state.tabs[4].state.folderPaneVisible, config[4]);

    // toggle the state for the current tab so we can be sure that it knows how
    // to change things.
    toggle_folder_pane();

    tabmail.restoreTabs(state);
    await verifyTabs(config);
    closeTabs();

    // toggle the first tab again.  This sets closed properly for the second pass and
    // restores it to open for when we are done.
    toggle_folder_pane();
  }
  // For one last time, make sure.
  assert_folder_pane_visible();
});