1919import android .content .Context ;
2020
2121import com .optimizely .ab .android .datafile_handler .DatafileHandler ;
22+ import com .optimizely .ab .android .event_handler .DefaultEventHandler ;
2223import com .optimizely .ab .android .shared .DatafileConfig ;
2324import com .optimizely .ab .android .user_profile .DefaultUserProfileService ;
2425import com .optimizely .ab .bucketing .UserProfileService ;
2829import com .optimizely .ab .event .EventProcessor ;
2930import com .optimizely .ab .notification .NotificationCenter ;
3031
32+ import org .junit .Before ;
3133import org .junit .Test ;
3234import org .junit .runner .RunWith ;
3335import org .mockito .runners .MockitoJUnitRunner ;
36+ import org .powermock .api .mockito .PowerMockito ;
37+ import org .powermock .core .classloader .annotations .PowerMockIgnore ;
3438import org .powermock .core .classloader .annotations .PrepareForTest ;
3539import org .powermock .modules .junit4 .PowerMockRunner ;
3640import org .slf4j .Logger ;
5458import static org .mockito .Mockito .never ;
5559import static org .mockito .Mockito .verify ;
5660import static org .mockito .Mockito .when ;
61+ import static org .powermock .api .mockito .PowerMockito .mockStatic ;
5762import static org .powermock .api .mockito .PowerMockito .verifyNew ;
5863import static org .powermock .api .mockito .PowerMockito .whenNew ;
5964
6065
6166@ RunWith (PowerMockRunner .class )
62- @ PrepareForTest ({OptimizelyManager .class , BatchEventProcessor .class })
67+ @ PowerMockIgnore ("jdk.internal.reflect.*" )
68+ @ PrepareForTest ({OptimizelyManager .class , BatchEventProcessor .class , DefaultEventHandler .class })
6369public class OptimizelyManagerIntervalTest {
6470
6571 private Logger logger ;
72+ private Context mockContext ;
73+ private DefaultEventHandler mockEventHandler ;
6674
67- // DatafileDownloadInterval
75+ @ Before
76+ public void setup () throws Exception {
77+ mockContext = mock (Context .class );
78+ when (mockContext .getApplicationContext ()).thenReturn (mockContext );
6879
69- @ Test
70- public void testBuildWithDatafileDownloadInterval () throws Exception {
7180 whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
81+ whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
7282
73- Context appContext = mock (Context .class );
74- when (appContext .getApplicationContext ()).thenReturn (appContext );
83+ mockEventHandler = mock (DefaultEventHandler .class );
84+ mockStatic (DefaultEventHandler .class );
85+ when (DefaultEventHandler .getInstance (any ())).thenReturn (mockEventHandler );
86+ }
87+
88+ // DatafileDownloadInterval
7589
90+ @ Test
91+ public void testBuildWithDatafileDownloadInterval () throws Exception {
7692 long goodNumber = 27 ;
7793 OptimizelyManager manager = OptimizelyManager .builder ("1" )
7894 .withLogger (logger )
7995 .withDatafileDownloadInterval (goodNumber , TimeUnit .MINUTES )
80- .build (appContext );
96+ .build (mockContext );
8197
8298 verifyNew (OptimizelyManager .class ).withArguments (anyString (),
8399 anyString (),
@@ -96,16 +112,11 @@ public void testBuildWithDatafileDownloadInterval() throws Exception {
96112
97113 @ Test
98114 public void testBuildWithDatafileDownloadIntervalDeprecated () throws Exception {
99- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
100-
101- Context appContext = mock (Context .class );
102- when (appContext .getApplicationContext ()).thenReturn (appContext );
103-
104115 long goodNumber = 1234L ;
105116 OptimizelyManager manager = OptimizelyManager .builder ("1" )
106117 .withLogger (logger )
107118 .withDatafileDownloadInterval (goodNumber ) // deprecated
108- .build (appContext );
119+ .build (mockContext );
109120
110121 verifyNew (OptimizelyManager .class ).withArguments (anyString (),
111122 anyString (),
@@ -124,17 +135,11 @@ public void testBuildWithDatafileDownloadIntervalDeprecated() throws Exception {
124135
125136 @ Test
126137 public void testBuildWithEventDispatchInterval () throws Exception {
127- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
128- whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
129-
130- Context appContext = mock (Context .class );
131- when (appContext .getApplicationContext ()).thenReturn (appContext );
132-
133138 long goodNumber = 100L ;
134139 OptimizelyManager manager = OptimizelyManager .builder ("1" )
135140 .withLogger (logger )
136141 .withEventDispatchInterval (goodNumber , TimeUnit .SECONDS )
137- .build (appContext );
142+ .build (mockContext );
138143
139144 verifyNew (BatchEventProcessor .class ).withArguments (any (BlockingQueue .class ),
140145 any (EventHandler .class ),
@@ -145,14 +150,16 @@ public void testBuildWithEventDispatchInterval() throws Exception {
145150 any (NotificationCenter .class ),
146151 any (Object .class ));
147152
153+ verify (mockEventHandler ).setDispatchInterval (-1L ); // default
154+
148155 verifyNew (OptimizelyManager .class ).withArguments (anyString (),
149156 anyString (),
150157 any (DatafileConfig .class ),
151158 any (Logger .class ),
152159 anyLong (),
153160 any (DatafileHandler .class ),
154161 any (ErrorHandler .class ),
155- eq (-1L ), // milliseconds
162+ eq (-1L ), // default
156163 any (EventHandler .class ),
157164 any (EventProcessor .class ),
158165 any (UserProfileService .class ),
@@ -162,19 +169,14 @@ public void testBuildWithEventDispatchInterval() throws Exception {
162169
163170 @ Test
164171 public void testBuildWithEventDispatchRetryInterval () throws Exception {
165- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
166- whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
167-
168- Context appContext = mock (Context .class );
169- when (appContext .getApplicationContext ()).thenReturn (appContext );
170-
171172 long goodNumber = 100L ;
172- long defaultEventFlushInterval = 30L ;
173+ TimeUnit timeUnit = TimeUnit .MINUTES ;
174+ long defaultEventFlushInterval = 30L ; // seconds
173175
174176 OptimizelyManager manager = OptimizelyManager .builder ("1" )
175177 .withLogger (logger )
176- .withEventDispatchRetryInterval (goodNumber , TimeUnit . MINUTES )
177- .build (appContext );
178+ .withEventDispatchRetryInterval (goodNumber , timeUnit )
179+ .build (mockContext );
178180
179181 verifyNew (BatchEventProcessor .class ).withArguments (any (BlockingQueue .class ),
180182 any (EventHandler .class ),
@@ -185,6 +187,8 @@ public void testBuildWithEventDispatchRetryInterval() throws Exception {
185187 any (NotificationCenter .class ),
186188 any (Object .class ));
187189
190+ verify (mockEventHandler ).setDispatchInterval (timeUnit .toMillis (goodNumber )); // milli-seconds
191+
188192 verifyNew (OptimizelyManager .class ).withArguments (anyString (),
189193 anyString (),
190194 any (DatafileConfig .class ),
@@ -202,17 +206,11 @@ public void testBuildWithEventDispatchRetryInterval() throws Exception {
202206
203207 @ Test
204208 public void testBuildWithEventDispatchIntervalDeprecated () throws Exception {
205- whenNew (OptimizelyManager .class ).withAnyArguments ().thenReturn (mock (OptimizelyManager .class ));
206- whenNew (BatchEventProcessor .class ).withAnyArguments ().thenReturn (mock (BatchEventProcessor .class ));
207-
208- Context appContext = mock (Context .class );
209- when (appContext .getApplicationContext ()).thenReturn (appContext );
210-
211209 long goodNumber = 1234L ;
212210 OptimizelyManager manager = OptimizelyManager .builder ("1" )
213211 .withLogger (logger )
214212 .withEventDispatchInterval (goodNumber ) // deprecated
215- .build (appContext );
213+ .build (mockContext );
216214
217215 verifyNew (BatchEventProcessor .class ).withArguments (any (BlockingQueue .class ),
218216 any (EventHandler .class ),
@@ -223,14 +221,16 @@ public void testBuildWithEventDispatchIntervalDeprecated() throws Exception {
223221 any (NotificationCenter .class ),
224222 any (Object .class ));
225223
224+ verify (mockEventHandler ).setDispatchInterval (-1L ); // deprecated api not change default retryInterval
225+
226226 verifyNew (OptimizelyManager .class ).withArguments (anyString (),
227227 anyString (),
228228 any (DatafileConfig .class ),
229229 any (Logger .class ),
230230 anyLong (),
231231 any (DatafileHandler .class ),
232232 any (ErrorHandler .class ),
233- eq (goodNumber ), // milliseconds
233+ eq (- 1L ), // deprecated api not change default retryInterval
234234 any (EventHandler .class ),
235235 any (EventProcessor .class ),
236236 any (UserProfileService .class ),
0 commit comments