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