summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_bug631374_tags_selector_scroll.js
blob: 5d35356a69ccc07bdb93a7b0c5da6e22649f3fbe (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
/**
 * This test checks that editing tags doesn't scroll the tags selector
 * listbox to wrong positions.
 */

const TEST_URL = "about:buildconfig";

function scrolledIntoView(item, parentItem) {
  let itemRect = item.getBoundingClientRect();
  let parentItemRect = parentItem.getBoundingClientRect();
  let pointInView = y => parentItemRect.top < y && y < parentItemRect.bottom;

  // Partially visible items are also considered visible.
  return pointInView(itemRect.top) || pointInView(itemRect.bottom);
}

add_task(async function runTest() {
  await PlacesUtils.bookmarks.eraseEverything();
  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(TEST_URL);
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    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/");
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    title: "mozilla",
    url: uri2.spec,
  });
  PlacesUtils.tagging.tagURI(uri2, tags);

  let win = await BrowserTestUtils.openNewBrowserWindow();
  await BrowserTestUtils.openNewForegroundTab({
    gBrowser: win.gBrowser,
    opening: TEST_URL,
    waitForStateStop: true,
  });

  registerCleanupFunction(async () => {
    bookmarkPanel.removeAttribute("animate");
    await BrowserTestUtils.closeWindow(win);
    await PlacesUtils.bookmarks.eraseEverything();
  });

  win.StarUI._createPanelIfNeeded();
  let bookmarkPanel = win.document.getElementById("editBookmarkPanel");
  bookmarkPanel.setAttribute("animate", false);
  let shownPromise = promisePopupShown(bookmarkPanel);

  let bookmarkStar = win.BookmarkingUI.star;
  bookmarkStar.click();

  await shownPromise;

  // Init panel.
  ok(win.gEditItemOverlay, "gEditItemOverlay is in context");
  ok(win.gEditItemOverlay.initialized, "gEditItemOverlay is initialized");

  await openTagSelector(win);
  let tagsSelector = win.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 scrollTop = tagsSelector.scrollTop;

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

    // Uncheck the tag.
    let promise = BrowserTestUtils.waitForEvent(
      tagsSelector,
      "BookmarkTagsSelectorUpdated"
    );
    EventUtils.synthesizeMouseAtCenter(listItem.firstElementChild, {}, win);
    await promise;
    is(scrollTop, tagsSelector.scrollTop, "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.hasAttribute("checked"), "New listItem is unchecked " + i);
    is(newItem.label, selectedTag, "Correct tag is still selected");

    // Check the tag.
    promise = BrowserTestUtils.waitForEvent(
      tagsSelector,
      "BookmarkTagsSelectorUpdated"
    );
    EventUtils.synthesizeMouseAtCenter(newItem.firstElementChild, {}, win);
    await promise;
    is(scrollTop, tagsSelector.scrollTop, "Scroll position did not change");
  }

  // Remove the second bookmark, then nuke some of the tags.
  await PlacesUtils.bookmarks.remove(bm2);

  // Allow the tag updates to complete
  await PlacesTestUtils.promiseAsyncUpdates();

  // 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);

    ok(listItem.hasAttribute("checked"), "Item is checked " + i);

    // Uncheck the tag.
    let promise = BrowserTestUtils.waitForEvent(
      tagsSelector,
      "BookmarkTagsSelectorUpdated"
    );
    EventUtils.synthesizeMouseAtCenter(listItem.firstElementChild, {}, win);
    await promise;
  }

  let hiddenPromise = promisePopupHidden(bookmarkPanel);
  let doneButton = win.document.getElementById("editBookmarkPanelDoneButton");
  doneButton.click();
  await hiddenPromise;
  // Cleanup.
  await PlacesUtils.bookmarks.remove(bm1);
});

function openTagSelector(win) {
  let promise = BrowserTestUtils.waitForEvent(
    win.document.getElementById("editBMPanel_tagsSelector"),
    "BookmarkTagsSelectorUpdated"
  );
  // Open the tags selector.
  win.document.getElementById("editBMPanel_tagsSelectorExpander").doCommand();
  return promise;
}