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