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.
16
14
17
15
package com .optimizely .ab .android .event_handler ;
18
16
19
17
import android .content .Context ;
20
18
import android .content .Intent ;
19
+ import android .net .NetworkInfo ;
21
20
import android .net .wifi .WifiManager ;
22
21
23
22
import org .junit .Before ;
26
25
import org .junit .runner .RunWith ;
27
26
import org .mockito .runners .MockitoJUnitRunner ;
28
27
import 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 ;
29
34
import static org .mockito .Mockito .mock ;
35
+ import static org .mockito .Mockito .spy ;
30
36
import static org .mockito .Mockito .verify ;
31
37
import static org .mockito .Mockito .when ;
32
38
39
+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
40
+
33
41
/**
34
42
* Unit tests for {@link EventRescheduler}
35
43
*/
36
- @ RunWith (MockitoJUnitRunner .class )
37
- @ Ignore
44
+ @ RunWith (AndroidJUnit4 .class )
38
45
public class EventReschedulerTest {
39
46
40
47
private Context context ;
@@ -47,8 +54,7 @@ public void setupEventRescheduler() {
47
54
context = mock (Context .class );
48
55
intent = mock (Intent .class );
49
56
logger = mock (Logger .class );
50
- rescheduler = mock (EventRescheduler .class );
51
- rescheduler = new EventRescheduler ();
57
+ rescheduler = spy (new EventRescheduler ());
52
58
rescheduler .logger = logger ;
53
59
}
54
60
@@ -71,6 +77,13 @@ public void onReceiveInvalidAction() {
71
77
verify (logger ).warn ("Received unsupported broadcast action to event rescheduler" );
72
78
}
73
79
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
+
74
87
@ Test
75
88
public void onReceiveValidBootComplete () {
76
89
when (intent .getAction ()).thenReturn (Intent .ACTION_BOOT_COMPLETED );
@@ -88,10 +101,12 @@ public void onReceiveValidPackageReplaced() {
88
101
@ Test
89
102
public void flushOnWifiConnectionIfScheduled () {
90
103
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
+
93
109
rescheduler .reschedule (context , intent );
94
- verify (context ).startService (eventServiceIntent );
95
110
verify (logger ).info ("Preemptively flushing events since wifi became available" );
96
111
}
97
112
}
0 commit comments