summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_bug581253.js
blob: 0c537c3d3d4d891e43a74d8b95785fc0220237bf (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

var testURL = "data:text/plain,nothing but plain text";
var testTag = "581253_tag";
var timerID = -1;

function test() {
  registerCleanupFunction(function() {
    PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId);
    if (timerID > 0) {
      clearTimeout(timerID);
    }
  });
  waitForExplicitFinish();

  let tab = gBrowser.selectedTab = gBrowser.addTab();
  tab.linkedBrowser.addEventListener("load", (function(event) {
    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);

    let uri = makeURI(testURL);
    let bmTxn =
      new PlacesCreateBookmarkTransaction(uri,
                                          PlacesUtils.unfiledBookmarksFolderId,
                                          -1, "", null, []);
    PlacesUtils.transactionManager.doTransaction(bmTxn);

    ok(PlacesUtils.bookmarks.isBookmarked(uri), "the test url is bookmarked");
    waitForStarChange(true, onStarred);
  }), true);

  content.location = testURL;
}

function waitForStarChange(aValue, aCallback) {
  let expectedStatus = aValue ? BookmarkingUI.STATUS_STARRED
                              : BookmarkingUI.STATUS_UNSTARRED;
  if (BookmarkingUI.status == BookmarkingUI.STATUS_UPDATING ||
      BookmarkingUI.status != expectedStatus) {
    info("Waiting for star button change.");
    setTimeout(waitForStarChange, 50, aValue, aCallback);
    return;
  }
  aCallback();
}

function onStarred() {
  is(BookmarkingUI.status, BookmarkingUI.STATUS_STARRED,
     "star button indicates that the page is bookmarked");

  let uri = makeURI(testURL);
  let tagTxn = new PlacesTagURITransaction(uri, [testTag]);
  PlacesUtils.transactionManager.doTransaction(tagTxn);

  StarUI.panel.addEventListener("popupshown", onPanelShown, false);
  BookmarkingUI.star.click();
}

function onPanelShown(aEvent) {
  if (aEvent.target == StarUI.panel) {
    StarUI.panel.removeEventListener("popupshown", arguments.callee, false);
    let tagsField = document.getElementById("editBMPanel_tagsField");
    ok(tagsField.value == testTag, "tags field value was set");
    tagsField.focus();

    StarUI.panel.addEventListener("popuphidden", onPanelHidden, false);
    let removeButton = document.getElementById("editBookmarkPanelRemoveButton");
    removeButton.click();
  }
}

function onPanelHidden(aEvent) {
  if (aEvent.target == StarUI.panel) {
    StarUI.panel.removeEventListener("popuphidden", arguments.callee, false);

    executeSoon(function() {
      ok(!PlacesUtils.bookmarks.isBookmarked(makeURI(testURL)),
         "the bookmark for the test url has been removed");
      is(BookmarkingUI.status, BookmarkingUI.STATUS_UNSTARRED,
         "star button indicates that the bookmark has been removed");
      gBrowser.removeCurrentTab();
      PlacesTestUtils.clearHistory().then(finish);
    });
  }
}