browser_gesture_navigation.js (6356B)
1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* vim: set sts=2 sw=2 et tw=80: */ 3 "use strict"; 4 5 add_setup(async () => { 6 // Disable window occlusion. See bug 1733955 / bug 1779559. 7 if (navigator.platform.indexOf("Win") == 0) { 8 await SpecialPowers.pushPrefEnv({ 9 set: [["widget.windows.window_occlusion_tracking.enabled", false]], 10 }); 11 } 12 }); 13 14 add_task(async () => { 15 // Open a new browser window to make sure there is no navigation history. 16 const newBrowser = await BrowserTestUtils.openNewBrowserWindow({}); 17 18 let event = { 19 direction: SimpleGestureEvent.DIRECTION_LEFT, 20 }; 21 ok(!newBrowser.gGestureSupport._shouldDoSwipeGesture(event)); 22 23 event = { 24 direction: SimpleGestureEvent.DIRECTION_RIGHT, 25 }; 26 ok(!newBrowser.gGestureSupport._shouldDoSwipeGesture(event)); 27 28 await BrowserTestUtils.closeWindow(newBrowser); 29 }); 30 31 function createSimpleGestureEvent(type, direction) { 32 let event = document.createEvent("SimpleGestureEvent"); 33 event.initSimpleGestureEvent( 34 type, 35 false /* canBubble */, 36 false /* cancelableArg */, 37 window, 38 0 /* detail */, 39 0 /* screenX */, 40 0 /* screenY */, 41 0 /* clientX */, 42 0 /* clientY */, 43 false /* ctrlKey */, 44 false /* altKey */, 45 false /* shiftKey */, 46 false /* metaKey */, 47 0 /* button */, 48 null /* relatedTarget */, 49 0 /* allowedDirections */, 50 direction, 51 1 /* delta */ 52 ); 53 return event; 54 } 55 56 add_task(async () => { 57 await SpecialPowers.pushPrefEnv({ 58 set: [["ui.swipeAnimationEnabled", false]], 59 }); 60 61 // Open a new browser window and load two pages so that the browser can go 62 // back but can't go forward. 63 const newWindow = await BrowserTestUtils.openNewBrowserWindow({}); 64 65 // gHistroySwipeAnimation gets initialized in a requestIdleCallback so we need 66 // to wait for the initialization. 67 await TestUtils.waitForCondition(() => { 68 return ( 69 // There's no explicit notification for the initialization, so we wait 70 // until `isLTR` matches the browser locale state. 71 newWindow.gHistorySwipeAnimation.isLTR != Services.locale.isAppLocaleRTL 72 ); 73 }); 74 75 BrowserTestUtils.startLoadingURIString( 76 newWindow.gBrowser.selectedBrowser, 77 "about:mozilla" 78 ); 79 await BrowserTestUtils.browserLoaded( 80 newWindow.gBrowser.selectedBrowser, 81 false, 82 "about:mozilla" 83 ); 84 BrowserTestUtils.startLoadingURIString( 85 newWindow.gBrowser.selectedBrowser, 86 "about:about" 87 ); 88 await BrowserTestUtils.browserLoaded( 89 newWindow.gBrowser.selectedBrowser, 90 false, 91 "about:about" 92 ); 93 94 let event = createSimpleGestureEvent( 95 "MozSwipeGestureMayStart", 96 SimpleGestureEvent.DIRECTION_LEFT 97 ); 98 newWindow.gGestureSupport._shouldDoSwipeGesture(event); 99 100 // Assuming we are on LTR environment. 101 is( 102 event.allowedDirections, 103 SimpleGestureEvent.DIRECTION_LEFT, 104 "Allows only swiping to left, i.e. backward" 105 ); 106 107 event = createSimpleGestureEvent( 108 "MozSwipeGestureMayStart", 109 SimpleGestureEvent.DIRECTION_RIGHT 110 ); 111 newWindow.gGestureSupport._shouldDoSwipeGesture(event); 112 is( 113 event.allowedDirections, 114 SimpleGestureEvent.DIRECTION_LEFT, 115 "Allows only swiping to left, i.e. backward" 116 ); 117 118 await BrowserTestUtils.closeWindow(newWindow); 119 }); 120 121 add_task(async () => { 122 await SpecialPowers.pushPrefEnv({ 123 set: [["ui.swipeAnimationEnabled", true]], 124 }); 125 126 // Open a new browser window and load two pages so that the browser can go 127 // back but can't go forward. 128 const newWindow = await BrowserTestUtils.openNewBrowserWindow({}); 129 130 if (!newWindow.gHistorySwipeAnimation._isSupported()) { 131 await BrowserTestUtils.closeWindow(newWindow); 132 return; 133 } 134 135 function sendSwipeSequence(sendEnd) { 136 let event = createSimpleGestureEvent( 137 "MozSwipeGestureMayStart", 138 SimpleGestureEvent.DIRECTION_LEFT 139 ); 140 newWindow.gGestureSupport.handleEvent(event); 141 142 event = createSimpleGestureEvent( 143 "MozSwipeGestureStart", 144 SimpleGestureEvent.DIRECTION_LEFT 145 ); 146 newWindow.gGestureSupport.handleEvent(event); 147 148 event = createSimpleGestureEvent( 149 "MozSwipeGestureUpdate", 150 SimpleGestureEvent.DIRECTION_LEFT 151 ); 152 newWindow.gGestureSupport.handleEvent(event); 153 154 event = createSimpleGestureEvent( 155 "MozSwipeGestureUpdate", 156 SimpleGestureEvent.DIRECTION_LEFT 157 ); 158 newWindow.gGestureSupport.handleEvent(event); 159 160 if (sendEnd) { 161 sendSwipeEnd(); 162 } 163 } 164 function sendSwipeEnd() { 165 let event = createSimpleGestureEvent( 166 "MozSwipeGestureEnd", 167 SimpleGestureEvent.DIRECTION_LEFT 168 ); 169 newWindow.gGestureSupport.handleEvent(event); 170 } 171 172 // gHistroySwipeAnimation gets initialized in a requestIdleCallback so we need 173 // to wait for the initialization. 174 await TestUtils.waitForCondition(() => { 175 return ( 176 // There's no explicit notification for the initialization, so we wait 177 // until `isLTR` matches the browser locale state. 178 newWindow.gHistorySwipeAnimation.isLTR != Services.locale.isAppLocaleRTL 179 ); 180 }); 181 182 BrowserTestUtils.startLoadingURIString( 183 newWindow.gBrowser.selectedBrowser, 184 "about:mozilla" 185 ); 186 await BrowserTestUtils.browserLoaded( 187 newWindow.gBrowser.selectedBrowser, 188 false, 189 "about:mozilla" 190 ); 191 BrowserTestUtils.startLoadingURIString( 192 newWindow.gBrowser.selectedBrowser, 193 "about:about" 194 ); 195 await BrowserTestUtils.browserLoaded( 196 newWindow.gBrowser.selectedBrowser, 197 false, 198 "about:about" 199 ); 200 201 // Start a swipe that's not enough to navigate 202 sendSwipeSequence(/* sendEnd = */ true); 203 204 // Wait two frames 205 await new Promise(r => 206 window.requestAnimationFrame(() => window.requestAnimationFrame(r)) 207 ); 208 209 // The transition to fully stopped shouldn't have had enough time yet to 210 // become fully stopped. 211 ok( 212 newWindow.gHistorySwipeAnimation._isStoppingAnimation, 213 "should be stopping anim" 214 ); 215 216 // Start another swipe. 217 sendSwipeSequence(/* sendEnd = */ false); 218 219 // Wait two frames 220 await new Promise(r => 221 window.requestAnimationFrame(() => window.requestAnimationFrame(r)) 222 ); 223 224 // We should have started a new swipe, ie we shouldn't be stopping. 225 ok( 226 !newWindow.gHistorySwipeAnimation._isStoppingAnimation, 227 "should not be stopping anim" 228 ); 229 230 sendSwipeEnd(); 231 232 await BrowserTestUtils.closeWindow(newWindow); 233 });