summaryrefslogtreecommitdiffstats
path: root/browser/components/pagedata/tests/browser/browser_pagedata_cache.js
blob: e41b4ea2f857bfb3cac0ece31a99c2d8c93f183f (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests for the page data cache.
 */

const TEST_URL =
  "data:text/html," +
  encodeURIComponent(`
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="twitter:card" content="summary_large_image">
      <meta name="twitter:site" content="@nytimes">
      <meta name="twitter:creator" content="@SarahMaslinNir">
      <meta name="twitter:title" content="Parade of Fans for Houston’s Funeral">
      <meta name="twitter:description" content="NEWARK - The guest list and parade of limousines">
      <meta name="twitter:image" content="http://graphics8.nytimes.com/images/2012/02/19/us/19whitney-span/19whitney-span-articleLarge.jpg">
    </head>
    <body>
    </body>
    </html>
`);

/**
 * Runs a task with a new page loaded into a tab in a new browser window.
 *
 * @param {string} url
 *   The url to load.
 * @param {Function} task
 *   The task to run. May return a promise.
 */
async function withBrowserInNewWindow(url, task) {
  let newWin = await BrowserTestUtils.openNewBrowserWindow();
  let tab = await BrowserTestUtils.openNewForegroundTab(newWin.gBrowser, url);
  await task(tab.linkedBrowser);
  await BrowserTestUtils.closeWindow(newWin);
}

add_task(async function test_pagedata_cache() {
  let promise = PageDataService.once("page-data");

  Assert.equal(
    PageDataService.getCached(TEST_URL),
    null,
    "Should be no data cached."
  );

  await BrowserTestUtils.withNewTab(TEST_URL, async () => {
    let pageData = await promise;

    Assert.deepEqual(
      PageDataService.getCached(TEST_URL),
      pageData,
      "Should return the same data from the cache"
    );

    delete pageData.date;

    Assert.deepEqual(
      pageData,
      {
        url: TEST_URL,
        siteName: "@nytimes",
        description: "NEWARK - The guest list and parade of limousines",
        image:
          "http://graphics8.nytimes.com/images/2012/02/19/us/19whitney-span/19whitney-span-articleLarge.jpg",
        data: {},
      },
      "Should have returned the right data"
    );
  });

  Assert.equal(
    PageDataService.getCached(TEST_URL),
    null,
    "Data should no longer be cached."
  );

  promise = PageDataService.once("page-data");

  // Checks that closing a window containing a tracked tab stops tracking the tab.
  await withBrowserInNewWindow(TEST_URL, async () => {
    let pageData = await promise;

    Assert.deepEqual(
      PageDataService.getCached(TEST_URL),
      pageData,
      "Should return the same data from the cache"
    );

    delete pageData.date;
    Assert.deepEqual(
      pageData,
      {
        url: TEST_URL,
        siteName: "@nytimes",
        description: "NEWARK - The guest list and parade of limousines",
        image:
          "http://graphics8.nytimes.com/images/2012/02/19/us/19whitney-span/19whitney-span-articleLarge.jpg",
        data: {},
      },
      "Should have returned the right data"
    );
  });

  Assert.equal(
    PageDataService.getCached(TEST_URL),
    null,
    "Data should no longer be cached."
  );

  let actor = {};
  PageDataService.lockEntry(actor, TEST_URL);

  promise = PageDataService.once("page-data");

  // Closing a tracked tab shouldn't expire the data here as we have another lock.
  await BrowserTestUtils.withNewTab(TEST_URL, async () => {
    await promise;
  });

  promise = PageDataService.once("page-data");

  // Closing a window with a tracked tab shouldn't expire the data here as we have another lock.
  await withBrowserInNewWindow(TEST_URL, async () => {
    await promise;
  });

  let cached = PageDataService.getCached(TEST_URL);
  delete cached.date;
  Assert.deepEqual(
    cached,
    {
      url: TEST_URL,
      siteName: "@nytimes",
      description: "NEWARK - The guest list and parade of limousines",
      image:
        "http://graphics8.nytimes.com/images/2012/02/19/us/19whitney-span/19whitney-span-articleLarge.jpg",
      data: {},
    },
    "Entry should still be cached"
  );

  PageDataService.unlockEntry(actor, TEST_URL);

  Assert.equal(
    PageDataService.getCached(TEST_URL),
    null,
    "Data should no longer be cached."
  );
});