Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 2 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 4 | |
michael@0 | 5 | // Tests that the spectrum color picker works correctly |
michael@0 | 6 | |
michael@0 | 7 | const TEST_URI = "chrome://browser/content/devtools/spectrum-frame.xhtml"; |
michael@0 | 8 | const {Spectrum} = devtools.require("devtools/shared/widgets/Spectrum"); |
michael@0 | 9 | |
michael@0 | 10 | let doc; |
michael@0 | 11 | |
michael@0 | 12 | function test() { |
michael@0 | 13 | waitForExplicitFinish(); |
michael@0 | 14 | addTab(TEST_URI, () => { |
michael@0 | 15 | doc = content.document; |
michael@0 | 16 | startTests(); |
michael@0 | 17 | }); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function endTests() { |
michael@0 | 21 | doc = null; |
michael@0 | 22 | gBrowser.removeCurrentTab(); |
michael@0 | 23 | finish(); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | function startTests() { |
michael@0 | 27 | testCreateAndDestroyShouldAppendAndRemoveElements(); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | function testCreateAndDestroyShouldAppendAndRemoveElements() { |
michael@0 | 31 | let containerElement = doc.querySelector("#spectrum"); |
michael@0 | 32 | ok(containerElement, "We have the root node to append spectrum to"); |
michael@0 | 33 | is(containerElement.childElementCount, 0, "Root node is empty"); |
michael@0 | 34 | |
michael@0 | 35 | let s = new Spectrum(containerElement, [255, 126, 255, 1]); |
michael@0 | 36 | s.show(); |
michael@0 | 37 | ok(containerElement.childElementCount > 0, "Spectrum has appended elements"); |
michael@0 | 38 | |
michael@0 | 39 | s.destroy(); |
michael@0 | 40 | is(containerElement.childElementCount, 0, "Destroying spectrum removed all nodes"); |
michael@0 | 41 | |
michael@0 | 42 | testPassingAColorAtInitShouldSetThatColor(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | function testPassingAColorAtInitShouldSetThatColor() { |
michael@0 | 46 | let initRgba = [255, 126, 255, 1]; |
michael@0 | 47 | |
michael@0 | 48 | let s = new Spectrum(doc.querySelector("#spectrum"), initRgba); |
michael@0 | 49 | s.show(); |
michael@0 | 50 | |
michael@0 | 51 | let setRgba = s.rgb; |
michael@0 | 52 | |
michael@0 | 53 | is(initRgba[0], setRgba[0], "Spectrum initialized with the right color"); |
michael@0 | 54 | is(initRgba[1], setRgba[1], "Spectrum initialized with the right color"); |
michael@0 | 55 | is(initRgba[2], setRgba[2], "Spectrum initialized with the right color"); |
michael@0 | 56 | is(initRgba[3], setRgba[3], "Spectrum initialized with the right color"); |
michael@0 | 57 | |
michael@0 | 58 | s.destroy(); |
michael@0 | 59 | testSettingAndGettingANewColor(); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | function testSettingAndGettingANewColor() { |
michael@0 | 63 | let s = new Spectrum(doc.querySelector("#spectrum"), [0, 0, 0, 1]); |
michael@0 | 64 | s.show(); |
michael@0 | 65 | |
michael@0 | 66 | let colorToSet = [255, 255, 255, 1]; |
michael@0 | 67 | s.rgb = colorToSet; |
michael@0 | 68 | let newColor = s.rgb; |
michael@0 | 69 | |
michael@0 | 70 | is(colorToSet[0], newColor[0], "Spectrum set with the right color"); |
michael@0 | 71 | is(colorToSet[1], newColor[1], "Spectrum set with the right color"); |
michael@0 | 72 | is(colorToSet[2], newColor[2], "Spectrum set with the right color"); |
michael@0 | 73 | is(colorToSet[3], newColor[3], "Spectrum set with the right color"); |
michael@0 | 74 | |
michael@0 | 75 | s.destroy(); |
michael@0 | 76 | testChangingColorShouldEmitEvents(); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function testChangingColorShouldEmitEvents() { |
michael@0 | 80 | let s = new Spectrum(doc.querySelector("#spectrum"), [255, 255, 255, 1]); |
michael@0 | 81 | s.show(); |
michael@0 | 82 | |
michael@0 | 83 | s.once("changed", (event, rgba, color) => { |
michael@0 | 84 | EventUtils.sendMouseEvent({type: "mouseup"}, s.dragger, doc.defaultView); |
michael@0 | 85 | |
michael@0 | 86 | ok(true, "Changed event was emitted on color change"); |
michael@0 | 87 | is(rgba[0], 128, "New color is correct"); |
michael@0 | 88 | is(rgba[1], 64, "New color is correct"); |
michael@0 | 89 | is(rgba[2], 64, "New color is correct"); |
michael@0 | 90 | is(rgba[3], 1, "New color is correct"); |
michael@0 | 91 | is("rgba(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2] + ", " + rgba[3] + ")", color, "RGBA and css color correspond"); |
michael@0 | 92 | |
michael@0 | 93 | s.destroy(); |
michael@0 | 94 | testSettingColorShoudUpdateTheUI(); |
michael@0 | 95 | }); |
michael@0 | 96 | |
michael@0 | 97 | executeSoon(() => { |
michael@0 | 98 | EventUtils.synthesizeMouse(s.dragger, s.dragger.offsetWidth/2, s.dragger.offsetHeight/2, {}, content); |
michael@0 | 99 | }); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | function testSettingColorShoudUpdateTheUI() { |
michael@0 | 103 | let s = new Spectrum(doc.querySelector("#spectrum"), [255, 255, 255, 1]); |
michael@0 | 104 | s.show(); |
michael@0 | 105 | let dragHelperOriginalPos = [s.dragHelper.style.top, s.dragHelper.style.left]; |
michael@0 | 106 | let alphaHelperOriginalPos = s.alphaSliderHelper.style.left; |
michael@0 | 107 | |
michael@0 | 108 | s.rgb = [50, 240, 234, .2]; |
michael@0 | 109 | s.updateUI(); |
michael@0 | 110 | |
michael@0 | 111 | ok(s.alphaSliderHelper.style.left != alphaHelperOriginalPos, "Alpha helper has moved"); |
michael@0 | 112 | ok(s.dragHelper.style.top !== dragHelperOriginalPos[0], "Drag helper has moved"); |
michael@0 | 113 | ok(s.dragHelper.style.left !== dragHelperOriginalPos[1], "Drag helper has moved"); |
michael@0 | 114 | |
michael@0 | 115 | s.rgb = [240, 32, 124, 0]; |
michael@0 | 116 | s.updateUI(); |
michael@0 | 117 | is(s.alphaSliderHelper.style.left, - (s.alphaSliderHelper.offsetWidth/2) + "px", |
michael@0 | 118 | "Alpha range UI has been updated again"); |
michael@0 | 119 | |
michael@0 | 120 | s.destroy(); |
michael@0 | 121 | executeSoon(endTests); |
michael@0 | 122 | } |