browser_1880230_hideInNonPrivateBrowsing.js (5639B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const kWidgetId = "pbm-only-test-widget"; 7 8 function assertWidgetExists(aWindow, aExists) { 9 if (aExists) { 10 ok( 11 aWindow.document.getElementById(kWidgetId), 12 "Should have found test widget in the window" 13 ); 14 } else { 15 is( 16 aWindow.document.getElementById(kWidgetId), 17 null, 18 "Should not have found test widget in the window" 19 ); 20 } 21 } 22 23 // A widget that is created with hideInNonPrivateBrowsing undefined should 24 // have that value default to false. 25 add_task(function () { 26 let wrapper = CustomizableUI.createWidget({ 27 id: kWidgetId, 28 }); 29 ok( 30 !wrapper.hideInNonPrivateBrowsing, 31 "hideInNonPrivateBrowsing should have defaulted to false." 32 ); 33 CustomizableUI.destroyWidget(kWidgetId); 34 }); 35 36 // Add a widget via the API with hideInNonPrivateBrowsing set to true 37 // and ensure it does not appear in pre-existing or newly created 38 // non-private windows. 39 add_task(async function () { 40 let plain1 = await openAndLoadWindow(); 41 let private1 = await openAndLoadWindow({ private: true }); 42 CustomizableUI.createWidget({ 43 id: kWidgetId, 44 removable: true, 45 hideInNonPrivateBrowsing: true, 46 }); 47 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_NAVBAR); 48 assertWidgetExists(plain1, false); 49 assertWidgetExists(private1, true); 50 51 // Now open up some new windows. The widget should not exist in the new 52 // plain window, but exist in the new private window. 53 let plain2 = await openAndLoadWindow(); 54 let private2 = await openAndLoadWindow({ private: true }); 55 assertWidgetExists(plain2, false); 56 assertWidgetExists(private2, true); 57 58 // Try moving the widget around and make sure it doesn't get added 59 // to the non-private windows. We'll start by appending it to the tabstrip. 60 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_TABSTRIP); 61 assertWidgetExists(plain1, false); 62 assertWidgetExists(plain2, false); 63 assertWidgetExists(private1, true); 64 assertWidgetExists(private2, true); 65 66 // And then move it to the beginning of the tabstrip. 67 CustomizableUI.moveWidgetWithinArea(kWidgetId, 0); 68 assertWidgetExists(plain1, false); 69 assertWidgetExists(plain2, false); 70 assertWidgetExists(private1, true); 71 assertWidgetExists(private2, true); 72 73 CustomizableUI.removeWidgetFromArea(kWidgetId); 74 assertWidgetExists(plain1, false); 75 assertWidgetExists(plain2, false); 76 assertWidgetExists(private1, false); 77 assertWidgetExists(private2, false); 78 79 await Promise.all( 80 [plain1, plain2, private1, private2].map(promiseWindowClosed) 81 ); 82 83 CustomizableUI.destroyWidget(kWidgetId); 84 }); 85 86 // Add a widget via the API with hideInNonPrivateBrowsing set to false, 87 // and ensure that it appears in pre-existing or newly created 88 // private browsing windows. 89 add_task(async function () { 90 let plain1 = await openAndLoadWindow(); 91 let private1 = await openAndLoadWindow({ private: true }); 92 93 CustomizableUI.createWidget({ 94 id: kWidgetId, 95 removable: true, 96 hideInNonPrivateBrowsing: false, 97 }); 98 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_NAVBAR); 99 assertWidgetExists(plain1, true); 100 assertWidgetExists(private1, true); 101 102 // Now open up some new windows. The widget should exist in the new 103 // plain window, but not the new private window. 104 let plain2 = await openAndLoadWindow(); 105 let private2 = await openAndLoadWindow({ private: true }); 106 107 assertWidgetExists(plain2, true); 108 assertWidgetExists(private2, true); 109 110 // Try moving the widget around and make sure it doesn't get added 111 // to the private windows. We'll start by appending it to the tabstrip. 112 CustomizableUI.addWidgetToArea(kWidgetId, CustomizableUI.AREA_TABSTRIP); 113 assertWidgetExists(plain1, true); 114 assertWidgetExists(plain2, true); 115 assertWidgetExists(private1, true); 116 assertWidgetExists(private2, true); 117 118 // And then move it to the beginning of the tabstrip. 119 CustomizableUI.moveWidgetWithinArea(kWidgetId, 0); 120 assertWidgetExists(plain1, true); 121 assertWidgetExists(plain2, true); 122 assertWidgetExists(private1, true); 123 assertWidgetExists(private2, true); 124 125 CustomizableUI.removeWidgetFromArea(kWidgetId); 126 assertWidgetExists(plain1, false); 127 assertWidgetExists(plain2, false); 128 assertWidgetExists(private1, false); 129 assertWidgetExists(private2, false); 130 131 await Promise.all( 132 [plain1, plain2, private1, private2].map(promiseWindowClosed) 133 ); 134 135 CustomizableUI.destroyWidget(kWidgetId); 136 }); 137 138 // Add a widget via the API with hideInNonPrivateBrowsing set to true 139 // and ensure it does not appear in the list of unused widgets in private 140 // windows. 141 add_task(async function testPrivateBrowsingCustomizeModeWidget() { 142 CustomizableUI.createWidget({ 143 id: kWidgetId, 144 hideInNonPrivateBrowsing: true, 145 }); 146 147 let normalWidgetArray = CustomizableUI.getUnusedWidgets(gNavToolbox.palette); 148 normalWidgetArray = normalWidgetArray.map(w => w.id); 149 is( 150 normalWidgetArray.indexOf(kWidgetId), 151 -1, 152 "Widget should not appear as unused in non-private window" 153 ); 154 155 let privateWindow = await openAndLoadWindow({ private: true }); 156 let privateWidgetArray = CustomizableUI.getUnusedWidgets( 157 privateWindow.gNavToolbox.palette 158 ); 159 privateWidgetArray = privateWidgetArray.map(w => w.id); 160 Assert.greater( 161 privateWidgetArray.indexOf(kWidgetId), 162 -1, 163 "Widget should appear as unused in private window" 164 ); 165 await promiseWindowClosed(privateWindow); 166 167 CustomizableUI.destroyWidget(kWidgetId); 168 }); 169 170 add_task(async function asyncCleanup() { 171 await resetCustomization(); 172 });