summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/chrome/test_bug631374_tags_selector_scroll.xul
blob: b1d73017f6cbb576da1972ff5eca0cc9c47fc3d5 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?xml version="1.0"?>

<!-- Any copyright is dedicated to the Public Domain.
     http://creativecommons.org/publicdomain/zero/1.0/ -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>

<?xml-stylesheet href="chrome://browser/skin/places/editBookmarkOverlay.css"?>
<?xml-stylesheet href="chrome://browser/content/places/places.css"?>
<?xml-stylesheet href="chrome://browser/skin/places/places.css"?>

<?xul-overlay href="chrome://browser/content/places/placesOverlay.xul"?>
<?xul-overlay href="chrome://browser/content/places/editBookmarkOverlay.xul"?>

<!DOCTYPE window [
  <!ENTITY % editBookmarkOverlayDTD SYSTEM "chrome://browser/locale/places/editBookmarkOverlay.dtd">
  %editBookmarkOverlayDTD;
]>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        title="Bug 631374 - Editing tags in the selector scrolls up the listbox"
        onload="runTest();">

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
  <script type="application/javascript"
          src="chrome://browser/content/places/editBookmarkOverlay.js"/>

  <body xmlns="http://www.w3.org/1999/xhtml" />

  <vbox id="editBookmarkPanelContent"/>

  <script type="application/javascript">
  <![CDATA[

     /**
      * This test checks that editing tags doesn't scroll the tags selector
      * listbox to wrong positions.
      */

    function runTest() {
      SimpleTest.waitForExplicitFinish();

      Task.spawn(function* () {
        let bs = PlacesUtils.bookmarks;

        let tags = ["a", "b", "c", "d", "e", "f", "g",
                    "h", "i", "l", "m", "n", "o", "p"];

        // Add a bookmark and tag it.
        let uri1 = Services.io.newURI("http://www1.mozilla.org/", null, null);
        let bm1 = yield bs.insert({
          parentGuid: bs.toolbarGuid,
          index: bs.DEFAULT_INDEX,
          type: bs.TYPE_BOOKMARK,
          title: "mozilla",
          url: uri1.spec
        });
        PlacesUtils.tagging.tagURI(uri1, tags);

        // Add a second bookmark so that tags won't disappear when unchecked.
        let uri2 = Services.io.newURI("http://www2.mozilla.org/", null, null);
        let bm2 = yield bs.insert({
          parentGuid: bs.toolbarGuid,
          index: bs.DEFAULT_INDEX,
          type: bs.TYPE_BOOKMARK,
          title: "mozilla",
          url: uri2.spec
        });
        PlacesUtils.tagging.tagURI(uri2, tags);

        // Init panel.
        ok(gEditItemOverlay, "gEditItemOverlay is in context");
        let node1 = yield PlacesUIUtils.promiseNodeLikeFromFetchInfo(bm1);
        gEditItemOverlay.initPanel({ node: node1 });
        ok(gEditItemOverlay.initialized, "gEditItemOverlay is initialized");

        yield openTagSelector();
        let tagsSelector = document.getElementById("editBMPanel_tagsSelector");

        // Go by two so there is some untouched tag in the middle.
        for (let i = 8; i < tags.length; i += 2) {
          tagsSelector.selectedIndex = i;
          let listItem = tagsSelector.selectedItem;
          isnot(listItem, null, "Valid listItem found");

          tagsSelector.ensureElementIsVisible(listItem);
          let visibleIndex = tagsSelector.getIndexOfFirstVisibleRow();

          ok(listItem.checked, "Item is checked " + i);
          let selectedTag = listItem.label;

          // Uncheck the tag.
          listItem.checked = false;
          is(visibleIndex, tagsSelector.getIndexOfFirstVisibleRow(),
             "Scroll position did not change");

          // The listbox is rebuilt, so we have to get the new element.
          let newItem = tagsSelector.selectedItem;
          isnot(newItem, null, "Valid new listItem found");
          ok(!newItem.checked, "New listItem is unchecked " + i);
          is(newItem.label, selectedTag, "Correct tag is still selected");

          // Check the tag.
          newItem.checked = true;
          is(visibleIndex, tagsSelector.getIndexOfFirstVisibleRow(),
             "Scroll position did not change");
        }

        // Remove the second bookmark, then nuke some of the tags.
        yield bs.remove(bm2.guid);

        // Doing this backwords tests more interesting paths.
        for (let i = tags.length - 1; i >= 0 ; i -= 2) {
          tagsSelector.selectedIndex = i;
          let listItem = tagsSelector.selectedItem;
          isnot(listItem, null, "Valid listItem found");

          tagsSelector.ensureElementIsVisible(listItem);
          let firstVisibleTag = tags[tagsSelector.getIndexOfFirstVisibleRow()];

          ok(listItem.checked, "Item is checked " + i);
          let selectedTag = listItem.label;

          // Uncheck the tag.
          listItem.checked = false;

          // Ensure the first visible tag is still visible in the list.
          let firstVisibleIndex = tagsSelector.getIndexOfFirstVisibleRow();
          let lastVisibleIndex = firstVisibleIndex + tagsSelector.getNumberOfVisibleRows() -1;
          let expectedTagIndex = tags.indexOf(firstVisibleTag);
          ok(expectedTagIndex >= firstVisibleIndex &&
             expectedTagIndex <= lastVisibleIndex,
             "Scroll position is correct");

          // The listbox is rebuilt, so we have to get the new element.
          let newItem = tagsSelector.selectedItem;
          isnot(newItem, null, "Valid new listItem found");
          ok(newItem.checked, "New listItem is checked " + i);
          is(tagsSelector.selectedItem.label,
             tags[Math.min(i + 1, tags.length - 2)],
             "The next tag is now selected");
        }

        // Cleanup.
        yield bs.remove(bm1.guid);
      }).then(SimpleTest.finish).catch(alert);
    }

    function openTagSelector() {
      // Wait for the tags selector to be open.
      let promise = new Promise(resolve => {
        let row = document.getElementById("editBMPanel_tagsSelectorRow");
        row.addEventListener("DOMAttrModified", function onAttrModified() {
          row.removeEventListener("DOMAttrModified", onAttrModified);
          resolve();
        });
      });

      // Open the tags selector.
      document.getElementById("editBMPanel_tagsSelectorExpander").doCommand();

      return promise;
    }
  ]]>
  </script>

</window>