11describe ( 'Ionic Platform Service' , function ( ) {
2- var window ;
2+ var window , ionicPlatform , rootScope ;
33
4- beforeEach ( inject ( function ( $window ) {
4+ beforeEach ( module ( 'ionic.service.platform' ) ) ;
5+
6+ beforeEach ( inject ( function ( $window , $ionicPlatform , $rootScope ) {
57 window = $window ;
68 ionic . Platform . ua = '' ;
9+ ionicPlatform = $ionicPlatform ;
10+ rootScope = $rootScope ;
711 } ) ) ;
812
913 it ( 'should set platform name' , function ( ) {
@@ -114,7 +118,7 @@ describe('Ionic Platform Service', function() {
114118 window . cordova = { } ;
115119 ionic . Platform . setPlatform ( 'iOS' ) ;
116120 ionic . Platform . setVersion ( '7.0.3' ) ;
117-
121+
118122 ionic . Platform . _checkPlatforms ( )
119123
120124 expect ( ionic . Platform . platforms [ 0 ] ) . toEqual ( 'cordova' ) ;
@@ -127,7 +131,7 @@ describe('Ionic Platform Service', function() {
127131 window . cordova = { } ;
128132 ionic . Platform . setPlatform ( 'android' ) ;
129133 ionic . Platform . setVersion ( '4.2.3' ) ;
130-
134+
131135 ionic . Platform . _checkPlatforms ( )
132136
133137 expect ( ionic . Platform . platforms [ 0 ] ) . toEqual ( 'cordova' ) ;
@@ -243,4 +247,62 @@ describe('Ionic Platform Service', function() {
243247 expect ( ionic . Platform . is ( 'android' ) ) . toEqual ( false ) ;
244248 } ) ;
245249
250+ it ( 'should register/deregister a hardware back button action and add it to $ionicPlatform.backButtonActions' , function ( ) {
251+ var deregisterFn = ionicPlatform . registerBackButtonAction ( function ( ) { } ) ;
252+ expect ( Object . keys ( rootScope . $backButtonActions ) . length ) . toEqual ( 1 ) ;
253+ deregisterFn ( ) ;
254+ expect ( Object . keys ( rootScope . $backButtonActions ) . length ) . toEqual ( 0 ) ;
255+ } ) ;
256+
257+ it ( 'should register multiple back button actions and only call the highest priority on hardwareBackButtonClick' , function ( ) {
258+ ionicPlatform . registerBackButtonAction ( function ( ) { } , 1 , 'action1' ) ;
259+ ionicPlatform . registerBackButtonAction ( function ( ) { } , 2 , 'action2' ) ;
260+ ionicPlatform . registerBackButtonAction ( function ( ) { } , 3 , 'action3' ) ;
261+
262+ var rsp = ionicPlatform . hardwareBackButtonClick ( ) ;
263+ expect ( rsp . priority ) . toEqual ( 3 ) ;
264+ expect ( rsp . id ) . toEqual ( 'action3' ) ;
265+ } ) ;
266+
267+ it ( 'should register multiple back button actions w/ the same priority and only call the last highest priority on hardwareBackButtonClick' , function ( ) {
268+ ionicPlatform . registerBackButtonAction ( function ( ) { } , 3 , 'action1' ) ;
269+ ionicPlatform . registerBackButtonAction ( function ( ) { } , 3 , 'action2' ) ;
270+ ionicPlatform . registerBackButtonAction ( function ( ) { } , 3 , 'action3' ) ;
271+
272+ var rsp = ionicPlatform . hardwareBackButtonClick ( ) ;
273+ expect ( rsp . priority ) . toEqual ( 3 ) ;
274+ expect ( rsp . id ) . toEqual ( 'action3' ) ;
275+ } ) ;
276+
277+ it ( 'should register no back button actions and do nothing on hardwareBackButtonClick' , function ( ) {
278+ var rsp = ionicPlatform . hardwareBackButtonClick ( ) ;
279+ expect ( rsp ) . toBeUndefined ( ) ;
280+ } ) ;
281+
282+ it ( 'should register multiple back button actions, call hardwareBackButtonClick, deregister, and call hardwareBackButtonClick again' , function ( ) {
283+ var dereg1 = ionicPlatform . registerBackButtonAction ( function ( ) { } , 1 , 'action1' ) ;
284+ var dereg2 = ionicPlatform . registerBackButtonAction ( function ( ) { } , 2 , 'action2' ) ;
285+ var dereg3 = ionicPlatform . registerBackButtonAction ( function ( ) { } , 3 , 'action3' ) ;
286+
287+ var rsp = ionicPlatform . hardwareBackButtonClick ( ) ;
288+ expect ( rsp . priority ) . toEqual ( 3 ) ;
289+ expect ( rsp . id ) . toEqual ( 'action3' ) ;
290+
291+ dereg3 ( ) ;
292+
293+ rsp = ionicPlatform . hardwareBackButtonClick ( ) ;
294+ expect ( rsp . priority ) . toEqual ( 2 ) ;
295+ expect ( rsp . id ) . toEqual ( 'action2' ) ;
296+
297+ dereg2 ( ) ;
298+
299+ rsp = ionicPlatform . hardwareBackButtonClick ( ) ;
300+ expect ( rsp . priority ) . toEqual ( 1 ) ;
301+ expect ( rsp . id ) . toEqual ( 'action1' ) ;
302+
303+ dereg1 ( ) ;
304+ rsp = ionicPlatform . hardwareBackButtonClick ( ) ;
305+ expect ( rsp ) . toBeUndefined ( ) ;
306+ } ) ;
307+
246308} ) ;
0 commit comments