19
19
import android .content .Context ;
20
20
21
21
import com .optimizely .ab .android .datafile_handler .DatafileHandler ;
22
+ import com .optimizely .ab .android .datafile_handler .DefaultDatafileHandler ;
23
+ import com .optimizely .ab .android .shared .WorkerScheduler ;
22
24
import com .optimizely .ab .android .user_profile .DefaultUserProfileService ;
23
25
import com .optimizely .ab .error .ErrorHandler ;
24
26
import com .optimizely .ab .event .EventHandler ;
25
27
28
+ import org .junit .Before ;
26
29
import org .junit .Test ;
27
30
import org .junit .runner .RunWith ;
28
31
import org .mockito .runners .MockitoJUnitRunner ;
29
32
import org .slf4j .Logger ;
30
33
31
34
import static junit .framework .Assert .assertEquals ;
35
+ import static org .mockito .Matchers .any ;
36
+ import static org .mockito .Matchers .eq ;
32
37
import static org .mockito .Mockito .mock ;
38
+ import static org .mockito .Mockito .never ;
39
+ import static org .mockito .Mockito .spy ;
40
+ import static org .mockito .Mockito .verify ;
33
41
import static org .mockito .Mockito .when ;
34
42
43
+ import java .sql .Time ;
44
+ import java .util .concurrent .TimeUnit ;
45
+
35
46
@ RunWith (MockitoJUnitRunner .class )
36
47
public class OptimizelyManagerBuilderTest {
37
48
38
49
private String testProjectId = "7595190003" ;
50
+ private String testSdkKey = "1234" ;
39
51
private Logger logger ;
40
52
41
53
private String minDatafile = "{\n " +
@@ -49,78 +61,71 @@ public class OptimizelyManagerBuilderTest {
49
61
"events: [ ],\n " +
50
62
"revision: \" 1\" \n " +
51
63
"}" ;
52
- /**
53
- * Verify that building the {@link OptimizelyManager} with a polling interval less than 60
54
- * seconds defaults to 60 seconds.
55
- */
56
- // @Test
57
- // public void testBuildWithInvalidPollingInterval() {
58
- // Context appContext = mock(Context.class);
59
- // when(appContext.getApplicationContext()).thenReturn(appContext);
60
- // OptimizelyManager manager = OptimizelyManager.builder("1")
61
- // .withDatafileDownloadInterval(5L)
62
- // .build(appContext);
63
- //
64
- // assertEquals(900L, manager.getDatafileDownloadInterval().longValue());
65
- // }
64
+
65
+ private Context mockContext ;
66
+ private DefaultDatafileHandler mockDatafileHandler ;
67
+
68
+ @ Before
69
+ public void setup () throws Exception {
70
+ mockContext = mock (Context .class );
71
+ when (mockContext .getApplicationContext ()).thenReturn (mockContext );
72
+ mockDatafileHandler = mock (DefaultDatafileHandler .class );
73
+ }
66
74
67
75
/**
68
76
* Verify that building the {@link OptimizelyManager} with a polling interval greater than 60
69
77
* seconds is properly registered.
70
78
*/
71
79
@ Test
72
80
public void testBuildWithValidPollingInterval () {
73
- Context appContext = mock (Context .class );
74
- when (appContext .getApplicationContext ()).thenReturn (appContext );
75
- OptimizelyManager manager = OptimizelyManager .builder ("1" )
76
- .withDatafileDownloadInterval (901L )
77
- .build (appContext );
81
+ Long interval = 16L ;
82
+ TimeUnit timeUnit = TimeUnit .MINUTES ;
78
83
79
- assertEquals (901L , manager .getDatafileDownloadInterval ().longValue ());
84
+ OptimizelyManager manager = OptimizelyManager .builder ()
85
+ .withSDKKey (testSdkKey )
86
+ .withDatafileDownloadInterval (interval , timeUnit )
87
+ .build (mockContext );
88
+
89
+ assertEquals (interval * 60L , manager .getDatafileDownloadInterval ().longValue ());
80
90
}
81
91
82
92
@ Test
83
93
public void testBuildWithEventHandler () {
84
- Context appContext = mock (Context .class );
85
- when (appContext .getApplicationContext ()).thenReturn (appContext );
86
94
EventHandler eventHandler = mock (EventHandler .class );
87
- OptimizelyManager manager = OptimizelyManager .builder (testProjectId )
88
- .withDatafileDownloadInterval (901L )
95
+ OptimizelyManager manager = OptimizelyManager .builder ()
96
+ .withSDKKey (testSdkKey )
97
+ .withDatafileDownloadInterval (901L , TimeUnit .SECONDS )
89
98
.withEventHandler (eventHandler )
90
- .build (appContext );
99
+ .build (mockContext );
91
100
92
101
assertEquals (901L , manager .getDatafileDownloadInterval ().longValue ());
93
- assertEquals (manager .getEventHandler (appContext ), eventHandler );
94
-
95
-
102
+ assertEquals (manager .getEventHandler (mockContext ), eventHandler );
96
103
}
97
104
98
105
@ Test
99
106
public void testBuildWithErrorHandler () {
100
- Context appContext = mock (Context .class );
101
- when (appContext .getApplicationContext ()).thenReturn (appContext );
102
107
ErrorHandler errorHandler = mock (ErrorHandler .class );
103
- OptimizelyManager manager = OptimizelyManager .builder (testProjectId )
104
- .withDatafileDownloadInterval (61L )
108
+ OptimizelyManager manager = OptimizelyManager .builder ()
109
+ .withSDKKey (testSdkKey )
110
+ .withDatafileDownloadInterval (61L , TimeUnit .SECONDS )
105
111
.withErrorHandler (errorHandler )
106
- .build (appContext );
112
+ .build (mockContext );
107
113
108
- manager .initialize (appContext , minDatafile );
114
+ manager .initialize (mockContext , minDatafile );
109
115
110
- assertEquals (manager .getErrorHandler (appContext ), errorHandler );
116
+ assertEquals (manager .getErrorHandler (mockContext ), errorHandler );
111
117
}
112
118
113
119
@ Test
114
120
public void testBuildWithDatafileHandler () {
115
- Context appContext = mock (Context .class );
116
- when (appContext .getApplicationContext ()).thenReturn (appContext );
117
- DatafileHandler dfHandler = mock (DatafileHandler .class );
118
- OptimizelyManager manager = OptimizelyManager .builder (testProjectId )
119
- .withDatafileDownloadInterval (61L )
121
+ DefaultDatafileHandler dfHandler = mock (DefaultDatafileHandler .class );
122
+ OptimizelyManager manager = OptimizelyManager .builder ()
123
+ .withSDKKey (testSdkKey )
124
+ .withDatafileDownloadInterval (61L , TimeUnit .SECONDS )
120
125
.withDatafileHandler (dfHandler )
121
- .build (appContext );
126
+ .build (mockContext );
122
127
123
- manager .initialize (appContext , minDatafile );
128
+ manager .initialize (mockContext , minDatafile );
124
129
125
130
assertEquals (manager .getDatafileHandler (), dfHandler );
126
131
}
@@ -130,13 +135,62 @@ public void testBuildWithUserProfileService() {
130
135
Context appContext = mock (Context .class );
131
136
when (appContext .getApplicationContext ()).thenReturn (appContext );
132
137
DefaultUserProfileService ups = mock (DefaultUserProfileService .class );
133
- OptimizelyManager manager = OptimizelyManager .builder (testProjectId )
134
- .withDatafileDownloadInterval (61L )
138
+ OptimizelyManager manager = OptimizelyManager .builder ()
139
+ .withSDKKey (testSdkKey )
140
+ .withDatafileDownloadInterval (61L , TimeUnit .SECONDS )
135
141
.withUserProfileService (ups )
136
142
.build (appContext );
137
143
138
144
manager .initialize (appContext , minDatafile );
139
145
140
146
assertEquals (manager .getUserProfileService (), ups );
141
147
}
148
+
149
+ // BackgroundDatafile worker tests
150
+
151
+ @ Test
152
+ public void testBuildWithDatafileDownloadInterval_workerScheduled () throws Exception {
153
+ long goodNumber = 1 ;
154
+ OptimizelyManager manager = OptimizelyManager .builder ()
155
+ .withSDKKey (testSdkKey )
156
+ .withDatafileHandler (mockDatafileHandler )
157
+ .withDatafileDownloadInterval (goodNumber , TimeUnit .MINUTES )
158
+ .build (mockContext );
159
+ OptimizelyManager spyManager = spy (manager );
160
+ when (spyManager .isAndroidVersionSupported ()).thenReturn (true );
161
+ spyManager .initialize (mockContext , "" );
162
+
163
+ verify (mockDatafileHandler ).stopBackgroundUpdates (any (), any ());
164
+ verify (mockDatafileHandler ).startBackgroundUpdates (any (), any (), eq (goodNumber * 60L ), any ());
165
+ }
166
+
167
+ @ Test
168
+ public void testBuildWithDatafileDownloadInterval_workerCancelledWhenIntervalIsNotPositive () throws Exception {
169
+ OptimizelyManager manager = OptimizelyManager .builder ()
170
+ .withSDKKey (testSdkKey )
171
+ .withDatafileHandler (mockDatafileHandler )
172
+ .withDatafileDownloadInterval (-1 , TimeUnit .MINUTES )
173
+ .build (mockContext );
174
+ OptimizelyManager spyManager = spy (manager );
175
+ when (spyManager .isAndroidVersionSupported ()).thenReturn (true );
176
+ spyManager .initialize (mockContext , "" );
177
+
178
+ verify (mockDatafileHandler ).stopBackgroundUpdates (any (), any ());
179
+ verify (mockDatafileHandler , never ()).startBackgroundUpdates (any (), any (), any (), any ());
180
+ }
181
+
182
+ @ Test
183
+ public void testBuildWithDatafileDownloadInterval_workerCancelledWhenNoIntervalProvided () throws Exception {
184
+ OptimizelyManager manager = OptimizelyManager .builder ()
185
+ .withSDKKey (testSdkKey )
186
+ .withDatafileHandler (mockDatafileHandler )
187
+ .build (mockContext );
188
+ OptimizelyManager spyManager = spy (manager );
189
+ when (spyManager .isAndroidVersionSupported ()).thenReturn (true );
190
+ spyManager .initialize (mockContext , "" );
191
+
192
+ verify (mockDatafileHandler ).stopBackgroundUpdates (any (), any ());
193
+ verify (mockDatafileHandler , never ()).startBackgroundUpdates (any (), any (), any (), any ());
194
+ }
195
+
142
196
}
0 commit comments