1- /****************************************************************************
2- * Copyright 2016,2021, Optimizely, Inc. and contributors *
3- * *
4- * Licensed under the Apache License, Version 2.0 (the "License"); *
5- * you may not use this file except in compliance with the License. *
6- * You may obtain a copy of the License at *
7- * *
8- * http://www.apache.org/licenses/LICENSE-2.0 *
9- * *
10- * Unless required by applicable law or agreed to in writing, software *
11- * distributed under the License is distributed on an "AS IS" BASIS, *
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13- * See the License for the specific language governing permissions and *
14- * limitations under the License. *
15- ***************************************************************************/
1+ // Copyright 2016,2021,2023, Optimizely, Inc. and contributors
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // https://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
1614
1715package com .optimizely .ab .android .event_handler ;
1816
1917import android .content .Context ;
2018import android .content .Intent ;
19+ import android .net .NetworkInfo ;
2120import android .net .wifi .WifiManager ;
2221
2322import org .junit .Before ;
2625import org .junit .runner .RunWith ;
2726import org .mockito .runners .MockitoJUnitRunner ;
2827import org .slf4j .Logger ;
28+
29+ import static org .mockito .Matchers .any ;
30+ import static org .mockito .Matchers .matches ;
31+ import static org .mockito .Mockito .doNothing ;
32+ import static org .mockito .Mockito .doReturn ;
33+ import static org .mockito .Mockito .doThrow ;
2934import static org .mockito .Mockito .mock ;
35+ import static org .mockito .Mockito .spy ;
3036import static org .mockito .Mockito .verify ;
3137import static org .mockito .Mockito .when ;
3238
39+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
40+
3341/**
3442 * Unit tests for {@link EventRescheduler}
3543 */
36- @ RunWith (MockitoJUnitRunner .class )
37- @ Ignore
44+ @ RunWith (AndroidJUnit4 .class )
3845public class EventReschedulerTest {
3946
4047 private Context context ;
@@ -47,8 +54,7 @@ public void setupEventRescheduler() {
4754 context = mock (Context .class );
4855 intent = mock (Intent .class );
4956 logger = mock (Logger .class );
50- rescheduler = mock (EventRescheduler .class );
51- rescheduler = new EventRescheduler ();
57+ rescheduler = spy (new EventRescheduler ());
5258 rescheduler .logger = logger ;
5359 }
5460
@@ -71,6 +77,13 @@ public void onReceiveInvalidAction() {
7177 verify (logger ).warn ("Received unsupported broadcast action to event rescheduler" );
7278 }
7379
80+ @ Test
81+ public void onReceiveWhenRescheduleWithException () {
82+ when (intent .getAction ()).thenThrow (new IllegalStateException ());
83+ rescheduler .onReceive (context , intent );
84+ verify (logger ).warn (matches ("WorkScheduler failed to reschedule an event service.*" ));
85+ }
86+
7487 @ Test
7588 public void onReceiveValidBootComplete () {
7689 when (intent .getAction ()).thenReturn (Intent .ACTION_BOOT_COMPLETED );
@@ -88,10 +101,12 @@ public void onReceiveValidPackageReplaced() {
88101 @ Test
89102 public void flushOnWifiConnectionIfScheduled () {
90103 final Intent eventServiceIntent = mock (Intent .class );
91- when (intent .getAction ()).thenReturn (WifiManager .SUPPLICANT_CONNECTION_CHANGE_ACTION );
92- when (intent .getBooleanExtra (WifiManager .EXTRA_SUPPLICANT_CONNECTED , false )).thenReturn (true );
104+ when (intent .getAction ()).thenReturn (WifiManager .WIFI_STATE_CHANGED_ACTION );
105+ NetworkInfo info = mock (NetworkInfo .class );
106+ when (info .isConnected ()).thenReturn (true );
107+ when (intent .getParcelableExtra (WifiManager .EXTRA_NETWORK_INFO )).thenReturn (info );
108+
93109 rescheduler .reschedule (context , intent );
94- verify (context ).startService (eventServiceIntent );
95110 verify (logger ).info ("Preemptively flushing events since wifi became available" );
96111 }
97112}
0 commit comments