summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_theme_registration.js
blob: 6f5d2bc6796b108490e40009e828d0050fe5f8d9 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test for dynamically registering and unregistering themes
const CHROME_URL =
  "chrome://mochitests/content/browser/devtools/client/framework/test/";
const TEST_THEME_NAME = "test-theme";
const LIGHT_THEME_NAME = "light";

var toolbox;

add_task(async function themeRegistration() {
  const tab = await addTab("data:text/html,test");
  toolbox = await gDevTools.showToolboxForTab(tab, { toolId: "options" });

  const themeId = await new Promise(resolve => {
    gDevTools.once("theme-registered", registeredThemeId => {
      resolve(registeredThemeId);
    });

    gDevTools.registerTheme({
      id: TEST_THEME_NAME,
      label: "Test theme",
      stylesheets: [CHROME_URL + "doc_theme.css"],
      classList: ["theme-test"],
    });
  });

  is(themeId, TEST_THEME_NAME, "theme-registered event handler sent theme id");

  ok(gDevTools.getThemeDefinitionMap().has(themeId), "theme added to map");
});

add_task(async function themeInOptionsPanel() {
  const panelWin = toolbox.getCurrentPanel().panelWin;
  const doc = panelWin.frameElement.contentDocument;
  const themeBox = doc.getElementById("devtools-theme-box");
  const testThemeOption = themeBox.querySelector(
    `input[type=radio][value=${TEST_THEME_NAME}]`
  );
  const eventsRecorded = [];

  function onThemeChanged(theme) {
    eventsRecorded.push(theme);
  }
  gDevTools.on("theme-changed", onThemeChanged);

  ok(testThemeOption, "new theme exists in the Options panel");

  const lightThemeOption = themeBox.querySelector(
    `input[type=radio][value=${LIGHT_THEME_NAME}]`
  );

  let color = panelWin.getComputedStyle(themeBox).color;
  isnot(color, "rgb(255, 0, 0)", "style unapplied");

  let onThemeSwithComplete = once(panelWin, "theme-switch-complete");

  // Select test theme.
  testThemeOption.click();

  info("Waiting for theme to finish loading");
  await onThemeSwithComplete;

  is(
    gDevTools.getTheme(),
    TEST_THEME_NAME,
    "getTheme returns the expected theme"
  );
  is(
    eventsRecorded.pop(),
    TEST_THEME_NAME,
    "theme-changed fired with the expected theme"
  );

  color = panelWin.getComputedStyle(themeBox).color;
  is(color, "rgb(255, 0, 0)", "style applied");

  onThemeSwithComplete = once(panelWin, "theme-switch-complete");

  // Select light theme
  lightThemeOption.click();

  info("Waiting for theme to finish loading");
  await onThemeSwithComplete;

  is(
    gDevTools.getTheme(),
    LIGHT_THEME_NAME,
    "getTheme returns the expected theme"
  );
  is(
    eventsRecorded.pop(),
    LIGHT_THEME_NAME,
    "theme-changed fired with the expected theme"
  );

  color = panelWin.getComputedStyle(themeBox).color;
  isnot(color, "rgb(255, 0, 0)", "style unapplied");

  onThemeSwithComplete = once(panelWin, "theme-switch-complete");
  // Select test theme again.
  testThemeOption.click();
  await onThemeSwithComplete;
  is(
    gDevTools.getTheme(),
    TEST_THEME_NAME,
    "getTheme returns the expected theme"
  );
  is(
    eventsRecorded.pop(),
    TEST_THEME_NAME,
    "theme-changed fired with the expected theme"
  );

  gDevTools.off("theme-changed", onThemeChanged);
});

add_task(async function themeUnregistration() {
  const panelWin = toolbox.getCurrentPanel().panelWin;
  const onUnRegisteredTheme = once(gDevTools, "theme-unregistered");
  const onThemeSwitchComplete = once(panelWin, "theme-switch-complete");
  const eventsRecorded = [];

  function onThemeChanged(theme) {
    eventsRecorded.push(theme);
  }
  gDevTools.on("theme-changed", onThemeChanged);

  gDevTools.unregisterTheme(TEST_THEME_NAME);
  await onUnRegisteredTheme;
  await onThemeSwitchComplete;

  is(
    gDevTools.getTheme(),
    gDevTools.getAutoTheme(),
    "getTheme returns the expected theme"
  );
  is(
    eventsRecorded.pop(),
    gDevTools.getAutoTheme(),
    "theme-changed fired with the expected theme"
  );
  ok(
    !gDevTools.getThemeDefinitionMap().has(TEST_THEME_NAME),
    "theme removed from map"
  );

  const doc = panelWin.frameElement.contentDocument;
  const themeBox = doc.getElementById("devtools-theme-box");

  // The default theme must be selected now.
  ok(
    themeBox.querySelector(`#devtools-theme-box [value=auto]`).checked,
    `auto theme must be selected`
  );

  gDevTools.off("theme-changed", onThemeChanged);
});

add_task(async function cleanup() {
  await toolbox.destroy();
  toolbox = null;
});