1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.documentsui; 18 19 import static junit.framework.Assert.assertEquals; 20 21 import android.annotation.Nullable; 22 import android.app.Activity; 23 import android.app.LoaderManager; 24 import android.content.ComponentName; 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentSender; 29 import android.content.pm.PackageManager; 30 import android.content.res.Resources; 31 import android.net.Uri; 32 import android.test.mock.MockContentResolver; 33 import android.util.Pair; 34 35 import com.android.documentsui.AbstractActionHandler.CommonAddons; 36 import com.android.documentsui.base.DocumentInfo; 37 import com.android.documentsui.base.RootInfo; 38 import com.android.documentsui.testing.TestEnv; 39 import com.android.documentsui.testing.TestEventHandler; 40 import com.android.documentsui.testing.TestEventListener; 41 import com.android.documentsui.testing.TestLoaderManager; 42 import com.android.documentsui.testing.TestPackageManager; 43 import com.android.documentsui.testing.TestResources; 44 45 import org.mockito.Mockito; 46 47 /** 48 * Abstract to avoid having to implement unnecessary Activity stuff. 49 * Instances are created using {@link #create()}. 50 */ 51 public abstract class TestActivity extends AbstractBase { 52 53 public TestResources resources; 54 public TestPackageManager packageMgr; 55 public Intent intent; 56 public RootInfo currentRoot; 57 public MockContentResolver contentResolver; 58 public TestLoaderManager loaderManager; 59 60 public TestEventListener<Intent> startActivity; 61 public TestEventListener<Intent> startService; 62 public TestEventListener<Pair<IntentSender, Integer>> startIntentSender; 63 public TestEventListener<RootInfo> rootPicked; 64 public TestEventListener<Integer> refreshCurrentRootAndDirectory; 65 public TestEventListener<Boolean> setRootsDrawerOpen; 66 public TestEventListener<Uri> notifyDirectoryNavigated; 67 public TestEventHandler<Void> finishedHandler; 68 create(TestEnv env)69 public static TestActivity create(TestEnv env) { 70 TestActivity activity = Mockito.mock(TestActivity.class, Mockito.CALLS_REAL_METHODS); 71 activity.init(env); 72 return activity; 73 } 74 init(TestEnv env)75 public void init(TestEnv env) { 76 resources = TestResources.create(); 77 packageMgr = TestPackageManager.create(); 78 intent = new Intent(); 79 80 startActivity = new TestEventListener<>(); 81 startService = new TestEventListener<>(); 82 startIntentSender = new TestEventListener<>(); 83 rootPicked = new TestEventListener<>(); 84 refreshCurrentRootAndDirectory = new TestEventListener<>(); 85 setRootsDrawerOpen = new TestEventListener<>(); 86 notifyDirectoryNavigated = new TestEventListener<>(); 87 contentResolver = env.contentResolver; 88 loaderManager = new TestLoaderManager(); 89 finishedHandler = new TestEventHandler<>(); 90 } 91 92 @Override getPackageName()93 public final String getPackageName() { 94 return "Banarama"; 95 } 96 97 @Override startActivity(Intent intent)98 public final void startActivity(Intent intent) { 99 startActivity.accept(intent); 100 } 101 assertActivityStarted(String expectedAction)102 public final void assertActivityStarted(String expectedAction) { 103 assertEquals(expectedAction, startActivity.getLastValue().getAction()); 104 } 105 106 @Override startService(Intent intent)107 public final ComponentName startService(Intent intent) { 108 startService.accept(intent); 109 return null; 110 } 111 assertServiceStarted(String expectedAction)112 public final void assertServiceStarted(String expectedAction) { 113 assertEquals(expectedAction, startService.getLastValue().getAction()); 114 } 115 116 @Override getIntent()117 public final Intent getIntent() { 118 return intent; 119 } 120 121 @Override getResources()122 public final Resources getResources() { 123 return resources; 124 } 125 126 @Override getPackageManager()127 public final PackageManager getPackageManager() { 128 return packageMgr; 129 } 130 131 @Override startIntentSenderForResult(IntentSender intent, int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)132 public final void startIntentSenderForResult(IntentSender intent, int requestCode, 133 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 134 throws IntentSender.SendIntentException { 135 startIntentSender.accept(new Pair<>(intent, requestCode)); 136 } 137 138 @Override onRootPicked(RootInfo root)139 public final void onRootPicked(RootInfo root) { 140 rootPicked.accept(root); 141 } 142 143 @Override onDocumentPicked(DocumentInfo doc)144 public final void onDocumentPicked(DocumentInfo doc) { 145 throw new UnsupportedOperationException(); 146 } 147 148 @Override notifyDirectoryNavigated(Uri uri)149 public final void notifyDirectoryNavigated(Uri uri) { 150 notifyDirectoryNavigated.accept(uri); 151 } 152 153 @Override refreshCurrentRootAndDirectory(int anim)154 public final void refreshCurrentRootAndDirectory(int anim) { 155 refreshCurrentRootAndDirectory.accept(anim); 156 } 157 158 @Override getCurrentRoot()159 public final RootInfo getCurrentRoot() { 160 return currentRoot; 161 } 162 163 @Override setRootsDrawerOpen(boolean open)164 public final void setRootsDrawerOpen(boolean open) { 165 setRootsDrawerOpen.accept(open); 166 } 167 168 @Override getContentResolver()169 public final ContentResolver getContentResolver() { 170 return contentResolver; 171 } 172 173 @Override getApplicationContext()174 public final Context getApplicationContext() { 175 return this; 176 } 177 178 @Override isDestroyed()179 public boolean isDestroyed() { 180 return false; 181 } 182 183 @Override updateNavigator()184 public final void updateNavigator() {} 185 186 @Override getLoaderManager()187 public final LoaderManager getLoaderManager() { 188 return loaderManager; 189 } 190 191 @Override finish()192 public final void finish() { 193 finishedHandler.accept(null); 194 } 195 } 196 197 // Trick Mockito into finding our Addons methods correctly. W/o this 198 // hack, Mockito thinks Addons methods are not implemented. 199 abstract class AbstractBase extends Activity implements CommonAddons {} 200