summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_graphs-03.js
blob: b44d4620ac46ec753c4f8c322fc1177ae7538cfb (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that graph widgets can handle clients getting/setting the
// selection or cursor.

const LineGraphWidget = require("devtools/client/shared/widgets/LineGraphWidget");

add_task(function* () {
  yield addTab("about:blank");
  yield performTest();
  gBrowser.removeCurrentTab();
});

function* performTest() {
  let [host,, doc] = yield createHost();
  let graph = new LineGraphWidget(doc.body, "fps");
  yield graph.once("ready");

  yield testSelection(graph);
  yield testCursor(graph);

  yield graph.destroy();
  host.destroy();
}

function* testSelection(graph) {
  ok(graph.getSelection().start === null,
    "The graph's selection should initially have a null start value.");
  ok(graph.getSelection().end === null,
    "The graph's selection should initially have a null end value.");
  ok(!graph.hasSelection(),
    "There shouldn't initially be any selection.");

  let selected = graph.once("selecting");
  graph.setSelection({ start: 100, end: 200 });

  yield selected;
  ok(true, "A 'selecting' event has been fired.");

  ok(graph.hasSelection(),
    "There should now be a selection.");
  is(graph.getSelection().start, 100,
    "The graph's selection now has an updated start value.");
  is(graph.getSelection().end, 200,
    "The graph's selection now has an updated end value.");

  let thrown;
  try {
    graph.setSelection({ start: null, end: null });
  } catch (e) {
    thrown = true;
  }
  ok(thrown, "Setting a null selection shouldn't work.");

  ok(graph.hasSelection(),
    "There should still be a selection.");

  let deselected = graph.once("deselecting");
  graph.dropSelection();

  yield deselected;
  ok(true, "A 'deselecting' event has been fired.");

  ok(!graph.hasSelection(),
    "There shouldn't be any selection anymore.");
  ok(graph.getSelection().start === null,
    "The graph's selection now has a null start value.");
  ok(graph.getSelection().end === null,
    "The graph's selection now has a null end value.");
}

function* testCursor(graph) {
  ok(graph.getCursor().x === null,
    "The graph's cursor should initially have a null X value.");
  ok(graph.getCursor().y === null,
    "The graph's cursor should initially have a null Y value.");
  ok(!graph.hasCursor(),
    "There shouldn't initially be any cursor.");

  graph.setCursor({ x: 100, y: 50 });

  ok(graph.hasCursor(),
    "There should now be a cursor.");
  is(graph.getCursor().x, 100,
    "The graph's cursor now has an updated start value.");
  is(graph.getCursor().y, 50,
    "The graph's cursor now has an updated end value.");

  let thrown;
  try {
    graph.setCursor({ x: null, y: null });
  } catch (e) {
    thrown = true;
  }
  ok(thrown, "Setting a null cursor shouldn't work.");

  ok(graph.hasCursor(),
    "There should still be a cursor.");

  graph.dropCursor();

  ok(!graph.hasCursor(),
    "There shouldn't be any cursor anymore.");
  ok(graph.getCursor().x === null,
    "The graph's cursor now has a null start value.");
  ok(graph.getCursor().y === null,
    "The graph's cursor now has a null end value.");
}