summaryrefslogtreecommitdiffstats
path: root/toolkit/components/autocomplete/tests/unit/test_finalCompleteValueSelectedIndex.js
blob: 6556a26dc5834953eb81fca47a1c113bf374daee (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
function AutoCompleteResult(aResultValues) {
  this._values = aResultValues.map(x => x[0]);
  this._finalCompleteValues = aResultValues.map(x => x[1]);
}
AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);

var selectByWasCalled = false;
function AutoCompleteInput(aSearches) {
  this.searches = aSearches;
  this.popup.selectedIndex = 0;
  this.popup.selectBy = function(reverse, page) {
    Assert.equal(selectByWasCalled, false);
    selectByWasCalled = true;
    Assert.equal(reverse, false);
    Assert.equal(page, false);
    this.selectedIndex += (reverse ? -1 : 1) * (page ? 100 : 1);
  };
  this.completeSelectedIndex = true;
}
AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype);

add_test(function test_handleEnter_key() {
  let results = [
    ["mozilla.com", "http://www.mozilla.com"],
    ["mozilla.org", "http://www.mozilla.org"],
  ];
  // First check the case where we do select a value with the keyboard:
  doSearch("moz", results, function(aController) {
    Assert.equal(aController.input.textValue, "moz");
    Assert.equal(aController.getFinalCompleteValueAt(0), "http://www.mozilla.com");
    Assert.equal(aController.getFinalCompleteValueAt(1), "http://www.mozilla.org");

    Assert.equal(aController.input.popup.selectedIndex, 0);
    aController.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_DOWN);
    Assert.equal(aController.input.popup.selectedIndex, 1);
    // Simulate mouse interaction changing selectedIndex
    // ie NOT keyboard interaction:
    aController.input.popup.selectedIndex = 0;

    aController.handleEnter(false);
    // Verify that the keyboard-selected thing got inserted,
    // and not the mouse selection:
    Assert.equal(aController.input.textValue, "http://www.mozilla.org");
  });
});

add_test(function test_handleEnter_mouse() {
  let results = [
    ["mozilla.com", "http://www.mozilla.com"],
    ["mozilla.org", "http://www.mozilla.org"],
  ];
  // Then the case where we do not:
  doSearch("moz", results, function(aController) {
    Assert.equal(aController.input.textValue, "moz");
    Assert.equal(aController.getFinalCompleteValueAt(0), "http://www.mozilla.com");
    Assert.equal(aController.getFinalCompleteValueAt(1), "http://www.mozilla.org");

    Assert.equal(aController.input.popup.selectedIndex, 0);
    aController.input.popupOpen = true;
    // Simulate mouse interaction changing selectedIndex
    // ie NOT keyboard interaction:
    aController.input.popup.selectedIndex = 1;
    Assert.equal(selectByWasCalled, false);
    Assert.equal(aController.input.popup.selectedIndex, 1);

    aController.handleEnter(false);
    // Verify that the input stayed the same, because no selection was made
    // with the keyboard:
    Assert.equal(aController.input.textValue, "moz");
  });
});

add_test(function test_handleEnter_preselected() {
  let results = [
    ["mozilla.com", "http://www.mozilla.com"],
    ["mozilla.org", "http://www.mozilla.org"],
  ];
  // Then test a preselection.
  doSearch("moz", results, function(aController) {
    Assert.equal(aController.input.textValue, "moz");
    Assert.equal(aController.getFinalCompleteValueAt(0), "http://www.mozilla.com");
    Assert.equal(aController.getFinalCompleteValueAt(1), "http://www.mozilla.org");

    aController.setInitiallySelectedIndex(0);

    aController.handleEnter(false);
    // Verify that the input stayed the same, because no selection was made
    // with the keyboard:
    Assert.equal(aController.input.textValue, "http://www.mozilla.com");
  });
});

function doSearch(aSearchString, aResults, aOnCompleteCallback) {
  selectByWasCalled = false;
  let search = new AutoCompleteSearchBase(
    "search",
    new AutoCompleteResult(aResults)
  );
  registerAutoCompleteSearch(search);

  let controller = Cc["@mozilla.org/autocomplete/controller;1"].
                   getService(Ci.nsIAutoCompleteController);

  // Make an AutoCompleteInput that uses our searches and confirms results.
  let input = new AutoCompleteInput([ search.name ]);
  input.textValue = aSearchString;

  controller.input = input;
  controller.startSearch(aSearchString);

  input.onSearchComplete = function onSearchComplete() {
    aOnCompleteCallback(controller);

    // Clean up.
    unregisterAutoCompleteSearch(search);
    run_next_test();
  };
}