Screenshots.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. import {GlobalOverrider} from "test/unit/utils";
  3. import {Screenshots} from "lib/Screenshots.jsm";
  4. const URL = "foo.com";
  5. const FAKE_THUMBNAIL_PATH = "fake/path/thumb.jpg";
  6. describe("Screenshots", () => {
  7. let globals;
  8. let sandbox;
  9. let fakeServices;
  10. let testFile;
  11. beforeEach(() => {
  12. globals = new GlobalOverrider();
  13. sandbox = globals.sandbox;
  14. fakeServices = {
  15. wm: {
  16. getEnumerator() {
  17. return Array(10);
  18. },
  19. },
  20. };
  21. globals.set("BackgroundPageThumbs", {captureIfMissing: sandbox.spy(() => Promise.resolve())});
  22. globals.set("PageThumbs", {
  23. _store: sandbox.stub(),
  24. getThumbnailPath: sandbox.spy(() => FAKE_THUMBNAIL_PATH),
  25. });
  26. globals.set("PrivateBrowsingUtils", {isWindowPrivate: sandbox.spy(() => false)});
  27. testFile = {size: 1};
  28. globals.set("Services", fakeServices);
  29. globals.set("fetch", sandbox.spy(() => Promise.resolve({blob: () => Promise.resolve(testFile)})));
  30. });
  31. afterEach(() => {
  32. globals.restore();
  33. });
  34. describe("#getScreenshotForURL", () => {
  35. it("should call BackgroundPageThumbs.captureIfMissing with the correct url", async () => {
  36. await Screenshots.getScreenshotForURL(URL);
  37. assert.calledWith(global.BackgroundPageThumbs.captureIfMissing, URL);
  38. });
  39. it("should call PageThumbs.getThumbnailPath with the correct url", async () => {
  40. await Screenshots.getScreenshotForURL(URL);
  41. assert.calledWith(global.PageThumbs.getThumbnailPath, URL);
  42. });
  43. it("should call fetch", async () => {
  44. await Screenshots.getScreenshotForURL(URL);
  45. assert.calledOnce(global.fetch);
  46. });
  47. it("should have the necessary keys in the response object", async () => {
  48. const screenshot = await Screenshots.getScreenshotForURL(URL);
  49. assert.notEqual(screenshot.path, undefined);
  50. assert.notEqual(screenshot.data, undefined);
  51. });
  52. it("should get null if something goes wrong", async () => {
  53. globals.set("BackgroundPageThumbs", {captureIfMissing: () => Promise.reject(new Error("Cannot capture thumbnail"))});
  54. const screenshot = await Screenshots.getScreenshotForURL(URL);
  55. assert.calledOnce(global.PageThumbs._store);
  56. assert.equal(screenshot, null);
  57. });
  58. it("should get null without storing if existing thumbnail is empty", async () => {
  59. testFile.size = 0;
  60. const screenshot = await Screenshots.getScreenshotForURL(URL);
  61. assert.notCalled(global.PageThumbs._store);
  62. assert.equal(screenshot, null);
  63. });
  64. });
  65. describe("#maybeCacheScreenshot", () => {
  66. let link;
  67. beforeEach(() => {
  68. link = {__sharedCache: {updateLink: (prop, val) => { link[prop] = val; }}};
  69. });
  70. it("should call getScreenshotForURL", () => {
  71. sandbox.stub(Screenshots, "getScreenshotForURL");
  72. sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
  73. Screenshots.maybeCacheScreenshot(link, "mozilla.com", "image", sinon.stub());
  74. assert.calledOnce(Screenshots.getScreenshotForURL);
  75. assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
  76. });
  77. it("should not call getScreenshotForURL twice if a fetch is in progress", () => {
  78. sandbox.stub(Screenshots, "getScreenshotForURL").returns(new Promise(() => {}));
  79. sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
  80. Screenshots.maybeCacheScreenshot(link, "mozilla.com", "image", sinon.stub());
  81. Screenshots.maybeCacheScreenshot(link, "mozilla.org", "image", sinon.stub());
  82. assert.calledOnce(Screenshots.getScreenshotForURL);
  83. assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
  84. });
  85. it("should not call getScreenshotsForURL if property !== undefined", async () => {
  86. sandbox.stub(Screenshots, "getScreenshotForURL").returns(Promise.resolve(null));
  87. sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
  88. await Screenshots.maybeCacheScreenshot(link, "mozilla.com", "image", sinon.stub());
  89. await Screenshots.maybeCacheScreenshot(link, "mozilla.org", "image", sinon.stub());
  90. assert.calledOnce(Screenshots.getScreenshotForURL);
  91. assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
  92. });
  93. it("should check if we are in private browsing before getting screenshots", async () => {
  94. sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
  95. await Screenshots.maybeCacheScreenshot(link, "mozilla.com", "image", sinon.stub());
  96. assert.calledOnce(Screenshots._shouldGetScreenshots);
  97. });
  98. it("should not get a screenshot if we are in private browsing", async () => {
  99. sandbox.stub(Screenshots, "getScreenshotForURL");
  100. sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(false);
  101. await Screenshots.maybeCacheScreenshot(link, "mozilla.com", "image", sinon.stub());
  102. assert.notCalled(Screenshots.getScreenshotForURL);
  103. });
  104. });
  105. describe("#_shouldGetScreenshots", () => {
  106. beforeEach(() => {
  107. let more = 2;
  108. sandbox.stub(global.Services.wm, "getEnumerator").callsFake(() => Array(Math.max(more--, 0)));
  109. });
  110. it("should use private browsing utils to determine if a window is private", () => {
  111. Screenshots._shouldGetScreenshots();
  112. assert.calledOnce(global.PrivateBrowsingUtils.isWindowPrivate);
  113. });
  114. it("should return true if there exists at least 1 non-private window", () => {
  115. assert.isTrue(Screenshots._shouldGetScreenshots());
  116. });
  117. it("should return false if there exists private windows", () => {
  118. global.PrivateBrowsingUtils = {isWindowPrivate: sandbox.spy(() => true)};
  119. assert.isFalse(Screenshots._shouldGetScreenshots());
  120. assert.calledTwice(global.PrivateBrowsingUtils.isWindowPrivate);
  121. });
  122. });
  123. });