summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/shared-modules/CustomizationHelpers.jsm
blob: 01adbb0cda206ea05cdf67c58cb16984315043cb (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
/* 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/. */

"use strict";

const EXPORTED_SYMBOLS = ["CustomizeDialogHelper"];

var wh = ChromeUtils.import(
  "resource://testing-common/mozmill/WindowHelpers.jsm"
);

var { Assert } = ChromeUtils.importESModule(
  "resource://testing-common/Assert.sys.mjs"
);
var EventUtils = ChromeUtils.import(
  "resource://testing-common/mozmill/EventUtils.jsm"
);

var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");

var USE_SHEET_PREF = "toolbar.customization.usesheet";

/**
 * Initialize the help for a customization dialog
 *
 * @param {} aToolbarId
 *   the ID of the toolbar to be customized
 * @param {} aOpenElementId
 *   the ID of the element to be clicked on to open the dialog
 * @param {} aWindowType
 *   the windowType of the window containing the dialog to be opened
 */
function CustomizeDialogHelper(aToolbarId, aOpenElementId, aWindowType) {
  this._toolbarId = aToolbarId;
  this._openElementId = aOpenElementId;
  this._windowType = aWindowType;
  this._openInWindow = !Services.prefs.getBoolPref(USE_SHEET_PREF);
}

CustomizeDialogHelper.prototype = {
  /**
   * Open a customization dialog by clicking on a given element.
   *
   * @param {} aController
   *   the controller object of the window for which the customization
   *   dialog should be opened
   * @returns a controller for the customization dialog
   */
  async open(aController) {
    aController.window.document.getElementById(this._openElementId).click();

    let ctc;
    // Depending on preferences the customization dialog is
    // either a normal window or embedded into a sheet.
    if (!this._openInWindow) {
      ctc = wh.wait_for_frame_load(
        aController.window.document.getElementById(
          "customizeToolbarSheetIFrame"
        ),
        "chrome://messenger/content/customizeToolbar.xhtml"
      );
    } else {
      ctc = wh.wait_for_existing_window(this._windowType);
    }
    utils.sleep(500);
    return ctc;
  },

  /**
   * Close the customization dialog.
   *
   * @param {} aCtc
   *   the controller object of the customization dialog which should be closed
   */
  close(aCtc) {
    if (this._openInWindow) {
      wh.plan_for_window_close(aCtc);
    }

    let doneButton = aCtc.window.document.getElementById("donebutton");
    EventUtils.synthesizeMouseAtCenter(doneButton, {}, doneButton.ownerGlobal);
    utils.sleep(0);
    // XXX There should be an equivalent for testing the closure of
    // XXX the dialog embedded in a sheet, but I do not know how.
    if (this._openInWindow) {
      wh.wait_for_window_close();
      Assert.ok(aCtc.window.closed, "The customization dialog is not closed.");
    }
  },

  /**
   *  Restore the default buttons in the header pane toolbar
   *  by clicking the corresponding button in the palette dialog
   *  and check if it worked.
   *
   * @param {} aController
   *   the controller object of the window for which the customization
   *   dialog should be opened
   */
  async restoreDefaultButtons(aController) {
    let ctc = await this.open(aController);
    let restoreButton = ctc.window.document
      .getElementById("main-box")
      .querySelector("[oncommand*='overlayRestoreDefaultSet();']");

    EventUtils.synthesizeMouseAtCenter(
      restoreButton,
      {},
      restoreButton.ownerGlobal
    );
    utils.sleep(0);

    this.close(ctc);

    let toolbar = aController.window.document.getElementById(this._toolbarId);
    let defaultSet = toolbar.getAttribute("defaultset");

    Assert.equal(toolbar.currentSet, defaultSet);
  },
};