| 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.webapps; | 5 package org.chromium.chrome.browser.webapps; |
| 6 | 6 |
| 7 import android.content.Intent; | 7 import android.content.Intent; |
| 8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
| 9 import android.net.Uri; | 9 import android.net.Uri; |
| 10 import android.os.AsyncTask; | 10 import android.os.AsyncTask; |
| 11 import android.os.Build; | 11 import android.os.Build; |
| 12 import android.support.test.InstrumentationRegistry; |
| 12 import android.support.test.filters.SmallTest; | 13 import android.support.test.filters.SmallTest; |
| 13 import android.test.InstrumentationTestCase; | 14 |
| 15 import org.junit.After; |
| 16 import org.junit.Assert; |
| 17 import org.junit.Before; |
| 18 import org.junit.Test; |
| 19 import org.junit.runner.RunWith; |
| 14 | 20 |
| 15 import org.chromium.base.FileUtils; | 21 import org.chromium.base.FileUtils; |
| 16 import org.chromium.base.metrics.RecordHistogram; | 22 import org.chromium.base.metrics.RecordHistogram; |
| 17 import org.chromium.base.test.util.AdvancedMockContext; | 23 import org.chromium.base.test.util.AdvancedMockContext; |
| 18 import org.chromium.base.test.util.Feature; | 24 import org.chromium.base.test.util.Feature; |
| 25 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 19 import org.chromium.content.browser.test.util.Criteria; | 26 import org.chromium.content.browser.test.util.Criteria; |
| 20 import org.chromium.content.browser.test.util.CriteriaHelper; | 27 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 21 | 28 |
| 22 import java.io.File; | 29 import java.io.File; |
| 23 import java.util.HashSet; | 30 import java.util.HashSet; |
| 24 import java.util.Set; | 31 import java.util.Set; |
| 25 import java.util.concurrent.Callable; | 32 import java.util.concurrent.Callable; |
| 26 | 33 |
| 27 /** | 34 /** |
| 28 * Tests that directories for WebappActivities are managed correctly. | 35 * Tests that directories for WebappActivities are managed correctly. |
| 29 */ | 36 */ |
| 30 public class WebappDirectoryManagerTest extends InstrumentationTestCase { | 37 @RunWith(ChromeJUnit4ClassRunner.class) |
| 38 public class WebappDirectoryManagerTest { |
| 31 private static final String TAG = "webapps_WebappDirect"; | 39 private static final String TAG = "webapps_WebappDirect"; |
| 32 | 40 |
| 33 private static final String WEBAPP_ID_1 = "webapp_1"; | 41 private static final String WEBAPP_ID_1 = "webapp_1"; |
| 34 private static final String WEBAPP_ID_2 = "webapp_2"; | 42 private static final String WEBAPP_ID_2 = "webapp_2"; |
| 35 private static final String WEBAPP_ID_3 = "webapp_3"; | 43 private static final String WEBAPP_ID_3 = "webapp_3"; |
| 36 | 44 |
| 37 private static class TestWebappDirectoryManager extends WebappDirectoryManag
er { | 45 private static class TestWebappDirectoryManager extends WebappDirectoryManag
er { |
| 38 private Set<Intent> mBaseIntents = new HashSet<Intent>(); | 46 private Set<Intent> mBaseIntents = new HashSet<Intent>(); |
| 39 | 47 |
| 40 @Override | 48 @Override |
| 41 protected Set<Intent> getBaseIntentsForAllTasks() { | 49 protected Set<Intent> getBaseIntentsForAllTasks() { |
| 42 return mBaseIntents; | 50 return mBaseIntents; |
| 43 } | 51 } |
| 44 } | 52 } |
| 45 | 53 |
| 46 private class WebappMockContext extends AdvancedMockContext { | 54 private static class WebappMockContext extends AdvancedMockContext { |
| 47 public WebappMockContext() { | 55 public WebappMockContext() { |
| 48 super(getInstrumentation().getTargetContext()); | 56 super(InstrumentationRegistry.getInstrumentation().getTargetContext(
)); |
| 49 } | 57 } |
| 50 | 58 |
| 51 /** Returns a directory for test data inside the cache folder. */ | 59 /** Returns a directory for test data inside the cache folder. */ |
| 52 public String getBaseDirectory() { | 60 public String getBaseDirectory() { |
| 53 File cacheDirectory = getInstrumentation().getTargetContext().getCac
heDir(); | 61 File cacheDirectory = |
| 62 InstrumentationRegistry.getInstrumentation().getTargetContex
t().getCacheDir(); |
| 54 return new File(cacheDirectory, "WebappDirectoryManagerTest").getAbs
olutePath(); | 63 return new File(cacheDirectory, "WebappDirectoryManagerTest").getAbs
olutePath(); |
| 55 } | 64 } |
| 56 | 65 |
| 57 @Override | 66 @Override |
| 58 public ApplicationInfo getApplicationInfo() { | 67 public ApplicationInfo getApplicationInfo() { |
| 59 ApplicationInfo mockInfo = new ApplicationInfo(); | 68 ApplicationInfo mockInfo = new ApplicationInfo(); |
| 60 mockInfo.dataDir = getBaseDirectory(); | 69 mockInfo.dataDir = getBaseDirectory(); |
| 61 return mockInfo; | 70 return mockInfo; |
| 62 } | 71 } |
| 63 | 72 |
| 64 @Override | 73 @Override |
| 65 public File getDir(String name, int mode) { | 74 public File getDir(String name, int mode) { |
| 66 File appDirectory = new File(getApplicationInfo().dataDir, "app_" +
name); | 75 File appDirectory = new File(getApplicationInfo().dataDir, "app_" +
name); |
| 67 assertTrue(appDirectory.exists() || appDirectory.mkdirs()); | 76 Assert.assertTrue(appDirectory.exists() || appDirectory.mkdirs()); |
| 68 return appDirectory; | 77 return appDirectory; |
| 69 } | 78 } |
| 70 } | 79 } |
| 71 | 80 |
| 72 private WebappMockContext mMockContext; | 81 private WebappMockContext mMockContext; |
| 73 private TestWebappDirectoryManager mWebappDirectoryManager; | 82 private TestWebappDirectoryManager mWebappDirectoryManager; |
| 74 | 83 |
| 75 @Override | 84 @Before |
| 76 public void setUp() throws Exception { | 85 public void setUp() throws Exception { |
| 77 super.setUp(); | |
| 78 RecordHistogram.setDisabledForTests(true); | 86 RecordHistogram.setDisabledForTests(true); |
| 79 mMockContext = new WebappMockContext(); | 87 mMockContext = new WebappMockContext(); |
| 80 mWebappDirectoryManager = new TestWebappDirectoryManager(); | 88 mWebappDirectoryManager = new TestWebappDirectoryManager(); |
| 81 | 89 |
| 82 // Set up the base directories. | 90 // Set up the base directories. |
| 83 File baseDirectory = new File(mMockContext.getBaseDirectory()); | 91 File baseDirectory = new File(mMockContext.getBaseDirectory()); |
| 84 FileUtils.recursivelyDeleteFile(baseDirectory); | 92 FileUtils.recursivelyDeleteFile(baseDirectory); |
| 85 assertTrue(baseDirectory.mkdirs()); | 93 Assert.assertTrue(baseDirectory.mkdirs()); |
| 86 } | 94 } |
| 87 | 95 |
| 88 @Override | 96 @After |
| 89 public void tearDown() throws Exception { | 97 public void tearDown() throws Exception { |
| 90 FileUtils.recursivelyDeleteFile(new File(mMockContext.getBaseDirectory()
)); | 98 FileUtils.recursivelyDeleteFile(new File(mMockContext.getBaseDirectory()
)); |
| 91 RecordHistogram.setDisabledForTests(false); | 99 RecordHistogram.setDisabledForTests(false); |
| 92 super.tearDown(); | |
| 93 } | 100 } |
| 94 | 101 |
| 102 @Test |
| 95 @SmallTest | 103 @SmallTest |
| 96 @Feature({"Webapps"}) | 104 @Feature({"Webapps"}) |
| 97 public void testDeletesOwnDirectory() throws Exception { | 105 public void testDeletesOwnDirectory() throws Exception { |
| 98 File webappDirectory = new File( | 106 File webappDirectory = new File( |
| 99 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_1); | 107 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_1); |
| 100 assertTrue(webappDirectory.mkdirs()); | 108 Assert.assertTrue(webappDirectory.mkdirs()); |
| 101 assertTrue(webappDirectory.exists()); | 109 Assert.assertTrue(webappDirectory.exists()); |
| 102 | 110 |
| 103 // Confirm that it deletes the current web app's directory. | 111 // Confirm that it deletes the current web app's directory. |
| 104 runCleanup(); | 112 runCleanup(); |
| 105 assertFalse(webappDirectory.exists()); | 113 Assert.assertFalse(webappDirectory.exists()); |
| 106 } | 114 } |
| 107 | 115 |
| 108 /** | 116 /** |
| 109 * On Lollipop and higher, the {@link WebappDirectoryManager} also deletes d
irectories for web | 117 * On Lollipop and higher, the {@link WebappDirectoryManager} also deletes d
irectories for web |
| 110 * apps that no longer correspond to tasks in Recents. | 118 * apps that no longer correspond to tasks in Recents. |
| 111 */ | 119 */ |
| 120 @Test |
| 112 @SmallTest | 121 @SmallTest |
| 113 @Feature({"Webapps"}) | 122 @Feature({"Webapps"}) |
| 114 public void testDeletesDirectoriesForDeadTasks() throws Exception { | 123 public void testDeletesDirectoriesForDeadTasks() throws Exception { |
| 115 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; | 124 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; |
| 116 | 125 |
| 117 // Track the three web app directories. | 126 // Track the three web app directories. |
| 118 File directory1 = new File( | 127 File directory1 = new File( |
| 119 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_1); | 128 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_1); |
| 120 File directory2 = new File( | 129 File directory2 = new File( |
| 121 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_2); | 130 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_2); |
| 122 File directory3 = new File( | 131 File directory3 = new File( |
| 123 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_3); | 132 mWebappDirectoryManager.getBaseWebappDirectory(mMockContext), WE
BAPP_ID_3); |
| 124 | 133 |
| 125 // Seed the directory with folders for web apps. | 134 // Seed the directory with folders for web apps. |
| 126 assertTrue(directory1.mkdirs()); | 135 Assert.assertTrue(directory1.mkdirs()); |
| 127 assertTrue(directory2.mkdirs()); | 136 Assert.assertTrue(directory2.mkdirs()); |
| 128 assertTrue(directory3.mkdirs()); | 137 Assert.assertTrue(directory3.mkdirs()); |
| 129 | 138 |
| 130 // Indicate that another of the web apps is listed in Recents; in real u
sage this web app | 139 // Indicate that another of the web apps is listed in Recents; in real u
sage this web app |
| 131 // would not be in the foreground and would have persisted its state. | 140 // would not be in the foreground and would have persisted its state. |
| 132 mWebappDirectoryManager.mBaseIntents.add( | 141 mWebappDirectoryManager.mBaseIntents.add( |
| 133 new Intent(Intent.ACTION_VIEW, Uri.parse("webapp://webapp_2"))); | 142 new Intent(Intent.ACTION_VIEW, Uri.parse("webapp://webapp_2"))); |
| 134 | 143 |
| 135 // Only the directory for the background web app should survive. | 144 // Only the directory for the background web app should survive. |
| 136 runCleanup(); | 145 runCleanup(); |
| 137 assertFalse(directory1.exists()); | 146 Assert.assertFalse(directory1.exists()); |
| 138 assertTrue(directory2.exists()); | 147 Assert.assertTrue(directory2.exists()); |
| 139 assertFalse(directory3.exists()); | 148 Assert.assertFalse(directory3.exists()); |
| 140 } | 149 } |
| 141 | 150 |
| 151 @Test |
| 142 @SmallTest | 152 @SmallTest |
| 143 @Feature({"Webapps"}) | 153 @Feature({"Webapps"}) |
| 144 public void testDeletesObsoleteDirectories() throws Exception { | 154 public void testDeletesObsoleteDirectories() throws Exception { |
| 145 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; | 155 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; |
| 146 | 156 |
| 147 // Seed the base directory with folders that correspond to pre-L web app
s. | 157 // Seed the base directory with folders that correspond to pre-L web app
s. |
| 148 File baseDirectory = new File(mMockContext.getBaseDirectory()); | 158 File baseDirectory = new File(mMockContext.getBaseDirectory()); |
| 149 File webappDirectory1 = new File(baseDirectory, "app_WebappActivity1"); | 159 File webappDirectory1 = new File(baseDirectory, "app_WebappActivity1"); |
| 150 File webappDirectory6 = new File(baseDirectory, "app_WebappActivity6"); | 160 File webappDirectory6 = new File(baseDirectory, "app_WebappActivity6"); |
| 151 File nonWebappDirectory = new File(baseDirectory, "app_ChromeDocumentAct
ivity"); | 161 File nonWebappDirectory = new File(baseDirectory, "app_ChromeDocumentAct
ivity"); |
| 152 assertTrue(webappDirectory1.mkdirs()); | 162 Assert.assertTrue(webappDirectory1.mkdirs()); |
| 153 assertTrue(webappDirectory6.mkdirs()); | 163 Assert.assertTrue(webappDirectory6.mkdirs()); |
| 154 assertTrue(nonWebappDirectory.mkdirs()); | 164 Assert.assertTrue(nonWebappDirectory.mkdirs()); |
| 155 | 165 |
| 156 // Make sure only the web app folders are deleted. | 166 // Make sure only the web app folders are deleted. |
| 157 runCleanup(); | 167 runCleanup(); |
| 158 assertFalse(webappDirectory1.exists()); | 168 Assert.assertFalse(webappDirectory1.exists()); |
| 159 assertFalse(webappDirectory6.exists()); | 169 Assert.assertFalse(webappDirectory6.exists()); |
| 160 assertTrue(nonWebappDirectory.exists()); | 170 Assert.assertTrue(nonWebappDirectory.exists()); |
| 161 } | 171 } |
| 162 | 172 |
| 163 private void runCleanup() throws Exception { | 173 private void runCleanup() throws Exception { |
| 164 final AsyncTask task = | 174 final AsyncTask task = |
| 165 mWebappDirectoryManager.cleanUpDirectories(mMockContext, WEBAPP_
ID_1); | 175 mWebappDirectoryManager.cleanUpDirectories(mMockContext, WEBAPP_
ID_1); |
| 166 CriteriaHelper.pollInstrumentationThread( | 176 CriteriaHelper.pollInstrumentationThread( |
| 167 Criteria.equals(AsyncTask.Status.FINISHED, new Callable<AsyncTas
k.Status>() { | 177 Criteria.equals(AsyncTask.Status.FINISHED, new Callable<AsyncTas
k.Status>() { |
| 168 @Override | 178 @Override |
| 169 public AsyncTask.Status call() { | 179 public AsyncTask.Status call() { |
| 170 return task.getStatus(); | 180 return task.getStatus(); |
| 171 } | 181 } |
| 172 })); | 182 })); |
| 173 } | 183 } |
| 174 } | 184 } |
| OLD | NEW |