Loading tests/common/com/android/documentsui/TestActivity.java +9 −0 Original line number Diff line number Diff line Loading @@ -19,11 +19,13 @@ package com.android.documentsui; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Resources; import com.android.documentsui.AbstractActionHandler.CommonAddons; import com.android.documentsui.base.RootInfo; import com.android.documentsui.testing.TestEventListener; import com.android.documentsui.testing.android.TestPackageManager; import com.android.documentsui.testing.android.TestResources; import org.mockito.Mockito; Loading @@ -35,6 +37,7 @@ import org.mockito.Mockito; public abstract class TestActivity extends AbstractBase { public TestResources resources; public TestPackageManager packageMgr; public Intent intent; public TestEventListener<Intent> startActivity; Loading @@ -49,6 +52,7 @@ public abstract class TestActivity extends AbstractBase { public void init() { resources = TestResources.create(); packageMgr = TestPackageManager.create(); intent = new Intent(); startActivity = new TestEventListener<>(); Loading Loading @@ -82,6 +86,11 @@ public abstract class TestActivity extends AbstractBase { return resources; } @Override public PackageManager getPackageManager() { return packageMgr; } @Override public final void onRootPicked(RootInfo root) { rootPicked.accept(root); Loading tests/common/com/android/documentsui/testing/TestRootsAccess.java +21 −1 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.documentsui.testing; import com.android.documentsui.base.RootInfo; import com.android.documentsui.base.State; import com.android.documentsui.roots.RootsAccess; import com.android.documentsui.testing.android.TestPackageManager; import java.util.ArrayList; import java.util.Collection; Loading @@ -31,6 +32,8 @@ public class TestRootsAccess implements RootsAccess { public static final RootInfo DOWNLOADS; public static final RootInfo HOME; public static final RootInfo HAMMY; public static final RootInfo PICKLES; static { DOWNLOADS = new RootInfo(); Loading @@ -40,6 +43,14 @@ public class TestRootsAccess implements RootsAccess { HOME = new RootInfo(); HOME.authority = "com.android.externalstorage.documents"; HOME.rootId = "home"; HAMMY = new RootInfo(); HAMMY.authority = "yummies"; HAMMY.rootId = "hamsandwich"; PICKLES = new RootInfo(); PICKLES.authority = "yummies"; PICKLES.rootId = "pickles"; } public final Map<String, Collection<RootInfo>> roots = new HashMap<>(); Loading @@ -48,15 +59,24 @@ public class TestRootsAccess implements RootsAccess { public TestRootsAccess() { add(DOWNLOADS); add(HOME); add(HAMMY); add(PICKLES); } public void add(RootInfo root) { private void add(RootInfo root) { if (!roots.containsKey(root.authority)) { roots.put(root.authority, new ArrayList<>()); } roots.get(root.authority).add(root); } public void configurePm(TestPackageManager pm) { pm.addStubContentProviderForRoot(TestRootsAccess.DOWNLOADS); pm.addStubContentProviderForRoot(TestRootsAccess.HOME); pm.addStubContentProviderForRoot(TestRootsAccess.HAMMY); pm.addStubContentProviderForRoot(TestRootsAccess.PICKLES); } @Override public RootInfo getRootOneshot(String authority, String rootId) { if (roots.containsKey(authority)) { Loading tests/common/com/android/documentsui/testing/android/TestPackageManager.java 0 → 100644 +68 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.documentsui.testing.android; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ProviderInfo; import android.content.pm.ResolveInfo; import com.android.documentsui.base.RootInfo; import org.mockito.Mockito; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Abstract to avoid having to implement unnecessary Activity stuff. * Instances are created using {@link #create()}. */ public abstract class TestPackageManager extends PackageManager { public Map<String, ResolveInfo> contentProviders; private TestPackageManager() {} public void addStubContentProviderForRoot(RootInfo... roots) { for (RootInfo root : roots) { // only one entry per authority is required. if (!contentProviders.containsKey(root.authority)) { ResolveInfo info = new ResolveInfo(); contentProviders.put(root.authority, info); info.providerInfo = new ProviderInfo(); info.providerInfo.authority = root.authority; } } } public static TestPackageManager create() { TestPackageManager pm = Mockito.mock( TestPackageManager.class, Mockito.CALLS_REAL_METHODS); pm.contentProviders = new HashMap<>(); return pm; } @Override public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) { List<ResolveInfo> result = new ArrayList<>(); result.addAll(contentProviders.values()); return result; } } tests/functional/com/android/documentsui/ActivityTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -23,14 +23,14 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.os.RemoteException; import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; import android.support.test.uiautomator.Configurator; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObjectNotFoundException; import android.test.ActivityInstrumentationTestCase2; import android.view.MotionEvent; import com.android.documentsui.DocumentsProviderHelper; import com.android.documentsui.StubProvider; import com.android.documentsui.base.RootInfo; import com.android.documentsui.bots.Bots; import com.android.documentsui.bots.UiBot; Loading Loading @@ -138,6 +138,7 @@ public abstract class ActivityTest<T extends Activity> extends ActivityInstrumen UiBot.TARGET_PKG); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); if (getInitialRoot() != null) { intent.setAction(DocumentsContract.ACTION_BROWSE); intent.setData(getInitialRoot().getUri()); } setActivityIntent(intent); Loading tests/unit/com/android/documentsui/files/ActionHandlerTest.java +30 −4 Original line number Diff line number Diff line Loading @@ -19,17 +19,18 @@ package com.android.documentsui.files; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import android.content.Intent; import android.net.Uri; import android.provider.DocumentsContract; import android.support.test.filters.MediumTest; import android.support.test.runner.AndroidJUnit4; import com.android.documentsui.R; import com.android.documentsui.base.RootInfo; import com.android.documentsui.base.Shared; import com.android.documentsui.dirlist.MultiSelectManager.Selection; import com.android.documentsui.files.ActionHandler; import com.android.documentsui.testing.TestConfirmationCallback; import com.android.documentsui.testing.TestEnv; import com.android.documentsui.testing.TestRootsAccess; import com.android.documentsui.ui.TestDialogController; import org.junit.Before; Loading @@ -53,6 +54,7 @@ public class ActionHandlerTest { mActivity = TestActivity.create(); mDialogs = new TestDialogController(); mCallback = new TestConfirmationCallback(); mEnv.roots.configurePm(mActivity.packageMgr); mHandler = new ActionHandler<>( mActivity, Loading Loading @@ -89,11 +91,35 @@ public class ActionHandlerTest { } @Test public void testInitLocation_DefaultsToHome() throws Exception { public void testInitLocation_DefaultsToDownloads() throws Exception { mActivity.resources.bools.put(R.bool.productivity_device, false); mHandler.initLocation(mActivity.getIntent()); assertRootPicked(TestRootsAccess.DOWNLOADS.getUri()); } @Test public void testInitLocation_ProductivityDefaultsToHome() throws Exception { mActivity.resources.bools.put(R.bool.productivity_device, true); mHandler.initLocation(mActivity.getIntent()); assertRootPicked(Shared.getDefaultRootUri(mActivity)); assertRootPicked(TestRootsAccess.HOME.getUri()); } @Test public void testInitLocation_LoadsFromRootUri() throws Exception { mActivity.resources.bools.put(R.bool.productivity_device, true); Intent intent = mActivity.getIntent(); intent.setAction(DocumentsContract.ACTION_BROWSE); intent.setData(TestRootsAccess.PICKLES.getUri()); mHandler.initLocation(intent); assertRootPicked(TestRootsAccess.PICKLES); } private void assertRootPicked(RootInfo root) throws Exception { assertRootPicked(root.getUri()); } private void assertRootPicked(Uri expectedUri) throws Exception { Loading Loading
tests/common/com/android/documentsui/TestActivity.java +9 −0 Original line number Diff line number Diff line Loading @@ -19,11 +19,13 @@ package com.android.documentsui; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Resources; import com.android.documentsui.AbstractActionHandler.CommonAddons; import com.android.documentsui.base.RootInfo; import com.android.documentsui.testing.TestEventListener; import com.android.documentsui.testing.android.TestPackageManager; import com.android.documentsui.testing.android.TestResources; import org.mockito.Mockito; Loading @@ -35,6 +37,7 @@ import org.mockito.Mockito; public abstract class TestActivity extends AbstractBase { public TestResources resources; public TestPackageManager packageMgr; public Intent intent; public TestEventListener<Intent> startActivity; Loading @@ -49,6 +52,7 @@ public abstract class TestActivity extends AbstractBase { public void init() { resources = TestResources.create(); packageMgr = TestPackageManager.create(); intent = new Intent(); startActivity = new TestEventListener<>(); Loading Loading @@ -82,6 +86,11 @@ public abstract class TestActivity extends AbstractBase { return resources; } @Override public PackageManager getPackageManager() { return packageMgr; } @Override public final void onRootPicked(RootInfo root) { rootPicked.accept(root); Loading
tests/common/com/android/documentsui/testing/TestRootsAccess.java +21 −1 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.documentsui.testing; import com.android.documentsui.base.RootInfo; import com.android.documentsui.base.State; import com.android.documentsui.roots.RootsAccess; import com.android.documentsui.testing.android.TestPackageManager; import java.util.ArrayList; import java.util.Collection; Loading @@ -31,6 +32,8 @@ public class TestRootsAccess implements RootsAccess { public static final RootInfo DOWNLOADS; public static final RootInfo HOME; public static final RootInfo HAMMY; public static final RootInfo PICKLES; static { DOWNLOADS = new RootInfo(); Loading @@ -40,6 +43,14 @@ public class TestRootsAccess implements RootsAccess { HOME = new RootInfo(); HOME.authority = "com.android.externalstorage.documents"; HOME.rootId = "home"; HAMMY = new RootInfo(); HAMMY.authority = "yummies"; HAMMY.rootId = "hamsandwich"; PICKLES = new RootInfo(); PICKLES.authority = "yummies"; PICKLES.rootId = "pickles"; } public final Map<String, Collection<RootInfo>> roots = new HashMap<>(); Loading @@ -48,15 +59,24 @@ public class TestRootsAccess implements RootsAccess { public TestRootsAccess() { add(DOWNLOADS); add(HOME); add(HAMMY); add(PICKLES); } public void add(RootInfo root) { private void add(RootInfo root) { if (!roots.containsKey(root.authority)) { roots.put(root.authority, new ArrayList<>()); } roots.get(root.authority).add(root); } public void configurePm(TestPackageManager pm) { pm.addStubContentProviderForRoot(TestRootsAccess.DOWNLOADS); pm.addStubContentProviderForRoot(TestRootsAccess.HOME); pm.addStubContentProviderForRoot(TestRootsAccess.HAMMY); pm.addStubContentProviderForRoot(TestRootsAccess.PICKLES); } @Override public RootInfo getRootOneshot(String authority, String rootId) { if (roots.containsKey(authority)) { Loading
tests/common/com/android/documentsui/testing/android/TestPackageManager.java 0 → 100644 +68 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.documentsui.testing.android; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ProviderInfo; import android.content.pm.ResolveInfo; import com.android.documentsui.base.RootInfo; import org.mockito.Mockito; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Abstract to avoid having to implement unnecessary Activity stuff. * Instances are created using {@link #create()}. */ public abstract class TestPackageManager extends PackageManager { public Map<String, ResolveInfo> contentProviders; private TestPackageManager() {} public void addStubContentProviderForRoot(RootInfo... roots) { for (RootInfo root : roots) { // only one entry per authority is required. if (!contentProviders.containsKey(root.authority)) { ResolveInfo info = new ResolveInfo(); contentProviders.put(root.authority, info); info.providerInfo = new ProviderInfo(); info.providerInfo.authority = root.authority; } } } public static TestPackageManager create() { TestPackageManager pm = Mockito.mock( TestPackageManager.class, Mockito.CALLS_REAL_METHODS); pm.contentProviders = new HashMap<>(); return pm; } @Override public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) { List<ResolveInfo> result = new ArrayList<>(); result.addAll(contentProviders.values()); return result; } }
tests/functional/com/android/documentsui/ActivityTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -23,14 +23,14 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.os.RemoteException; import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; import android.support.test.uiautomator.Configurator; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObjectNotFoundException; import android.test.ActivityInstrumentationTestCase2; import android.view.MotionEvent; import com.android.documentsui.DocumentsProviderHelper; import com.android.documentsui.StubProvider; import com.android.documentsui.base.RootInfo; import com.android.documentsui.bots.Bots; import com.android.documentsui.bots.UiBot; Loading Loading @@ -138,6 +138,7 @@ public abstract class ActivityTest<T extends Activity> extends ActivityInstrumen UiBot.TARGET_PKG); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); if (getInitialRoot() != null) { intent.setAction(DocumentsContract.ACTION_BROWSE); intent.setData(getInitialRoot().getUri()); } setActivityIntent(intent); Loading
tests/unit/com/android/documentsui/files/ActionHandlerTest.java +30 −4 Original line number Diff line number Diff line Loading @@ -19,17 +19,18 @@ package com.android.documentsui.files; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import android.content.Intent; import android.net.Uri; import android.provider.DocumentsContract; import android.support.test.filters.MediumTest; import android.support.test.runner.AndroidJUnit4; import com.android.documentsui.R; import com.android.documentsui.base.RootInfo; import com.android.documentsui.base.Shared; import com.android.documentsui.dirlist.MultiSelectManager.Selection; import com.android.documentsui.files.ActionHandler; import com.android.documentsui.testing.TestConfirmationCallback; import com.android.documentsui.testing.TestEnv; import com.android.documentsui.testing.TestRootsAccess; import com.android.documentsui.ui.TestDialogController; import org.junit.Before; Loading @@ -53,6 +54,7 @@ public class ActionHandlerTest { mActivity = TestActivity.create(); mDialogs = new TestDialogController(); mCallback = new TestConfirmationCallback(); mEnv.roots.configurePm(mActivity.packageMgr); mHandler = new ActionHandler<>( mActivity, Loading Loading @@ -89,11 +91,35 @@ public class ActionHandlerTest { } @Test public void testInitLocation_DefaultsToHome() throws Exception { public void testInitLocation_DefaultsToDownloads() throws Exception { mActivity.resources.bools.put(R.bool.productivity_device, false); mHandler.initLocation(mActivity.getIntent()); assertRootPicked(TestRootsAccess.DOWNLOADS.getUri()); } @Test public void testInitLocation_ProductivityDefaultsToHome() throws Exception { mActivity.resources.bools.put(R.bool.productivity_device, true); mHandler.initLocation(mActivity.getIntent()); assertRootPicked(Shared.getDefaultRootUri(mActivity)); assertRootPicked(TestRootsAccess.HOME.getUri()); } @Test public void testInitLocation_LoadsFromRootUri() throws Exception { mActivity.resources.bools.put(R.bool.productivity_device, true); Intent intent = mActivity.getIntent(); intent.setAction(DocumentsContract.ACTION_BROWSE); intent.setData(TestRootsAccess.PICKLES.getUri()); mHandler.initLocation(intent); assertRootPicked(TestRootsAccess.PICKLES); } private void assertRootPicked(RootInfo root) throws Exception { assertRootPicked(root.getUri()); } private void assertRootPicked(Uri expectedUri) throws Exception { Loading