test_accessible_row_context_menu.html (5113B)
1 <!-- This Source Code Form is subject to the terms of the Mozilla Public 2 - License, v. 2.0. If a copy of the MPL was not distributed with this 3 - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> 4 <!DOCTYPE HTML> 5 <html> 6 <!-- 7 Test that openLink function is called if accessible object property is rendered as a link. 8 --> 9 <head> 10 <meta charset="utf-8"> 11 <title>AccessibilityRow context menu test</title> 12 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 13 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 14 <link rel="stylesheet" href="chrome://devtools/skin/light-theme.css" type="text/css"> 15 </head> 16 <body> 17 <pre id="test"> 18 <script src="head.js" type="application/javascript"></script> 19 <script type="application/javascript"> 20 21 "use strict"; 22 23 window.onload = async function() { 24 try { 25 const { gDevTools } = require("devtools/client/framework/devtools"); 26 const ReactDOM = browserRequire("devtools/client/shared/vendor/react-dom"); 27 const { createFactory, createElement } = 28 browserRequire("devtools/client/shared/vendor/react"); 29 const { Provider } = require("devtools/client/shared/vendor/react-redux"); 30 const createStore = require("devtools/client/shared/redux/create-store"); 31 const { Simulate } = 32 browserRequire("devtools/client/shared/vendor/react-dom-test-utils"); 33 const AccessibilityRow = createFactory( 34 browserRequire("devtools/client/accessibility/components/AccessibilityRow")); 35 const { FILTERS } = browserRequire("devtools/client/accessibility/constants"); 36 37 async function withMockEnv(func) { 38 const { gTelemetry: originalTelemetry } = window; 39 window.gTelemetry = null; 40 41 await func(); 42 43 window.gTelemetry = originalTelemetry; 44 } 45 46 function renderAccessibilityRow(newProps, newState) { 47 let container = document.getElementById("container"); 48 if (container) { 49 container.remove(); 50 } 51 52 const accRow = AccessibilityRow(newProps); 53 const mockStore = createStore((state, action) => 54 action ? { ...state, ...action } : state, { initialState: newState }); 55 const provider = createElement(Provider, { store: mockStore }, accRow); 56 57 container = document.createElement("div"); 58 container.id = "container"; 59 document.body.appendChild(container); 60 return ReactDOM.render(provider, container); 61 } 62 63 const ROW_ID = "test-row"; 64 const JSON_URL_PREFIX = "data:application/json;charset=UTF-8,"; 65 const SNAPSHOT = { "snapshot": true }; 66 const defaultProps = { 67 id: ROW_ID, 68 member: { 69 object: { 70 name: "test", 71 value: "test", 72 loading: false, 73 selected: false, 74 hasChildren: false, 75 snapshot: async () => SNAPSHOT, 76 on: () => {}, 77 off: () => {}, 78 // This accessible mock has no actorID and should be treated as 79 // destroyed. 80 isDestroyed: () => true, 81 }, 82 }, 83 columns: [ 84 { "id": "default", "title": "role" }, 85 { "id": "value", "title": "name" }, 86 ], 87 provider: { 88 getValue: (object, id) => object[id], 89 }, 90 hasContextMenu: true, 91 toolboxDoc: document, 92 }; 93 94 const auditState = { audit: { filters: { [FILTERS.CONTRAST]: false }}}; 95 96 const defaultState = { 97 ui: { supports: {} }, 98 ...auditState, 99 }; 100 101 info("Check contextmenu default behaviour."); 102 renderAccessibilityRow(defaultProps, defaultState); 103 const row = document.getElementById(ROW_ID); 104 105 info("Get topmost document where the context meny will be rendered"); 106 const menuDoc = document.defaultView.windowRoot.ownerGlobal.document; 107 await withMockEnv(async function() { 108 Simulate.contextMenu(row); 109 110 const menu = menuDoc.getElementById("accessibility-row-contextmenu"); 111 const printtojsonMenuItem = menuDoc.getElementById("menu-printtojson"); 112 113 ok(menu, "Accessibility row context menu is open"); 114 ok(printtojsonMenuItem, "Print to JSON menu item is visible"); 115 116 const browserWindow = Services.wm.getMostRecentWindow(gDevTools.chromeWindowType); 117 const defaultOpenWebLinkIn = browserWindow.openWebLinkIn; 118 119 let openWebLinkInCalled; 120 const openWebLinkInPromise = new Promise(resolve => { 121 openWebLinkInCalled = resolve; 122 }); 123 124 // Mock top chrome window's @openWebLinkIn method. 125 browserWindow.openWebLinkIn = (...args) => { 126 openWebLinkInCalled(args); 127 }; 128 printtojsonMenuItem.click(); 129 130 const [ url, where ] = await openWebLinkInPromise; 131 is(url, `${JSON_URL_PREFIX}${encodeURIComponent(JSON.stringify(SNAPSHOT))}`, 132 "Correct URL is opened"); 133 is(where, "tab", "URL was opened correctly"); 134 135 // Reset @openWebLinkIn to default. 136 browserWindow.openWebLinkIn = defaultOpenWebLinkIn; 137 menu.remove(); 138 }); 139 } catch (e) { 140 ok(false, "Got an error: " + DevToolsUtils.safeErrorString(e)); 141 } finally { 142 SimpleTest.finish(); 143 } 144 }; 145 </script> 146 </pre> 147 </body> 148 </html>