summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/xpcshell/test_WallpaperFeed.js
blob: c6c12c17bff5d9b429a9c3bf261e8592698fb98f (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { WallpaperFeed } = ChromeUtils.importESModule(
  "resource://activity-stream/lib/WallpaperFeed.sys.mjs"
);

const { actionCreators: ac, actionTypes: at } = ChromeUtils.importESModule(
  "resource://activity-stream/common/Actions.mjs"
);

ChromeUtils.defineESModuleGetters(this, {
  Utils: "resource://services-settings/Utils.sys.mjs",
  sinon: "resource://testing-common/Sinon.sys.mjs",
});

const PREF_WALLPAPERS_ENABLED =
  "browser.newtabpage.activity-stream.newtabWallpapers.enabled";

add_task(async function test_construction() {
  let feed = new WallpaperFeed();

  info("WallpaperFeed constructor should create initial values");

  Assert.ok(feed, "Could construct a WallpaperFeed");
  Assert.ok(feed.loaded === false, "WallpaperFeed is not loaded");
  Assert.ok(
    feed.wallpaperClient === "",
    "wallpaperClient is initialized as an empty string"
  );
  Assert.ok(
    feed.wallpaperDB === "",
    "wallpaperDB is initialized as an empty string"
  );
  Assert.ok(
    feed.baseAttachmentURL === "",
    "baseAttachmentURL is initialized as an empty string"
  );
});

add_task(async function test_onAction_INIT() {
  let sandbox = sinon.createSandbox();
  let feed = new WallpaperFeed();
  Services.prefs.setBoolPref(PREF_WALLPAPERS_ENABLED, true);
  const attachment = {
    attachment: {
      location: "attachment",
    },
  };
  sandbox.stub(feed, "RemoteSettings").returns({
    get: () => [attachment],
    on: () => {},
  });
  sandbox.stub(Utils, "SERVER_URL").returns("http://localhost:8888/v1");
  feed.store = {
    dispatch: sinon.spy(),
  };
  sandbox.stub(feed, "fetch").resolves({
    json: () => ({
      capabilities: {
        attachments: {
          base_url: "http://localhost:8888/base_url/",
        },
      },
    }),
  });

  info("WallpaperFeed.onAction INIT should initialize wallpapers");

  await feed.onAction({
    type: at.INIT,
  });

  Assert.ok(feed.store.dispatch.calledOnce);
  Assert.ok(
    feed.store.dispatch.calledWith(
      ac.BroadcastToContent({
        type: at.WALLPAPERS_SET,
        data: [
          {
            ...attachment,
            wallpaperUrl: "http://localhost:8888/base_url/attachment",
          },
        ],
        meta: {
          isStartup: true,
        },
      })
    )
  );
  Services.prefs.clearUserPref(PREF_WALLPAPERS_ENABLED);
  sandbox.restore();
});

add_task(async function test_onAction_PREF_CHANGED() {
  let sandbox = sinon.createSandbox();
  let feed = new WallpaperFeed();
  Services.prefs.setBoolPref(PREF_WALLPAPERS_ENABLED, true);
  sandbox.stub(feed, "wallpaperSetup").returns();

  info("WallpaperFeed.onAction PREF_CHANGED should call wallpaperSetup");

  feed.onAction({
    type: at.PREF_CHANGED,
    data: { name: "newtabWallpapers.enabled" },
  });

  Assert.ok(feed.wallpaperSetup.calledOnce);
  Assert.ok(feed.wallpaperSetup.calledWith(false));

  Services.prefs.clearUserPref(PREF_WALLPAPERS_ENABLED);
  sandbox.restore();
});