Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/tabmodel/MultiInstanceMigrationTest.java

Issue 2766373004: Convert the rest of chrome_public_test_apk InstrumentationTestCases to JUnit4 (Closed)
Patch Set: nits and rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.tabmodel; 5 package org.chromium.chrome.browser.tabmodel;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.support.test.InstrumentationRegistry;
8 import android.support.test.filters.MediumTest; 9 import android.support.test.filters.MediumTest;
9 import android.test.InstrumentationTestCase; 10
11 import org.junit.After;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
10 16
11 import org.chromium.base.ContextUtils; 17 import org.chromium.base.ContextUtils;
12 import org.chromium.base.ThreadUtils; 18 import org.chromium.base.ThreadUtils;
13 import org.chromium.base.test.util.AdvancedMockContext; 19 import org.chromium.base.test.util.AdvancedMockContext;
14 import org.chromium.base.test.util.Feature; 20 import org.chromium.base.test.util.Feature;
15 import org.chromium.chrome.browser.TabState; 21 import org.chromium.chrome.browser.TabState;
22 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
16 import org.chromium.chrome.test.util.ApplicationData; 23 import org.chromium.chrome.test.util.ApplicationData;
17 import org.chromium.chrome.test.util.browser.tabmodel.MockTabModelSelector; 24 import org.chromium.chrome.test.util.browser.tabmodel.MockTabModelSelector;
18 25
19 import java.io.File; 26 import java.io.File;
20 import java.io.IOException; 27 import java.io.IOException;
21 28
22 /** 29 /**
23 * Test that migrating the old multi-instance tab state folder structure to the new one works. 30 * Test that migrating the old multi-instance tab state folder structure to the new one works.
24 * Previously each instance had its own subdirectory for storing files. Now ther e is one 31 * Previously each instance had its own subdirectory for storing files. Now ther e is one
25 * shared directory. 32 * shared directory.
26 */ 33 */
27 public class MultiInstanceMigrationTest extends InstrumentationTestCase { 34 @RunWith(ChromeJUnit4ClassRunner.class)
28 35 public class MultiInstanceMigrationTest {
29 private Context mAppContext; 36 private Context mAppContext;
30 37
31 @Override 38 @Before
32 protected void setUp() throws Exception { 39 public void setUp() throws Exception {
33 super.setUp(); 40 mAppContext = new AdvancedMockContext(InstrumentationRegistry.getInstrum entation()
34 mAppContext = new AdvancedMockContext( 41 .getTargetContext()
35 getInstrumentation().getTargetContext().getApplicationContext()) ; 42 .getApplicationContext());
36 ContextUtils.initApplicationContextForTests(mAppContext); 43 ContextUtils.initApplicationContextForTests(mAppContext);
37 ApplicationData.clearAppData(mAppContext); 44 ApplicationData.clearAppData(mAppContext);
38 45
39 // Set the shared pref stating that the legacy file migration has occurr ed. The 46 // Set the shared pref stating that the legacy file migration has occurr ed. The
40 // multi-instance migration won't happen if the legacy path is taken. 47 // multi-instance migration won't happen if the legacy path is taken.
41 ContextUtils.getAppSharedPreferences().edit().putBoolean( 48 ContextUtils.getAppSharedPreferences().edit().putBoolean(
42 TabbedModeTabPersistencePolicy.PREF_HAS_RUN_FILE_MIGRATION, true ).apply(); 49 TabbedModeTabPersistencePolicy.PREF_HAS_RUN_FILE_MIGRATION, true ).apply();
43 } 50 }
44 51
45 @Override 52 @After
46 protected void tearDown() throws Exception { 53 public void tearDown() throws Exception {
47 super.tearDown();
48 ApplicationData.clearAppData(mAppContext); 54 ApplicationData.clearAppData(mAppContext);
49 } 55 }
50 56
51 private void buildPersistentStoreAndWaitForMigration() { 57 private void buildPersistentStoreAndWaitForMigration() {
52 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 58 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
53 @Override 59 @Override
54 public void run() { 60 public void run() {
55 MockTabModelSelector selector = new MockTabModelSelector(0, 0, n ull); 61 MockTabModelSelector selector = new MockTabModelSelector(0, 0, n ull);
56 TabbedModeTabPersistencePolicy persistencePolicy = 62 TabbedModeTabPersistencePolicy persistencePolicy =
57 new TabbedModeTabPersistencePolicy(0, false); 63 new TabbedModeTabPersistencePolicy(0, false);
58 TabPersistentStore store = new TabPersistentStore( 64 TabPersistentStore store = new TabPersistentStore(
59 persistencePolicy, selector, null, null); 65 persistencePolicy, selector, null, null);
60 store.waitForMigrationToFinish(); 66 store.waitForMigrationToFinish();
61 } 67 }
62 }); 68 });
63 } 69 }
64 70
65 /** 71 /**
66 * Tests that normal migration of multi-instance state files works. 72 * Tests that normal migration of multi-instance state files works.
67 */ 73 */
74 @Test
68 @MediumTest 75 @MediumTest
69 @Feature({"TabPersistentStore"}) 76 @Feature({"TabPersistentStore"})
70 public void testMigrateData() throws IOException { 77 public void testMigrateData() throws IOException {
71 // Write old state files. 78 // Write old state files.
72 File[] stateDirs = createOldStateDirs(TabWindowManager.MAX_SIMULTANEOUS_ SELECTORS, true); 79 File[] stateDirs = createOldStateDirs(TabWindowManager.MAX_SIMULTANEOUS_ SELECTORS, true);
73 File stateFile0 = new File( 80 File stateFile0 = new File(
74 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 81 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
75 File stateFile1 = new File( 82 File stateFile1 = new File(
76 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 83 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
77 File stateFile2 = new File( 84 File stateFile2 = new File(
78 stateDirs[2], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 85 stateDirs[2], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
79 File customTabsStateFile = new File( 86 File customTabsStateFile = new File(
80 stateDirs[3], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 87 stateDirs[3], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
81 88
82 assertTrue("Could not create state file 0", stateFile0.createNewFile()); 89 Assert.assertTrue("Could not create state file 0", stateFile0.createNewF ile());
83 assertTrue("Could not create state file 1", stateFile1.createNewFile()); 90 Assert.assertTrue("Could not create state file 1", stateFile1.createNewF ile());
84 assertTrue("Could not create state file 2", stateFile2.createNewFile()); 91 Assert.assertTrue("Could not create state file 2", stateFile2.createNewF ile());
85 assertTrue("Could not create custom tabs state file", customTabsStateFil e.createNewFile()); 92 Assert.assertTrue(
93 "Could not create custom tabs state file", customTabsStateFile.c reateNewFile());
86 94
87 // Create a couple of tabs for each tab state subdirectory. 95 // Create a couple of tabs for each tab state subdirectory.
88 File tab0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREFIX + "0"); 96 File tab0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREFIX + "0");
89 File tab1 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREFIX + "1"); 97 File tab1 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREFIX + "1");
90 File tab2 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PREFIX + "2"); 98 File tab2 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PREFIX + "2");
91 File tab3 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PREFIX_ INCOGNITO + "3"); 99 File tab3 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PREFIX_ INCOGNITO + "3");
92 File tab4 = new File(stateDirs[2], TabState.SAVED_TAB_STATE_FILE_PREFIX + "4"); 100 File tab4 = new File(stateDirs[2], TabState.SAVED_TAB_STATE_FILE_PREFIX + "4");
93 File tab5 = new File(stateDirs[2], TabState.SAVED_TAB_STATE_FILE_PREFIX_ INCOGNITO + "5"); 101 File tab5 = new File(stateDirs[2], TabState.SAVED_TAB_STATE_FILE_PREFIX_ INCOGNITO + "5");
94 File tab6 = new File(stateDirs[3], TabState.SAVED_TAB_STATE_FILE_PREFIX + "6"); 102 File tab6 = new File(stateDirs[3], TabState.SAVED_TAB_STATE_FILE_PREFIX + "6");
95 103
96 assertTrue("Could not create tab 0 file", tab0.createNewFile()); 104 Assert.assertTrue("Could not create tab 0 file", tab0.createNewFile());
97 assertTrue("Could not create tab 1 file", tab1.createNewFile()); 105 Assert.assertTrue("Could not create tab 1 file", tab1.createNewFile());
98 assertTrue("Could not create tab 2 file", tab2.createNewFile()); 106 Assert.assertTrue("Could not create tab 2 file", tab2.createNewFile());
99 assertTrue("Could not create tab 3 file", tab3.createNewFile()); 107 Assert.assertTrue("Could not create tab 3 file", tab3.createNewFile());
100 assertTrue("Could not create tab 4 file", tab4.createNewFile()); 108 Assert.assertTrue("Could not create tab 4 file", tab4.createNewFile());
101 assertTrue("Could not create tab 5 file", tab5.createNewFile()); 109 Assert.assertTrue("Could not create tab 5 file", tab5.createNewFile());
102 assertTrue("Could not create tab 6 file", tab6.createNewFile()); 110 Assert.assertTrue("Could not create tab 6 file", tab6.createNewFile());
103 111
104 // Build the TabPersistentStore which will try to move the files. 112 // Build the TabPersistentStore which will try to move the files.
105 buildPersistentStoreAndWaitForMigration(); 113 buildPersistentStoreAndWaitForMigration();
106 114
107 // Make sure we don't hit the migration path again. 115 // Make sure we don't hit the migration path again.
108 assertTrue(ContextUtils.getAppSharedPreferences().getBoolean( 116 Assert.assertTrue(ContextUtils.getAppSharedPreferences().getBoolean(
109 TabbedModeTabPersistencePolicy.PREF_HAS_RUN_MULTI_INSTANCE_FILE_ MIGRATION, false)); 117 TabbedModeTabPersistencePolicy.PREF_HAS_RUN_MULTI_INSTANCE_FILE_ MIGRATION, false));
110 118
111 // Check that all metadata files moved. 119 // Check that all metadata files moved.
112 File newStateFile0 = new File( 120 File newStateFile0 = new File(
113 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(0) ); 121 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(0) );
114 File newStateFile1 = new File( 122 File newStateFile1 = new File(
115 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(1) ); 123 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(1) );
116 File newStateFile2 = new File( 124 File newStateFile2 = new File(
117 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(2) ); 125 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(2) );
118 File newCustomTabsStateFile = new File( 126 File newCustomTabsStateFile = new File(
119 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName( 127 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(
120 TabModelSelectorImpl.CUSTOM_TABS_SELECTOR_INDEX)); 128 TabModelSelectorImpl.CUSTOM_TABS_SELECTOR_INDEX));
121 assertTrue("Could not find new state file 0", newStateFile0.exists()); 129 Assert.assertTrue("Could not find new state file 0", newStateFile0.exist s());
122 assertTrue("Could not find new state file 1", newStateFile1.exists()); 130 Assert.assertTrue("Could not find new state file 1", newStateFile1.exist s());
123 assertTrue("Could not find new state file 2", newStateFile2.exists()); 131 Assert.assertTrue("Could not find new state file 2", newStateFile2.exist s());
124 assertTrue("Could not find new custom tabs state file", newCustomTabsSta teFile.exists()); 132 Assert.assertTrue(
125 assertFalse("Could still find old state file 0", stateFile0.exists()); 133 "Could not find new custom tabs state file", newCustomTabsStateF ile.exists());
126 assertFalse("Could still find old state file 1", stateFile1.exists()); 134 Assert.assertFalse("Could still find old state file 0", stateFile0.exist s());
127 assertFalse("Could still find old state file 2", stateFile2.exists()); 135 Assert.assertFalse("Could still find old state file 1", stateFile1.exist s());
128 assertFalse("Could still find old custom tabs state file", customTabsSta teFile.exists()); 136 Assert.assertFalse("Could still find old state file 2", stateFile2.exist s());
137 Assert.assertFalse(
138 "Could still find old custom tabs state file", customTabsStateFi le.exists());
129 139
130 // Check that tab 0 and 1 did not move. 140 // Check that tab 0 and 1 did not move.
131 assertTrue("Could not find tab 0 file", tab0.exists()); 141 Assert.assertTrue("Could not find tab 0 file", tab0.exists());
132 assertTrue("Could not find tab 1 file", tab1.exists()); 142 Assert.assertTrue("Could not find tab 1 file", tab1.exists());
133 143
134 // Check that tabs 2-5 did move. 144 // Check that tabs 2-5 did move.
135 File newTab2 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "2"); 145 File newTab2 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "2");
136 File newTab3 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX_INCOGNITO + "3"); 146 File newTab3 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX_INCOGNITO + "3");
137 File newTab4 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "4"); 147 File newTab4 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "4");
138 File newTab5 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX_INCOGNITO + "5"); 148 File newTab5 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX_INCOGNITO + "5");
139 assertTrue("Could not find new tab 2 file", newTab2.exists()); 149 Assert.assertTrue("Could not find new tab 2 file", newTab2.exists());
140 assertTrue("Could not find new tab 3 file", newTab3.exists()); 150 Assert.assertTrue("Could not find new tab 3 file", newTab3.exists());
141 assertTrue("Could not find new tab 4 file", newTab4.exists()); 151 Assert.assertTrue("Could not find new tab 4 file", newTab4.exists());
142 assertTrue("Could not find new tab 5 file", newTab5.exists()); 152 Assert.assertTrue("Could not find new tab 5 file", newTab5.exists());
143 assertFalse("Could still find old tab 2 file", tab2.exists()); 153 Assert.assertFalse("Could still find old tab 2 file", tab2.exists());
144 assertFalse("Could still find old tab 3 file", tab3.exists()); 154 Assert.assertFalse("Could still find old tab 3 file", tab3.exists());
145 assertFalse("Could still find old tab 4 file", tab4.exists()); 155 Assert.assertFalse("Could still find old tab 4 file", tab4.exists());
146 assertFalse("Could still find old tab 5 file", tab5.exists()); 156 Assert.assertFalse("Could still find old tab 5 file", tab5.exists());
147 157
148 // Check that the custom tab (tab 6) was deleted. 158 // Check that the custom tab (tab 6) was deleted.
149 File newTab6 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "6"); 159 File newTab6 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "6");
150 assertFalse("Could still find old tab 6 file", tab6.exists()); 160 Assert.assertFalse("Could still find old tab 6 file", tab6.exists());
151 assertFalse("Found new tab 6 file. It should have been deleted.", newTab 6.exists()); 161 Assert.assertFalse("Found new tab 6 file. It should have been deleted.", newTab6.exists());
152 162
153 // Check that old directories were deleted. 163 // Check that old directories were deleted.
154 assertFalse("Could still find old state dir 1", stateDirs[1].exists()); 164 Assert.assertFalse("Could still find old state dir 1", stateDirs[1].exis ts());
155 assertFalse("Could still find old state dir 2", stateDirs[2].exists()); 165 Assert.assertFalse("Could still find old state dir 2", stateDirs[2].exis ts());
156 assertFalse("Could still find old custom tabs state dir", stateDirs[3].e xists()); 166 Assert.assertFalse("Could still find old custom tabs state dir", stateDi rs[3].exists());
157 } 167 }
158 168
159 /** 169 /**
160 * Tests that the state file migration skips unrelated files. Also tests tha t migration works 170 * Tests that the state file migration skips unrelated files. Also tests tha t migration works
161 * if the number of tab state subdirectories to migrate is less than 171 * if the number of tab state subdirectories to migrate is less than
162 * {@code TabWindowManager.MAX_SIMULTANEOUS_SELECTORS} 172 * {@code TabWindowManager.MAX_SIMULTANEOUS_SELECTORS}
163 */ 173 */
174 @Test
164 @MediumTest 175 @MediumTest
165 @Feature({"TabPersistentStore"}) 176 @Feature({"TabPersistentStore"})
166 public void testMigrationLeavesOtherFilesAlone() throws IOException { 177 public void testMigrationLeavesOtherFilesAlone() throws IOException {
167 // Write old state files and an extra file. 178 // Write old state files and an extra file.
168 File[] stateDirs = createOldStateDirs(2, false); 179 File[] stateDirs = createOldStateDirs(2, false);
169 File stateFile0 = new File( 180 File stateFile0 = new File(
170 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 181 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
171 File stateFile1 = new File( 182 File stateFile1 = new File(
172 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 183 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
173 File tab0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREFIX + "0"); 184 File tab0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREFIX + "0");
174 File tab1 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PREFIX + "1"); 185 File tab1 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PREFIX + "1");
175 File otherFile = new File(stateDirs[1], "other.file"); 186 File otherFile = new File(stateDirs[1], "other.file");
176 187
177 assertTrue("Could not create state file 0", stateFile0.createNewFile()); 188 Assert.assertTrue("Could not create state file 0", stateFile0.createNewF ile());
178 assertTrue("Could not create state file 1", stateFile1.createNewFile()); 189 Assert.assertTrue("Could not create state file 1", stateFile1.createNewF ile());
179 assertTrue("Could not create tab 0 file", tab0.createNewFile()); 190 Assert.assertTrue("Could not create tab 0 file", tab0.createNewFile());
180 assertTrue("Could not create tab 1 file", tab1.createNewFile()); 191 Assert.assertTrue("Could not create tab 1 file", tab1.createNewFile());
181 assertTrue("Could not create other file", otherFile.createNewFile()); 192 Assert.assertTrue("Could not create other file", otherFile.createNewFile ());
182 193
183 // Build the TabPersistentStore which will try to move the files. 194 // Build the TabPersistentStore which will try to move the files.
184 buildPersistentStoreAndWaitForMigration(); 195 buildPersistentStoreAndWaitForMigration();
185 196
186 // Check that the other file wasn't moved. 197 // Check that the other file wasn't moved.
187 File newOtherFile = new File(stateDirs[0], "other.file"); 198 File newOtherFile = new File(stateDirs[0], "other.file");
188 assertFalse("Could find new other file", newOtherFile.exists()); 199 Assert.assertFalse("Could find new other file", newOtherFile.exists());
189 assertTrue("Could not find original other file", otherFile.exists()); 200 Assert.assertTrue("Could not find original other file", otherFile.exists ());
190 201
191 // Check that the metadata files were renamed and/or moved. 202 // Check that the metadata files were renamed and/or moved.
192 File newStateFile0 = new File( 203 File newStateFile0 = new File(
193 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(0) ); 204 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(0) );
194 File newStateFile1 = new File( 205 File newStateFile1 = new File(
195 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(1) ); 206 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(1) );
196 assertTrue("Could not find new state file 0", newStateFile0.exists()); 207 Assert.assertTrue("Could not find new state file 0", newStateFile0.exist s());
197 assertTrue("Could not find new state file 1", newStateFile1.exists()); 208 Assert.assertTrue("Could not find new state file 1", newStateFile1.exist s());
198 assertFalse("Could still find old state file 0", stateFile0.exists()); 209 Assert.assertFalse("Could still find old state file 0", stateFile0.exist s());
199 assertFalse("Could still find old state file 1", stateFile1.exists()); 210 Assert.assertFalse("Could still find old state file 1", stateFile1.exist s());
200 211
201 // Check that tab 0 did not move. 212 // Check that tab 0 did not move.
202 assertTrue("Could not find tab 0 file", tab0.exists()); 213 Assert.assertTrue("Could not find tab 0 file", tab0.exists());
203 214
204 // Check that tab 1 did move. 215 // Check that tab 1 did move.
205 File newTab1 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "1"); 216 File newTab1 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PREF IX + "1");
206 assertTrue("Could not find tab 1 file", newTab1.exists()); 217 Assert.assertTrue("Could not find tab 1 file", newTab1.exists());
207 assertFalse("Could still find old tab 1 file", tab1.exists()); 218 Assert.assertFalse("Could still find old tab 1 file", tab1.exists());
208 } 219 }
209 220
210 /** 221 /**
211 * Tests that migration of multi-instance state files works when tab files w ith the same name 222 * Tests that migration of multi-instance state files works when tab files w ith the same name
212 * exists in both directories. 223 * exists in both directories.
213 */ 224 */
225 @Test
214 @MediumTest 226 @MediumTest
215 @Feature({"TabPersistentStore"}) 227 @Feature({"TabPersistentStore"})
216 public void testMigrateDataDuplicateTabFiles() throws IOException { 228 public void testMigrateDataDuplicateTabFiles() throws IOException {
217 // Write old state files. 229 // Write old state files.
218 File[] stateDirs = createOldStateDirs(2, false); 230 File[] stateDirs = createOldStateDirs(2, false);
219 File stateFile0 = new File( 231 File stateFile0 = new File(
220 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 232 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
221 File stateFile1 = new File( 233 File stateFile1 = new File(
222 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 234 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
223 235
224 assertTrue("Could not create state file 0", stateFile0.createNewFile()); 236 Assert.assertTrue("Could not create state file 0", stateFile0.createNewF ile());
225 assertTrue("Could not create state file 1", stateFile1.createNewFile()); 237 Assert.assertTrue("Could not create state file 1", stateFile1.createNewF ile());
226 238
227 // Create duplicate "tab0" files and ensure tab0Dir1 has been modified m ore recently so that 239 // Create duplicate "tab0" files and ensure tab0Dir1 has been modified m ore recently so that
228 // it overwrites tab0Dir0. 240 // it overwrites tab0Dir0.
229 File tab0Dir0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "0"); 241 File tab0Dir0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "0");
230 File tab0Dir1 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "0"); 242 File tab0Dir1 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "0");
231 assertTrue("Could not create tab 0-0 file", tab0Dir0.createNewFile()); 243 Assert.assertTrue("Could not create tab 0-0 file", tab0Dir0.createNewFil e());
232 assertTrue("Could not create tab 0-1 file", tab0Dir1.createNewFile()); 244 Assert.assertTrue("Could not create tab 0-1 file", tab0Dir1.createNewFil e());
233 long expectedTab0LastModifiedTime = tab0Dir0.lastModified() + 1000; 245 long expectedTab0LastModifiedTime = tab0Dir0.lastModified() + 1000;
234 if (!tab0Dir1.setLastModified(expectedTab0LastModifiedTime)) { 246 if (!tab0Dir1.setLastModified(expectedTab0LastModifiedTime)) {
235 fail("Failed to set last modified time."); 247 Assert.fail("Failed to set last modified time.");
236 } 248 }
237 249
238 // Create duplicate "tab1" files and ensure tab1Dir0 has been modified m ore recently so that 250 // Create duplicate "tab1" files and ensure tab1Dir0 has been modified m ore recently so that
239 // it does not get overwritten. 251 // it does not get overwritten.
240 File tab1Dir0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "1"); 252 File tab1Dir0 = new File(stateDirs[0], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "1");
241 File tab1Dir1 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "1"); 253 File tab1Dir1 = new File(stateDirs[1], TabState.SAVED_TAB_STATE_FILE_PRE FIX + "1");
242 assertTrue("Could not create tab 1-0 file", tab1Dir0.createNewFile()); 254 Assert.assertTrue("Could not create tab 1-0 file", tab1Dir0.createNewFil e());
243 assertTrue("Could not create tab 1-1 file", tab1Dir1.createNewFile()); 255 Assert.assertTrue("Could not create tab 1-1 file", tab1Dir1.createNewFil e());
244 long expectedTab1LastModifiedTime = tab1Dir1.lastModified() + 1000; 256 long expectedTab1LastModifiedTime = tab1Dir1.lastModified() + 1000;
245 if (!tab1Dir0.setLastModified(expectedTab1LastModifiedTime)) { 257 if (!tab1Dir0.setLastModified(expectedTab1LastModifiedTime)) {
246 fail("Failed to set last modified time."); 258 Assert.fail("Failed to set last modified time.");
247 } 259 }
248 260
249 // Build the TabPersistentStore which will try to move the files. 261 // Build the TabPersistentStore which will try to move the files.
250 buildPersistentStoreAndWaitForMigration(); 262 buildPersistentStoreAndWaitForMigration();
251 263
252 // Check that "tab0" still exists and has the expected last modified tim e. 264 // Check that "tab0" still exists and has the expected last modified tim e.
253 assertTrue("Could not find tab 0 file", tab0Dir0.exists()); 265 Assert.assertTrue("Could not find tab 0 file", tab0Dir0.exists());
254 assertFalse("Could still find old tab 0 file", tab0Dir1.exists()); 266 Assert.assertFalse("Could still find old tab 0 file", tab0Dir1.exists()) ;
255 assertEquals("tab 0 file not overwritten properly", 267 Assert.assertEquals("tab 0 file not overwritten properly", expectedTab0L astModifiedTime,
256 expectedTab0LastModifiedTime,
257 tab0Dir0.lastModified()); 268 tab0Dir0.lastModified());
258 269
259 // Check that "tab1" still exists and has the expected last modified tim e. 270 // Check that "tab1" still exists and has the expected last modified tim e.
260 assertTrue("Could not find tab 1 file", tab1Dir0.exists()); 271 Assert.assertTrue("Could not find tab 1 file", tab1Dir0.exists());
261 assertFalse("Could still find old tab 1 file", tab1Dir1.exists()); 272 Assert.assertFalse("Could still find old tab 1 file", tab1Dir1.exists()) ;
262 assertEquals("tab 1 file unexpectedly overwritten", 273 Assert.assertEquals("tab 1 file unexpectedly overwritten", expectedTab1L astModifiedTime,
263 expectedTab1LastModifiedTime,
264 tab1Dir0.lastModified()); 274 tab1Dir0.lastModified());
265 275
266 // Check that old directory was deleted. 276 // Check that old directory was deleted.
267 assertFalse("Could still find old state dir 1", stateDirs[1].exists()); 277 Assert.assertFalse("Could still find old state dir 1", stateDirs[1].exis ts());
268 } 278 }
269 279
270 /** 280 /**
271 * Tests that tab_state0 is not overwritten if it already exists when migrat ion is attempted. 281 * Tests that tab_state0 is not overwritten if it already exists when migrat ion is attempted.
272 */ 282 */
283 @Test
273 @MediumTest 284 @MediumTest
274 @Feature({"TabPersistentStore"}) 285 @Feature({"TabPersistentStore"})
275 public void testNewMetataFileExists() throws IOException { 286 public void testNewMetataFileExists() throws IOException {
276 // Set up two old metadata files. 287 // Set up two old metadata files.
277 File[] stateDirs = createOldStateDirs(TabWindowManager.MAX_SIMULTANEOUS_ SELECTORS, true); 288 File[] stateDirs = createOldStateDirs(TabWindowManager.MAX_SIMULTANEOUS_ SELECTORS, true);
278 File stateFile0 = new File( 289 File stateFile0 = new File(
279 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 290 stateDirs[0], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
280 File stateFile1 = new File( 291 File stateFile1 = new File(
281 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE); 292 stateDirs[1], TabbedModeTabPersistencePolicy.LEGACY_SAVED_STATE_ FILE);
282 293
283 assertTrue("Could not create state file 0", stateFile0.createNewFile()); 294 Assert.assertTrue("Could not create state file 0", stateFile0.createNewF ile());
284 assertTrue("Could not create state file 1", stateFile1.createNewFile()); 295 Assert.assertTrue("Could not create state file 1", stateFile1.createNewF ile());
285 296
286 // Create a new metadata file. 297 // Create a new metadata file.
287 File newStateFile0 = new File( 298 File newStateFile0 = new File(
288 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(0) ); 299 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(0) );
289 assertTrue("Could not create new state file 0", newStateFile0.createNewF ile()); 300 Assert.assertTrue("Could not create new state file 0", newStateFile0.cre ateNewFile());
290 long expectedLastModifiedTime = newStateFile0.lastModified() - 1000000; 301 long expectedLastModifiedTime = newStateFile0.lastModified() - 1000000;
291 if (!newStateFile0.setLastModified(expectedLastModifiedTime)) { 302 if (!newStateFile0.setLastModified(expectedLastModifiedTime)) {
292 fail("Failed to set last modified time."); 303 Assert.fail("Failed to set last modified time.");
293 } 304 }
294 305
295 // Build the TabPersistentStore which will try to move the files. 306 // Build the TabPersistentStore which will try to move the files.
296 buildPersistentStoreAndWaitForMigration(); 307 buildPersistentStoreAndWaitForMigration();
297 308
298 // Check that new metadata file was not overwritten during migration. 309 // Check that new metadata file was not overwritten during migration.
299 assertEquals("State file 0 unexpectedly overwritten", expectedLastModifi edTime, 310 Assert.assertEquals("State file 0 unexpectedly overwritten", expectedLas tModifiedTime,
300 newStateFile0.lastModified()); 311 newStateFile0.lastModified());
301 312
302 // Check that migration of other files still occurred. 313 // Check that migration of other files still occurred.
303 File newStateFile1 = new File( 314 File newStateFile1 = new File(
304 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(1) ); 315 stateDirs[0], TabbedModeTabPersistencePolicy.getStateFileName(1) );
305 assertTrue("Could not find new state file 1", newStateFile1.exists()); 316 Assert.assertTrue("Could not find new state file 1", newStateFile1.exist s());
306 } 317 }
307 318
308 /** 319 /**
309 * Creates tab state directories using the old pre-multi-instance migration file paths where 320 * Creates tab state directories using the old pre-multi-instance migration file paths where
310 * each tab model had its own state directory. 321 * each tab model had its own state directory.
311 * 322 *
312 * @param numRegularDirsToCreate The number of regular tab state directories to create. 323 * @param numRegularDirsToCreate The number of regular tab state directories to create.
313 * @param createCustomTabsDir Whether a custom tabs directory should be crea ted. 324 * @param createCustomTabsDir Whether a custom tabs directory should be crea ted.
314 * @return An array containing the tab state directories. If createCustomTab sDir is true, 325 * @return An array containing the tab state directories. If createCustomTab sDir is true,
315 * a directory for custom tabs will be added to the end of the array . 326 * a directory for custom tabs will be added to the end of the array .
316 */ 327 */
317 private File[] createOldStateDirs(int numRegularDirsToCreate, boolean create CustomTabsDir) { 328 private File[] createOldStateDirs(int numRegularDirsToCreate, boolean create CustomTabsDir) {
318 int numDirsToCreate = 329 int numDirsToCreate =
319 createCustomTabsDir ? numRegularDirsToCreate + 1 : numRegularDir sToCreate; 330 createCustomTabsDir ? numRegularDirsToCreate + 1 : numRegularDir sToCreate;
320 File[] stateDirs = new File[numDirsToCreate]; 331 File[] stateDirs = new File[numDirsToCreate];
321 for (int i = 0; i < numRegularDirsToCreate; i++) { 332 for (int i = 0; i < numRegularDirsToCreate; i++) {
322 stateDirs[i] = new File(TabPersistentStore.getOrCreateBaseStateDirec tory(), 333 stateDirs[i] = new File(TabPersistentStore.getOrCreateBaseStateDirec tory(),
323 Integer.toString(i)); 334 Integer.toString(i));
324 if (!stateDirs[i].exists()) { 335 if (!stateDirs[i].exists()) {
325 assertTrue("Could not create state dir " + i, stateDirs[i].mkdir ()); 336 Assert.assertTrue("Could not create state dir " + i, stateDirs[i ].mkdir());
326 } 337 }
327 } 338 }
328 339
329 if (createCustomTabsDir) { 340 if (createCustomTabsDir) {
330 stateDirs[numDirsToCreate - 1] = new File( 341 stateDirs[numDirsToCreate - 1] = new File(
331 TabPersistentStore.getOrCreateBaseStateDirectory(), 342 TabPersistentStore.getOrCreateBaseStateDirectory(),
332 Integer.toString(TabModelSelectorImpl.CUSTOM_TABS_SELECTOR_I NDEX)); 343 Integer.toString(TabModelSelectorImpl.CUSTOM_TABS_SELECTOR_I NDEX));
333 if (!stateDirs[numDirsToCreate - 1].exists()) { 344 if (!stateDirs[numDirsToCreate - 1].exists()) {
334 assertTrue("Could not create custom tab state dir", 345 Assert.assertTrue("Could not create custom tab state dir",
335 stateDirs[numDirsToCreate - 1].mkdir()); 346 stateDirs[numDirsToCreate - 1].mkdir());
336 } 347 }
337 } 348 }
338 349
339 return stateDirs; 350 return stateDirs;
340 } 351 }
341 } 352 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698