| Index: chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappDirectoryManagerTest.java
|
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappDirectoryManagerTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappDirectoryManagerTest.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..199ce0f3db9f0b42eeaef981fc01873396b85969
|
| --- /dev/null
|
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappDirectoryManagerTest.java
|
| @@ -0,0 +1,156 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.chrome.browser.webapps;
|
| +
|
| +import android.content.Intent;
|
| +import android.net.Uri;
|
| +import android.os.AsyncTask;
|
| +import android.os.Build;
|
| +import android.test.InstrumentationTestCase;
|
| +import android.test.suitebuilder.annotation.SmallTest;
|
| +
|
| +import org.chromium.base.FileUtils;
|
| +import org.chromium.content.browser.test.util.Criteria;
|
| +import org.chromium.content.browser.test.util.CriteriaHelper;
|
| +
|
| +import java.io.File;
|
| +import java.util.HashSet;
|
| +import java.util.Set;
|
| +
|
| +/**
|
| + * Tests that directories for WebappActivities are managed correctly.
|
| + */
|
| +public class WebappDirectoryManagerTest extends InstrumentationTestCase {
|
| + private static final String TAG = "webapps_WebappDirect";
|
| +
|
| + private static final String WEBAPP_ID_1 = "webapp_1";
|
| + private static final String WEBAPP_ID_2 = "webapp_2";
|
| + private static final String WEBAPP_ID_3 = "webapp_3";
|
| +
|
| + private class TestWebappDirectoryManager extends WebappDirectoryManager {
|
| + private Set<Intent> mBaseIntents = new HashSet<Intent>();
|
| +
|
| + @Override
|
| + protected String getBaseDataDirectory() {
|
| + File cacheDirectory = getInstrumentation().getTargetContext().getCacheDir();
|
| + return new File(cacheDirectory, "WebappDirectoryManagerTest").getAbsolutePath();
|
| + }
|
| +
|
| + @Override
|
| + protected File getBaseWebappDirectory() {
|
| + return new File(getBaseDataDirectory(), "app_" + WEBAPP_DIRECTORY_NAME);
|
| + }
|
| +
|
| + @Override
|
| + protected File getWebappDirectory(WebappActivity activity) {
|
| + // Pretend that the handed in WebappActivity has the ID WEBAPP_ID_1. Ideally we'd be
|
| + // able to pass in a WebappActivity with the right ID but that'd require launching one.
|
| + return WebappDirectoryManager.getWebappDirectory(WEBAPP_ID_1);
|
| + }
|
| +
|
| + @Override
|
| + protected Set<Intent> getBaseIntentsForAllTasks() {
|
| + return mBaseIntents;
|
| + }
|
| + }
|
| +
|
| + private TestWebappDirectoryManager mWebappDirectoryManager;
|
| +
|
| + @Override
|
| + public void setUp() throws Exception {
|
| + super.setUp();
|
| + mWebappDirectoryManager = new TestWebappDirectoryManager();
|
| +
|
| + // Set up the base directories.
|
| + File baseDirectory = new File(mWebappDirectoryManager.getBaseDataDirectory());
|
| + FileUtils.recursivelyDeleteFile(baseDirectory);
|
| + assertTrue(baseDirectory.mkdirs());
|
| + }
|
| +
|
| + @Override
|
| + public void tearDown() throws Exception {
|
| + // Nuke everything.
|
| + File baseDirectory = new File(mWebappDirectoryManager.getBaseDataDirectory());
|
| + FileUtils.recursivelyDeleteFile(baseDirectory);
|
| +
|
| + super.tearDown();
|
| + }
|
| +
|
| + @SmallTest
|
| + public void testDeletesOwnDirectory() throws Exception {
|
| + File webappDirectory = new File(
|
| + mWebappDirectoryManager.getBaseWebappDirectory(), WEBAPP_ID_1);
|
| + assertTrue(webappDirectory.mkdirs());
|
| +
|
| + // Confirm that it deletes the current web app's directory.
|
| + mWebappDirectoryManager.execute();
|
| + waitForAsyncTaskToFinish(mWebappDirectoryManager);
|
| + assertFalse(webappDirectory.exists());
|
| + }
|
| +
|
| + /**
|
| + * On Lollipop and higher, the {@link WebappDirectoryManager} also deletes directories for web
|
| + * apps that no longer correspond to tasks in Recents.
|
| + */
|
| + @SmallTest
|
| + public void testDeletesDirectoriesForDeadTasks() throws Exception {
|
| + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
|
| +
|
| + // Track the three web app directories.
|
| + File directory1 = new File(
|
| + mWebappDirectoryManager.getBaseWebappDirectory(), WEBAPP_ID_1);
|
| + File directory2 = new File(
|
| + mWebappDirectoryManager.getBaseWebappDirectory(), WEBAPP_ID_2);
|
| + File directory3 = new File(
|
| + mWebappDirectoryManager.getBaseWebappDirectory(), WEBAPP_ID_3);
|
| +
|
| + // Seed the directory with folders for web apps.
|
| + assertTrue(directory1.mkdirs());
|
| + assertTrue(directory2.mkdirs());
|
| + assertTrue(directory3.mkdirs());
|
| +
|
| + // Indicate that another of the web apps is listed in Recents; in real usage this web app
|
| + // would not be in the foreground and would have persisted its state.
|
| + mWebappDirectoryManager.mBaseIntents.add(
|
| + new Intent(Intent.ACTION_VIEW, Uri.parse("webapp://webapp_2")));
|
| +
|
| + // Only the directory for the background web app should survive.
|
| + mWebappDirectoryManager.execute();
|
| + waitForAsyncTaskToFinish(mWebappDirectoryManager);
|
| + assertFalse(directory1.exists());
|
| + assertTrue(directory2.exists());
|
| + assertFalse(directory3.exists());
|
| + }
|
| +
|
| + @SmallTest
|
| + public void testDeletesObsoleteDirectories() throws Exception {
|
| + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
|
| +
|
| + // Seed the base directory with folders that correspond to pre-L web apps.
|
| + File baseDirectory = new File(mWebappDirectoryManager.getBaseDataDirectory());
|
| + File webappDirectory1 = new File(baseDirectory, "app_WebappActivity1");
|
| + File webappDirectory6 = new File(baseDirectory, "app_WebappActivity6");
|
| + File nonWebappDirectory = new File(baseDirectory, "app_ChromeDocumentActivity");
|
| + assertTrue(webappDirectory1.mkdirs());
|
| + assertTrue(webappDirectory6.mkdirs());
|
| + assertTrue(nonWebappDirectory.mkdirs());
|
| +
|
| + // Make sure only the web app folders are deleted.
|
| + mWebappDirectoryManager.execute();
|
| + waitForAsyncTaskToFinish(mWebappDirectoryManager);
|
| + assertFalse(webappDirectory1.exists());
|
| + assertFalse(webappDirectory6.exists());
|
| + assertTrue(nonWebappDirectory.exists());
|
| + }
|
| +
|
| + private void waitForAsyncTaskToFinish(final AsyncTask task) throws Exception {
|
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
|
| + @Override
|
| + public boolean isSatisfied() {
|
| + return task.getStatus() == AsyncTask.Status.FINISHED;
|
| + }
|
| + }));
|
| + }
|
| +}
|
|
|