Bug 1034512 - Part 12: Avoid using CPOWs in test browser_css_colors.js by running it in a toolbox host. r=miker
authorPatrick Brosset <pbrosset@mozilla.com>
Mon, 26 Jan 2015 02:37:00 -0500
changeset 225656 9201029f4d4862aac596cfc7f93ef41e3800621e
parent 225655 9ad5627c881dc4228641d9575aabaf51ccf31d09
child 225657 252f2c47f271043792b564918112d0d90397c40f
push id10991
push userryanvm@gmail.com
push dateMon, 26 Jan 2015 15:05:08 +0000
treeherderfx-team@252f2c47f271 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmiker
bugs1034512
milestone38.0a1
Bug 1034512 - Part 12: Avoid using CPOWs in test browser_css_colors.js by running it in a toolbox host. r=miker
browser/devtools/shared/test/browser_css_color.js
--- a/browser/devtools/shared/test/browser_css_color.js
+++ b/browser/devtools/shared/test/browser_css_color.js
@@ -1,90 +1,78 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const COLOR_UNIT_PREF = "devtools.defaultColorUnit";
-
+const TEST_URI = "data:text/html;charset=utf-8,browser_css_color.js";
+let {colorUtils} = devtools.require("devtools/css-color");
 let origColorUnit;
-let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
-let {Loader} = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {});
-let {colorUtils} = devtools.require("devtools/css-color");
 
-function test() {
-  waitForExplicitFinish();
+add_task(function*() {
+  yield promiseTab("about:blank");
+  let [host, win, doc] = yield createHost("bottom", TEST_URI);
+  origColorUnit = Services.prefs.getCharPref(COLOR_UNIT_PREF);
 
-  gBrowser.selectedTab = gBrowser.addTab();
-  gBrowser.selectedBrowser.addEventListener("load", function onload() {
-    gBrowser.selectedBrowser.removeEventListener("load", onload, true);
-    waitForFocus(init, content);
-  }, true);
-
-  content.location = "data:text/html;charset=utf-8,browser_css_color.js";
-}
+  info("Creating a test canvas element to test colors");
+  let canvas = createTestCanvas(doc);
+  info("Starting the test");
+  testColorUtils(canvas);
 
-function init() {
-  origColorUnit = Services.prefs.getCharPref(COLOR_UNIT_PREF);
-  createDocument();
-}
+  host.destroy();
+  gBrowser.removeCurrentTab();
+});
 
-function createDocument()
-{
-  let doc = content.document;
-
+function createTestCanvas(doc) {
   let canvas = doc.createElement("canvas");
   canvas.width = canvas.height = 10;
   doc.body.appendChild(canvas);
-
-  testColorUtils();
+  return canvas;
 }
 
-function testColorUtils() {
+function testColorUtils(canvas) {
   let data = getTestData();
 
   for (let {authored, name, hex, hsl, rgb} of data) {
     let color = new colorUtils.CssColor(authored);
 
     // Check all values.
     info("Checking values for " + authored);
     is(color.name, name, "color.name === name");
     is(color.hex, hex, "color.hex === hex");
     is(color.hsl, hsl, "color.hsl === hsl");
     is(color.rgb, rgb, "color.rgb === rgb");
 
     testToString(color, name, hex, hsl, rgb);
-    testColorMatch(name, hex, hsl, rgb, color.rgba);
+    testColorMatch(name, hex, hsl, rgb, color.rgba, canvas);
   }
+
   testProcessCSSString();
   testSetAlpha();
-  finishUp();
 }
 
 function testToString(color, name, hex, hsl, rgb) {
   switchColorUnit(colorUtils.CssColor.COLORUNIT.name);
   is(color.toString(), name, "toString() with authored type");
 
   switchColorUnit(colorUtils.CssColor.COLORUNIT.hex);
   is(color.toString(), hex, "toString() with hex type");
 
   switchColorUnit(colorUtils.CssColor.COLORUNIT.hsl);
   is(color.toString(), hsl, "toString() with hsl type");
 
   switchColorUnit(colorUtils.CssColor.COLORUNIT.rgb);
   is(color.toString(), rgb, "toString() with rgb type");
-
 }
 
 function switchColorUnit(unit) {
   Services.prefs.setCharPref(COLOR_UNIT_PREF, unit);
 }
 
-function testColorMatch(name, hex, hsl, rgb, rgba) {
+function testColorMatch(name, hex, hsl, rgb, rgba, canvas) {
   let target;
-
-  let canvas = content.document.querySelector("canvas");
   let ctx = canvas.getContext("2d");
 
   let clearCanvas = function() {
     canvas.width = 1;
   };
   let setColor = function(aColor) {
     ctx.fillStyle = aColor;
     ctx.fillRect(0, 0, 1, 1);
@@ -157,22 +145,16 @@ function testSetAlpha() {
     ok(false, "Should fail when passing in an invalid color.");
   } catch (e) {
     ok(true, "Fails when setAlpha receives an invalid color.");
   }
 
   is(colorUtils.setAlpha("#fff"), "rgba(255, 255, 255, 1)", "sets alpha to 1 if invalid.");
 }
 
-function finishUp() {
-  Services = colorUtils = Loader = null;
-  gBrowser.removeCurrentTab();
-  finish();
-}
-
 function getTestData() {
   return [
     {authored: "aliceblue", name: "aliceblue", hex: "#F0F8FF", hsl: "hsl(208, 100%, 97%)", rgb: "rgb(240, 248, 255)"},
     {authored: "antiquewhite", name: "antiquewhite", hex: "#FAEBD7", hsl: "hsl(34, 78%, 91%)", rgb: "rgb(250, 235, 215)"},
     {authored: "aqua", name: "aqua", hex: "#0FF", hsl: "hsl(180, 100%, 50%)", rgb: "rgb(0, 255, 255)"},
     {authored: "aquamarine", name: "aquamarine", hex: "#7FFFD4", hsl: "hsl(160, 100%, 75%)", rgb: "rgb(127, 255, 212)"},
     {authored: "azure", name: "azure", hex: "#F0FFFF", hsl: "hsl(180, 100%, 97%)", rgb: "rgb(240, 255, 255)"},
     {authored: "beige", name: "beige", hex: "#F5F5DC", hsl: "hsl(60, 56%, 91%)", rgb: "rgb(245, 245, 220)"},