summaryrefslogtreecommitdiff
path: root/browser/devtools/scratchpad/test/browser_scratchpad_bug650345_find_ui.js
blob: 81e45aeef7f9fc719ab31dd9608f03eb592fbc57 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function test()
{
  waitForExplicitFinish();

  gBrowser.selectedTab = gBrowser.addTab();
  gBrowser.selectedBrowser.addEventListener("load", function browserLoad() {
    gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true);
    openScratchpad(runTests);
  }, true);

  content.location = "data:text/html,<p>test the Find feature in Scratchpad";
}

function runTests(aWindow, aScratchpad)
{
  let editor = aScratchpad.editor;
  let text = "foobar bug650345\nBug650345 bazbaz\nfoobar omg\ntest";
  editor.setText(text);

  let needle = "foobar";
  editor.setSelection(0, needle.length);

  let oldPrompt = Services.prompt;
  Services.prompt = {
    prompt: function() { return true; },
  };

  let findKey = "F";
  info("test Ctrl/Cmd-" + findKey + " (find)");
  EventUtils.synthesizeKey(findKey, {accelKey: true}, aWindow);
  let selection = editor.getSelection();
  let newIndex = text.indexOf(needle, needle.length);
  is(selection.start, newIndex, "selection.start is correct");
  is(selection.end, newIndex + needle.length, "selection.end is correct");

  info("test cmd_find");
  aWindow.goDoCommand("cmd_find");
  selection = editor.getSelection();
  is(selection.start, 0, "selection.start is correct");
  is(selection.end, needle.length, "selection.end is correct");

  let findNextKey = Services.appinfo.OS == "Darwin" ? "G" : "VK_F3";
  let findNextKeyOptions = Services.appinfo.OS == "Darwin" ?
                           {accelKey: true} : {};

  info("test " + findNextKey + " (findNext)");
  EventUtils.synthesizeKey(findNextKey, findNextKeyOptions, aWindow);
  selection = editor.getSelection();
  is(selection.start, newIndex, "selection.start is correct");
  is(selection.end, newIndex + needle.length, "selection.end is correct");

  info("test cmd_findAgain");
  aWindow.goDoCommand("cmd_findAgain");
  selection = editor.getSelection();
  is(selection.start, 0, "selection.start is correct");
  is(selection.end, needle.length, "selection.end is correct");

  let findPreviousKey = Services.appinfo.OS == "Darwin" ? "G" : "VK_F3";
  let findPreviousKeyOptions = Services.appinfo.OS == "Darwin" ?
                           {accelKey: true, shiftKey: true} : {shiftKey: true};

  info("test " + findPreviousKey + " (findPrevious)");
  EventUtils.synthesizeKey(findPreviousKey, findPreviousKeyOptions, aWindow);
  selection = editor.getSelection();
  is(selection.start, newIndex, "selection.start is correct");
  is(selection.end, newIndex + needle.length, "selection.end is correct");

  info("test cmd_findPrevious");
  aWindow.goDoCommand("cmd_findPrevious");
  selection = editor.getSelection();
  is(selection.start, 0, "selection.start is correct");
  is(selection.end, needle.length, "selection.end is correct");

  needle = "BAZbaz";
  newIndex = text.toLowerCase().indexOf(needle.toLowerCase());

  Services.prompt = {
    prompt: function(aWindow, aTitle, aMessage, aValue) {
      aValue.value = needle;
      return true;
    },
  };

  info("test Ctrl/Cmd-" + findKey + " (find) with a custom value");
  EventUtils.synthesizeKey(findKey, {accelKey: true}, aWindow);
  selection = editor.getSelection();
  is(selection.start, newIndex, "selection.start is correct");
  is(selection.end, newIndex + needle.length, "selection.end is correct");

  Services.prompt = oldPrompt;

  finish();
}