summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutconfig/test/browser/browser_clipboard.js
blob: bcaa2c03284f736def1dc1c8b1ec8a4dd47fa7f9 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["test.aboutconfig.copy.false", false],
      ["test.aboutconfig.copy.number", 10],
      ["test.aboutconfig.copy.spaces.1", " "],
      ["test.aboutconfig.copy.spaces.2", "  "],
      ["test.aboutconfig.copy.spaces.3", "   "],
      ["test.aboutconfig.copy.string", "010.5"],
    ],
  });
});

add_task(async function test_copy() {
  await AboutConfigTest.withNewTab(async function () {
    for (let [name, expectedString] of [
      [PREF_BOOLEAN_DEFAULT_TRUE, "true"],
      [PREF_BOOLEAN_USERVALUE_TRUE, "true"],
      [PREF_STRING_DEFAULT_EMPTY, ""],
      ["test.aboutconfig.copy.false", "false"],
      ["test.aboutconfig.copy.number", "10"],
      ["test.aboutconfig.copy.spaces.1", " "],
      ["test.aboutconfig.copy.spaces.2", "  "],
      ["test.aboutconfig.copy.spaces.3", "   "],
      ["test.aboutconfig.copy.string", "010.5"],
    ]) {
      // Limit the number of preferences shown so all the rows are visible.
      this.search(name);
      let row = this.getRow(name);

      let selectText = async target => {
        let { width, height } = target.getBoundingClientRect();
        EventUtils.synthesizeMouse(
          target,
          1,
          1,
          { type: "mousedown" },
          this.browser.contentWindow
        );
        EventUtils.synthesizeMouse(
          target,
          width - 1,
          height - 1,
          { type: "mousemove" },
          this.browser.contentWindow
        );
        EventUtils.synthesizeMouse(
          target,
          width - 1,
          height - 1,
          { type: "mouseup" },
          this.browser.contentWindow
        );
      };

      // Drag across the name cell.
      await selectText(row.nameCell);
      Assert.ok(row.nameCell.contains(this.window.getSelection().anchorNode));
      await SimpleTest.promiseClipboardChange(name, async () => {
        await BrowserTestUtils.synthesizeKey(
          "c",
          { accelKey: true },
          this.browser
        );
      });

      // Drag across the value cell.
      await selectText(row.valueCell);
      let selection = this.window.getSelection();
      Assert.ok(row.valueCell.contains(selection.anchorNode));

      if (expectedString !== "") {
        // Non-empty values should have a selection.
        Assert.ok(!selection.isCollapsed);
        await SimpleTest.promiseClipboardChange(expectedString, async () => {
          await BrowserTestUtils.synthesizeKey(
            "c",
            { accelKey: true },
            this.browser
          );
        });
      } else {
        // Nothing is selected for an empty value.
        Assert.equal(selection.toString(), "");
      }
    }
  });
});

add_task(async function test_copy_multiple() {
  await AboutConfigTest.withNewTab(async function () {
    // Lines are separated by a single LF character on all platforms.
    let expectedString =
      "test.aboutconfig.copy.false\tfalse\t\n" +
      "test.aboutconfig.copy.number\t10\t\n" +
      "test.aboutconfig.copy.spaces.1\t \t\n" +
      "test.aboutconfig.copy.spaces.2\t  \t\n" +
      "test.aboutconfig.copy.spaces.3\t   \t\n" +
      "test.aboutconfig.copy.string\t010.5";

    this.search("test.aboutconfig.copy.");
    let startRow = this.getRow("test.aboutconfig.copy.false");
    let endRow = this.getRow("test.aboutconfig.copy.string");
    let { width, height } = endRow.valueCell.getBoundingClientRect();

    // Drag from the top left of the first row to the bottom right of the last.
    EventUtils.synthesizeMouse(
      startRow.nameCell,
      1,
      1,
      { type: "mousedown" },
      this.browser.contentWindow
    );

    EventUtils.synthesizeMouse(
      endRow.valueCell,
      width - 1,
      height - 1,
      { type: "mousemove" },
      this.browser.contentWindow
    );
    EventUtils.synthesizeMouse(
      endRow.valueCell,
      width - 1,
      height - 1,
      { type: "mouseup" },
      this.browser.contentWindow
    );

    await SimpleTest.promiseClipboardChange(expectedString, async () => {
      await BrowserTestUtils.synthesizeKey(
        "c",
        { accelKey: true },
        this.browser
      );
    });
  });
});