DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6057e17f856)

VCS Links

Line Code
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
<?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();

      Components.utils.import("resource://gre/modules/NetUtil.jsm");

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

      // Add a bookmark and tag it.
      let uri1 = NetUtil.newURI("http://www1.mozilla.org/");
      let id1 = PlacesUtils.bookmarks.insertBookmark(
        PlacesUtils.toolbarFolderId, uri1,
        PlacesUtils.bookmarks.DEFAULT_INDEX, "mozilla"
      );
      PlacesUtils.tagging.tagURI(uri1, tags);

      // Add a second bookmark so that tags won't disappear when unchecked.
      let uri2 = NetUtil.newURI("http://www2.mozilla.org/");
      let id2 = PlacesUtils.bookmarks.insertBookmark(
        PlacesUtils.toolbarFolderId, uri2,
        PlacesUtils.bookmarks.DEFAULT_INDEX, "mozilla"
      );
      PlacesUtils.tagging.tagURI(uri2, tags);

      // Init panel.
      ok(gEditItemOverlay, "gEditItemOverlay is in context");
      gEditItemOverlay.initPanel(id1);
      ok(gEditItemOverlay._initialized, "gEditItemOverlay is initialized");

      // Wait for the tags selector to be open.
      let tagsSelectorRow = document.getElementById("editBMPanel_tagsSelectorRow");
      tagsSelectorRow.addEventListener("DOMAttrModified", function (event) {
        tagsSelectorRow.removeEventListener("DOMAttrModified", arguments.callee, false);
        SimpleTest.executeSoon(function () {
          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;
            SimpleTest.isnot(listItem, null, "Valid listItem found");

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

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

            // Uncheck the tag.
            listItem.checked = false;
            SimpleTest.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;
            SimpleTest.isnot(newItem, null, "Valid new listItem found");
            SimpleTest.ok(!newItem.checked, "New listItem is unchecked " + i);
            SimpleTest.is(newItem.label, selectedTag,
                          "Correct tag is still selected");

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

          // Remove the second bookmark, then nuke some of the tags.
          PlacesUtils.bookmarks.removeItem(id2);

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

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

            SimpleTest.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);
            SimpleTest.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;
            SimpleTest.isnot(newItem, null, "Valid new listItem found");
            SimpleTest.ok(newItem.checked, "New listItem is checked " + i);
            SimpleTest.is(tagsSelector.selectedItem.label,
                          tags[Math.min(i + 1, tags.length - 2)],
                          "The next tag is now selected");
          }

          // Cleanup.
          PlacesUtils.bookmarks.removeItem(id1);

          SimpleTest.finish();
        });
      }, false);

      // Open the tags selector.
      document.getElementById("editBMPanel_tagsSelectorExpander").doCommand();
    }
  ]]>
  </script>

</window>