|
1 | 1 | /****************************************************************************
|
2 |
| - * Copyright 2016,2021, Optimizely, Inc. and contributors * |
| 2 | + * Copyright 2016, 2021-2022, Optimizely, Inc. and contributors * |
3 | 3 | * *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); *
|
5 | 5 | * you may not use this file except in compliance with the License. *
|
|
21 | 21 | import android.content.Intent;
|
22 | 22 | import android.os.Build;
|
23 | 23 |
|
| 24 | +import androidx.test.filters.SdkSuppress; |
24 | 25 | import androidx.test.platform.app.InstrumentationRegistry;
|
25 | 26 |
|
26 | 27 | import com.optimizely.ab.android.shared.Cache;
|
27 | 28 | import com.optimizely.ab.android.shared.DatafileConfig;
|
28 | 29 |
|
| 30 | +import org.junit.After; |
29 | 31 | import org.junit.Before;
|
30 |
| -import org.junit.Ignore; |
31 | 32 | import org.junit.Test;
|
32 | 33 | import org.junit.runner.RunWith;
|
33 | 34 | import org.junit.runners.JUnit4;
|
34 | 35 | import org.mockito.ArgumentCaptor;
|
35 | 36 | import org.slf4j.Logger;
|
36 | 37 |
|
37 | 38 | import static junit.framework.Assert.assertEquals;
|
| 39 | +import static junit.framework.Assert.fail; |
38 | 40 | import static org.mockito.Matchers.any;
|
| 41 | +import static org.mockito.Mockito.doNothing; |
39 | 42 | import static org.mockito.Mockito.mock;
|
| 43 | +import static org.mockito.Mockito.never; |
| 44 | +import static org.mockito.Mockito.spy; |
40 | 45 | import static org.mockito.Mockito.times;
|
41 | 46 | import static org.mockito.Mockito.verify;
|
42 | 47 | import static org.mockito.Mockito.when;
|
43 | 48 |
|
| 49 | +import java.util.ArrayList; |
| 50 | +import java.util.List; |
| 51 | +import java.util.concurrent.ExecutorService; |
| 52 | +import java.util.concurrent.Executors; |
| 53 | +import java.util.concurrent.TimeUnit; |
| 54 | + |
44 | 55 | /**
|
45 | 56 | * Tests for {@link DatafileRescheduler}
|
46 | 57 | */
|
47 | 58 | @RunWith(JUnit4.class)
|
48 |
| -@Ignore |
49 |
| -// Tests pass locally but not on travis |
50 |
| -// probably starting too many services |
51 | 59 | public class DatafileReschedulerTest {
|
52 | 60 |
|
53 | 61 | private DatafileRescheduler datafileRescheduler;
|
54 | 62 | private Logger logger;
|
| 63 | + private Context context; |
| 64 | + private Cache cache; |
| 65 | + private BackgroundWatchersCache backgroundWatchersCache; |
| 66 | + private DatafileRescheduler.Dispatcher dispatcher; |
| 67 | + private ArgumentCaptor<DatafileConfig> captor; |
55 | 68 |
|
56 | 69 | @Before
|
57 | 70 | public void setup() {
|
58 |
| - datafileRescheduler = new DatafileRescheduler(); |
| 71 | + context = mock(Context.class); |
59 | 72 | logger = mock(Logger.class);
|
| 73 | + cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger); |
| 74 | + cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME); |
| 75 | + backgroundWatchersCache = new BackgroundWatchersCache(cache, logger); |
| 76 | + captor = ArgumentCaptor.forClass(DatafileConfig.class); |
| 77 | + |
| 78 | + dispatcher = spy(new DatafileRescheduler.Dispatcher(context, backgroundWatchersCache, logger)); |
| 79 | + doNothing().when(dispatcher).rescheduleService(any()); |
| 80 | + |
| 81 | + datafileRescheduler = new DatafileRescheduler(); |
60 | 82 | datafileRescheduler.logger = logger;
|
61 | 83 | }
|
62 | 84 |
|
| 85 | + @After |
| 86 | + public void teardown() { |
| 87 | + cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME); |
| 88 | + } |
| 89 | + |
63 | 90 | @Test
|
64 | 91 | public void receivingNullContext() {
|
65 | 92 | datafileRescheduler.onReceive(null, mock(Intent.class));
|
@@ -91,64 +118,61 @@ public void receivedActionMyPackageReplaced() {
|
91 | 118 | }
|
92 | 119 |
|
93 | 120 | @Test
|
94 |
| - public void dispatchingOneWithoutEnvironment() { |
95 |
| - Context mockContext = mock(Context.class); |
96 |
| - Cache cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger); |
97 |
| - BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger); |
| 121 | + @SdkSuppress(maxSdkVersion = Build.VERSION_CODES.N) |
| 122 | + public void dispatchingOneWithoutEnvironment() throws InterruptedException { |
| 123 | + // projectId: number string |
| 124 | + // sdkKey: alphabet string |
98 | 125 | backgroundWatchersCache.setIsWatching(new DatafileConfig("1", null), true);
|
99 |
| - Logger logger = mock(Logger.class); |
100 |
| - DatafileRescheduler.Dispatcher dispatcher = new DatafileRescheduler.Dispatcher(mockContext, backgroundWatchersCache, logger); |
101 | 126 | dispatcher.dispatch();
|
102 |
| - ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
103 |
| - verify(mockContext).startService(captor.capture()); |
104 |
| - assertEquals(new DatafileConfig("1", null).toJSONString(), captor.getValue().getStringExtra(DatafileService.EXTRA_DATAFILE_CONFIG)); |
105 |
| - verify(logger).info("Rescheduled data file watching for project {}", "1"); |
106 |
| - cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME); |
| 127 | + TimeUnit.SECONDS.sleep(1); |
| 128 | + |
| 129 | + verify(dispatcher).rescheduleService(captor.capture()); |
| 130 | + assertEquals(new DatafileConfig("1", null), captor.getValue()); |
107 | 131 | }
|
108 | 132 |
|
109 | 133 | @Test
|
110 |
| - public void dispatchingOneWithEnvironment() { |
111 |
| - Context mockContext = mock(Context.class); |
112 |
| - Cache cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger); |
113 |
| - BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger); |
114 |
| - backgroundWatchersCache.setIsWatching(new DatafileConfig("1", "2"), true); |
115 |
| - Logger logger = mock(Logger.class); |
116 |
| - DatafileRescheduler.Dispatcher dispatcher = new DatafileRescheduler.Dispatcher(mockContext, backgroundWatchersCache, logger); |
| 134 | + @SdkSuppress(maxSdkVersion = Build.VERSION_CODES.N) |
| 135 | + public void dispatchingOneWithEnvironment() throws InterruptedException { |
| 136 | + // projectId: number string |
| 137 | + // sdkKey: alphabet string |
| 138 | + backgroundWatchersCache.setIsWatching(new DatafileConfig(null, "A"), true); |
117 | 139 | dispatcher.dispatch();
|
118 |
| - ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
119 |
| - verify(mockContext).startService(captor.capture()); |
120 |
| - assertEquals(new DatafileConfig("1", "2").toJSONString(), captor.getValue().getStringExtra(DatafileService.EXTRA_DATAFILE_CONFIG)); |
121 |
| - verify(logger).info("Rescheduled data file watching for project {}", "2"); |
122 |
| - cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME); |
| 140 | + TimeUnit.SECONDS.sleep(1); |
| 141 | + |
| 142 | + verify(dispatcher).rescheduleService(captor.capture()); |
| 143 | + assertEquals(new DatafileConfig(null, "A"), captor.getValue()); |
123 | 144 | }
|
124 | 145 |
|
125 | 146 | @Test
|
126 |
| - public void dispatchingManyWithoutEnvironment() { |
127 |
| - Context mockContext = mock(Context.class); |
128 |
| - Cache cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger); |
129 |
| - BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger); |
| 147 | + @SdkSuppress(maxSdkVersion = Build.VERSION_CODES.N) |
| 148 | + public void dispatchingManyForLegacy() throws InterruptedException { |
130 | 149 | backgroundWatchersCache.setIsWatching(new DatafileConfig("1", null), true);
|
131 |
| - backgroundWatchersCache.setIsWatching(new DatafileConfig("2", null), true); |
| 150 | + backgroundWatchersCache.setIsWatching(new DatafileConfig("2", "A"), true); |
| 151 | + backgroundWatchersCache.setIsWatching(new DatafileConfig(null, "B"), true); |
132 | 152 | backgroundWatchersCache.setIsWatching(new DatafileConfig("3", null), true);
|
133 |
| - Logger logger = mock(Logger.class); |
134 |
| - DatafileRescheduler.Dispatcher dispatcher = new DatafileRescheduler.Dispatcher(mockContext, backgroundWatchersCache, logger); |
135 | 153 | dispatcher.dispatch();
|
136 |
| - verify(mockContext, times(3)).startService(any(Intent.class)); |
137 |
| - cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME); |
| 154 | + TimeUnit.SECONDS.sleep(1); |
| 155 | + |
| 156 | + verify(dispatcher, times(4)).rescheduleService(captor.capture()); |
| 157 | + List array = new ArrayList<String>(); |
| 158 | + for(DatafileConfig config : captor.getAllValues()) { |
| 159 | + array.add(config.toString()); |
| 160 | + } |
| 161 | + assert(array.contains(new DatafileConfig("1", null).toString())); |
| 162 | + assert(array.contains(new DatafileConfig(null, "A").toString())); |
| 163 | + assert(array.contains(new DatafileConfig(null, "B").toString())); |
| 164 | + assert(array.contains(new DatafileConfig("3", null).toString())); |
138 | 165 | }
|
139 | 166 |
|
140 | 167 | @Test
|
141 |
| - public void dispatchingManyWithEnvironment() { |
142 |
| - Context mockContext = mock(Context.class); |
143 |
| - Cache cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger); |
144 |
| - BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger); |
145 |
| - backgroundWatchersCache.setIsWatching(new DatafileConfig("1", "1"), true); |
146 |
| - backgroundWatchersCache.setIsWatching(new DatafileConfig("2", "1"), true); |
147 |
| - backgroundWatchersCache.setIsWatching(new DatafileConfig("3", "1"), true); |
148 |
| - Logger logger = mock(Logger.class); |
149 |
| - DatafileRescheduler.Dispatcher dispatcher = new DatafileRescheduler.Dispatcher(mockContext, backgroundWatchersCache, logger); |
| 168 | + @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
| 169 | + public void dispatchingManyForJobScheduler() throws InterruptedException { |
| 170 | + backgroundWatchersCache.setIsWatching(new DatafileConfig("1", null), true); |
| 171 | + backgroundWatchersCache.setIsWatching(new DatafileConfig(null, "A"), true); |
150 | 172 | dispatcher.dispatch();
|
151 |
| - verify(mockContext, times(3)).startService(any(Intent.class)); |
152 |
| - cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME); |
| 173 | + TimeUnit.SECONDS.sleep(1); |
| 174 | + |
| 175 | + verify(dispatcher, never()).rescheduleService(captor.capture()); |
153 | 176 | }
|
| 177 | + |
154 | 178 | }
|
0 commit comments