summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/composition/browser_imageInsertionDialog.js
blob: 83a6e3d52f635c17ddcccc65ee6c94c76dd8d415 (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
/* 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 the image insertion dialog functionality.
 */

"use strict";

var { close_compose_window, open_compose_new_mail } = ChromeUtils.import(
  "resource://testing-common/mozmill/ComposeHelpers.jsm"
);
var { input_value } = ChromeUtils.import(
  "resource://testing-common/mozmill/KeyboardHelpers.jsm"
);

var {
  click_menus_in_sequence,
  plan_for_modal_dialog,
  wait_for_window_close,
  wait_for_modal_dialog,
} = ChromeUtils.import("resource://testing-common/mozmill/WindowHelpers.jsm");

add_task(async function test_image_insertion_dialog_persist() {
  let cwc = open_compose_new_mail();

  // First focus on the editor element
  cwc.window.document.getElementById("messageEditor").focus();

  // Now open the image window
  plan_for_modal_dialog("Mail:image", async function insert_image(mwc) {
    // Insert the url of the image.
    let srcloc = mwc.window.document.getElementById("srcInput");
    srcloc.focus();

    let file = new FileUtils.File(getTestFilePath("data/tb-logo.png"));
    input_value(mwc, Services.io.newFileURI(file).spec);

    // Don't add alternate text
    let noAlt = mwc.window.document.getElementById("noAltTextRadio");
    EventUtils.synthesizeMouseAtCenter(noAlt, {}, noAlt.ownerGlobal);
    await new Promise(resolve => setTimeout(resolve));
    mwc.window.document.documentElement.querySelector("dialog").acceptDialog();
  });

  let insertMenu = cwc.window.document.getElementById("InsertPopupButton");
  let insertMenuPopup = cwc.window.document.getElementById("InsertPopup");

  EventUtils.synthesizeMouseAtCenter(insertMenu, {}, insertMenu.ownerGlobal);
  await click_menus_in_sequence(insertMenuPopup, [{ id: "InsertImageItem" }]);

  wait_for_modal_dialog();
  wait_for_window_close();
  await new Promise(resolve => setTimeout(resolve));

  let img = cwc.window.document
    .getElementById("messageEditor")
    .contentDocument.querySelector("img");
  Assert.ok(!!img, "editor should contain an image");

  info("Will check that radio option persists");

  // Check that the radio option persists
  plan_for_modal_dialog("Mail:image", async function insert_image(mwc) {
    Assert.ok(
      mwc.window.document.getElementById("noAltTextRadio").selected,
      "We should persist the previously selected value"
    );
    // We change to "use alt text"
    let altTextRadio = mwc.window.document.getElementById("altTextRadio");
    EventUtils.synthesizeMouseAtCenter(
      altTextRadio,
      {},
      altTextRadio.ownerGlobal
    );
    await new Promise(resolve => setTimeout(resolve));
    mwc.window.document.documentElement.querySelector("dialog").cancelDialog();
  });

  EventUtils.synthesizeMouseAtCenter(insertMenu, {}, insertMenu.ownerGlobal);
  await click_menus_in_sequence(insertMenuPopup, [{ id: "InsertImageItem" }]);
  wait_for_modal_dialog();
  wait_for_window_close();
  await new Promise(resolve => setTimeout(resolve));

  info("Will check that radio option really persists");

  // Check that the radio option still persists (be really sure)
  plan_for_modal_dialog("Mail:image", function insert_image(mwc) {
    Assert.ok(
      mwc.window.document.getElementById("altTextRadio").selected,
      "We should persist the previously selected value"
    );
    // Accept the dialog
    mwc.window.document.documentElement.querySelector("dialog").cancelDialog();
  });

  EventUtils.synthesizeMouseAtCenter(insertMenu, {}, insertMenu.ownerGlobal);
  await click_menus_in_sequence(insertMenuPopup, [{ id: "InsertImageItem" }]);
  wait_for_modal_dialog();
  wait_for_window_close();

  info("Will check we switch to 'no alt text'");

  // Get the inserted image, double-click it, make sure we switch to "no alt
  // text", despite the persisted value being "use alt text"
  plan_for_modal_dialog("Mail:image", function insert_image(mwc) {
    Assert.ok(
      mwc.window.document.getElementById("noAltTextRadio").selected,
      "We shouldn't use the persisted value because the insert image has no alt text"
    );
    mwc.window.document.documentElement.querySelector("dialog").cancelDialog();
  });
  EventUtils.synthesizeMouseAtCenter(img, { clickCount: 2 }, img.ownerGlobal);
  wait_for_modal_dialog();
  wait_for_window_close();

  info("Will check using alt text");

  // Now use some alt text for the edit image dialog
  plan_for_modal_dialog("Mail:image", async function insert_image(mwc) {
    Assert.ok(
      mwc.window.document.getElementById("noAltTextRadio").selected,
      "That value should persist still..."
    );
    let altTextRadio = mwc.window.document.getElementById("altTextRadio");
    EventUtils.synthesizeMouseAtCenter(
      altTextRadio,
      {},
      altTextRadio.ownerGlobal
    );

    let srcloc = mwc.window.document.getElementById("altTextInput");
    srcloc.focus();
    input_value(mwc, "some alt text");
    await new Promise(resolve => setTimeout(resolve));
    // Accept the dialog
    mwc.window.document.documentElement.querySelector("dialog").acceptDialog();
  });
  EventUtils.synthesizeMouseAtCenter(img, { clickCount: 2 }, img.ownerGlobal);
  wait_for_modal_dialog();
  wait_for_window_close();

  info("Will check next time we edit, we still have 'use alt text' selected");

  // Make sure next time we edit it, we still have "use alt text" selected.
  img = cwc.window.document
    .getElementById("messageEditor")
    .contentDocument.querySelector("img");
  plan_for_modal_dialog("Mail:image", function insert_image(mwc) {
    Assert.ok(
      mwc.window.document.getElementById("altTextRadio").selected,
      "We edited the image to make it have alt text, we should keep it selected"
    );
    // Accept the dialog
    mwc.window.document.documentElement.querySelector("dialog").cancelDialog();
  });
  EventUtils.synthesizeMouseAtCenter(img, { clickCount: 2 }, img.ownerGlobal);
  wait_for_modal_dialog();
  wait_for_window_close();

  close_compose_window(cwc);
});