browser_885530_showInPrivateBrowsing.js (4675B)
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 5 "use strict"; 6 7 const kWidgetId = "some-widget"; 8 9 function assertWidgetExists(aWindow, aExists) { 10 if (aExists) { 11 ok( 12 aWindow.document.getElementById(kWidgetId), 13 "Should have found test widget in the window" 14 ); 15 } else { 16 is( 17 aWindow.document.getElementById(kWidgetId), 18 null, 19 "Should not have found test widget in the window" 20 ); 21 } 22 } 23 24 // A widget that is created with showInPrivateBrowsing undefined should 25 // have that value default to true. 26 add_task(function () { 27 let wrapper = CustomizableUI.createWidget({ 28 id: kWidgetId, 29 }); 30 ok( 31 wrapper.showInPrivateBrowsing, 32 "showInPrivateBrowsing should have defaulted to true." 33 ); 34 CustomizableUI.destroyWidget(kWidgetId); 35 }); 36 37 // Add a widget via the API with showInPrivateBrowsing set to false 38 // and ensure it does not appear in pre-existing or newly created 39 // private windows. 40 add_task(async function () { 41 let plain1 = await openAndLoadWindow(); 42 let private1 = await openAndLoadWindow({ private: true }); 43 CustomizableUI.createWidget({ 44 id: kWidgetId, 45 removable: true, 46 showInPrivateBrowsing: false, 47 }); 48 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_NAVBAR); 49 assertWidgetExists(plain1, true); 50 assertWidgetExists(private1, false); 51 52 // Now open up some new windows. The widget should exist in the new 53 // plain window, but not the new private window. 54 let plain2 = await openAndLoadWindow(); 55 let private2 = await openAndLoadWindow({ private: true }); 56 assertWidgetExists(plain2, true); 57 assertWidgetExists(private2, false); 58 59 // Try moving the widget around and make sure it doesn't get added 60 // to the private windows. We'll start by appending it to the tabstrip. 61 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_TABSTRIP); 62 assertWidgetExists(plain1, true); 63 assertWidgetExists(plain2, true); 64 assertWidgetExists(private1, false); 65 assertWidgetExists(private2, false); 66 67 // And then move it to the beginning of the tabstrip. 68 CustomizableUI.moveWidgetWithinArea(kWidgetId, 0); 69 assertWidgetExists(plain1, true); 70 assertWidgetExists(plain2, true); 71 assertWidgetExists(private1, false); 72 assertWidgetExists(private2, false); 73 74 CustomizableUI.removeWidgetFromArea("some-widget"); 75 assertWidgetExists(plain1, false); 76 assertWidgetExists(plain2, false); 77 assertWidgetExists(private1, false); 78 assertWidgetExists(private2, false); 79 80 await Promise.all( 81 [plain1, plain2, private1, private2].map(promiseWindowClosed) 82 ); 83 84 CustomizableUI.destroyWidget("some-widget"); 85 }); 86 87 // Add a widget via the API with showInPrivateBrowsing set to true, 88 // and ensure that it appears in pre-existing or newly created 89 // private browsing windows. 90 add_task(async function () { 91 let plain1 = await openAndLoadWindow(); 92 let private1 = await openAndLoadWindow({ private: true }); 93 94 CustomizableUI.createWidget({ 95 id: kWidgetId, 96 removable: true, 97 showInPrivateBrowsing: true, 98 }); 99 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_NAVBAR); 100 assertWidgetExists(plain1, true); 101 assertWidgetExists(private1, true); 102 103 // Now open up some new windows. The widget should exist in the new 104 // plain window, but not the new private window. 105 let plain2 = await openAndLoadWindow(); 106 let private2 = await openAndLoadWindow({ private: true }); 107 108 assertWidgetExists(plain2, true); 109 assertWidgetExists(private2, true); 110 111 // Try moving the widget around and make sure it doesn't get added 112 // to the private windows. We'll start by appending it to the tabstrip. 113 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_TABSTRIP); 114 assertWidgetExists(plain1, true); 115 assertWidgetExists(plain2, true); 116 assertWidgetExists(private1, true); 117 assertWidgetExists(private2, true); 118 119 // And then move it to the beginning of the tabstrip. 120 CustomizableUI.moveWidgetWithinArea(kWidgetId, 0); 121 assertWidgetExists(plain1, true); 122 assertWidgetExists(plain2, true); 123 assertWidgetExists(private1, true); 124 assertWidgetExists(private2, true); 125 126 CustomizableUI.removeWidgetFromArea("some-widget"); 127 assertWidgetExists(plain1, false); 128 assertWidgetExists(plain2, false); 129 assertWidgetExists(private1, false); 130 assertWidgetExists(private2, false); 131 132 await Promise.all( 133 [plain1, plain2, private1, private2].map(promiseWindowClosed) 134 ); 135 136 CustomizableUI.destroyWidget("some-widget"); 137 }); 138 139 add_task(async function asyncCleanup() { 140 await resetCustomization(); 141 });