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.ActivityManager; 24 import android.app.LoaderManager; 25 import android.content.ComponentName; 26 import android.content.ContentResolver; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.IntentSender; 30 import android.content.pm.PackageManager; 31 import android.content.res.Resources; 32 import android.net.Uri; 33 import android.test.mock.MockContentResolver; 34 import android.util.Pair; 35 36 import com.android.documentsui.AbstractActionHandler.CommonAddons; 37 import com.android.documentsui.base.DocumentInfo; 38 import com.android.documentsui.base.RootInfo; 39 import com.android.documentsui.testing.TestEnv; 40 import com.android.documentsui.testing.TestEventHandler; 41 import com.android.documentsui.testing.TestEventListener; 42 import com.android.documentsui.testing.TestLoaderManager; 43 import com.android.documentsui.testing.TestPackageManager; 44 import com.android.documentsui.testing.TestResources; 45 46 import org.mockito.Mockito; 47 48 /** 49 * Abstract to avoid having to implement unnecessary Activity stuff. 50 * Instances are created using {@link #create()}. 51 */ 52 public abstract class TestActivity extends AbstractBase { 53 54 public TestResources resources; 55 public TestPackageManager packageMgr; 56 public Intent intent; 57 public RootInfo currentRoot; 58 public MockContentResolver contentResolver; 59 public TestLoaderManager loaderManager; 60 public ActivityManager activityManager; 61 62 public TestEventListener<Intent> startActivity; 63 public TestEventListener<Intent> startService; 64 public TestEventListener<Pair<IntentSender, Integer>> startIntentSender; 65 public TestEventListener<RootInfo> rootPicked; 66 public TestEventListener<Void> restoreRootAndDirectory; 67 public TestEventListener<Integer> refreshCurrentRootAndDirectory; 68 public TestEventListener<Boolean> setRootsDrawerOpen; 69 public TestEventListener<Uri> notifyDirectoryNavigated; 70 public TestEventHandler<Void> finishedHandler; 71 create(TestEnv env)72 public static TestActivity create(TestEnv env) { 73 TestActivity activity = Mockito.mock(TestActivity.class, Mockito.CALLS_REAL_METHODS); 74 activity.init(env); 75 return activity; 76 } 77 init(TestEnv env)78 public void init(TestEnv env) { 79 resources = TestResources.create(); 80 packageMgr = TestPackageManager.create(); 81 intent = new Intent(); 82 83 startActivity = new TestEventListener<>(); 84 startService = new TestEventListener<>(); 85 startIntentSender = new TestEventListener<>(); 86 rootPicked = new TestEventListener<>(); 87 restoreRootAndDirectory = new TestEventListener<>(); 88 refreshCurrentRootAndDirectory = new TestEventListener<>(); 89 setRootsDrawerOpen = new TestEventListener<>(); 90 notifyDirectoryNavigated = new TestEventListener<>(); 91 contentResolver = env.contentResolver; 92 loaderManager = new TestLoaderManager(); 93 finishedHandler = new TestEventHandler<>(); 94 } 95 96 @Override getPackageName()97 public final String getPackageName() { 98 return "Banarama"; 99 } 100 101 @Override startActivity(Intent intent)102 public final void startActivity(Intent intent) { 103 startActivity.accept(intent); 104 } 105 assertActivityStarted(String expectedAction)106 public final void assertActivityStarted(String expectedAction) { 107 assertEquals(expectedAction, startActivity.getLastValue().getAction()); 108 } 109 110 @Override startService(Intent intent)111 public final ComponentName startService(Intent intent) { 112 startService.accept(intent); 113 return null; 114 } 115 assertServiceStarted(String expectedAction)116 public final void assertServiceStarted(String expectedAction) { 117 assertEquals(expectedAction, startService.getLastValue().getAction()); 118 } 119 120 @Override getIntent()121 public final Intent getIntent() { 122 return intent; 123 } 124 125 @Override getResources()126 public final Resources getResources() { 127 return resources; 128 } 129 130 @Override getPackageManager()131 public final PackageManager getPackageManager() { 132 return packageMgr; 133 } 134 135 @Override startIntentSenderForResult(IntentSender intent, int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)136 public final void startIntentSenderForResult(IntentSender intent, int requestCode, 137 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 138 throws IntentSender.SendIntentException { 139 startIntentSender.accept(new Pair<>(intent, requestCode)); 140 } 141 142 @Override onRootPicked(RootInfo root)143 public final void onRootPicked(RootInfo root) { 144 rootPicked.accept(root); 145 } 146 147 @Override onDocumentPicked(DocumentInfo doc)148 public final void onDocumentPicked(DocumentInfo doc) { 149 throw new UnsupportedOperationException(); 150 } 151 152 @Override notifyDirectoryNavigated(Uri uri)153 public final void notifyDirectoryNavigated(Uri uri) { 154 notifyDirectoryNavigated.accept(uri); 155 } 156 157 @Override restoreRootAndDirectory()158 public final void restoreRootAndDirectory() { 159 restoreRootAndDirectory.accept(null); 160 } 161 162 @Override refreshCurrentRootAndDirectory(int anim)163 public final void refreshCurrentRootAndDirectory(int anim) { 164 refreshCurrentRootAndDirectory.accept(anim); 165 } 166 167 @Override getCurrentRoot()168 public final RootInfo getCurrentRoot() { 169 return currentRoot; 170 } 171 172 @Override setRootsDrawerOpen(boolean open)173 public final void setRootsDrawerOpen(boolean open) { 174 setRootsDrawerOpen.accept(open); 175 } 176 177 @Override getContentResolver()178 public final ContentResolver getContentResolver() { 179 return contentResolver; 180 } 181 182 @Override getApplicationContext()183 public final Context getApplicationContext() { 184 return this; 185 } 186 187 @Override isDestroyed()188 public boolean isDestroyed() { 189 return false; 190 } 191 192 @Override updateNavigator()193 public final void updateNavigator() {} 194 195 @Override getLoaderManager()196 public final LoaderManager getLoaderManager() { 197 return loaderManager; 198 } 199 200 @Override getSystemService(String service)201 public final Object getSystemService(String service) { 202 switch (service) { 203 case Context.ACTIVITY_SERVICE: 204 return activityManager; 205 } 206 207 throw new IllegalArgumentException("Unknown service " + service); 208 } 209 210 @Override finish()211 public final void finish() { 212 finishedHandler.accept(null); 213 } 214 } 215 216 // Trick Mockito into finding our Addons methods correctly. W/o this 217 // hack, Mockito thinks Addons methods are not implemented. 218 abstract class AbstractBase extends Activity implements CommonAddons {} 219