worker-browser-quirks.spec.js 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const fs = require("node:fs");
  2. const path = require("node:path");
  3. const { test, expect } = require("@playwright/test");
  4. const workerSource = fs.readFileSync(path.join(__dirname, "..", "..", "speedtest_worker.js"), "utf8");
  5. const userAgents = {
  6. safariMac:
  7. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15",
  8. safariIos:
  9. "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1",
  10. chromeIos:
  11. "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/123.0.6312.52 Mobile/15E148 Safari/604.1",
  12. firefoxIos:
  13. "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/124.0 Mobile/15E148 Safari/605.1.15",
  14. chromeAndroid:
  15. "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36",
  16. edge16:
  17. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299",
  18. playstation4: "Mozilla/5.0 (PlayStation 4 5.55) AppleWebKit/601.2 (KHTML, like Gecko)"
  19. };
  20. /*
  21. Loads speedtest_worker.js into a page that reports the given user agent, feeds it a "start"
  22. command and reports back the settings the worker resolved. The "_" test step is a no-op delay,
  23. so the worker settles its settings without performing any network I/O.
  24. */
  25. async function resolveSettings(browser, userAgent, customSettings) {
  26. const context = await browser.newContext({ userAgent });
  27. try {
  28. const page = await context.newPage();
  29. await page.goto("about:blank");
  30. await page.addScriptTag({ content: workerSource });
  31. return await page.evaluate(custom => {
  32. window.dispatchEvent(new MessageEvent("message", { data: "start " + JSON.stringify(custom) }));
  33. return {
  34. userAgent: navigator.userAgent,
  35. forceIE11Workaround: settings.forceIE11Workaround,
  36. xhr_ul_blob_megabytes: settings.xhr_ul_blob_megabytes
  37. };
  38. }, Object.assign({ test_order: "_" }, customSettings));
  39. } finally {
  40. await context.close();
  41. }
  42. }
  43. test.describe("speedtest_worker browser quirks", () => {
  44. test("Safari uses the accurate upload test instead of the IE11 workaround", async ({ browser }) => {
  45. for (const ua of [userAgents.safariMac, userAgents.safariIos]) {
  46. const resolved = await resolveSettings(browser, ua);
  47. expect(resolved.userAgent, "context user agent should be applied").toBe(ua);
  48. expect(resolved.forceIE11Workaround, `${ua} should not force the IE11 workaround`).toBe(false);
  49. expect(resolved.xhr_ul_blob_megabytes).toBe(20);
  50. }
  51. });
  52. test("Safari, Chrome and Firefox on iOS resolve to the same upload path", async ({ browser }) => {
  53. const safari = await resolveSettings(browser, userAgents.safariIos);
  54. const chrome = await resolveSettings(browser, userAgents.chromeIos);
  55. const firefox = await resolveSettings(browser, userAgents.firefoxIos);
  56. expect(safari.forceIE11Workaround).toBe(chrome.forceIE11Workaround);
  57. expect(safari.forceIE11Workaround).toBe(firefox.forceIE11Workaround);
  58. });
  59. test("Safari in MPOT mode also uses the accurate upload test", async ({ browser }) => {
  60. const resolved = await resolveSettings(browser, userAgents.safariMac, { mpot: true });
  61. expect(resolved.forceIE11Workaround).toBe(false);
  62. });
  63. test("Chrome mobile still caps the upload blob at 4 megabytes", async ({ browser }) => {
  64. const resolved = await resolveSettings(browser, userAgents.chromeAndroid);
  65. expect(resolved.xhr_ul_blob_megabytes).toBe(4);
  66. expect(resolved.forceIE11Workaround).toBe(false);
  67. });
  68. test("Edge and the PlayStation 4 browser keep the IE11 workaround by default", async ({ browser }) => {
  69. for (const ua of [userAgents.edge16, userAgents.playstation4]) {
  70. const resolved = await resolveSettings(browser, ua);
  71. expect(resolved.forceIE11Workaround, `${ua} should keep the IE11 workaround`).toBe(true);
  72. }
  73. });
  74. test("an explicitly passed forceIE11Workaround is not overwritten by the quirks", async ({ browser }) => {
  75. for (const ua of [userAgents.edge16, userAgents.playstation4]) {
  76. const off = await resolveSettings(browser, ua, { forceIE11Workaround: false });
  77. expect(off.forceIE11Workaround, `${ua} should honour an explicit false`).toBe(false);
  78. }
  79. const on = await resolveSettings(browser, userAgents.safariMac, { forceIE11Workaround: true });
  80. expect(on.forceIE11Workaround, "an explicit true should still force the workaround").toBe(true);
  81. });
  82. });