Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / test / android / javatests / src / org / chromium / chrome / test / util / ApplicationData.java
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.test.util;
6
7 import android.content.Context;
8
9 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
10
11 import org.chromium.content.browser.test.util.Criteria;
12 import org.chromium.content.browser.test.util.CriteriaHelper;
13
14 import java.io.File;
15 import java.util.concurrent.TimeUnit;
16
17 /**
18  * A utility class to do operations on the application data directory, such as clearing the
19  * application data.
20  */
21 public final class ApplicationData {
22
23     private static final long MAX_CLEAR_APP_DATA_TIMEOUT_MS =
24         scaleTimeout(TimeUnit.SECONDS.toMillis(3));
25     private static final long CLEAR_APP_DATA_POLL_INTERVAL_MS = MAX_CLEAR_APP_DATA_TIMEOUT_MS / 10;
26
27     private ApplicationData() {}
28
29     /**
30      * Clear all files in target contexts application directory, except the directory 'lib'.
31      * The timeout is specified in |MAX_CLEAR_APP_DATA_TIMEOUT_MS|.
32      *
33      * When deleting all files is complete, the 'cache' directory is recreated, to ensure that the
34      * framework does not try to create it for sandbox processes and fail.
35      *
36      * When this is invoked from tests, the target context from the instrumentation must be used.
37      *
38      * @param targetContext the target Context.
39      *
40      * @return Whether clearing the application data was successful.
41      */
42     public static boolean clearAppData(Context targetContext) throws InterruptedException {
43         final String appDir = getAppDirFromTargetContext(targetContext);
44         return CriteriaHelper.pollForCriteria(
45                 new Criteria() {
46                     private boolean mDataRemoved = false;
47
48                     @Override
49                     public boolean isSatisfied() {
50                         if (!mDataRemoved && !removeAppData(appDir)) {
51                             return false;
52                         }
53                         mDataRemoved = true;
54                         // We have to make sure the cache directory still exists, as the framework
55                         // will try to create it otherwise and will fail for sandbox processes with
56                         // a NullPointerException.
57                         File cacheDir = new File(appDir, "cache");
58                         return cacheDir.exists() || cacheDir.mkdir();
59                     }
60                 },
61                 MAX_CLEAR_APP_DATA_TIMEOUT_MS, CLEAR_APP_DATA_POLL_INTERVAL_MS);
62     }
63
64     /**
65      * Find the absolute path of the application data directory for the given target context.
66      *
67      * When this is invoked from tests, the target context from the instrumentation must be used.
68      *
69      * @param targetContext the target Context.
70      *
71      * @return the absolute path of the application data directory.
72      */
73     public static String getAppDirFromTargetContext(Context targetContext) {
74         String cacheDir = targetContext.getCacheDir().getAbsolutePath();
75         return cacheDir.substring(0, cacheDir.lastIndexOf('/'));
76     }
77
78     /**
79      * Remove all files and directories under the given application directory, except 'lib'.
80      *
81      * @param appDir the application directory to remove.
82      *
83      * @return whether removal succeeded.
84      */
85     private static boolean removeAppData(String appDir) {
86         File[] files = new File(appDir).listFiles();
87         if (files == null)
88             return true;
89         for (File file : files) {
90             if (!file.getAbsolutePath().endsWith("/lib") && !removeFile(file))
91                 return false;
92         }
93         return true;
94     }
95
96     /**
97      * Remove the given file or directory.
98      *
99      * @param file the file or directory to remove.
100      *
101      * @return whether removal succeeded.
102      */
103     private static boolean removeFile(File file) {
104         if (file.isDirectory()) {
105             File[] files = file.listFiles();
106             if (files == null)
107                 return true;
108             for (File sub_file : files) {
109                 if (!removeFile(sub_file))
110                     return false;
111             }
112         }
113         return file.delete();
114     }
115 }