browser_device_change.js (3898B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Tests changing viewport device (need HTTP load for proper UA testing) 7 8 const TEST_URL = `${URL_ROOT}doc_page_state.html`; 9 const DEFAULT_DPPX = window.devicePixelRatio; 10 11 const Types = require("resource://devtools/client/responsive/types.js"); 12 13 const testDevice = { 14 name: "Fake Phone RDM Test", 15 width: 320, 16 height: 570, 17 pixelRatio: 5.5, 18 userAgent: "Mozilla/5.0 (Mobile; rv:39.0) Gecko/39.0 Firefox/39.0", 19 touch: true, 20 firefoxOS: true, 21 os: "custom", 22 featured: true, 23 }; 24 25 // Add the new device to the list 26 addDeviceForTest(testDevice); 27 28 // Add the laptop to the device list 29 const { 30 updatePreferredDevices, 31 } = require("resource://devtools/client/responsive/actions/devices.js"); 32 updatePreferredDevices({ 33 added: ["Laptop with MDPI screen"], 34 removed: [], 35 }); 36 37 addRDMTask( 38 TEST_URL, 39 async function ({ ui }) { 40 reloadOnUAChange(true); 41 42 // Test defaults 43 testViewportDimensions(ui, 320, 480); 44 info("Should have default UA at the start of the test"); 45 await testUserAgent(ui, DEFAULT_UA); 46 await testDevicePixelRatio(ui, DEFAULT_DPPX); 47 testTouchEventsOverride(ui, false); 48 testViewportDeviceMenuLabel(ui, "Responsive"); 49 50 // Test device with custom properties 51 await selectDevice(ui, "Fake Phone RDM Test"); 52 await waitForViewportResizeTo(ui, testDevice.width, testDevice.height); 53 info("Should have device UA now that device is applied"); 54 await testUserAgent(ui, testDevice.userAgent); 55 await testDevicePixelRatio(ui, testDevice.pixelRatio); 56 testTouchEventsOverride(ui, true); 57 58 // Test resetting device when resizing viewport 59 await testViewportResize( 60 ui, 61 ".viewport-vertical-resize-handle", 62 [-10, -10], 63 [0, -10], 64 { 65 hasDevice: true, 66 } 67 ); 68 69 info("Should have kept device profile resizing viewport"); 70 await testUserAgent(ui, testDevice.userAgent); 71 await testDevicePixelRatio(ui, testDevice.pixelRatio); 72 testTouchEventsOverride(ui, true); 73 testViewportDeviceMenuLabel(ui, "Responsive"); 74 75 // Test device with generic properties 76 await selectDevice(ui, "Laptop with MDPI screen"); 77 await waitForViewportResizeTo(ui, 1280, 800); 78 info("Should have default UA when using device without specific UA"); 79 await testUserAgent(ui, DEFAULT_UA); 80 await testDevicePixelRatio(ui, 1); 81 testTouchEventsOverride(ui, false); 82 83 reloadOnUAChange(false); 84 }, 85 { waitForDeviceList: true } 86 ); 87 88 addRDMTask( 89 null, 90 async function () { 91 const tab = await addTab(TEST_URL); 92 const { ui } = await openRDM(tab); 93 94 const { store } = ui.toolWindow; 95 96 reloadOnUAChange(true); 97 98 // Wait until the viewport has been added and the device list has been loaded 99 await waitUntilState( 100 store, 101 state => 102 state.viewports.length == 1 && 103 state.viewports[0].device === "Laptop with MDPI screen" && 104 state.devices.listState == Types.loadableState.LOADED 105 ); 106 107 // Select device with custom UA 108 const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser()); 109 await selectDevice(ui, "Fake Phone RDM Test"); 110 await waitForReload(); 111 await waitForViewportResizeTo(ui, testDevice.width, testDevice.height); 112 info("Should have device UA now that device is applied"); 113 await testUserAgent(ui, testDevice.userAgent); 114 115 // Browser will reload to clear the UA on RDM close 116 const onReload = BrowserTestUtils.browserLoaded(ui.getViewportBrowser()); 117 await closeRDM(tab); 118 await onReload; 119 120 // Ensure UA is reset to default after closing RDM 121 info("Should have default UA after closing RDM"); 122 await testUserAgentFromBrowser(tab.linkedBrowser, DEFAULT_UA); 123 124 await removeTab(tab); 125 126 reloadOnUAChange(false); 127 }, 128 { onlyPrefAndTask: true } 129 );