summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_identity_indication.js
blob: 096f7a6f5bb229dd54655255cfa427c98972b4d0 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

function confirmDefaults() {
  if (gURLBar.searchButton) {
    is(
      getComputedStyle(document.getElementById("identity-box")).display,
      "none",
      "Identity box should be hidden"
    );
  } else {
    is(
      getComputedStyle(document.getElementById("identity-icon")).listStyleImage,
      'url("chrome://global/skin/icons/search-glass.svg")',
      "Identity icon should be the search icon"
    );
  }

  let label = document.getElementById("identity-icon-label");
  ok(
    BrowserTestUtils.is_hidden(label),
    "No label should be used before the extension is started"
  );
}

async function waitForIndentityBoxMutation({ expectExtensionIcon }) {
  const el = document.getElementById("identity-box");
  await BrowserTestUtils.waitForMutationCondition(
    el,
    {
      attributeFilter: ["class"],
    },
    () => el.classList.contains("extensionPage") == expectExtensionIcon
  );
}

function confirmExtensionPage() {
  let identityIconEl = document.getElementById("identity-icon");

  is(
    getComputedStyle(identityIconEl).listStyleImage,
    'url("chrome://mozapps/skin/extensions/extension.svg")',
    "Identity icon should be the default extension icon"
  );

  is(
    identityIconEl.tooltipText,
    "Loaded by extension: Test Extension",
    "The correct tooltip should be used"
  );

  let label = document.getElementById("identity-icon-label");
  is(
    label.value,
    "Extension (Test Extension)",
    "The correct label should be used"
  );
  ok(BrowserTestUtils.is_visible(label), "No label should be visible");
}

add_task(async function testIdentityIndication() {
  let extension = ExtensionTestUtils.loadExtension({
    background() {
      browser.test.sendMessage("url", browser.runtime.getURL("icon.png"));
    },
    manifest: {
      name: "Test Extension",
    },
    files: {
      "icon.png": "",
    },
  });

  await extension.startup();

  confirmDefaults();

  let url = await extension.awaitMessage("url");

  const promiseIdentityBoxExtension = waitForIndentityBoxMutation({
    expectExtensionIcon: true,
  });
  await BrowserTestUtils.withNewTab({ gBrowser, url }, async function () {
    await promiseIdentityBoxExtension;
    confirmExtensionPage();
  });

  const promiseIdentityBoxDefault = waitForIndentityBoxMutation({
    expectExtensionIcon: false,
  });
  await extension.unload();
  await promiseIdentityBoxDefault;

  confirmDefaults();
});

add_task(async function testIdentityIndicationNewTab() {
  let extension = ExtensionTestUtils.loadExtension({
    background() {
      browser.test.sendMessage("url", browser.runtime.getURL("newtab.html"));
    },
    manifest: {
      name: "Test Extension",
      browser_specific_settings: {
        gecko: {
          id: "@newtab",
        },
      },
      chrome_url_overrides: {
        newtab: "newtab.html",
      },
    },
    files: {
      "newtab.html": "<h1>New tab!</h1>",
    },
    useAddonManager: "temporary",
  });

  await extension.startup();

  confirmDefaults();

  let url = await extension.awaitMessage("url");
  const promiseIdentityBoxExtension = waitForIndentityBoxMutation({
    expectExtensionIcon: true,
  });
  await BrowserTestUtils.withNewTab({ gBrowser, url }, async function () {
    await promiseIdentityBoxExtension;
    confirmExtensionPage();
    is(gURLBar.value, "", "The URL bar is blank");
  });

  const promiseIdentityBoxDefault = waitForIndentityBoxMutation({
    expectExtensionIcon: false,
  });
  await extension.unload();
  await promiseIdentityBoxDefault;

  confirmDefaults();
});