michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: michael@0: const GCONF_BG_COLOR_KEY = "/desktop/gnome/background/primary_color"; michael@0: michael@0: var gShell; michael@0: var gGConf; michael@0: michael@0: /** michael@0: * Converts from a rgb numerical color valule (r << 16 | g << 8 | b) michael@0: * into a hex string in #RRGGBB format. michael@0: */ michael@0: function colorToHex(aColor) { michael@0: const rMask = 4294901760; michael@0: const gMask = 65280; michael@0: const bMask = 255; michael@0: michael@0: var r = (aColor & rMask) >> 16; michael@0: var g = (aColor & gMask) >> 8; michael@0: var b = (aColor & bMask); michael@0: michael@0: return "#" + [r, g, b].map(function(aInt) michael@0: aInt.toString(16).replace(/^(.)$/, "0$1")) michael@0: .join("").toUpperCase(); michael@0: } michael@0: michael@0: /** michael@0: * Converts a color string in #RRGGBB format to a rgb numerical color value michael@0: * (r << 16 | g << 8 | b). michael@0: */ michael@0: function hexToColor(aString) { michael@0: return parseInt(aString.substring(1,3), 16) << 16 | michael@0: parseInt(aString.substring(3,5), 16) << 8 | michael@0: parseInt(aString.substring(5,7), 16); michael@0: } michael@0: michael@0: /** michael@0: * Checks that setting the GConf background key to aGConfColor will michael@0: * result in the Shell component returning a background color equals michael@0: * to aExpectedShellColor in #RRGGBB format. michael@0: */ michael@0: function checkGConfToShellColor(aGConfColor, aExpectedShellColor) { michael@0: michael@0: gGConf.setString(GCONF_BG_COLOR_KEY, aGConfColor); michael@0: var shellColor = colorToHex(gShell.desktopBackgroundColor); michael@0: michael@0: do_check_eq(shellColor, aExpectedShellColor); michael@0: } michael@0: michael@0: /** michael@0: * Checks that setting the background color (in #RRGGBB format) using the Shell michael@0: * component will result in having a GConf key for the background color set to michael@0: * aExpectedGConfColor. michael@0: */ michael@0: function checkShellToGConfColor(aShellColor, aExpectedGConfColor) { michael@0: michael@0: gShell.desktopBackgroundColor = hexToColor(aShellColor); michael@0: var gconfColor = gGConf.getString(GCONF_BG_COLOR_KEY); michael@0: michael@0: do_check_eq(gconfColor, aExpectedGConfColor); michael@0: } michael@0: michael@0: function run_test() { michael@0: michael@0: // This test is Linux specific for now michael@0: if (!("@mozilla.org/gnome-gconf-service;1" in Cc)) michael@0: return; michael@0: michael@0: try { michael@0: // If GSettings is available, then the GConf tests michael@0: // will fail michael@0: var gsettings = Cc["@mozilla.org/gsettings-service;1"]. michael@0: getService(Ci.nsIGSettingsService). michael@0: getCollectionForSchema("org.gnome.desktop.background"); michael@0: return; michael@0: } catch(e) { } michael@0: michael@0: gGConf = Cc["@mozilla.org/gnome-gconf-service;1"]. michael@0: getService(Ci.nsIGConfService); michael@0: michael@0: gShell = Cc["@mozilla.org/browser/shell-service;1"]. michael@0: getService(Ci.nsIShellService); michael@0: michael@0: // Save the original background color so that we can restore it michael@0: // after the test. michael@0: var origGConfColor = gGConf.getString(GCONF_BG_COLOR_KEY); michael@0: michael@0: try { michael@0: michael@0: checkGConfToShellColor("#000", "#000000"); michael@0: checkGConfToShellColor("#00f", "#0000FF"); michael@0: checkGConfToShellColor("#b2f", "#BB22FF"); michael@0: checkGConfToShellColor("#fff", "#FFFFFF"); michael@0: michael@0: checkGConfToShellColor("#000000", "#000000"); michael@0: checkGConfToShellColor("#0000ff", "#0000FF"); michael@0: checkGConfToShellColor("#b002f0", "#B002F0"); michael@0: checkGConfToShellColor("#ffffff", "#FFFFFF"); michael@0: michael@0: checkGConfToShellColor("#000000000", "#000000"); michael@0: checkGConfToShellColor("#00f00f00f", "#000000"); michael@0: checkGConfToShellColor("#aaabbbccc", "#AABBCC"); michael@0: checkGConfToShellColor("#fffffffff", "#FFFFFF"); michael@0: michael@0: checkGConfToShellColor("#000000000000", "#000000"); michael@0: checkGConfToShellColor("#000f000f000f", "#000000"); michael@0: checkGConfToShellColor("#00ff00ff00ff", "#000000"); michael@0: checkGConfToShellColor("#aaaabbbbcccc", "#AABBCC"); michael@0: checkGConfToShellColor("#111122223333", "#112233"); michael@0: checkGConfToShellColor("#ffffffffffff", "#FFFFFF"); michael@0: michael@0: checkShellToGConfColor("#000000", "#000000000000"); michael@0: checkShellToGConfColor("#0000FF", "#00000000ffff"); michael@0: checkShellToGConfColor("#FFFFFF", "#ffffffffffff"); michael@0: checkShellToGConfColor("#0A0B0C", "#0a0a0b0b0c0c"); michael@0: checkShellToGConfColor("#A0B0C0", "#a0a0b0b0c0c0"); michael@0: checkShellToGConfColor("#AABBCC", "#aaaabbbbcccc"); michael@0: michael@0: } finally { michael@0: gGConf.setString(GCONF_BG_COLOR_KEY, origGConfColor); michael@0: } michael@0: }