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

const PRELOAD_PREF = "browser.newtab.preload";

gDirectorySource = "data:application/json," + JSON.stringify({
  "directory": [{
    url: "http://example.com/organic",
    type: "organic"
  }, {
    url: "http://localhost/sponsored",
    type: "sponsored"
  }]
});

add_task(function* () {
  yield pushPrefs([PRELOAD_PREF, false]);

  let originalReportSitesAction  = DirectoryLinksProvider.reportSitesAction;
  registerCleanupFunction(() => {
    DirectoryLinksProvider.reportSitesAction = originalReportSitesAction;
  });

  let expected = {};

  function expectReportSitesAction() {
    return new Promise(resolve => {
      DirectoryLinksProvider.reportSitesAction = function(sites, action, siteIndex) {
        let {link} = sites[siteIndex];
        is(link.type, expected.type, "got expected type");
        is(action, expected.action, "got expected action");
        is(NewTabUtils.pinnedLinks.isPinned(link), expected.pinned, "got expected pinned");
        resolve();
      }
    });
  }

  // Test that the last visible site (index 1) is reported
  let reportSitesPromise = expectReportSitesAction();
  expected.type = "sponsored";
  expected.action = "view";
  expected.pinned = false;
  yield* addNewTabPageTab();
  yield reportSitesPromise;

  // Click the pin button on the link in the 1th tile spot
  expected.action = "pin";
  // tiles become "history" when pinned
  expected.type = "history";
  expected.pinned = true;
  let pagesUpdatedPromise = whenPagesUpdated();
  reportSitesPromise = expectReportSitesAction();

  yield BrowserTestUtils.synthesizeMouseAtCenter(".newtab-cell + .newtab-cell .newtab-control-pin", {}, gBrowser.selectedBrowser);
  yield pagesUpdatedPromise;
  yield reportSitesPromise;

  // Unpin that link
  expected.action = "unpin";
  expected.pinned = false;
  pagesUpdatedPromise = whenPagesUpdated();
  reportSitesPromise = expectReportSitesAction();
  yield BrowserTestUtils.synthesizeMouseAtCenter(".newtab-cell + .newtab-cell .newtab-control-pin", {}, gBrowser.selectedBrowser);
  yield pagesUpdatedPromise;
  yield reportSitesPromise;

  // Block the site in the 0th tile spot
  expected.type = "organic";
  expected.action = "block";
  expected.pinned = false;
  pagesUpdatedPromise = whenPagesUpdated();
  reportSitesPromise = expectReportSitesAction();
  yield BrowserTestUtils.synthesizeMouseAtCenter(".newtab-site .newtab-control-block", {}, gBrowser.selectedBrowser);
  yield pagesUpdatedPromise;
  yield reportSitesPromise;

  // Click the 1th link now in the 0th tile spot
  expected.type = "history";
  expected.action = "click";
  reportSitesPromise = expectReportSitesAction();
  yield BrowserTestUtils.synthesizeMouseAtCenter(".newtab-site", {}, gBrowser.selectedBrowser);
  yield reportSitesPromise;
});