• 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.files;
18 
19 import static com.android.documentsui.testing.IntentAsserts.assertHasAction;
20 import static com.android.documentsui.testing.IntentAsserts.assertHasData;
21 import static com.android.documentsui.testing.IntentAsserts.assertHasExtra;
22 import static com.android.documentsui.testing.IntentAsserts.assertHasExtraIntent;
23 import static com.android.documentsui.testing.IntentAsserts.assertHasExtraList;
24 import static com.android.documentsui.testing.IntentAsserts.assertHasExtraUri;
25 import static com.android.documentsui.testing.IntentAsserts.assertTargetsComponent;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32 
33 import android.app.Activity;
34 import android.app.PendingIntent;
35 import android.content.ClipData;
36 import android.content.Intent;
37 import android.net.Uri;
38 import android.os.Parcelable;
39 import android.provider.DocumentsContract;
40 import android.provider.DocumentsContract.Path;
41 import android.support.test.InstrumentationRegistry;
42 import android.support.test.filters.MediumTest;
43 import android.support.test.runner.AndroidJUnit4;
44 import android.util.Pair;
45 import android.view.DragEvent;
46 
47 import com.android.documentsui.AbstractActionHandler;
48 import com.android.documentsui.R;
49 import com.android.documentsui.TestActionModeAddons;
50 import com.android.documentsui.archives.ArchivesProvider;
51 import com.android.documentsui.base.DocumentInfo;
52 import com.android.documentsui.base.DocumentStack;
53 import com.android.documentsui.base.RootInfo;
54 import com.android.documentsui.base.Shared;
55 import com.android.documentsui.inspector.InspectorActivity;
56 import com.android.documentsui.testing.ClipDatas;
57 import com.android.documentsui.testing.DocumentStackAsserts;
58 import com.android.documentsui.testing.Roots;
59 import com.android.documentsui.testing.TestActivityConfig;
60 import com.android.documentsui.testing.TestDocumentClipper;
61 import com.android.documentsui.testing.TestDragAndDropManager;
62 import com.android.documentsui.testing.TestEnv;
63 import com.android.documentsui.testing.TestFeatures;
64 import com.android.documentsui.testing.TestProvidersAccess;
65 import com.android.documentsui.ui.TestDialogController;
66 import com.android.internal.util.Preconditions;
67 
68 import org.junit.Before;
69 import org.junit.Test;
70 import org.junit.runner.RunWith;
71 
72 import java.util.Arrays;
73 
74 @RunWith(AndroidJUnit4.class)
75 @MediumTest
76 public class ActionHandlerTest {
77 
78     private TestEnv mEnv;
79     private TestActivity mActivity;
80     private TestActionModeAddons mActionModeAddons;
81     private TestDialogController mDialogs;
82     private ActionHandler<TestActivity> mHandler;
83     private TestDocumentClipper mClipper;
84     private TestDragAndDropManager mDragAndDropManager;
85     private TestFeatures mFeatures;
86     private boolean refreshAnswer = false;
87 
88     @Before
setUp()89     public void setUp() {
90         mFeatures = new TestFeatures();
91         mEnv = TestEnv.create(mFeatures);
92         mActivity = TestActivity.create(mEnv);
93         mActionModeAddons = new TestActionModeAddons();
94         mDialogs = new TestDialogController();
95         mClipper = new TestDocumentClipper();
96         mDragAndDropManager = new TestDragAndDropManager();
97 
98         mEnv.providers.configurePm(mActivity.packageMgr);
99         ((TestActivityConfig) mEnv.injector.config).nextDocumentEnabled = true;
100         mEnv.injector.dialogs = mDialogs;
101 
102         mHandler = createHandler();
103 
104         mDialogs.confirmNext();
105 
106         mEnv.selectDocument(TestEnv.FILE_GIF);
107     }
108 
109     @Test
testOpenSelectedInNewWindow()110     public void testOpenSelectedInNewWindow() {
111         mHandler.openSelectedInNewWindow();
112 
113         DocumentStack path = new DocumentStack(Roots.create("123"), mEnv.model.getDocument("1"));
114 
115         Intent expected = LauncherActivity.createLaunchIntent(mActivity);
116         expected.putExtra(Shared.EXTRA_STACK, (Parcelable) path);
117 
118         Intent actual = mActivity.startActivity.getLastValue();
119         assertEquals(expected.toString(), actual.toString());
120     }
121 
122     @Test
testSpringOpenDirectory()123     public void testSpringOpenDirectory() {
124         mHandler.springOpenDirectory(TestEnv.FOLDER_0);
125         assertTrue(mActionModeAddons.finishActionModeCalled);
126         assertEquals(TestEnv.FOLDER_0, mEnv.state.stack.peek());
127     }
128 
129     @Test
testCutSelectedDocuments_NoGivenSelection()130     public void testCutSelectedDocuments_NoGivenSelection() {
131         mEnv.populateStack();
132 
133         mEnv.selectionMgr.clearSelection();
134         mHandler.cutToClipboard();
135         mDialogs.assertDocumentsClippedNotShown();
136     }
137 
138     @Test
testCutSelectedDocuments_ContainsNonMovableItem()139     public void testCutSelectedDocuments_ContainsNonMovableItem() {
140         mEnv.populateStack();
141         mEnv.selectDocument(TestEnv.FILE_READ_ONLY);
142 
143         mHandler.cutToClipboard();
144         mDialogs.assertDocumentsClippedNotShown();
145         mDialogs.assertShowOperationUnsupported();
146         mClipper.clipForCut.assertNotCalled();
147     }
148 
149     @Test
testCopySelectedDocuments_NoGivenSelection()150     public void testCopySelectedDocuments_NoGivenSelection() {
151         mEnv.populateStack();
152 
153         mEnv.selectionMgr.clearSelection();
154         mHandler.copyToClipboard();
155         mDialogs.assertDocumentsClippedNotShown();
156     }
157 
158     @Test
testDeleteSelectedDocuments_NoSelection()159     public void testDeleteSelectedDocuments_NoSelection() {
160         mEnv.populateStack();
161 
162         mEnv.selectionMgr.clearSelection();
163         mHandler.deleteSelectedDocuments();
164         mDialogs.assertNoFileFailures();
165         mActivity.startService.assertNotCalled();
166         mActionModeAddons.finishOnConfirmed.assertNeverCalled();
167     }
168 
169     @Test
testDeleteSelectedDocuments_Cancelable()170     public void testDeleteSelectedDocuments_Cancelable() {
171         mEnv.populateStack();
172 
173         mDialogs.rejectNext();
174         mHandler.deleteSelectedDocuments();
175         mDialogs.assertNoFileFailures();
176         mActivity.startService.assertNotCalled();
177         mActionModeAddons.finishOnConfirmed.assertRejected();
178     }
179 
180     // Recents root means when deleting the srcParent will be null.
181     @Test
testDeleteSelectedDocuments_RecentsRoot()182     public void testDeleteSelectedDocuments_RecentsRoot() {
183         mEnv.state.stack.changeRoot(TestProvidersAccess.RECENTS);
184 
185         mHandler.deleteSelectedDocuments();
186         mDialogs.assertNoFileFailures();
187         mActivity.startService.assertCalled();
188         mActionModeAddons.finishOnConfirmed.assertCalled();
189     }
190 
191     @Test
testShareSelectedDocuments_ShowsChooser()192     public void testShareSelectedDocuments_ShowsChooser() {
193         mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
194         mHandler.shareSelectedDocuments();
195 
196         mActivity.assertActivityStarted(Intent.ACTION_CHOOSER);
197     }
198 
199     @Test
testShareSelectedDocuments_Single()200     public void testShareSelectedDocuments_Single() {
201         mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
202         mHandler.shareSelectedDocuments();
203 
204         Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
205         assertHasAction(intent, Intent.ACTION_SEND);
206         assertFalse(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
207         assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
208         assertHasExtraUri(intent, Intent.EXTRA_STREAM);
209     }
210 
211     @Test
testShareSelectedDocuments_ArchivedFile()212     public void testShareSelectedDocuments_ArchivedFile() {
213         mEnv = TestEnv.create(ArchivesProvider.AUTHORITY);
214         mHandler = createHandler();
215 
216         mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
217         mEnv.selectionMgr.clearSelection();
218         mEnv.selectDocument(TestEnv.FILE_PDF);
219         mHandler.shareSelectedDocuments();
220 
221         Intent intent = mActivity.startActivity.getLastValue();
222         assertNull(intent);
223     }
224 
225     @Test
testShareSelectedDocuments_Multiple()226     public void testShareSelectedDocuments_Multiple() {
227         mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
228         mEnv.selectDocument(TestEnv.FILE_PDF);
229         mHandler.shareSelectedDocuments();
230 
231         Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
232         assertHasAction(intent, Intent.ACTION_SEND_MULTIPLE);
233         assertFalse(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
234         assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
235         assertHasExtraList(intent, Intent.EXTRA_STREAM, 2);
236     }
237 
238     @Test
testShareSelectedDocuments_VirtualFiles()239     public void testShareSelectedDocuments_VirtualFiles() {
240         if (!mEnv.features.isVirtualFilesSharingEnabled()) {
241             return;
242         }
243 
244         mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
245         mEnv.selectionMgr.clearSelection();
246         mEnv.selectDocument(TestEnv.FILE_VIRTUAL);
247         mHandler.shareSelectedDocuments();
248 
249         Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
250         assertHasAction(intent, Intent.ACTION_SEND);
251         assertTrue(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
252         assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
253         assertHasExtraUri(intent, Intent.EXTRA_STREAM);
254     }
255 
256     @Test
testShareSelectedDocuments_RegularAndVirtualFiles()257     public void testShareSelectedDocuments_RegularAndVirtualFiles() {
258         mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
259         mEnv.selectDocument(TestEnv.FILE_PNG);
260         mEnv.selectDocument(TestEnv.FILE_VIRTUAL);
261         mHandler.shareSelectedDocuments();
262 
263         Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
264         assertHasAction(intent, Intent.ACTION_SEND_MULTIPLE);
265 
266         assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
267         if (mEnv.features.isVirtualFilesSharingEnabled()) {
268             assertTrue(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
269             assertHasExtraList(intent, Intent.EXTRA_STREAM, 3);
270         }else {
271             assertHasExtraList(intent, Intent.EXTRA_STREAM, 2);
272         }
273     }
274 
275     @Test
testShareSelectedDocuments_OmitsPartialFiles()276     public void testShareSelectedDocuments_OmitsPartialFiles() {
277         mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
278         mEnv.selectDocument(TestEnv.FILE_PARTIAL);
279         mEnv.selectDocument(TestEnv.FILE_PNG);
280         mHandler.shareSelectedDocuments();
281 
282         Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
283         assertHasAction(intent, Intent.ACTION_SEND_MULTIPLE);
284         assertFalse(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
285         assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
286         assertHasExtraList(intent, Intent.EXTRA_STREAM, 2);
287     }
288 
289     @Test
testDocumentPicked_DefaultsToView()290     public void testDocumentPicked_DefaultsToView() throws Exception {
291         mActivity.currentRoot = TestProvidersAccess.HOME;
292 
293         mHandler.openDocument(TestEnv.FILE_GIF, ActionHandler.VIEW_TYPE_PREVIEW,
294                 ActionHandler.VIEW_TYPE_REGULAR);
295         mActivity.assertActivityStarted(Intent.ACTION_VIEW);
296     }
297 
298     @Test
testDocumentPicked_InArchive_QuickViewable()299     public void testDocumentPicked_InArchive_QuickViewable() throws Exception {
300         mActivity.resources.setQuickViewerPackage("corptropolis.viewer");
301         mActivity.currentRoot = TestProvidersAccess.HOME;
302 
303         mHandler.openDocument(TestEnv.FILE_IN_ARCHIVE, ActionHandler.VIEW_TYPE_PREVIEW,
304                 ActionHandler.VIEW_TYPE_REGULAR);
305         mActivity.assertActivityStarted(Intent.ACTION_QUICK_VIEW);
306     }
307 
308     @Test
testDocumentPicked_InArchive_Unopenable()309     public void testDocumentPicked_InArchive_Unopenable() throws Exception {
310         mActivity.currentRoot = TestProvidersAccess.HOME;
311 
312         mHandler.openDocument(TestEnv.FILE_IN_ARCHIVE, ActionHandler.VIEW_TYPE_PREVIEW,
313                 ActionHandler.VIEW_TYPE_REGULAR);
314         mDialogs.assertViewInArchivesShownUnsupported();
315     }
316 
317     @Test
testDocumentPicked_PreviewsWhenResourceSet()318     public void testDocumentPicked_PreviewsWhenResourceSet() throws Exception {
319         mActivity.resources.setQuickViewerPackage("corptropolis.viewer");
320         mActivity.currentRoot = TestProvidersAccess.HOME;
321 
322         mHandler.openDocument(TestEnv.FILE_GIF, ActionHandler.VIEW_TYPE_PREVIEW,
323                 ActionHandler.VIEW_TYPE_REGULAR);
324         mActivity.assertActivityStarted(Intent.ACTION_QUICK_VIEW);
325     }
326 
327     @Test
testDocumentPicked_Downloads_ManagesApks()328     public void testDocumentPicked_Downloads_ManagesApks() throws Exception {
329         mActivity.currentRoot = TestProvidersAccess.DOWNLOADS;
330 
331         mHandler.openDocument(TestEnv.FILE_APK, ActionHandler.VIEW_TYPE_PREVIEW,
332                 ActionHandler.VIEW_TYPE_REGULAR);
333         mActivity.assertActivityStarted(DocumentsContract.ACTION_MANAGE_DOCUMENT);
334     }
335 
336     @Test
testDocumentPicked_Home_SendsActionViewForApks()337     public void testDocumentPicked_Home_SendsActionViewForApks() throws Exception {
338         mActivity.currentRoot = TestProvidersAccess.HOME;
339 
340         mHandler.openDocument(TestEnv.FILE_APK, ActionHandler.VIEW_TYPE_PREVIEW,
341                 ActionHandler.VIEW_TYPE_REGULAR);
342         mActivity.assertActivityStarted(Intent.ACTION_VIEW);
343     }
344 
345     @Test
testDocumentPicked_Downloads_ManagesPartialFiles()346     public void testDocumentPicked_Downloads_ManagesPartialFiles() throws Exception {
347         mActivity.currentRoot = TestProvidersAccess.DOWNLOADS;
348 
349         mHandler.openDocument(TestEnv.FILE_PARTIAL, ActionHandler.VIEW_TYPE_PREVIEW,
350                 ActionHandler.VIEW_TYPE_REGULAR);
351         mActivity.assertActivityStarted(DocumentsContract.ACTION_MANAGE_DOCUMENT);
352     }
353 
354     @Test
testDocumentPicked_OpensArchives()355     public void testDocumentPicked_OpensArchives() throws Exception {
356         mActivity.currentRoot = TestProvidersAccess.HOME;
357         mEnv.docs.nextDocument = TestEnv.FILE_ARCHIVE;
358 
359         mHandler.openDocument(TestEnv.FILE_ARCHIVE, ActionHandler.VIEW_TYPE_PREVIEW,
360                 ActionHandler.VIEW_TYPE_REGULAR);
361         assertEquals(TestEnv.FILE_ARCHIVE, mEnv.state.stack.peek());
362     }
363 
364     @Test
testDocumentPicked_OpensDirectories()365     public void testDocumentPicked_OpensDirectories() throws Exception {
366         mActivity.currentRoot = TestProvidersAccess.HOME;
367 
368         mHandler.openDocument(TestEnv.FOLDER_1, ActionHandler.VIEW_TYPE_PREVIEW,
369                 ActionHandler.VIEW_TYPE_REGULAR);
370         assertEquals(TestEnv.FOLDER_1, mEnv.state.stack.peek());
371     }
372 
373     @Test
testShowChooser()374     public void testShowChooser() throws Exception {
375         mActivity.currentRoot = TestProvidersAccess.DOWNLOADS;
376 
377         mHandler.showChooserForDoc(TestEnv.FILE_PDF);
378         mActivity.assertActivityStarted(Intent.ACTION_CHOOSER);
379     }
380 
381     @Test
testInitLocation_RestoresIfStackIsLoaded()382     public void testInitLocation_RestoresIfStackIsLoaded() throws Exception {
383         mEnv.state.stack.changeRoot(TestProvidersAccess.DOWNLOADS);
384         mEnv.state.stack.push(TestEnv.FOLDER_0);
385 
386         mHandler.initLocation(mActivity.getIntent());
387         mActivity.restoreRootAndDirectory.assertCalled();
388     }
389 
390     @Test
testInitLocation_LoadsRootDocIfStackOnlyHasRoot()391     public void testInitLocation_LoadsRootDocIfStackOnlyHasRoot() throws Exception {
392         mEnv.state.stack.changeRoot(TestProvidersAccess.HAMMY);
393 
394         mHandler.initLocation(mActivity.getIntent());
395         assertRootPicked(TestProvidersAccess.HAMMY.getUri());
396     }
397 
398     @Test
testInitLocation_DefaultsToDownloads()399     public void testInitLocation_DefaultsToDownloads() throws Exception {
400         mActivity.resources.bools.put(R.bool.show_documents_root, false);
401 
402         mHandler.initLocation(mActivity.getIntent());
403         assertRootPicked(TestProvidersAccess.DOWNLOADS.getUri());
404     }
405 
406     @Test
testInitLocation_DocumentsRootEnabled()407     public void testInitLocation_DocumentsRootEnabled() throws Exception {
408         mActivity.resources.bools.put(R.bool.show_documents_root, true);
409         mActivity.resources.strings.put(R.string.default_root_uri, TestProvidersAccess.HOME.getUri().toString());
410 
411         mHandler.initLocation(mActivity.getIntent());
412         assertRootPicked(TestProvidersAccess.HOME.getUri());
413     }
414 
415     @Test
testInitLocation_BrowseRoot()416     public void testInitLocation_BrowseRoot() throws Exception {
417         Intent intent = mActivity.getIntent();
418         intent.setAction(Intent.ACTION_VIEW);
419         intent.setData(TestProvidersAccess.PICKLES.getUri());
420 
421         mHandler.initLocation(intent);
422         assertRootPicked(TestProvidersAccess.PICKLES.getUri());
423     }
424 
425     @Test
testInitLocation_LaunchToDocuments()426     public void testInitLocation_LaunchToDocuments() throws Exception {
427         if (!mEnv.features.isLaunchToDocumentEnabled()) {
428             return;
429         }
430 
431         mEnv.docs.nextIsDocumentsUri = true;
432         mEnv.docs.nextPath = new Path(
433                 TestProvidersAccess.HOME.rootId,
434                 Arrays.asList(
435                         TestEnv.FOLDER_0.documentId,
436                         TestEnv.FOLDER_1.documentId));
437         mEnv.docs.nextDocuments =
438                 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1);
439 
440         mActivity.refreshCurrentRootAndDirectory.assertNotCalled();
441         Intent intent = mActivity.getIntent();
442         intent.setAction(Intent.ACTION_VIEW);
443         intent.setData(TestEnv.FOLDER_1.derivedUri);
444         mHandler.initLocation(intent);
445 
446         mEnv.beforeAsserts();
447 
448         DocumentStackAsserts.assertEqualsTo(mEnv.state.stack, TestProvidersAccess.HOME,
449                 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1));
450         mActivity.refreshCurrentRootAndDirectory.assertCalled();
451     }
452 
453     @Test
testDragAndDrop_OnReadOnlyRoot()454     public void testDragAndDrop_OnReadOnlyRoot() throws Exception {
455         RootInfo root = new RootInfo(); // root by default has no SUPPORT_CREATE flag
456         DragEvent event = DragEvent.obtain(DragEvent.ACTION_DROP, 1, 1, null, null, null,
457                 null, true);
458         assertFalse(mHandler.dropOn(event, root));
459     }
460 
461     @Test
testDragAndDrop_OnLibraryRoot()462     public void testDragAndDrop_OnLibraryRoot() throws Exception {
463         DragEvent event = DragEvent.obtain(DragEvent.ACTION_DROP, 1, 1, null, null, null,
464                 null, true);
465         assertFalse(mHandler.dropOn(event, TestProvidersAccess.RECENTS));
466     }
467 
468     @Test
testDragAndDrop_DropsOnWritableRoot()469     public void testDragAndDrop_DropsOnWritableRoot() throws Exception {
470         // DragEvent gets recycled in Android, so it is possible that by the time the callback is
471         // called, event.getLocalState() and event.getClipData() returns null. This tests to ensure
472         // our Clipper is getting the original CipData passed in.
473         Object localState = new Object();
474         ClipData clipData = ClipDatas.createTestClipData();
475         DragEvent event = DragEvent.obtain(DragEvent.ACTION_DROP, 1, 1, localState, null, clipData,
476                 null, true);
477 
478         mHandler.dropOn(event, TestProvidersAccess.DOWNLOADS);
479         event.recycle();
480 
481         Pair<ClipData, RootInfo> actual = mDragAndDropManager.dropOnRootHandler.getLastValue();
482         assertSame(clipData, actual.first);
483         assertSame(TestProvidersAccess.DOWNLOADS, actual.second);
484     }
485 
486     @Test
testRefresh_nullUri()487     public void testRefresh_nullUri() throws Exception {
488         refreshAnswer = true;
489         mHandler.refreshDocument(null, (boolean answer) -> {
490             refreshAnswer = answer;
491         });
492 
493         mEnv.beforeAsserts();
494         assertFalse(refreshAnswer);
495     }
496 
497     @Test
testRefresh_emptyStack()498     public void testRefresh_emptyStack() throws Exception {
499         refreshAnswer = true;
500         assertTrue(mEnv.state.stack.isEmpty());
501         mHandler.refreshDocument(new DocumentInfo(), (boolean answer) -> {
502             refreshAnswer = answer;
503         });
504 
505         mEnv.beforeAsserts();
506         assertFalse(refreshAnswer);
507     }
508 
509     @Test
testRefresh()510     public void testRefresh() throws Exception {
511         refreshAnswer = false;
512         mEnv.populateStack();
513         mHandler.refreshDocument(mEnv.model.getDocument("1"), (boolean answer) -> {
514             refreshAnswer = answer;
515         });
516 
517         mEnv.beforeAsserts();
518         if (mEnv.features.isContentRefreshEnabled()) {
519             assertTrue(refreshAnswer);
520         } else {
521             assertFalse(refreshAnswer);
522         }
523     }
524 
525     @Test
testAuthentication()526     public void testAuthentication() throws Exception {
527         PendingIntent intent = PendingIntent.getActivity(
528                 InstrumentationRegistry.getInstrumentation().getTargetContext(), 0, new Intent(),
529                 0);
530 
531         mHandler.startAuthentication(intent);
532         assertEquals(intent.getIntentSender(), mActivity.startIntentSender.getLastValue().first);
533         assertEquals(AbstractActionHandler.CODE_AUTHENTICATION,
534                 mActivity.startIntentSender.getLastValue().second.intValue());
535     }
536 
537     @Test
testOnActivityResult_onOK()538     public void testOnActivityResult_onOK() throws Exception {
539         mHandler.onActivityResult(AbstractActionHandler.CODE_AUTHENTICATION, Activity.RESULT_OK,
540                 null);
541         mActivity.refreshCurrentRootAndDirectory.assertCalled();
542     }
543 
544     @Test
testOnActivityResult_onNotOK()545     public void testOnActivityResult_onNotOK() throws Exception {
546         mHandler.onActivityResult(0, Activity.RESULT_OK, null);
547         mActivity.refreshCurrentRootAndDirectory.assertNotCalled();
548 
549         mHandler.onActivityResult(AbstractActionHandler.CODE_AUTHENTICATION,
550                 Activity.RESULT_CANCELED, null);
551         mActivity.refreshCurrentRootAndDirectory.assertNotCalled();
552     }
553 
554     @Test
testShowInspector()555     public void testShowInspector() throws Exception {
556         mHandler.showInspector(TestEnv.FILE_GIF);
557 
558         mActivity.startActivity.assertCalled();
559         Intent intent = mActivity.startActivity.getLastValue();
560         assertTargetsComponent(intent, InspectorActivity.class);
561         assertHasData(intent, TestEnv.FILE_GIF.derivedUri);
562 
563         // should only send this under especial circumstances. See test below.
564         assertFalse(intent.getExtras().containsKey(Intent.EXTRA_TITLE));
565     }
566 
567     @Test
testShowInspector_DebugDisabled()568     public void testShowInspector_DebugDisabled() throws Exception {
569         mFeatures.debugSupport = false;
570 
571         mHandler.showInspector(TestEnv.FILE_GIF);
572         Intent intent = mActivity.startActivity.getLastValue();
573 
574         assertHasExtra(intent, Shared.EXTRA_SHOW_DEBUG);
575         assertFalse(intent.getExtras().getBoolean(Shared.EXTRA_SHOW_DEBUG));
576     }
577 
578     @Test
testShowInspector_DebugEnabled()579     public void testShowInspector_DebugEnabled() throws Exception {
580         mFeatures.debugSupport = true;
581 
582         mHandler.showInspector(TestEnv.FILE_GIF);
583         Intent intent = mActivity.startActivity.getLastValue();
584 
585         assertHasExtra(intent, Shared.EXTRA_SHOW_DEBUG);
586         assertTrue(intent.getExtras().getBoolean(Shared.EXTRA_SHOW_DEBUG));
587     }
588 
589     @Test
testShowInspector_OverridesRootDocumentName()590     public void testShowInspector_OverridesRootDocumentName() throws Exception {
591         mActivity.currentRoot = TestProvidersAccess.PICKLES;
592         mEnv.populateStack();
593 
594         // Verify test setup is correct, but not an assert related to the logic of our test.
595         Preconditions.checkState(mEnv.state.stack.size() == 1);
596         Preconditions.checkNotNull(mEnv.state.stack.peek());
597 
598         DocumentInfo rootDoc = mEnv.state.stack.peek();
599         rootDoc.displayName = "poodles";
600 
601         mHandler.showInspector(rootDoc);
602         Intent intent = mActivity.startActivity.getLastValue();
603         assertEquals(
604                 TestProvidersAccess.PICKLES.title,
605                 intent.getExtras().getString(Intent.EXTRA_TITLE));
606     }
607 
608     @Test
testShowInspector_OverridesRootDocumentNameX()609     public void testShowInspector_OverridesRootDocumentNameX() throws Exception {
610         mActivity.currentRoot = TestProvidersAccess.PICKLES;
611         mEnv.populateStack();
612         mEnv.state.stack.push(TestEnv.FOLDER_2);
613 
614         // Verify test setup is correct, but not an assert related to the logic of our test.
615         Preconditions.checkState(mEnv.state.stack.size() == 2);
616         Preconditions.checkNotNull(mEnv.state.stack.peek());
617 
618         DocumentInfo rootDoc = mEnv.state.stack.peek();
619         rootDoc.displayName = "poodles";
620 
621         mHandler.showInspector(rootDoc);
622         Intent intent = mActivity.startActivity.getLastValue();
623         assertFalse(intent.getExtras().containsKey(Intent.EXTRA_TITLE));
624     }
625 
assertRootPicked(Uri expectedUri)626     private void assertRootPicked(Uri expectedUri) throws Exception {
627         mEnv.beforeAsserts();
628 
629         mActivity.rootPicked.assertCalled();
630         RootInfo root = mActivity.rootPicked.getLastValue();
631         assertNotNull(root);
632         assertEquals(expectedUri, root.getUri());
633     }
634 
createHandler()635     private ActionHandler<TestActivity> createHandler() {
636         return new ActionHandler<>(
637                 mActivity,
638                 mEnv.state,
639                 mEnv.providers,
640                 mEnv.docs,
641                 mEnv.searchViewManager,
642                 mEnv::lookupExecutor,
643                 mActionModeAddons,
644                 mClipper,
645                 null,  // clip storage, not utilized unless we venture into *jumbo* clip territory.
646                 mDragAndDropManager,
647                 mEnv.injector
648         );
649     }
650 }
651