summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/startup/browser_preXULSkeletonUIRegistry.js
blob: 7b96764eba483deef7b053d36a244a718e89b481 (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
ChromeUtils.defineESModuleGetters(this, {
  WindowsRegistry: "resource://gre/modules/WindowsRegistry.sys.mjs",
});

function getFirefoxExecutableFile() {
  let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  file = Services.dirsvc.get("GreBinD", Ci.nsIFile);

  file.append(AppConstants.MOZ_APP_NAME + ".exe");
  return file;
}

// This is copied from WindowsRegistry.sys.mjs, but extended to support
// TYPE_BINARY, as that is how we represent doubles in the registry for
// the skeleton UI. However, we didn't extend WindowsRegistry.sys.mjs itself,
// because TYPE_BINARY is kind of a footgun for javascript callers - our
// use case is just trivial (checking that the value is non-zero).
function readRegKeyExtended(aRoot, aPath, aKey, aRegistryNode = 0) {
  const kRegMultiSz = 7;
  const kMode = Ci.nsIWindowsRegKey.ACCESS_READ | aRegistryNode;
  let registry = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
    Ci.nsIWindowsRegKey
  );
  try {
    registry.open(aRoot, aPath, kMode);
    if (registry.hasValue(aKey)) {
      let type = registry.getValueType(aKey);
      switch (type) {
        case kRegMultiSz:
          // nsIWindowsRegKey doesn't support REG_MULTI_SZ type out of the box.
          let str = registry.readStringValue(aKey);
          return str.split("\0").filter(v => v);
        case Ci.nsIWindowsRegKey.TYPE_STRING:
          return registry.readStringValue(aKey);
        case Ci.nsIWindowsRegKey.TYPE_INT:
          return registry.readIntValue(aKey);
        case Ci.nsIWindowsRegKey.TYPE_BINARY:
          return registry.readBinaryValue(aKey);
        default:
          throw new Error("Unsupported registry value.");
      }
    }
  } catch (ex) {
  } finally {
    registry.close();
  }
  return undefined;
}

add_task(async function testWritesEnabledOnPrefChange() {
  Services.prefs.setBoolPref("browser.startup.preXulSkeletonUI", true);

  const win = await BrowserTestUtils.openNewBrowserWindow();

  const firefoxPath = getFirefoxExecutableFile().path;
  let enabled = WindowsRegistry.readRegKey(
    Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    "Software\\Mozilla\\Firefox\\PreXULSkeletonUISettings",
    `${firefoxPath}|Enabled`
  );
  is(enabled, 1, "Pre-XUL skeleton UI is enabled in the Windows registry");

  Services.prefs.setBoolPref("browser.startup.preXulSkeletonUI", false);
  enabled = WindowsRegistry.readRegKey(
    Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    "Software\\Mozilla\\Firefox\\PreXULSkeletonUISettings",
    `${firefoxPath}|Enabled`
  );
  is(enabled, 0, "Pre-XUL skeleton UI is disabled in the Windows registry");

  Services.prefs.setBoolPref("browser.startup.preXulSkeletonUI", true);
  Services.prefs.setIntPref("browser.tabs.inTitlebar", 0);
  enabled = WindowsRegistry.readRegKey(
    Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    "Software\\Mozilla\\Firefox\\PreXULSkeletonUISettings",
    `${firefoxPath}|Enabled`
  );
  is(enabled, 0, "Pre-XUL skeleton UI is disabled in the Windows registry");

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function testPersistsNecessaryValuesOnChange() {
  // Enable the skeleton UI, since if it's disabled we won't persist the size values
  await SpecialPowers.pushPrefEnv({
    set: [["browser.startup.preXulSkeletonUI", true]],
  });

  const regKeys = [
    "Width",
    "Height",
    "ScreenX",
    "ScreenY",
    "UrlbarCSSSpan",
    "CssToDevPixelScaling",
    "SpringsCSSSpan",
    "SearchbarCSSSpan",
    "Theme",
    "Flags",
    "Progress",
  ];

  // Remove all of the registry values to ensure old tests aren't giving us false
  // positives
  for (let key of regKeys) {
    WindowsRegistry.removeRegKey(
      Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
      "Software\\Mozilla\\Firefox\\PreXULSkeletonUISettings",
      key
    );
  }

  const win = await BrowserTestUtils.openNewBrowserWindow();
  const firefoxPath = getFirefoxExecutableFile().path;
  for (let key of regKeys) {
    let value = readRegKeyExtended(
      Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
      "Software\\Mozilla\\Firefox\\PreXULSkeletonUISettings",
      `${firefoxPath}|${key}`
    );
    isnot(
      typeof value,
      "undefined",
      `Skeleton UI registry values should have a defined value for ${key}`
    );
    if (value.length) {
      let hasNonZero = false;
      for (var i = 0; i < value.length; i++) {
        hasNonZero = hasNonZero || value[i];
      }
      ok(hasNonZero, `Value should have non-zero components for ${key}`);
    }
  }

  await BrowserTestUtils.closeWindow(win);
});