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() { |
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. |
208 */ | 226 */ |
| 227 @Test |
209 @Feature("TabPersistentStore") | 228 @Feature("TabPersistentStore") |
210 @MediumTest | 229 @MediumTest |
211 public void testCleanupTask() throws Exception { | 230 public void testCleanupTask() throws Throwable { |
212 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto
ry(); | 231 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto
ry(); |
213 assertNotNull(baseStateDirectory); | 232 Assert.assertNotNull(baseStateDirectory); |
214 | 233 |
215 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy
(2, false); | 234 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy
(2, false); |
216 File stateDirectory = policy.getOrCreateStateDirectory(); | 235 File stateDirectory = policy.getOrCreateStateDirectory(); |
217 assertNotNull(stateDirectory); | 236 Assert.assertNotNull(stateDirectory); |
218 | 237 |
219 final AtomicReference<List<String>> filesToDelete = new AtomicReference<
>(); | 238 final AtomicReference<List<String>> filesToDelete = new AtomicReference<
>(); |
220 final CallbackHelper callbackSignal = new CallbackHelper(); | 239 final CallbackHelper callbackSignal = new CallbackHelper(); |
221 Callback<List<String>> filesToDeleteCallback = new Callback<List<String>
>() { | 240 Callback<List<String>> filesToDeleteCallback = new Callback<List<String>
>() { |
222 @Override | 241 @Override |
223 public void onResult(List<String> fileNames) { | 242 public void onResult(List<String> fileNames) { |
224 filesToDelete.set(fileNames); | 243 filesToDelete.set(fileNames); |
225 callbackSignal.notifyCalled(); | 244 callbackSignal.notifyCalled(); |
226 } | 245 } |
227 }; | 246 }; |
228 | 247 |
229 // Test when no files have been created. | 248 // Test when no files have been created. |
230 policy.cleanupUnusedFiles(filesToDeleteCallback); | 249 policy.cleanupUnusedFiles(filesToDeleteCallback); |
231 callbackSignal.waitForCallback(0); | 250 callbackSignal.waitForCallback(0); |
232 assertEmpty(filesToDelete.get()); | 251 Assert.assertThat(filesToDelete.get(), Matchers.emptyIterable()); |
233 | 252 |
234 // Create an unreferenced tab state file and ensure it is marked for del
etion. | 253 // Create an unreferenced tab state file and ensure it is marked for del
etion. |
235 File tab999File = TabState.getTabStateFile(stateDirectory, 999, false); | 254 File tab999File = TabState.getTabStateFile(stateDirectory, 999, false); |
236 assertTrue(tab999File.createNewFile()); | 255 Assert.assertTrue(tab999File.createNewFile()); |
237 policy.cleanupUnusedFiles(filesToDeleteCallback); | 256 policy.cleanupUnusedFiles(filesToDeleteCallback); |
238 callbackSignal.waitForCallback(1); | 257 callbackSignal.waitForCallback(1); |
239 assertContentsInAnyOrder(filesToDelete.get(), tab999File.getName()); | 258 Assert.assertThat(filesToDelete.get(), Matchers.containsInAnyOrder(tab99
9File.getName())); |
240 | 259 |
241 // Reference the tab state file and ensure it is no longer marked for de
letion. | 260 // Reference the tab state file and ensure it is no longer marked for de
letion. |
242 CustomTabActivity cct1 = buildTestCustomTabActivity(1, new int[] {999},
null); | 261 mRule.runOnUiThread(new Runnable() { |
243 ApplicationStatus.onStateChangeForTesting(cct1, ActivityState.CREATED); | 262 @Override |
| 263 public void run() { |
| 264 CustomTabActivity cct1 = buildTestCustomTabActivity(1, new int[]
{999}, null); |
| 265 ApplicationStatus.onStateChangeForTesting(cct1, ActivityState.CR
EATED); |
| 266 } |
| 267 }); |
244 policy.cleanupUnusedFiles(filesToDeleteCallback); | 268 policy.cleanupUnusedFiles(filesToDeleteCallback); |
245 callbackSignal.waitForCallback(2); | 269 callbackSignal.waitForCallback(2); |
246 assertEmpty(filesToDelete.get()); | 270 Assert.assertThat(filesToDelete.get(), Matchers.emptyIterable()); |
247 | 271 |
248 // Create a tab model and associated tabs. Ensure it is not marked for d
eletion as it is | 272 // Create a tab model and associated tabs. Ensure it is not marked for d
eletion as it is |
249 // new enough. | 273 // new enough. |
250 final TabModelSelectorImpl selectorImpl = buildTestTabModelSelector( | |
251 new int[] {111, 222, 333 }, null); | |
252 byte[] data = ThreadUtils.runOnUiThreadBlockingNoException(new Callable<
byte[]>() { | 274 byte[] data = ThreadUtils.runOnUiThreadBlockingNoException(new Callable<
byte[]>() { |
253 @Override | 275 @Override |
254 public byte[] call() throws Exception { | 276 public byte[] call() throws Exception { |
| 277 TabModelSelectorImpl selectorImpl = |
| 278 buildTestTabModelSelector(new int[] {111, 222, 333}, nul
l); |
255 return TabPersistentStore.serializeTabModelSelector(selectorImpl
, null); | 279 return TabPersistentStore.serializeTabModelSelector(selectorImpl
, null); |
256 } | 280 } |
257 }); | 281 }); |
258 FileOutputStream fos = null; | 282 FileOutputStream fos = null; |
259 File metadataFile = new File( | 283 File metadataFile = new File( |
260 stateDirectory, TabPersistentStore.getStateFileName("3")); | 284 stateDirectory, TabPersistentStore.getStateFileName("3")); |
261 try { | 285 try { |
262 fos = new FileOutputStream(metadataFile); | 286 fos = new FileOutputStream(metadataFile); |
263 fos.write(data); | 287 fos.write(data); |
264 } finally { | 288 } finally { |
265 StreamUtil.closeQuietly(fos); | 289 StreamUtil.closeQuietly(fos); |
266 } | 290 } |
267 File tab111File = TabState.getTabStateFile(stateDirectory, 111, false); | 291 File tab111File = TabState.getTabStateFile(stateDirectory, 111, false); |
268 assertTrue(tab111File.createNewFile()); | 292 Assert.assertTrue(tab111File.createNewFile()); |
269 File tab222File = TabState.getTabStateFile(stateDirectory, 222, false); | 293 File tab222File = TabState.getTabStateFile(stateDirectory, 222, false); |
270 assertTrue(tab222File.createNewFile()); | 294 Assert.assertTrue(tab222File.createNewFile()); |
271 File tab333File = TabState.getTabStateFile(stateDirectory, 333, false); | 295 File tab333File = TabState.getTabStateFile(stateDirectory, 333, false); |
272 assertTrue(tab333File.createNewFile()); | 296 Assert.assertTrue(tab333File.createNewFile()); |
273 policy.cleanupUnusedFiles(filesToDeleteCallback); | 297 policy.cleanupUnusedFiles(filesToDeleteCallback); |
274 callbackSignal.waitForCallback(3); | 298 callbackSignal.waitForCallback(3); |
275 assertEmpty(filesToDelete.get()); | 299 Assert.assertThat(filesToDelete.get(), Matchers.emptyIterable()); |
276 | 300 |
277 // Set the age of the metadata file to be past the expiration threshold
and ensure it along | 301 // 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. | 302 // with the associated tab files are marked for deletion. |
279 assertTrue(metadataFile.setLastModified(1234)); | 303 Assert.assertTrue(metadataFile.setLastModified(1234)); |
280 policy.cleanupUnusedFiles(filesToDeleteCallback); | 304 policy.cleanupUnusedFiles(filesToDeleteCallback); |
281 callbackSignal.waitForCallback(4); | 305 callbackSignal.waitForCallback(4); |
282 assertContentsInAnyOrder(filesToDelete.get(), tab111File.getName(), tab2
22File.getName(), | 306 Assert.assertThat(filesToDelete.get(), |
283 tab333File.getName(), metadataFile.getName()); | 307 Matchers.containsInAnyOrder(tab111File.getName(), tab222File.get
Name(), |
| 308 tab333File.getName(), metadataFile.getName())); |
284 } | 309 } |
285 | 310 |
286 /** | 311 /** |
287 * Ensure that the metadata file's last modified timestamp is updated on ini
tialization. | 312 * Ensure that the metadata file's last modified timestamp is updated on ini
tialization. |
288 */ | 313 */ |
| 314 @Test |
289 @Feature("TabPersistentStore") | 315 @Feature("TabPersistentStore") |
290 @MediumTest | 316 @MediumTest |
291 public void testMetadataTimestampRefreshed() throws Exception { | 317 public void testMetadataTimestampRefreshed() throws Exception { |
292 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto
ry(); | 318 File baseStateDirectory = TabPersistentStore.getOrCreateBaseStateDirecto
ry(); |
293 assertNotNull(baseStateDirectory); | 319 Assert.assertNotNull(baseStateDirectory); |
294 | 320 |
295 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy
(2, true); | 321 CustomTabTabPersistencePolicy policy = new CustomTabTabPersistencePolicy
(2, true); |
296 File stateDirectory = policy.getOrCreateStateDirectory(); | 322 File stateDirectory = policy.getOrCreateStateDirectory(); |
297 assertNotNull(stateDirectory); | 323 Assert.assertNotNull(stateDirectory); |
298 | 324 |
299 File metadataFile = new File(stateDirectory, policy.getStateFileName()); | 325 File metadataFile = new File(stateDirectory, policy.getStateFileName()); |
300 assertTrue(metadataFile.createNewFile()); | 326 Assert.assertTrue(metadataFile.createNewFile()); |
301 | 327 |
302 long previousTimestamp = | 328 long previousTimestamp = |
303 System.currentTimeMillis() - CustomTabTabPersistencePolicy.STATE
_EXPIRY_THRESHOLD; | 329 System.currentTimeMillis() - CustomTabTabPersistencePolicy.STATE
_EXPIRY_THRESHOLD; |
304 assertTrue(metadataFile.setLastModified(previousTimestamp)); | 330 Assert.assertTrue(metadataFile.setLastModified(previousTimestamp)); |
305 | 331 |
306 policy.performInitialization(AsyncTask.SERIAL_EXECUTOR); | 332 policy.performInitialization(AsyncTask.SERIAL_EXECUTOR); |
307 policy.waitForInitializationToFinish(); | 333 policy.waitForInitializationToFinish(); |
308 | 334 |
309 assertTrue(metadataFile.lastModified() > previousTimestamp); | 335 Assert.assertTrue(metadataFile.lastModified() > previousTimestamp); |
310 } | 336 } |
311 | 337 |
312 private static List<File> generateMaximumStateFiles(long currentTime) { | 338 private static List<File> generateMaximumStateFiles(long currentTime) { |
313 List<File> validFiles = new ArrayList<>(); | 339 List<File> validFiles = new ArrayList<>(); |
314 for (int i = 0; i < CustomTabTabPersistencePolicy.MAXIMUM_STATE_FILES; i
++) { | 340 for (int i = 0; i < CustomTabTabPersistencePolicy.MAXIMUM_STATE_FILES; i
++) { |
315 validFiles.add(buildTestFile("testfile" + i, currentTime)); | 341 validFiles.add(buildTestFile("testfile" + i, currentTime)); |
316 } | 342 } |
317 return validFiles; | 343 return validFiles; |
318 } | 344 } |
319 | 345 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 | 458 |
433 CustomTabActivity activity = new CustomTabActivity(); | 459 CustomTabActivity activity = new CustomTabActivity(); |
434 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.CREATE
D); | 460 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.CREATE
D); |
435 TabModelSelectorImpl selector = new TabModelSelectorImpl( | 461 TabModelSelectorImpl selector = new TabModelSelectorImpl( |
436 activity, activity, buildTestPersistencePolicy(), false, false); | 462 activity, activity, buildTestPersistencePolicy(), false, false); |
437 selector.initializeForTesting(normalTabModel, incognitoTabModel); | 463 selector.initializeForTesting(normalTabModel, incognitoTabModel); |
438 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.DESTRO
YED); | 464 ApplicationStatus.onStateChangeForTesting(activity, ActivityState.DESTRO
YED); |
439 return selector; | 465 return selector; |
440 } | 466 } |
441 } | 467 } |
OLD | NEW |