summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_devedition.js
blob: 06ee42e7e8495ad6d915e46bd53c5463438fa4e7 (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
/*
 * Testing changes for Developer Edition theme.
 * A special stylesheet should be added to the browser.xul document
 * when the firefox-devedition@mozilla.org lightweight theme
 * is applied.
 */

const PREF_LWTHEME_USED_THEMES = "lightweightThemes.usedThemes";
const PREF_DEVTOOLS_THEME = "devtools.theme";
const {LightweightThemeManager} = Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", {});

LightweightThemeManager.clearBuiltInThemes();
LightweightThemeManager.addBuiltInTheme(dummyLightweightTheme("firefox-devedition@mozilla.org"));

registerCleanupFunction(() => {
  // Set preferences back to their original values
  LightweightThemeManager.currentTheme = null;
  Services.prefs.clearUserPref(PREF_DEVTOOLS_THEME);
  Services.prefs.clearUserPref(PREF_LWTHEME_USED_THEMES);

  LightweightThemeManager.currentTheme = null;
  LightweightThemeManager.clearBuiltInThemes();
});

add_task(function* startTests() {
  Services.prefs.setCharPref(PREF_DEVTOOLS_THEME, "dark");

  info ("Setting the current theme to null");
  LightweightThemeManager.currentTheme = null;
  ok (!DevEdition.isStyleSheetEnabled, "There is no devedition style sheet when no lw theme is applied.");

  info ("Adding a lightweight theme.");
  LightweightThemeManager.currentTheme = dummyLightweightTheme("preview0");
  ok (!DevEdition.isStyleSheetEnabled, "The devedition stylesheet has been removed when a lightweight theme is applied.");

  info ("Applying the devedition lightweight theme.");
  let onAttributeAdded = waitForBrightTitlebarAttribute();
  LightweightThemeManager.currentTheme = LightweightThemeManager.getUsedTheme("firefox-devedition@mozilla.org");
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet has been added when the devedition lightweight theme is applied");
  yield onAttributeAdded;
  is (document.documentElement.getAttribute("brighttitlebarforeground"), "true",
     "The brighttitlebarforeground attribute is set on the window.");

  info ("Unapplying all themes.");
  LightweightThemeManager.currentTheme = null;
  ok (!DevEdition.isStyleSheetEnabled, "There is no devedition style sheet when no lw theme is applied.");

  info ("Applying the devedition lightweight theme.");
  onAttributeAdded = waitForBrightTitlebarAttribute();
  LightweightThemeManager.currentTheme = LightweightThemeManager.getUsedTheme("firefox-devedition@mozilla.org");
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet has been added when the devedition lightweight theme is applied");
  yield onAttributeAdded;
  ok (document.documentElement.hasAttribute("brighttitlebarforeground"),
     "The brighttitlebarforeground attribute is set on the window with dark devtools theme.");
});

add_task(function* testDevtoolsTheme() {
  info ("Checking stylesheet and :root attributes based on devtools theme.");
  Services.prefs.setCharPref(PREF_DEVTOOLS_THEME, "light");
  is (document.documentElement.getAttribute("devtoolstheme"), "light",
    "The documentElement has an attribute based on devtools theme.");
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet is still there with the light devtools theme.");
  ok (!document.documentElement.hasAttribute("brighttitlebarforeground"),
     "The brighttitlebarforeground attribute is not set on the window with light devtools theme.");

  Services.prefs.setCharPref(PREF_DEVTOOLS_THEME, "dark");
  is (document.documentElement.getAttribute("devtoolstheme"), "dark",
    "The documentElement has an attribute based on devtools theme.");
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet is still there with the dark devtools theme.");
  is (document.documentElement.getAttribute("brighttitlebarforeground"), "true",
     "The brighttitlebarforeground attribute is set on the window with dark devtools theme.");

  Services.prefs.setCharPref(PREF_DEVTOOLS_THEME, "foobar");
  is (document.documentElement.getAttribute("devtoolstheme"), "light",
    "The documentElement has 'light' as a default for the devtoolstheme attribute");
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet is still there with the foobar devtools theme.");
  ok (!document.documentElement.hasAttribute("brighttitlebarforeground"),
     "The brighttitlebarforeground attribute is not set on the window with light devtools theme.");
});

function dummyLightweightTheme(id) {
  return {
    id: id,
    name: id,
    headerURL: "resource:///chrome/browser/content/browser/defaultthemes/devedition.header.png",
    iconURL: "resource:///chrome/browser/content/browser/defaultthemes/devedition.icon.png",
    textcolor: "red",
    accentcolor: "blue"
  };
}

add_task(function* testLightweightThemePreview() {
  info ("Setting devedition to current and the previewing others");
  LightweightThemeManager.currentTheme = LightweightThemeManager.getUsedTheme("firefox-devedition@mozilla.org");
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet is enabled.");
  LightweightThemeManager.previewTheme(dummyLightweightTheme("preview0"));
  ok (!DevEdition.isStyleSheetEnabled, "The devedition stylesheet is not enabled after a lightweight theme preview.");
  LightweightThemeManager.resetPreview();
  LightweightThemeManager.previewTheme(dummyLightweightTheme("preview1"));
  ok (!DevEdition.isStyleSheetEnabled, "The devedition stylesheet is not enabled after a second lightweight theme preview.");
  LightweightThemeManager.resetPreview();
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet is enabled again after resetting the preview.");
  LightweightThemeManager.currentTheme = null;
  ok (!DevEdition.isStyleSheetEnabled, "The devedition stylesheet is gone after removing the current theme.");

  info ("Previewing the devedition theme");
  LightweightThemeManager.previewTheme(LightweightThemeManager.getUsedTheme("firefox-devedition@mozilla.org"));
  ok (DevEdition.isStyleSheetEnabled, "The devedition stylesheet is enabled.");
  LightweightThemeManager.previewTheme(dummyLightweightTheme("preview2"));
  LightweightThemeManager.resetPreview();
  ok (!DevEdition.isStyleSheetEnabled, "The devedition stylesheet is now disabled after resetting the preview.");
});

// Use a mutation observer to wait for the brighttitlebarforeground
// attribute to change.  Using this instead of waiting for the load
// event on the DevEdition styleSheet.
function waitForBrightTitlebarAttribute() {
  return new Promise((resolve, reject) => {
    let mutationObserver = new MutationObserver(function (mutations) {
      for (let mutation of mutations) {
        if (mutation.attributeName == "brighttitlebarforeground") {
          mutationObserver.disconnect();
          resolve();
        }
      }
    });
    mutationObserver.observe(document.documentElement, { attributes: true });
  });
}