debug-target-info.test.js (9766B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Unit tests for the DebugTargetInfo component. 8 */ 9 10 const renderer = require("react-test-renderer"); 11 const React = require("resource://devtools/client/shared/vendor/react.mjs"); 12 const DebugTargetInfo = React.createFactory( 13 require("resource://devtools/client/framework/components/DebugTargetInfo.js") 14 ); 15 const { 16 CONNECTION_TYPES, 17 } = require("resource://devtools/client/shared/remote-debugging/constants.js"); 18 const DESCRIPTOR_TYPES = require("resource://devtools/client/fronts/descriptors/descriptor-types.js"); 19 20 /** 21 * Stub for the L10N property expected by the DebugTargetInfo component. 22 */ 23 const stubL10N = { 24 getStr: id => id, 25 getFormatStr: (id, ...args) => [id, ...args].join("-"), 26 }; 27 28 const findByClassName = (testInstance, className) => { 29 return testInstance.findAll(node => { 30 return node.props.className && node.props.className.includes(className); 31 }); 32 }; 33 34 function buildProps(base, extraDebugTargetData) { 35 const props = Object.assign({}, base); 36 Object.assign(props.debugTargetData, extraDebugTargetData); 37 return props; 38 } 39 40 const TEST_TOOLBOX = { 41 target: { 42 name: "Test Tab Name", 43 url: "http://some.target/url", 44 targetForm: { 45 traits: { 46 navigation: true, 47 }, 48 }, 49 getTrait: trait => { 50 return TEST_TOOLBOX.target.targetForm.traits[trait]; 51 }, 52 }, 53 doc: {}, 54 }; 55 56 const TEST_TOOLBOX_NO_NAME = { 57 target: { 58 url: "http://some.target/without/a/name", 59 targetForm: { 60 traits: { 61 navigation: true, 62 }, 63 }, 64 getTrait: trait => { 65 return TEST_TOOLBOX.target.targetForm.traits[trait]; 66 }, 67 }, 68 doc: {}, 69 }; 70 71 const USB_DEVICE_DESCRIPTION = { 72 deviceName: "usbDeviceName", 73 icon: "chrome://devtools/skin/images/aboutdebugging-firefox-release.svg", 74 name: "usbRuntimeBrandName", 75 version: "1.0.0", 76 }; 77 78 const THIS_FIREFOX_DEVICE_DESCRIPTION = { 79 icon: "chrome://devtools/skin/images/aboutdebugging-firefox-release.svg", 80 version: "1.0.0", 81 name: "thisFirefoxRuntimeBrandName", 82 }; 83 84 const USB_TARGET_INFO = { 85 debugTargetData: { 86 connectionType: CONNECTION_TYPES.USB, 87 runtimeInfo: USB_DEVICE_DESCRIPTION, 88 descriptorType: DESCRIPTOR_TYPES.TAB, 89 descriptorName: "Usb debugging context", 90 }, 91 toolbox: TEST_TOOLBOX, 92 L10N: stubL10N, 93 }; 94 95 const THIS_FIREFOX_TARGET_INFO = { 96 debugTargetData: { 97 connectionType: CONNECTION_TYPES.THIS_FIREFOX, 98 runtimeInfo: THIS_FIREFOX_DEVICE_DESCRIPTION, 99 descriptorType: DESCRIPTOR_TYPES.TAB, 100 descriptorName: "This firefox first tab", 101 }, 102 toolbox: TEST_TOOLBOX, 103 L10N: stubL10N, 104 }; 105 106 const THIS_FIREFOX_NO_NAME_TARGET_INFO = { 107 debugTargetData: { 108 connectionType: CONNECTION_TYPES.THIS_FIREFOX, 109 runtimeInfo: THIS_FIREFOX_DEVICE_DESCRIPTION, 110 descriptorType: DESCRIPTOR_TYPES.TAB, 111 /* Avoid passing a name via 'descriptorName' attribute */ 112 }, 113 toolbox: TEST_TOOLBOX_NO_NAME, 114 L10N: stubL10N, 115 }; 116 117 describe("DebugTargetInfo component", () => { 118 describe("Connection info", () => { 119 it("displays connection info for USB Release target", () => { 120 const component = renderer.create(DebugTargetInfo(USB_TARGET_INFO)); 121 expect( 122 findByClassName(component.root, "qa-connection-info").length 123 ).toEqual(1); 124 }); 125 126 it("renders the expected snapshot for USB Release target", () => { 127 const component = renderer.create(DebugTargetInfo(USB_TARGET_INFO)); 128 expect(component.toJSON()).toMatchSnapshot(); 129 }); 130 131 it("hides the connection info for This Firefox target", () => { 132 const component = renderer.create( 133 DebugTargetInfo(THIS_FIREFOX_TARGET_INFO) 134 ); 135 expect( 136 findByClassName(component.root, "qa-connection-info").length 137 ).toEqual(0); 138 }); 139 }); 140 141 describe("Target title", () => { 142 it("displays the target title if the target of the Toolbox has a name", () => { 143 const component = renderer.create( 144 DebugTargetInfo(THIS_FIREFOX_TARGET_INFO) 145 ); 146 expect( 147 findByClassName(component.root, "qa-descriptor-title").length 148 ).toEqual(1); 149 }); 150 151 it("renders the expected snapshot for This Firefox target", () => { 152 const component = renderer.create( 153 DebugTargetInfo(THIS_FIREFOX_TARGET_INFO) 154 ); 155 expect(component.toJSON()).toMatchSnapshot(); 156 }); 157 158 it("doesn't display the target title if the target of the Toolbox has no name", () => { 159 const component = renderer.create( 160 DebugTargetInfo(THIS_FIREFOX_NO_NAME_TARGET_INFO) 161 ); 162 expect( 163 findByClassName(component.root, "qa-descriptor-title").length 164 ).toEqual(0); 165 }); 166 167 it("renders the expected snapshot for a Toolbox with an unnamed target", () => { 168 const component = renderer.create( 169 DebugTargetInfo(THIS_FIREFOX_NO_NAME_TARGET_INFO) 170 ); 171 expect(component.toJSON()).toMatchSnapshot(); 172 }); 173 }); 174 175 describe("Target icon", () => { 176 it("renders the expected snapshot for a tab target", () => { 177 const props = buildProps(USB_TARGET_INFO, { 178 descriptorType: DESCRIPTOR_TYPES.TAB, 179 }); 180 const component = renderer.create(DebugTargetInfo(props)); 181 expect(component.toJSON()).toMatchSnapshot(); 182 }); 183 184 it("renders the expected snapshot for a worker target", () => { 185 const props = buildProps(USB_TARGET_INFO, { 186 descriptorType: DESCRIPTOR_TYPES.WORKER, 187 }); 188 const component = renderer.create(DebugTargetInfo(props)); 189 expect(component.toJSON()).toMatchSnapshot(); 190 }); 191 192 it("renders the expected snapshot for an extension target", () => { 193 const props = buildProps(USB_TARGET_INFO, { 194 descriptorType: DESCRIPTOR_TYPES.EXTENSION, 195 }); 196 const component = renderer.create(DebugTargetInfo(props)); 197 expect(component.toJSON()).toMatchSnapshot(); 198 }); 199 200 it("renders the expected snapshot for an local extension target", () => { 201 const props = buildProps(THIS_FIREFOX_TARGET_INFO, { 202 descriptorType: DESCRIPTOR_TYPES.EXTENSION, 203 }); 204 const component = renderer.create(DebugTargetInfo(props)); 205 expect(component.toJSON()).toMatchSnapshot(); 206 }); 207 208 it("renders the expected snapshot for a process target", () => { 209 const props = buildProps(USB_TARGET_INFO, { 210 descriptorType: DESCRIPTOR_TYPES.PROCESS, 211 }); 212 const component = renderer.create(DebugTargetInfo(props)); 213 expect(component.toJSON()).toMatchSnapshot(); 214 }); 215 }); 216 217 describe("Always on top button", () => { 218 it("displays always on top button for local webextension target", () => { 219 const component = renderer.create( 220 DebugTargetInfo( 221 buildProps(THIS_FIREFOX_TARGET_INFO, { 222 descriptorType: DESCRIPTOR_TYPES.EXTENSION, 223 }) 224 ) 225 ); 226 expect( 227 findByClassName(component.root, "toolbox-always-on-top").length 228 ).toEqual(1); 229 }); 230 231 it(`does not display "Always on top" button for remote webextension toolbox`, () => { 232 const component = renderer.create( 233 DebugTargetInfo( 234 buildProps(USB_TARGET_INFO, { 235 descriptorType: DESCRIPTOR_TYPES.EXTENSION, 236 }) 237 ) 238 ); 239 expect( 240 findByClassName(component.root, "toolbox-always-on-top").length 241 ).toEqual(0); 242 }); 243 244 it(`does not display "Always on top" button for local tab toolbox`, () => { 245 const component = renderer.create( 246 DebugTargetInfo( 247 buildProps(THIS_FIREFOX_TARGET_INFO, { 248 descriptorType: DESCRIPTOR_TYPES.TAB, 249 }) 250 ) 251 ); 252 expect( 253 findByClassName(component.root, "toolbox-always-on-top").length 254 ).toEqual(0); 255 }); 256 257 it(`does not display "Always on top" button for remote tab toolbox`, () => { 258 const component = renderer.create( 259 DebugTargetInfo( 260 buildProps(USB_TARGET_INFO, { 261 descriptorType: DESCRIPTOR_TYPES.TAB, 262 }) 263 ) 264 ); 265 expect( 266 findByClassName(component.root, "toolbox-always-on-top").length 267 ).toEqual(0); 268 }); 269 270 it(`does not display "Always on top" button for local worker toolbox`, () => { 271 const component = renderer.create( 272 DebugTargetInfo( 273 buildProps(THIS_FIREFOX_TARGET_INFO, { 274 descriptorType: DESCRIPTOR_TYPES.WORKER, 275 }) 276 ) 277 ); 278 expect( 279 findByClassName(component.root, "toolbox-always-on-top").length 280 ).toEqual(0); 281 }); 282 283 it(`does not display "Always on top" button for remote worker toolbox`, () => { 284 const component = renderer.create( 285 DebugTargetInfo( 286 buildProps(USB_TARGET_INFO, { 287 descriptorType: DESCRIPTOR_TYPES.WORKER, 288 }) 289 ) 290 ); 291 expect( 292 findByClassName(component.root, "toolbox-always-on-top").length 293 ).toEqual(0); 294 }); 295 296 it(`does not display "Always on top" button for local process toolbox`, () => { 297 const component = renderer.create( 298 DebugTargetInfo( 299 buildProps(THIS_FIREFOX_TARGET_INFO, { 300 descriptorType: DESCRIPTOR_TYPES.PROCESS, 301 }) 302 ) 303 ); 304 expect( 305 findByClassName(component.root, "toolbox-always-on-top").length 306 ).toEqual(0); 307 }); 308 309 it(`does not display "Always on top" button for remote process toolbox`, () => { 310 const component = renderer.create( 311 DebugTargetInfo( 312 buildProps(USB_TARGET_INFO, { 313 descriptorType: DESCRIPTOR_TYPES.PROCESS, 314 }) 315 ) 316 ); 317 expect( 318 findByClassName(component.root, "toolbox-always-on-top").length 319 ).toEqual(0); 320 }); 321 }); 322 });