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

"use strict";

// Tests that theme utilities work

const {
  getColor,
  getTheme,
  setTheme,
} = require("resource://devtools/client/shared/theme.js");
const { PrefObserver } = require("resource://devtools/client/shared/prefs.js");

add_task(async function () {
  testGetTheme();
  testSetTheme();
  testGetColor();
  testColorExistence();
});

function testGetTheme() {
  const originalTheme = getTheme();
  ok(originalTheme, "has some theme to start with.");
  Services.prefs.setCharPref("devtools.theme", "light");
  is(getTheme(), "light", "getTheme() correctly returns light theme");
  Services.prefs.setCharPref("devtools.theme", "dark");
  is(getTheme(), "dark", "getTheme() correctly returns dark theme");
  Services.prefs.setCharPref("devtools.theme", "unknown");
  is(getTheme(), "unknown", "getTheme() correctly returns an unknown theme");
  Services.prefs.setCharPref("devtools.theme", originalTheme);
}

function testSetTheme() {
  const originalTheme = getTheme();
  // Put this in a variable rather than hardcoding it because the default
  // changes between aurora and nightly
  const otherTheme = originalTheme == "dark" ? "light" : "dark";

  const prefObserver = new PrefObserver("devtools.");
  prefObserver.once("devtools.theme", () => {
    const newValue = Services.prefs.getCharPref("devtools.theme");
    is(
      newValue,
      otherTheme,
      "A preference event triggered by setTheme comes after the value is set."
    );
  });
  setTheme(otherTheme);
  is(
    Services.prefs.getCharPref("devtools.theme"),
    otherTheme,
    "setTheme() correctly sets another theme."
  );
  setTheme(originalTheme);
  is(
    Services.prefs.getCharPref("devtools.theme"),
    originalTheme,
    "setTheme() correctly sets the original theme."
  );
  setTheme("unknown");
  is(
    Services.prefs.getCharPref("devtools.theme"),
    "unknown",
    "setTheme() correctly sets an unknown theme."
  );
  Services.prefs.setCharPref("devtools.theme", originalTheme);

  prefObserver.destroy();
}

function testGetColor() {
  const BLUE_DARK = "#75bfff";
  const BLUE_LIGHT = "#0074e8";
  const originalTheme = getTheme();

  setTheme("dark");
  is(
    getColor("highlight-blue"),
    BLUE_DARK,
    "correctly gets color for enabled theme."
  );
  setTheme("light");
  is(
    getColor("highlight-blue"),
    BLUE_LIGHT,
    "correctly gets color for enabled theme."
  );
  setTheme("metal");
  is(
    getColor("highlight-blue"),
    BLUE_LIGHT,
    "correctly uses light for default theme if enabled theme not found"
  );

  is(
    getColor("highlight-blue", "dark"),
    BLUE_DARK,
    "if provided and found, uses the provided theme."
  );
  is(
    getColor("highlight-blue", "metal"),
    BLUE_LIGHT,
    "if provided and not found, defaults to light theme."
  );
  is(
    getColor("somecomponents"),
    null,
    "if a type cannot be found, should return null."
  );

  setTheme(originalTheme);
}

function testColorExistence() {
  const vars = [
    "body-background",
    "sidebar-background",
    "contrast-background",
    "tab-toolbar-background",
    "toolbar-background",
    "selection-background",
    "selection-color",
    "selection-background-hover",
    "splitter-color",
    "comment",
    "body-color",
    "text-color-alt",
    "text-color-inactive",
    "text-color-strong",
    "highlight-green",
    "highlight-blue",
    "highlight-bluegrey",
    "highlight-purple",
    "highlight-lightorange",
    "highlight-orange",
    "highlight-red",
    "highlight-pink",
  ];

  for (const type of vars) {
    ok(getColor(type, "light"), `${type} is a valid color in light theme`);
    ok(getColor(type, "dark"), `${type} is a valid color in dark theme`);
  }
}