summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/im/test/browser/browser_tooltips.js
blob: db8a7fd86bb2c11c4ce4b4dfad8f83a9800f6e45 (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
/* 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/. */

add_task(async function testMUCMessageSenderTooltip() {
  const account = IMServices.accounts.createAccount(
    "testuser",
    "prpl-mochitest"
  );
  account.password = "this is a test";
  account.connect();

  await openChatTab();
  const conversation = account.prplAccount.wrappedJSObject.makeMUC("tooltips");
  const convNode = getConversationItem(conversation);
  ok(convNode);

  await EventUtils.synthesizeMouseAtCenter(convNode, {});

  const chatConv = getChatConversationElement(conversation);
  ok(chatConv);
  ok(BrowserTestUtils.is_visible(chatConv));
  const messageParent = await getChatMessageParent(chatConv);

  conversation.addParticipant("foo", "1");
  conversation.addParticipant("bar", "2");
  conversation.addParticipant("loremipsum", "3");
  conversation.addMessages([
    // Message without alias
    {
      who: "foo",
      content: "hi",
      options: {
        incoming: true,
      },
    },
    // Message with alias
    {
      who: "bar",
      content: "o/",
      options: {
        incoming: true,
        _alias: "Bar",
      },
    },
    // Alias is not directly related to nick
    {
      who: "loremipsum",
      content: "what's up?",
      options: {
        incoming: true,
        _alias: "Dolor sit amet",
      },
    },
  ]);
  // Wait for at least one event.
  do {
    await BrowserTestUtils.waitForEvent(
      chatConv.convBrowser,
      "MessagesDisplayed"
    );
  } while (chatConv.convBrowser.getPendingMessagesCount() > 0);

  const tooltip = document.getElementById("imTooltip");
  const tooltipTests = [
    {
      messageIndex: 1,
      who: "foo",
      alias: "1",
      displayed: "foo",
    },
    {
      messageIndex: 2,
      who: "bar",
      alias: "2",
      displayed: "Bar",
    },
    {
      messageIndex: 3,
      who: "loremipsum",
      alias: "3",
      displayed: "Dolor sit amet",
    },
  ];
  window.windowUtils.disableNonTestMouseEvents(true);
  try {
    for (const testInfo of tooltipTests) {
      const usernameSelector = `.message:nth-child(${testInfo.messageIndex}) .ib-sender`;
      const username = messageParent.querySelector(usernameSelector);
      is(username.textContent, testInfo.displayed);

      let buddyInfo = TestUtils.topicObserved(
        "user-info-received",
        (subject, data) => data === testInfo.who
      );
      await showTooltip(usernameSelector, tooltip, chatConv.convBrowser);

      is(tooltip.getAttribute("displayname"), testInfo.who);
      await buddyInfo;
      is(tooltip.table.querySelector("td").textContent, testInfo.alias);
      await hideTooltip(tooltip, chatConv.convBrowser);
    }
  } finally {
    window.windowUtils.disableNonTestMouseEvents(false);
  }

  conversation.close();
  account.disconnect();
  IMServices.accounts.deleteAccount(account.id);
});

add_task(async function testTimestampTooltip() {
  const account = IMServices.accounts.createAccount(
    "testuser",
    "prpl-mochitest"
  );
  account.password = "this is a test";
  account.connect();

  await openChatTab();
  const conversation = account.prplAccount.wrappedJSObject.makeMUC("tooltips");
  const convNode = getConversationItem(conversation);
  ok(convNode);

  await EventUtils.synthesizeMouseAtCenter(convNode, {});

  const chatConv = getChatConversationElement(conversation);
  ok(chatConv);
  ok(BrowserTestUtils.is_visible(chatConv));

  const messageTime = Math.floor(Date.now() / 1000);

  conversation.addParticipant("foo", "1");
  conversation.addMessages([
    {
      who: "foo",
      content: "hi",
      options: {
        incoming: true,
      },
      time: messageTime,
    },
  ]);
  // Wait for at least one event.
  do {
    await BrowserTestUtils.waitForEvent(
      chatConv.convBrowser,
      "MessagesDisplayed"
    );
  } while (chatConv.convBrowser.getPendingMessagesCount() > 0);

  const tooltip = document.getElementById("imTooltip");
  window.windowUtils.disableNonTestMouseEvents(true);
  try {
    const messageSelector = ".message:nth-child(1)";
    const dateTimeFormatter = new Services.intl.DateTimeFormat(undefined, {
      timeStyle: "medium",
    });
    const expectedText = dateTimeFormatter.format(new Date(messageTime * 1000));

    await showTooltip(messageSelector, tooltip, chatConv.convBrowser);

    const htmlTooltip = tooltip.querySelector(".htmlTooltip");
    ok(BrowserTestUtils.is_visible(htmlTooltip));
    is(htmlTooltip.textContent, expectedText);
    await hideTooltip(tooltip, chatConv.convBrowser);
  } finally {
    window.windowUtils.disableNonTestMouseEvents(false);
  }

  conversation.close();
  account.disconnect();
  IMServices.accounts.deleteAccount(account.id);
});

async function showTooltip(elementSelector, tooltip, browser) {
  const popupShown = BrowserTestUtils.waitForEvent(tooltip, "popupshown");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    elementSelector,
    { type: "mousemove" },
    browser
  );
  return popupShown;
}

async function hideTooltip(tooltip, browser) {
  const popupHidden = BrowserTestUtils.waitForEvent(tooltip, "popuphidden");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    ".message .body",
    { type: "mousemove" },
    browser
  );
  return popupHidden;
}