OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.util.Base64; | 8 import android.util.Base64; |
9 | 9 |
| 10 import org.chromium.base.FileUtils; |
10 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
11 import org.chromium.base.StreamUtil; | 12 import org.chromium.base.StreamUtil; |
12 | 13 |
13 import java.io.File; | 14 import java.io.File; |
14 import java.io.FileNotFoundException; | 15 import java.io.FileNotFoundException; |
15 import java.io.FileOutputStream; | 16 import java.io.FileOutputStream; |
16 | 17 |
17 /** | 18 /** |
18 * Creates and fills up a directory with data suitable for testing code related
to {@link TabModel}. | 19 * Creates and fills up a directory with data suitable for testing code related
to {@link TabModel}. |
19 * Used for mocking out the real data directory. | 20 * Used for mocking out the real data directory. |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 * @param context Context to use. | 207 * @param context Context to use. |
207 * @param baseDirectoryName Name of the directory to store test data in. | 208 * @param baseDirectoryName Name of the directory to store test data in. |
208 * @param subdirectoryName Subdirectory of the base directory. May be null,
in which case the | 209 * @param subdirectoryName Subdirectory of the base directory. May be null,
in which case the |
209 * baseDirectoryName will store the data. This mock
s out how the | 210 * baseDirectoryName will store the data. This mock
s out how the |
210 * ChromeTabbedActivity instances all store their da
ta in different | 211 * ChromeTabbedActivity instances all store their da
ta in different |
211 * subdirectories of the base data directory. | 212 * subdirectories of the base data directory. |
212 */ | 213 */ |
213 public TestTabModelDirectory( | 214 public TestTabModelDirectory( |
214 Context context, String baseDirectoryName, String subdirectoryName)
throws Exception { | 215 Context context, String baseDirectoryName, String subdirectoryName)
throws Exception { |
215 mTestingDirectory = new File(context.getCacheDir(), baseDirectoryName); | 216 mTestingDirectory = new File(context.getCacheDir(), baseDirectoryName); |
216 if (mTestingDirectory.exists()) recursivelyDelete(mTestingDirectory); | 217 if (mTestingDirectory.exists()) FileUtils.recursivelyDeleteFile(mTesting
Directory); |
217 if (!mTestingDirectory.mkdirs()) { | 218 if (!mTestingDirectory.mkdirs()) { |
218 Log.e(TAG, "Failed to create: " + mTestingDirectory.getName()); | 219 Log.e(TAG, "Failed to create: " + mTestingDirectory.getName()); |
219 } | 220 } |
220 | 221 |
221 // Create the subdirectory. | 222 // Create the subdirectory. |
222 File dataDirectory = mTestingDirectory; | 223 File dataDirectory = mTestingDirectory; |
223 if (subdirectoryName != null) { | 224 if (subdirectoryName != null) { |
224 dataDirectory = new File(mTestingDirectory, subdirectoryName); | 225 dataDirectory = new File(mTestingDirectory, subdirectoryName); |
225 if (!dataDirectory.exists() && !dataDirectory.mkdirs()) { | 226 if (!dataDirectory.exists() && !dataDirectory.mkdirs()) { |
226 Log.e(TAG, "Failed to create subdirectory: " + dataDirectory.get
Name()); | 227 Log.e(TAG, "Failed to create subdirectory: " + dataDirectory.get
Name()); |
227 } | 228 } |
228 } | 229 } |
229 | 230 |
230 // Fill the subdirectory with mocked pre-generated TabState files and me
tadata for the | 231 // Fill the subdirectory with mocked pre-generated TabState files and me
tadata for the |
231 // TabPersistentStore. | 232 // TabPersistentStore. |
232 writeFile(dataDirectory, "tab_state", TAB_MODEL_METADATA_V4); | 233 writeFile(dataDirectory, "tab_state", TAB_MODEL_METADATA_V4); |
233 writeFile(dataDirectory, M18_NTP.filename, M18_NTP.encodedTabState); | 234 writeFile(dataDirectory, M18_NTP.filename, M18_NTP.encodedTabState); |
234 writeFile(dataDirectory, M26_GOOGLE_COM.filename, M26_GOOGLE_COM.encoded
TabState); | 235 writeFile(dataDirectory, M26_GOOGLE_COM.filename, M26_GOOGLE_COM.encoded
TabState); |
235 writeFile(dataDirectory, M18_GOOGLE_COM.filename, M18_GOOGLE_COM.encoded
TabState); | 236 writeFile(dataDirectory, M18_GOOGLE_COM.filename, M18_GOOGLE_COM.encoded
TabState); |
236 writeFile(dataDirectory, M26_GOOGLE_CA.filename, M26_GOOGLE_CA.encodedTa
bState); | 237 writeFile(dataDirectory, M26_GOOGLE_CA.filename, M26_GOOGLE_CA.encodedTa
bState); |
237 writeFile(dataDirectory, V2_BAIDU.filename, V2_BAIDU.encodedTabState); | 238 writeFile(dataDirectory, V2_BAIDU.filename, V2_BAIDU.encodedTabState); |
238 writeFile(dataDirectory, V2_DUCK_DUCK_GO.filename, V2_DUCK_DUCK_GO.encod
edTabState); | 239 writeFile(dataDirectory, V2_DUCK_DUCK_GO.filename, V2_DUCK_DUCK_GO.encod
edTabState); |
239 writeFile(dataDirectory, V2_HAARETZ.filename, V2_HAARETZ.encodedTabState
); | 240 writeFile(dataDirectory, V2_HAARETZ.filename, V2_HAARETZ.encodedTabState
); |
240 writeFile(dataDirectory, V2_TEXTAREA.filename, V2_TEXTAREA.encodedTabSta
te); | 241 writeFile(dataDirectory, V2_TEXTAREA.filename, V2_TEXTAREA.encodedTabSta
te); |
241 } | 242 } |
242 | 243 |
243 /** Nukes all the testing data. */ | 244 /** Nukes all the testing data. */ |
244 public void tearDown() throws Exception { | 245 public void tearDown() throws Exception { |
245 recursivelyDelete(mTestingDirectory); | 246 FileUtils.recursivelyDeleteFile(mTestingDirectory); |
246 } | 247 } |
247 | 248 |
248 /** Returns the base data directory. */ | 249 /** Returns the base data directory. */ |
249 public File getBaseDirectory() { | 250 public File getBaseDirectory() { |
250 return mTestingDirectory; | 251 return mTestingDirectory; |
251 } | 252 } |
252 | 253 |
253 private void writeFile(File directory, String filename, String data) throws
Exception { | 254 private void writeFile(File directory, String filename, String data) throws
Exception { |
254 File file = new File(directory, filename); | 255 File file = new File(directory, filename); |
255 FileOutputStream outputStream = null; | 256 FileOutputStream outputStream = null; |
256 try { | 257 try { |
257 outputStream = new FileOutputStream(file); | 258 outputStream = new FileOutputStream(file); |
258 outputStream.write(Base64.decode(data, 0)); | 259 outputStream.write(Base64.decode(data, 0)); |
259 } catch (FileNotFoundException e) { | 260 } catch (FileNotFoundException e) { |
260 assert false : "Failed to create " + filename; | 261 assert false : "Failed to create " + filename; |
261 } finally { | 262 } finally { |
262 StreamUtil.closeQuietly(outputStream); | 263 StreamUtil.closeQuietly(outputStream); |
263 } | 264 } |
264 } | 265 } |
265 | |
266 private void recursivelyDelete(File currentFile) throws Exception { | |
267 if (currentFile.isDirectory()) { | |
268 File[] files = currentFile.listFiles(); | |
269 if (files != null) { | |
270 for (File file : files) { | |
271 recursivelyDelete(file); | |
272 } | |
273 } | |
274 } | |
275 | |
276 if (!currentFile.delete()) Log.e(TAG, "Failed to delete: " + currentFile
); | |
277 } | |
278 } | 266 } |
OLD | NEW |