summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_eyedropper.js
blob: 0762066e329246298587c2255dab9331b7151563 (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
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test opening the eyedropper from the color picker. Pressing escape to close it, and
// clicking the page to select a color.

const TEST_URI = `
  <style type="text/css">
    body {
      background-color: white;
      padding: 0px
    }

    #div1 {
      background-color: #ff5;
      width: 20px;
      height: 20px;
    }

    #div2 {
      margin-left: 20px;
      width: 20px;
      height: 20px;
      background-color: #f09;
    }
  </style>
  <body><div id="div1"></div><div id="div2"></div></body>
`;

// #f09
const ORIGINAL_COLOR = "rgb(255, 0, 153)";
// #ff5
const EXPECTED_COLOR = "rgb(255, 255, 85)";

add_task(function* () {
  info("Add the test tab, open the rule-view and select the test node");
  yield addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
  let {testActor, inspector, view} = yield openRuleView();
  yield selectNode("#div2", inspector);

  info("Get the background-color property from the rule-view");
  let property = getRuleViewProperty(view, "#div2", "background-color");
  let swatch = property.valueSpan.querySelector(".ruleview-colorswatch");
  ok(swatch, "Color swatch is displayed for the bg-color property");

  info("Open the eyedropper from the colorpicker tooltip");
  yield openEyedropper(view, swatch);

  let tooltip = view.tooltips.colorPicker.tooltip;
  ok(!tooltip.isVisible(), "color picker tooltip is closed after opening eyedropper");

  info("Test that pressing escape dismisses the eyedropper");
  yield testESC(swatch, inspector, testActor);

  info("Open the eyedropper again");
  yield openEyedropper(view, swatch);

  info("Test that a color can be selected with the eyedropper");
  yield testSelect(view, swatch, inspector, testActor);

  let onHidden = tooltip.once("hidden");
  tooltip.hide();
  yield onHidden;
  ok(!tooltip.isVisible(), "color picker tooltip is closed");

  yield waitForTick();
});

function* testESC(swatch, inspector, testActor) {
  info("Press escape");
  let onCanceled = new Promise(resolve => {
    inspector.inspector.once("color-pick-canceled", resolve);
  });
  yield testActor.synthesizeKey({key: "VK_ESCAPE", options: {}});
  yield onCanceled;

  let color = swatch.style.backgroundColor;
  is(color, ORIGINAL_COLOR, "swatch didn't change after pressing ESC");
}

function* testSelect(view, swatch, inspector, testActor) {
  info("Click at x:10px y:10px");
  let onPicked = new Promise(resolve => {
    inspector.inspector.once("color-picked", resolve);
  });
  // The change to the content is done async after rule view change
  let onRuleViewChanged = view.once("ruleview-changed");

  yield testActor.synthesizeMouse({selector: "html", x: 10, y: 10,
                                   options: {type: "mousemove"}});
  yield testActor.synthesizeMouse({selector: "html", x: 10, y: 10,
                                   options: {type: "mousedown"}});
  yield testActor.synthesizeMouse({selector: "html", x: 10, y: 10,
                                   options: {type: "mouseup"}});

  yield onPicked;
  yield onRuleViewChanged;

  let color = swatch.style.backgroundColor;
  is(color, EXPECTED_COLOR, "swatch changed colors");

  is((yield getComputedStyleProperty("div", null, "background-color")),
     EXPECTED_COLOR,
     "div's color set to body color after dropper");
}

function* openEyedropper(view, swatch) {
  let tooltip = view.tooltips.colorPicker.tooltip;

  info("Click on the swatch");
  let onColorPickerReady = view.tooltips.colorPicker.once("ready");
  swatch.click();
  yield onColorPickerReady;

  let dropperButton = tooltip.doc.querySelector("#eyedropper-button");

  info("Click on the eyedropper icon");
  let onOpened = tooltip.once("eyedropper-opened");
  dropperButton.click();
  yield onOpened;
}