summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/favicons/test_expire_on_new_icons.js
blob: d5a7c42ba3acf50050fbcfb2d04e8e792a0cf053 (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Tests that adding new icons for a page expired old ones.
 */

add_task(async function test_expire_associated() {
  const TEST_URL = "http://mozilla.com/";
  await PlacesTestUtils.addVisits(TEST_URL);
  const TEST_URL2 = "http://test.mozilla.com/";
  await PlacesTestUtils.addVisits(TEST_URL2);

  let favicons = [
    {
      name: "favicon-normal16.png",
      mimeType: "image/png",
      expired: true,
    },
    {
      name: "favicon-normal32.png",
      mimeType: "image/png",
    },
    {
      name: "favicon-big64.png",
      mimeType: "image/png",
    },
  ];

  for (let icon of favicons) {
    let data = readFileData(do_get_file(icon.name));
    PlacesUtils.favicons.replaceFaviconData(
      NetUtil.newURI(TEST_URL + icon.name),
      data,
      icon.mimeType
    );
    await setFaviconForPage(TEST_URL, TEST_URL + icon.name);
    if (icon.expired) {
      await expireIconRelationsForPage(TEST_URL);
      // Add the same icon to another page.
      PlacesUtils.favicons.replaceFaviconData(
        NetUtil.newURI(TEST_URL + icon.name),
        data,
        icon.mimeType,
        icon.expire
      );
      await setFaviconForPage(TEST_URL2, TEST_URL + icon.name);
    }
  }

  // Only the second and the third icons should have survived.
  Assert.equal(
    await getFaviconUrlForPage(TEST_URL, 16),
    TEST_URL + favicons[1].name,
    "Should retrieve the 32px icon, not the 16px one."
  );
  Assert.equal(
    await getFaviconUrlForPage(TEST_URL, 64),
    TEST_URL + favicons[2].name,
    "Should retrieve the 64px icon"
  );

  // The expired icon for page 2 should have survived.
  Assert.equal(
    await getFaviconUrlForPage(TEST_URL2, 16),
    TEST_URL + favicons[0].name,
    "Should retrieve the expired 16px icon"
  );
});

add_task(async function test_expire_root() {
  async function countEntries(tablename) {
    await PlacesTestUtils.promiseAsyncUpdates();
    let db = await PlacesUtils.promiseDBConnection();
    let rows = await db.execute("SELECT * FROM " + tablename);
    return rows.length;
  }

  // Clear the database.
  let promise = promiseTopicObserved(PlacesUtils.TOPIC_FAVICONS_EXPIRED);
  PlacesUtils.favicons.expireAllFavicons();
  await promise;

  Assert.equal(await countEntries("moz_icons"), 0, "There should be no icons");

  let pageURI = NetUtil.newURI("http://root.mozilla.com/");
  await PlacesTestUtils.addVisits(pageURI);

  // Insert an expired icon.
  let iconURI = NetUtil.newURI(pageURI.spec + "favicon-normal16.png");
  PlacesUtils.favicons.replaceFaviconDataFromDataURL(
    iconURI,
    SMALLPNG_DATA_URI.spec,
    0,
    systemPrincipal
  );
  await setFaviconForPage(pageURI, iconURI);
  Assert.equal(
    await countEntries("moz_icons_to_pages"),
    1,
    "There should be 1 association"
  );
  // Set an expired time on the icon-page relation.
  await expireIconRelationsForPage(pageURI.spec);

  // Now insert a new root icon.
  let rootIconURI = NetUtil.newURI(pageURI.spec + "favicon.ico");
  PlacesUtils.favicons.replaceFaviconDataFromDataURL(
    rootIconURI,
    SMALLPNG_DATA_URI.spec,
    0,
    systemPrincipal
  );
  await setFaviconForPage(pageURI, rootIconURI);

  // Only the root icon should have survived.
  Assert.equal(
    await getFaviconUrlForPage(pageURI, 16),
    rootIconURI.spec,
    "Should retrieve the root icon."
  );
  Assert.equal(
    await countEntries("moz_icons_to_pages"),
    0,
    "There should be no associations"
  );
});

async function expireIconRelationsForPage(url) {
  // Set an expired time on the icon-page relation.
  await PlacesUtils.withConnectionWrapper("expireFavicon", async db => {
    await db.execute(
      `
      UPDATE moz_icons_to_pages SET expire_ms = 0
      WHERE page_id = (SELECT id FROM moz_pages_w_icons WHERE page_url = :url)
      `,
      { url }
    );
    // Also ensure the icon is not expired, here we should only replace entries
    // based on their association expiration, not the icon expiration.
    let count = (
      await db.execute(
        `
        SELECT count(*) FROM moz_icons
        WHERE expire_ms < strftime('%s','now','localtime','utc') * 1000
        `
      )
    )[0].getResultByIndex(0);
    Assert.equal(count, 0, "All the icons should have future expiration");
  });
}