OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.customtabs; | 5 package org.chromium.chrome.browser.customtabs; |
6 | 6 |
7 import static android.test.MoreAsserts.assertContentsInAnyOrder; | |
8 import static android.test.MoreAsserts.assertEmpty; | |
9 | |
10 import android.app.Activity; | 7 import android.app.Activity; |
11 import android.os.AsyncTask; | 8 import android.os.AsyncTask; |
9 import android.support.test.InstrumentationRegistry; | |
12 import android.support.test.filters.MediumTest; | 10 import android.support.test.filters.MediumTest; |
13 import android.support.test.filters.SmallTest; | 11 import android.support.test.filters.SmallTest; |
14 import android.test.InstrumentationTestCase; | 12 import android.support.test.rule.UiThreadTestRule; |
15 import android.test.UiThreadTest; | 13 |
14 import org.hamcrest.Matchers; | |
15 import org.junit.After; | |
16 import org.junit.Assert; | |
17 import org.junit.Before; | |
18 import org.junit.Rule; | |
19 import org.junit.Test; | |
20 import org.junit.runner.RunWith; | |
16 | 21 |
17 import org.chromium.base.ActivityState; | 22 import org.chromium.base.ActivityState; |
18 import org.chromium.base.ApplicationStatus; | 23 import org.chromium.base.ApplicationStatus; |
19 import org.chromium.base.Callback; | 24 import org.chromium.base.Callback; |
20 import org.chromium.base.ContextUtils; | 25 import org.chromium.base.ContextUtils; |
21 import org.chromium.base.StreamUtil; | 26 import org.chromium.base.StreamUtil; |
22 import org.chromium.base.ThreadUtils; | 27 import org.chromium.base.ThreadUtils; |
23 import org.chromium.base.test.util.AdvancedMockContext; | 28 import org.chromium.base.test.util.AdvancedMockContext; |
24 import org.chromium.base.test.util.CallbackHelper; | 29 import org.chromium.base.test.util.CallbackHelper; |
25 import org.chromium.base.test.util.Feature; | 30 import org.chromium.base.test.util.Feature; |
26 import org.chromium.chrome.browser.ChromeTabbedActivity; | 31 import org.chromium.chrome.browser.ChromeTabbedActivity; |
27 import org.chromium.chrome.browser.TabState; | 32 import org.chromium.chrome.browser.TabState; |
28 import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager; | 33 import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager; |
29 import org.chromium.chrome.browser.tab.Tab; | 34 import org.chromium.chrome.browser.tab.Tab; |
30 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 35 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
31 import org.chromium.chrome.browser.tabmodel.TabModelSelectorImpl; | 36 import org.chromium.chrome.browser.tabmodel.TabModelSelectorImpl; |
32 import org.chromium.chrome.browser.tabmodel.TabPersistencePolicy; | 37 import org.chromium.chrome.browser.tabmodel.TabPersistencePolicy; |
33 import org.chromium.chrome.browser.tabmodel.TabPersistentStore; | 38 import org.chromium.chrome.browser.tabmodel.TabPersistentStore; |
34 import org.chromium.chrome.browser.tabmodel.TestTabModelDirectory; | 39 import org.chromium.chrome.browser.tabmodel.TestTabModelDirectory; |
40 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
35 import org.chromium.chrome.test.util.browser.tabmodel.MockTabModel; | 41 import org.chromium.chrome.test.util.browser.tabmodel.MockTabModel; |
36 | 42 |
37 import java.io.File; | 43 import java.io.File; |
38 import java.io.FileOutputStream; | 44 import java.io.FileOutputStream; |
39 import java.lang.ref.WeakReference; | 45 import java.lang.ref.WeakReference; |
40 import java.util.ArrayList; | 46 import java.util.ArrayList; |
41 import java.util.HashSet; | 47 import java.util.HashSet; |
42 import java.util.List; | 48 import java.util.List; |
43 import java.util.Set; | 49 import java.util.Set; |
44 import java.util.concurrent.Callable; | 50 import java.util.concurrent.Callable; |
45 import java.util.concurrent.Executor; | 51 import java.util.concurrent.Executor; |
46 import java.util.concurrent.atomic.AtomicReference; | 52 import java.util.concurrent.atomic.AtomicReference; |
47 | 53 |
48 import javax.annotation.Nullable; | 54 import javax.annotation.Nullable; |
49 | 55 |
50 /** | 56 /** |
51 * Tests for the Custom Tab persistence logic. | 57 * Tests for the Custom Tab persistence logic. |
52 */ | 58 */ |
53 public class CustomTabTabPersistencePolicyTest extends InstrumentationTestCase { | 59 @RunWith(ChromeJUnit4ClassRunner.class) |
54 | 60 public class CustomTabTabPersistencePolicyTest { |
55 private TestTabModelDirectory mMockDirectory; | 61 private TestTabModelDirectory mMockDirectory; |
56 private AdvancedMockContext mAppContext; | 62 private AdvancedMockContext mAppContext; |
57 | 63 |
58 @Override | 64 @Rule |
65 public UiThreadTestRule mRule = new UiThreadTestRule(); | |
66 | |
67 @Before | |
59 public void setUp() throws Exception { | 68 public void setUp() throws Exception { |
60 super.setUp(); | 69 mAppContext = new AdvancedMockContext(InstrumentationRegistry.getInstrum entation() |
61 | 70 .getTargetContext() |
62 mAppContext = new AdvancedMockContext( | 71 .getApplicationContext()); |
63 getInstrumentation().getTargetContext().getApplicationContext()) ; | |
64 ContextUtils.initApplicationContextForTests(mAppContext); | 72 ContextUtils.initApplicationContextForTests(mAppContext); |
65 | 73 |
66 mMockDirectory = new TestTabModelDirectory( | 74 mMockDirectory = new TestTabModelDirectory( |
67 mAppContext, "CustomTabTabPersistencePolicyTest", | 75 mAppContext, "CustomTabTabPersistencePolicyTest", |
68 CustomTabTabPersistencePolicy.SAVED_STATE_DIRECTORY); | 76 CustomTabTabPersistencePolicy.SAVED_STATE_DIRECTORY); |
69 TabPersistentStore.setBaseStateDirectoryForTests(mMockDirectory.getBaseD irectory()); | 77 TabPersistentStore.setBaseStateDirectoryForTests(mMockDirectory.getBaseD irectory()); |
70 } | 78 } |
71 | 79 |
72 @Override | 80 @After |
73 public void tearDown() throws Exception { | 81 public void tearDown() throws Exception { |
74 mMockDirectory.tearDown(); | 82 mMockDirectory.tearDown(); |
75 | 83 |
76 List<WeakReference<Activity>> activities = ApplicationStatus.getRunningA ctivities(); | 84 List<WeakReference<Activity>> activities = ApplicationStatus.getRunningA ctivities(); |
77 for (int i = 0; i < activities.size(); i++) { | 85 for (int i = 0; i < activities.size(); i++) { |
78 Activity activity = activities.get(i).get(); | 86 Activity activity = activities.get(i).get(); |
79 if (activity == null) continue; | 87 if (activity == null) continue; |
80 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.DE STROYED); | 88 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.DE STROYED); |
81 } | 89 } |
82 super.tearDown(); | |
83 } | 90 } |
84 | 91 |
92 @Test | |
85 @Feature("TabPersistentStore") | 93 @Feature("TabPersistentStore") |
86 @SmallTest | 94 @SmallTest |
87 public void testDeletableMetadataSelection_NoFiles() { | 95 public void testDeletableMetadataSelection_NoFiles() { |
88 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( | 96 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( |
89 System.currentTimeMillis(), new ArrayList<File>()); | 97 System.currentTimeMillis(), new ArrayList<File>()); |
90 assertEmpty(deletableFiles); | 98 Assert.assertThat(deletableFiles, Matchers.emptyIterableOf(File.class)); |
91 } | 99 } |
92 | 100 |
101 @Test | |
93 @Feature("TabPersistentStore") | 102 @Feature("TabPersistentStore") |
94 @SmallTest | 103 @SmallTest |
95 public void testDeletableMetadataSelection_MaximumValidFiles() { | 104 public void testDeletableMetadataSelection_MaximumValidFiles() { |
96 long currentTime = System.currentTimeMillis(); | 105 long currentTime = System.currentTimeMillis(); |
97 | 106 |
98 // Test the maximum allowed number of state files where they are all val id in terms of age. | 107 // Test the maximum allowed number of state files where they are all val id in terms of age. |
99 List<File> filesToTest = new ArrayList<>(); | 108 List<File> filesToTest = new ArrayList<>(); |
100 filesToTest.addAll(generateMaximumStateFiles(currentTime)); | 109 filesToTest.addAll(generateMaximumStateFiles(currentTime)); |
101 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( | 110 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( |
102 currentTime, filesToTest); | 111 currentTime, filesToTest); |
103 assertEmpty(deletableFiles); | 112 Assert.assertThat(deletableFiles, Matchers.emptyIterableOf(File.class)); |
104 } | 113 } |
105 | 114 |
115 @Test | |
106 @Feature("TabPersistentStore") | 116 @Feature("TabPersistentStore") |
107 @SmallTest | 117 @SmallTest |
108 public void testDeletableMetadataSelection_ExceedsMaximumValidFiles() { | 118 public void testDeletableMetadataSelection_ExceedsMaximumValidFiles() { |
109 long currentTime = System.currentTimeMillis(); | 119 long currentTime = System.currentTimeMillis(); |
110 | 120 |
111 // Test where we exceed the maximum number of allowed state files and en sure it chooses the | 121 // Test where we exceed the maximum number of allowed state files and en sure it chooses the |
112 // older file to delete. | 122 // older file to delete. |
113 List<File> filesToTest = new ArrayList<>(); | 123 List<File> filesToTest = new ArrayList<>(); |
114 filesToTest.addAll(generateMaximumStateFiles(currentTime)); | 124 filesToTest.addAll(generateMaximumStateFiles(currentTime)); |
115 File slightlyOlderFile = buildTestFile("slightlyolderfile", currentTime - 1L); | 125 File slightlyOlderFile = buildTestFile("slightlyolderfile", currentTime - 1L); |
116 // Insert it into the middle just to ensure it is not picking the last f ile. | 126 // Insert it into the middle just to ensure it is not picking the last f ile. |
117 filesToTest.add(filesToTest.size() / 2, slightlyOlderFile); | 127 filesToTest.add(filesToTest.size() / 2, slightlyOlderFile); |
118 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( | 128 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( |
119 currentTime, filesToTest); | 129 currentTime, filesToTest); |
120 assertContentsInAnyOrder(deletableFiles, slightlyOlderFile); | 130 Assert.assertThat(deletableFiles, Matchers.containsInAnyOrder(slightlyOl derFile)); |
121 } | 131 } |
122 | 132 |
133 @Test | |
123 @Feature("TabPersistentStore") | 134 @Feature("TabPersistentStore") |
124 @SmallTest | 135 @SmallTest |
125 public void testDeletableMetadataSelection_ExceedExpiryThreshold() { | 136 public void testDeletableMetadataSelection_ExceedExpiryThreshold() { |
126 long currentTime = System.currentTimeMillis(); | 137 long currentTime = System.currentTimeMillis(); |
127 | 138 |
128 // Ensure that files that exceed the allowed time threshold are removed regardless of the | 139 // Ensure that files that exceed the allowed time threshold are removed regardless of the |
129 // number of possible files. | 140 // number of possible files. |
130 List<File> filesToTest = new ArrayList<>(); | 141 List<File> filesToTest = new ArrayList<>(); |
131 File expiredFile = buildTestFile("expired_file", | 142 File expiredFile = buildTestFile("expired_file", |
132 currentTime - CustomTabTabPersistencePolicy.STATE_EXPIRY_THRESHO LD); | 143 currentTime - CustomTabTabPersistencePolicy.STATE_EXPIRY_THRESHO LD); |
133 filesToTest.add(expiredFile); | 144 filesToTest.add(expiredFile); |
134 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( | 145 List<File> deletableFiles = CustomTabTabPersistencePolicy.getMetadataFil esForDeletion( |
135 currentTime, filesToTest); | 146 currentTime, filesToTest); |
136 assertContentsInAnyOrder(deletableFiles, expiredFile); | 147 Assert.assertThat(deletableFiles, Matchers.containsInAnyOrder(expiredFil e)); |
137 } | 148 } |
138 | 149 |
139 /** | 150 /** |
140 * Test to ensure that an existing metadata files are deleted if no restore is requested. | 151 * Test to ensure that an existing metadata files are deleted if no restore is requested. |
141 */ | 152 */ |
153 @Test | |
142 @Feature("TabPersistentStore") | 154 @Feature("TabPersistentStore") |
143 @MediumTest | 155 @MediumTest |
144 public void testExistingMetadataFileDeletedIfNoRestore() throws Exception { | 156 public void testExistingMetadataFileDeletedIfNoRestore() throws Exception { |
145 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto ry(); | 157 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto ry(); |
146 assertNotNull(baseStateDirectory); | 158 Assert.assertNotNull(baseStateDirectory); |
147 | 159 |
148 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy (7, false); | 160 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy (7, false); |
149 File stateDirectory = policy.getOrCreateStateDirectory(); | 161 File stateDirectory = policy.getOrCreateStateDirectory(); |
150 assertNotNull(stateDirectory); | 162 Assert.assertNotNull(stateDirectory); |
151 | 163 |
152 String stateFileName = policy.getStateFileName(); | 164 String stateFileName = policy.getStateFileName(); |
153 File existingStateFile = new File(stateDirectory, stateFileName); | 165 File existingStateFile = new File(stateDirectory, stateFileName); |
154 assertTrue(existingStateFile.createNewFile()); | 166 Assert.assertTrue(existingStateFile.createNewFile()); |
155 | 167 |
156 assertTrue(existingStateFile.exists()); | 168 Assert.assertTrue(existingStateFile.exists()); |
157 policy.performInitialization(AsyncTask.SERIAL_EXECUTOR); | 169 policy.performInitialization(AsyncTask.SERIAL_EXECUTOR); |
158 policy.waitForInitializationToFinish(); | 170 policy.waitForInitializationToFinish(); |
159 assertFalse(existingStateFile.exists()); | 171 Assert.assertFalse(existingStateFile.exists()); |
160 } | 172 } |
161 | 173 |
162 /** | 174 /** |
163 * Test the logic that gets all the live tab and task IDs. | 175 * Test the logic that gets all the live tab and task IDs. |
164 */ | 176 */ |
177 @Test | |
165 @Feature("TabPersistentStore") | 178 @Feature("TabPersistentStore") |
166 @SmallTest | 179 @SmallTest |
167 @UiThreadTest | 180 public void testGettingTabAndTaskIds() throws Throwable { |
168 public void testGettingTabAndTaskIds() { | 181 mRule.runOnUiThread(new Runnable() { |
Ted C
2017/03/29 00:06:41
We should figure out a way to have our own annotat
the real yoland
2017/03/29 01:13:23
Aha, one thing we can do is move things that neede
| |
169 Set<Integer> tabIds = new HashSet<>(); | 182 @Override |
170 Set<Integer> taskIds = new HashSet<>(); | 183 public void run() { |
171 CustomTabTabPersistencePolicy.getAllLiveTabAndTaskIds(tabIds, taskIds); | 184 Set<Integer> tabIds = new HashSet<>(); |
172 assertEmpty(tabIds); | 185 Set<Integer> taskIds = new HashSet<>(); |
173 assertEmpty(taskIds); | 186 CustomTabTabPersistencePolicy.getAllLiveTabAndTaskIds(tabIds, ta skIds); |
187 Assert.assertThat(tabIds, Matchers.emptyIterable()); | |
188 Assert.assertThat(taskIds, Matchers.emptyIterable()); | |
174 | 189 |
175 tabIds.clear(); | 190 tabIds.clear(); |
176 taskIds.clear(); | 191 taskIds.clear(); |
177 | 192 |
178 CustomTabActivity cct1 = buildTestCustomTabActivity(1, new int[] {4, 8, 9}, null); | 193 CustomTabActivity cct1 = buildTestCustomTabActivity(1, new int[] {4, 8, 9}, null); |
179 ApplicationStatus.onStateChangeForTesting(cct1, ActivityState.CREATED); | 194 ApplicationStatus.onStateChangeForTesting(cct1, ActivityState.CR EATED); |
180 | 195 |
181 CustomTabActivity cct2 = buildTestCustomTabActivity(5, new int[] {458}, new int[] {9878}); | 196 CustomTabActivity cct2 = |
182 ApplicationStatus.onStateChangeForTesting(cct2, ActivityState.CREATED); | 197 buildTestCustomTabActivity(5, new int[] {458}, new int[] {9878}); |
198 ApplicationStatus.onStateChangeForTesting(cct2, ActivityState.CR EATED); | |
183 | 199 |
184 // Add a tabbed mode activity to ensure that its IDs are not included in the returned CCT | 200 // Add a tabbed mode activity to ensure that its IDs are not inc luded in the |
185 // ID sets. | 201 // returned CCT ID sets. |
186 final TabModelSelectorImpl tabbedSelector = | 202 final TabModelSelectorImpl tabbedSelector = |
187 buildTestTabModelSelector(new int[] {12121212}, new int[] {15151 51515}); | 203 buildTestTabModelSelector(new int[] {12121212}, new int[ ] {1515151515}); |
188 ChromeTabbedActivity tabbedActivity = new ChromeTabbedActivity() { | 204 ChromeTabbedActivity tabbedActivity = new ChromeTabbedActivity() { |
189 @Override | 205 @Override |
190 public int getTaskId() { | 206 public int getTaskId() { |
191 return 888; | 207 return 888; |
208 } | |
209 | |
210 @Override | |
211 public TabModelSelector getTabModelSelector() { | |
212 return tabbedSelector; | |
213 } | |
214 }; | |
215 ApplicationStatus.onStateChangeForTesting(tabbedActivity, Activi tyState.CREATED); | |
216 | |
217 CustomTabTabPersistencePolicy.getAllLiveTabAndTaskIds(tabIds, ta skIds); | |
218 Assert.assertThat(tabIds, Matchers.containsInAnyOrder(4, 8, 9, 4 58, 9878)); | |
219 Assert.assertThat(taskIds, Matchers.containsInAnyOrder(1, 5)); | |
192 } | 220 } |
193 | 221 }); |
194 @Override | |
195 public TabModelSelector getTabModelSelector() { | |
196 return tabbedSelector; | |
197 } | |
198 }; | |
199 ApplicationStatus.onStateChangeForTesting(tabbedActivity, ActivityState. CREATED); | |
200 | |
201 CustomTabTabPersistencePolicy.getAllLiveTabAndTaskIds(tabIds, taskIds); | |
202 assertContentsInAnyOrder(tabIds, 4, 8, 9, 458, 9878); | |
203 assertContentsInAnyOrder(taskIds, 1, 5); | |
204 } | 222 } |
205 | 223 |
206 /** | 224 /** |
207 * Test the full cleanup task path that determines what files are eligible f or deletion. | 225 * Test the full cleanup task path that determines what files are eligible f or deletion. |
226 * @throws Throwable | |
Ted C
2017/03/29 00:06:41
remove
the real yoland
2017/03/29 01:13:23
Done
| |
208 */ | 227 */ |
228 @Test | |
209 @Feature("TabPersistentStore") | 229 @Feature("TabPersistentStore") |
210 @MediumTest | 230 @MediumTest |
211 public void testCleanupTask() throws Exception { | 231 public void testCleanupTask() throws Throwable { |
212 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto ry(); | 232 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto ry(); |
213 assertNotNull(baseStateDirectory); | 233 Assert.assertNotNull(baseStateDirectory); |
214 | 234 |
215 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy (2, false); | 235 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy (2, false); |
216 File stateDirectory = policy.getOrCreateStateDirectory(); | 236 File stateDirectory = policy.getOrCreateStateDirectory(); |
217 assertNotNull(stateDirectory); | 237 Assert.assertNotNull(stateDirectory); |
218 | 238 |
219 final AtomicReference<List<String>> filesToDelete = new AtomicReference< >(); | 239 final AtomicReference<List<String>> filesToDelete = new AtomicReference< >(); |
220 final CallbackHelper callbackSignal = new CallbackHelper(); | 240 final CallbackHelper callbackSignal = new CallbackHelper(); |
221 Callback<List<String>> filesToDeleteCallback = new Callback<List<String> >() { | 241 Callback<List<String>> filesToDeleteCallback = new Callback<List<String> >() { |
222 @Override | 242 @Override |
223 public void onResult(List<String> fileNames) { | 243 public void onResult(List<String> fileNames) { |
224 filesToDelete.set(fileNames); | 244 filesToDelete.set(fileNames); |
225 callbackSignal.notifyCalled(); | 245 callbackSignal.notifyCalled(); |
226 } | 246 } |
227 }; | 247 }; |
228 | 248 |
229 // Test when no files have been created. | 249 // Test when no files have been created. |
230 policy.cleanupUnusedFiles(filesToDeleteCallback); | 250 policy.cleanupUnusedFiles(filesToDeleteCallback); |
231 callbackSignal.waitForCallback(0); | 251 callbackSignal.waitForCallback(0); |
232 assertEmpty(filesToDelete.get()); | 252 Assert.assertThat(filesToDelete.get(), Matchers.emptyIterable()); |
233 | 253 |
234 // Create an unreferenced tab state file and ensure it is marked for del etion. | 254 // Create an unreferenced tab state file and ensure it is marked for del etion. |
235 File tab999File = TabState.getTabStateFile(stateDirectory, 999, false); | 255 File tab999File = TabState.getTabStateFile(stateDirectory, 999, false); |
236 assertTrue(tab999File.createNewFile()); | 256 Assert.assertTrue(tab999File.createNewFile()); |
237 policy.cleanupUnusedFiles(filesToDeleteCallback); | 257 policy.cleanupUnusedFiles(filesToDeleteCallback); |
238 callbackSignal.waitForCallback(1); | 258 callbackSignal.waitForCallback(1); |
239 assertContentsInAnyOrder(filesToDelete.get(), tab999File.getName()); | 259 Assert.assertThat(filesToDelete.get(), Matchers.containsInAnyOrder(tab99 9File.getName())); |
240 | 260 |
241 // Reference the tab state file and ensure it is no longer marked for de letion. | 261 // Reference the tab state file and ensure it is no longer marked for de letion. |
242 CustomTabActivity cct1 = buildTestCustomTabActivity(1, new int[] {999}, null); | 262 mRule.runOnUiThread(new Runnable() { |
243 ApplicationStatus.onStateChangeForTesting(cct1, ActivityState.CREATED); | 263 @Override |
264 public void run() { | |
265 CustomTabActivity cct1 = buildTestCustomTabActivity(1, new int[] {999}, null); | |
266 ApplicationStatus.onStateChangeForTesting(cct1, ActivityState.CR EATED); | |
267 } | |
268 }); | |
244 policy.cleanupUnusedFiles(filesToDeleteCallback); | 269 policy.cleanupUnusedFiles(filesToDeleteCallback); |
245 callbackSignal.waitForCallback(2); | 270 callbackSignal.waitForCallback(2); |
246 assertEmpty(filesToDelete.get()); | 271 Assert.assertThat(filesToDelete.get(), Matchers.emptyIterable()); |
247 | 272 |
248 // Create a tab model and associated tabs. Ensure it is not marked for d eletion as it is | 273 // Create a tab model and associated tabs. Ensure it is not marked for d eletion as it is |
249 // new enough. | 274 // new enough. |
250 final TabModelSelectorImpl selectorImpl = buildTestTabModelSelector( | |
251 new int[] {111, 222, 333 }, null); | |
252 byte[] data = ThreadUtils.runOnUiThreadBlockingNoException(new Callable< byte[]>() { | 275 byte[] data = ThreadUtils.runOnUiThreadBlockingNoException(new Callable< byte[]>() { |
253 @Override | 276 @Override |
254 public byte[] call() throws Exception { | 277 public byte[] call() throws Exception { |
278 TabModelSelectorImpl selectorImpl = | |
279 buildTestTabModelSelector(new int[] {111, 222, 333}, nul l); | |
255 return TabPersistentStore.serializeTabModelSelector(selectorImpl , null); | 280 return TabPersistentStore.serializeTabModelSelector(selectorImpl , null); |
256 } | 281 } |
257 }); | 282 }); |
258 FileOutputStream fos = null; | 283 FileOutputStream fos = null; |
259 File metadataFile = new File( | 284 File metadataFile = new File( |
260 stateDirectory, TabPersistentStore.getStateFileName("3")); | 285 stateDirectory, TabPersistentStore.getStateFileName("3")); |
261 try { | 286 try { |
262 fos = new FileOutputStream(metadataFile); | 287 fos = new FileOutputStream(metadataFile); |
263 fos.write(data); | 288 fos.write(data); |
264 } finally { | 289 } finally { |
265 StreamUtil.closeQuietly(fos); | 290 StreamUtil.closeQuietly(fos); |
266 } | 291 } |
267 File tab111File = TabState.getTabStateFile(stateDirectory, 111, false); | 292 File tab111File = TabState.getTabStateFile(stateDirectory, 111, false); |
268 assertTrue(tab111File.createNewFile()); | 293 Assert.assertTrue(tab111File.createNewFile()); |
269 File tab222File = TabState.getTabStateFile(stateDirectory, 222, false); | 294 File tab222File = TabState.getTabStateFile(stateDirectory, 222, false); |
270 assertTrue(tab222File.createNewFile()); | 295 Assert.assertTrue(tab222File.createNewFile()); |
271 File tab333File = TabState.getTabStateFile(stateDirectory, 333, false); | 296 File tab333File = TabState.getTabStateFile(stateDirectory, 333, false); |
272 assertTrue(tab333File.createNewFile()); | 297 Assert.assertTrue(tab333File.createNewFile()); |
273 policy.cleanupUnusedFiles(filesToDeleteCallback); | 298 policy.cleanupUnusedFiles(filesToDeleteCallback); |
274 callbackSignal.waitForCallback(3); | 299 callbackSignal.waitForCallback(3); |
275 assertEmpty(filesToDelete.get()); | 300 Assert.assertThat(filesToDelete.get(), Matchers.emptyIterable()); |
276 | 301 |
277 // Set the age of the metadata file to be past the expiration threshold and ensure it along | 302 // Set the age of the metadata file to be past the expiration threshold and ensure it along |
278 // with the associated tab files are marked for deletion. | 303 // with the associated tab files are marked for deletion. |
279 assertTrue(metadataFile.setLastModified(1234)); | 304 Assert.assertTrue(metadataFile.setLastModified(1234)); |
280 policy.cleanupUnusedFiles(filesToDeleteCallback); | 305 policy.cleanupUnusedFiles(filesToDeleteCallback); |
281 callbackSignal.waitForCallback(4); | 306 callbackSignal.waitForCallback(4); |
282 assertContentsInAnyOrder(filesToDelete.get(), tab111File.getName(), tab2 22File.getName(), | 307 Assert.assertThat(filesToDelete.get(), |
283 tab333File.getName(), metadataFile.getName()); | 308 Matchers.containsInAnyOrder(tab111File.getName(), tab222File.get Name(), |
309 tab333File.getName(), metadataFile.getName())); | |
284 } | 310 } |
285 | 311 |
286 /** | 312 /** |
287 * Ensure that the metadata file's last modified timestamp is updated on ini tialization. | 313 * Ensure that the metadata file's last modified timestamp is updated on ini tialization. |
288 */ | 314 */ |
315 @Test | |
289 @Feature("TabPersistentStore") | 316 @Feature("TabPersistentStore") |
290 @MediumTest | 317 @MediumTest |
291 public void testMetadataTimestampRefreshed() throws Exception { | 318 public void testMetadataTimestampRefreshed() throws Exception { |
292 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto ry(); | 319 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto ry(); |
293 assertNotNull(baseStateDirectory); | 320 Assert.assertNotNull(baseStateDirectory); |
294 | 321 |
295 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy (2, true); | 322 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy (2, true); |
296 File stateDirectory = policy.getOrCreateStateDirectory(); | 323 File stateDirectory = policy.getOrCreateStateDirectory(); |
297 assertNotNull(stateDirectory); | 324 Assert.assertNotNull(stateDirectory); |
298 | 325 |
299 File metadataFile = new File(stateDirectory, policy.getStateFileName()); | 326 File metadataFile = new File(stateDirectory, policy.getStateFileName()); |
300 assertTrue(metadataFile.createNewFile()); | 327 Assert.assertTrue(metadataFile.createNewFile()); |
301 | 328 |
302 long previousTimestamp = | 329 long previousTimestamp = |
303 System.currentTimeMillis() - CustomTabTabPersistencePolicy.STATE _EXPIRY_THRESHOLD; | 330 System.currentTimeMillis() - CustomTabTabPersistencePolicy.STATE _EXPIRY_THRESHOLD; |
304 assertTrue(metadataFile.setLastModified(previousTimestamp)); | 331 Assert.assertTrue(metadataFile.setLastModified(previousTimestamp)); |
305 | 332 |
306 policy.performInitialization(AsyncTask.SERIAL_EXECUTOR); | 333 policy.performInitialization(AsyncTask.SERIAL_EXECUTOR); |
307 policy.waitForInitializationToFinish(); | 334 policy.waitForInitializationToFinish(); |
308 | 335 |
309 assertTrue(metadataFile.lastModified() > previousTimestamp); | 336 Assert.assertTrue(metadataFile.lastModified() > previousTimestamp); |
310 } | 337 } |
311 | 338 |
312 private static List<File> generateMaximumStateFiles(long currentTime) { | 339 private static List<File> generateMaximumStateFiles(long currentTime) { |
313 List<File> validFiles = new ArrayList<>(); | 340 List<File> validFiles = new ArrayList<>(); |
314 for (int i = 0; i < CustomTabTabPersistencePolicy.MAXIMUM_STATE_FILES; i ++) { | 341 for (int i = 0; i < CustomTabTabPersistencePolicy.MAXIMUM_STATE_FILES; i ++) { |
315 validFiles.add(buildTestFile("testfile" + i, currentTime)); | 342 validFiles.add(buildTestFile("testfile" + i, currentTime)); |
316 } | 343 } |
317 return validFiles; | 344 return validFiles; |
318 } | 345 } |
319 | 346 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 | 459 |
433 CustomTabActivity activity = new CustomTabActivity(); | 460 CustomTabActivity activity = new CustomTabActivity(); |
434 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.CREATE D); | 461 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.CREATE D); |
435 TabModelSelectorImpl selector = new TabModelSelectorImpl( | 462 TabModelSelectorImpl selector = new TabModelSelectorImpl( |
436 activity, activity, buildTestPersistencePolicy(), false, false); | 463 activity, activity, buildTestPersistencePolicy(), false, false); |
437 selector.initializeForTesting(normalTabModel, incognitoTabModel); | 464 selector.initializeForTesting(normalTabModel, incognitoTabModel); |
438 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.DESTRO YED); | 465 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.DESTRO YED); |
439 return selector; | 466 return selector; |
440 } | 467 } |
441 } | 468 } |
OLD | NEW |