tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_WallpaperFeed.js (8064B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 ChromeUtils.defineESModuleGetters(this, {
      7  actionCreators: "resource://newtab/common/Actions.mjs",
      8  actionTypes: "resource://newtab/common/Actions.mjs",
      9  Utils: "resource://services-settings/Utils.sys.mjs",
     10  reducers: "resource://newtab/common/Reducers.sys.mjs",
     11  sinon: "resource://testing-common/Sinon.sys.mjs",
     12  WallpaperFeed: "resource://newtab/lib/Wallpapers/WallpaperFeed.sys.mjs",
     13 });
     14 
     15 const PREF_WALLPAPERS_ENABLED =
     16  "browser.newtabpage.activity-stream.newtabWallpapers.enabled";
     17 
     18 const PREF_WALLPAPERS_CUSTOM_WALLPAPER_UUID =
     19  "browser.newtabpage.activity-stream.newtabWallpapers.customWallpaper.uuid";
     20 
     21 function getWallpaperFeedForTest() {
     22  let feed = new WallpaperFeed();
     23 
     24  feed.store = {
     25    dispatch: sinon.spy(),
     26  };
     27 
     28  return feed;
     29 }
     30 
     31 add_task(async function test_construction() {
     32  let feed = new WallpaperFeed();
     33 
     34  info("WallpaperFeed constructor should create initial values");
     35 
     36  Assert.ok(feed, "Could construct a WallpaperFeed");
     37  Assert.strictEqual(feed.loaded, false, "WallpaperFeed is not loaded");
     38  Assert.strictEqual(
     39    feed.wallpaperClient,
     40    null,
     41    "wallpaperClient is initialized as null"
     42  );
     43 });
     44 
     45 add_task(async function test_onAction_INIT() {
     46  let sandbox = sinon.createSandbox();
     47  let feed = new WallpaperFeed();
     48  Services.prefs.setBoolPref(PREF_WALLPAPERS_ENABLED, true);
     49  const attachment = {
     50    attachment: {
     51      location: "attachment",
     52    },
     53  };
     54  sandbox.stub(feed, "RemoteSettings").returns({
     55    get: () => [attachment],
     56    on: () => {},
     57  });
     58  feed.store = {
     59    dispatch: sinon.spy(),
     60  };
     61  sandbox
     62    .stub(Utils, "baseAttachmentsURL")
     63    .returns("http://localhost:8888/base_url/");
     64 
     65  info("WallpaperFeed.onAction INIT should initialize wallpapers");
     66 
     67  await feed.onAction({
     68    type: actionTypes.INIT,
     69  });
     70 
     71  Assert.greaterOrEqual(feed.store.dispatch.callCount, 3);
     72 
     73  const matchingCall = feed.store.dispatch
     74    .getCalls()
     75    .find(call => call.args[0].type === actionTypes.WALLPAPERS_SET);
     76 
     77  Assert.ok(matchingCall, "Expected a WALLPAPERS_SET dispatch call");
     78  Assert.deepEqual(
     79    matchingCall.args[0],
     80    actionCreators.BroadcastToContent({
     81      type: actionTypes.WALLPAPERS_SET,
     82      data: [
     83        {
     84          ...attachment,
     85          background_position: "center",
     86          category: "",
     87          order: 0,
     88          wallpaperUrl: "http://localhost:8888/base_url/attachment",
     89        },
     90      ],
     91      meta: {
     92        isStartup: true,
     93      },
     94    })
     95  );
     96 
     97  Services.prefs.clearUserPref(PREF_WALLPAPERS_ENABLED);
     98  sandbox.restore();
     99 });
    100 
    101 add_task(async function test_onAction_PREF_CHANGED() {
    102  let sandbox = sinon.createSandbox();
    103  let feed = new WallpaperFeed();
    104  Services.prefs.setBoolPref(PREF_WALLPAPERS_ENABLED, true);
    105  sandbox.stub(feed, "wallpaperSetup").returns();
    106 
    107  info("WallpaperFeed.onAction PREF_CHANGED should call wallpaperSetup");
    108 
    109  feed.onAction({
    110    type: actionTypes.PREF_CHANGED,
    111    data: { name: "newtabWallpapers.enabled" },
    112  });
    113 
    114  Assert.ok(feed.wallpaperSetup.calledOnce);
    115  Assert.ok(feed.wallpaperSetup.calledWith(false));
    116 
    117  Services.prefs.clearUserPref(PREF_WALLPAPERS_ENABLED);
    118  sandbox.restore();
    119 });
    120 
    121 add_task(async function test_onAction_WALLPAPER_UPLOAD() {
    122  let sandbox = sinon.createSandbox();
    123  let feed = new WallpaperFeed();
    124  const fileData = {};
    125 
    126  Services.prefs.setBoolPref(PREF_WALLPAPERS_ENABLED, true);
    127  sandbox.stub(feed, "wallpaperUpload").returns();
    128 
    129  info("WallpaperFeed.onAction WALLPAPER_UPLOAD should call wallpaperUpload");
    130 
    131  feed.onAction({
    132    type: actionTypes.WALLPAPER_UPLOAD,
    133    data: { file: fileData },
    134  });
    135 
    136  Assert.ok(feed.wallpaperUpload.calledOnce);
    137  Assert.ok(feed.wallpaperUpload.calledWith(fileData));
    138 
    139  Services.prefs.clearUserPref(PREF_WALLPAPERS_ENABLED);
    140 
    141  sandbox.restore();
    142 });
    143 
    144 add_task(async function test_Wallpaper_Upload() {
    145  let sandbox = sinon.createSandbox();
    146  let feed = getWallpaperFeedForTest(sandbox);
    147 
    148  info(
    149    "File uploaded via WallpaperFeed.wallpaperUpload should match the saved file"
    150  );
    151 
    152  const fakeWorker = {
    153    post: sandbox.stub().resolves("light"),
    154    terminate: sandbox.stub(),
    155  };
    156 
    157  sandbox.stub(feed, "BasePromiseWorker").callsFake(() => fakeWorker);
    158  // Create test file to upload with custom contents to verify the same file was stored in the /wallpaper dir successfully
    159  const testUploadContents = "custom-wallpaper-upload-test";
    160  const testFileName = "test-wallpaper.jpg";
    161 
    162  const testWallpaperFile = await IOUtils.createUniqueFile(
    163    PathUtils.tempDir,
    164    testFileName
    165  );
    166 
    167  await IOUtils.writeUTF8(testWallpaperFile, testUploadContents);
    168  let testNsIFile = await IOUtils.getFile(testWallpaperFile);
    169  let testFileToUpload = await File.createFromNsIFile(testNsIFile);
    170 
    171  // Upload test file
    172  let writtenFile = await feed.wallpaperUpload(testFileToUpload);
    173 
    174  // Check if test file exists in WallpaperFeed directory
    175  Assert.ok(await IOUtils.exists(writtenFile));
    176 
    177  // Check if stored file has the same unique contents as the original test file contents
    178  const storedWallpaperFeedFileContent = await IOUtils.readUTF8(writtenFile);
    179  Assert.equal(storedWallpaperFeedFileContent, testUploadContents);
    180 
    181  // Check UUID of file name matches stored PREF_WALLPAPERS_CUSTOM_WALLPAPER_UUID pref value
    182  const writtenUUID = PathUtils.filename(writtenFile);
    183  const storedUUID = Services.prefs.getStringPref(
    184    PREF_WALLPAPERS_CUSTOM_WALLPAPER_UUID
    185  );
    186 
    187  // Confirm written filename UUID matches the stored UUID pref
    188  Assert.equal(writtenUUID, storedUUID);
    189 
    190  Assert.ok(
    191    feed.store.dispatch.calledWith(
    192      actionCreators.SetPref("newtabWallpapers.customWallpaper.theme", "light")
    193    )
    194  );
    195 
    196  // Cleanup files
    197  await IOUtils.remove(testWallpaperFile);
    198  await IOUtils.remove(writtenFile);
    199  Services.prefs.clearUserPref(PREF_WALLPAPERS_CUSTOM_WALLPAPER_UUID);
    200 
    201  sandbox.restore();
    202 });
    203 
    204 /**
    205 * Tests that the parent process sends down a moz-newtab-wallpaper:// protocol URI
    206 * to newtab to render as the background.
    207 */
    208 add_task(async function test_Wallpaper_protocolURI() {
    209  let sandbox = sinon.createSandbox();
    210  let feed = getWallpaperFeedForTest(sandbox);
    211 
    212  const fakeWorker = {
    213    post: sandbox.stub().resolves("light"),
    214    terminate: sandbox.stub(),
    215  };
    216 
    217  sandbox.stub(feed, "BasePromiseWorker").callsFake(() => fakeWorker);
    218 
    219  // Stub out a fake RemoteClient so that updateWallpapers won't complain
    220  // when we eventually call it.
    221  feed.wallpaperClient = {
    222    async get() {
    223      return [];
    224    },
    225  };
    226 
    227  const testUploadContents = "custom-wallpaper-upload-test";
    228  const testFileName = "test-wallpaper.jpg";
    229 
    230  const testWallpaperFile = await IOUtils.createUniqueFile(
    231    PathUtils.tempDir,
    232    testFileName
    233  );
    234 
    235  await IOUtils.writeUTF8(testWallpaperFile, testUploadContents);
    236  let testNsIFile = await IOUtils.getFile(testWallpaperFile);
    237  let testFileToUpload = await File.createFromNsIFile(testNsIFile);
    238 
    239  // Upload test file
    240  let writtenFile = await feed.wallpaperUpload(testFileToUpload);
    241 
    242  Assert.ok(
    243    feed.store.dispatch.calledWith(
    244      actionCreators.BroadcastToContent({
    245        type: actionTypes.WALLPAPERS_CUSTOM_SET,
    246        data: sandbox.match("moz-newtab-wallpaper://"),
    247      })
    248    ),
    249    "Should dispatch WALLPAPERS_CUSTOM_SET with moz-newtab-wallpaper:// URI"
    250  );
    251 
    252  // Verify the protocol URI uses the correct scheme and gets stored in state
    253  const [action] = feed.store.dispatch.getCall(0).args;
    254  const wallpaperURI = action.data;
    255 
    256  Assert.ok(
    257    wallpaperURI.startsWith("moz-newtab-wallpaper://"),
    258    "Wallpaper URI should use moz-newtab-wallpaper:// protocol"
    259  );
    260 
    261  const state = reducers.Wallpapers(null, action);
    262  Assert.equal(
    263    state.uploadedWallpaper,
    264    wallpaperURI,
    265    "Should have updated the state to include the protocol URI"
    266  );
    267 
    268  // Cleanup files
    269  await IOUtils.remove(testWallpaperFile);
    270  await IOUtils.remove(writtenFile);
    271  Services.prefs.clearUserPref(PREF_WALLPAPERS_CUSTOM_WALLPAPER_UUID);
    272 });