• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.google.common.truth.Truth.assertThat;
20 
21 import static junit.framework.Assert.assertEquals;
22 import static junit.framework.Assert.assertFalse;
23 import static junit.framework.Assert.assertTrue;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.when;
27 
28 import android.database.Cursor;
29 import android.provider.DocumentsContract.Document;
30 
31 import androidx.test.filters.MediumTest;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import com.android.documentsui.base.DocumentInfo;
35 import com.android.documentsui.base.State;
36 import com.android.documentsui.base.UserId;
37 import com.android.documentsui.testing.ActivityManagers;
38 import com.android.documentsui.testing.TestCursor;
39 import com.android.documentsui.testing.TestEnv;
40 import com.android.documentsui.testing.TestFileTypeLookup;
41 import com.android.documentsui.testing.TestImmediateExecutor;
42 import com.android.documentsui.testing.TestProvidersAccess;
43 import com.android.documentsui.testing.UserManagers;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 
49 import java.util.concurrent.CountDownLatch;
50 import java.util.concurrent.TimeUnit;
51 
52 @RunWith(AndroidJUnit4.class)
53 @MediumTest
54 public class RecentsLoaderTests {
55 
56     private TestEnv mEnv;
57     private TestActivity mActivity;
58     private RecentsLoader mLoader;
59     private boolean mContentChanged;
60 
61     @Before
setUp()62     public void setUp() {
63         mEnv = TestEnv.create();
64         mActivity = TestActivity.create(mEnv);
65         mActivity.activityManager = ActivityManagers.create(false);
66         mActivity.userManager = UserManagers.create();
67 
68         mEnv.state.action = State.ACTION_BROWSE;
69         mEnv.state.acceptMimes = new String[] { "*/*" };
70         mEnv.state.canShareAcrossProfile = true;
71 
72         mLoader = new RecentsLoader(mActivity, mEnv.providers, mEnv.state,
73                 TestImmediateExecutor.createLookup(), new TestFileTypeLookup(),
74                 UserId.DEFAULT_USER);
75     }
76 
77     @Test
testNotLocalOnlyRoot_beIgnored()78     public void testNotLocalOnlyRoot_beIgnored() {
79         assertTrue(mLoader.shouldIgnoreRoot(TestProvidersAccess.PICKLES));
80     }
81 
82     @Test
testLocalOnlyRoot_supportRecent_notIgnored()83     public void testLocalOnlyRoot_supportRecent_notIgnored() {
84         assertFalse(mLoader.shouldIgnoreRoot(TestProvidersAccess.DOWNLOADS));
85     }
86 
87     @Test
testLocalOnlyRoot_supportRecent_differentUser_beIgnored()88     public void testLocalOnlyRoot_supportRecent_differentUser_beIgnored() {
89         assertTrue(mLoader.shouldIgnoreRoot(TestProvidersAccess.OtherUser.DOWNLOADS));
90     }
91 
92     @Test
testDocumentsNotIncludeDirectory()93     public void testDocumentsNotIncludeDirectory() {
94         final DocumentInfo doc = mEnv.model.createFolder("test");
95         doc.lastModified = System.currentTimeMillis();
96 
97         mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
98                 .setNextChildDocumentsReturns(doc);
99 
100         final DirectoryResult result = mLoader.loadInBackground();
101 
102         final Cursor c = result.getCursor();
103         assertEquals(0, c.getCount());
104     }
105 
106     @Test
testShowOrHideHiddenFiles()107     public void testShowOrHideHiddenFiles() {
108         final DocumentInfo doc1 = mEnv.model.createFile(".test");
109         final DocumentInfo doc2 = mEnv.model.createFile("test");
110         doc1.documentId = ".test";
111         doc2.documentId = "parent_folder/.hidden_folder/test";
112         doc1.lastModified = System.currentTimeMillis();
113         doc2.lastModified = System.currentTimeMillis();
114         mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
115                 .setNextRecentDocumentsReturns(doc1, doc2);
116 
117         assertEquals(false, mLoader.mState.showHiddenFiles);
118         DirectoryResult result = mLoader.loadInBackground();
119         assertEquals(0, result.getCursor().getCount());
120 
121         mLoader.mState.showHiddenFiles = true;
122         result = mLoader.loadInBackground();
123         assertEquals(2, result.getCursor().getCount());
124     }
125 
126     @Test
testDocumentsNotMovable()127     public void testDocumentsNotMovable() {
128         final DocumentInfo doc = mEnv.model.createFile("freddy.jpg",
129                 Document.FLAG_SUPPORTS_MOVE
130                         | Document.FLAG_SUPPORTS_DELETE
131                         | Document.FLAG_SUPPORTS_REMOVE);
132         doc.lastModified = System.currentTimeMillis();
133         mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
134                 .setNextRecentDocumentsReturns(doc);
135 
136         final DirectoryResult result = mLoader.loadInBackground();
137 
138         final Cursor c = result.getCursor();
139         assertEquals(1, c.getCount());
140         for (int i = 0; i < c.getCount(); ++i) {
141             c.moveToNext();
142             final int flags = c.getInt(c.getColumnIndex(Document.COLUMN_FLAGS));
143             assertEquals(0, flags & Document.FLAG_SUPPORTS_DELETE);
144             assertEquals(0, flags & Document.FLAG_SUPPORTS_REMOVE);
145             assertEquals(0, flags & Document.FLAG_SUPPORTS_MOVE);
146         }
147     }
148 
149     @Test
testContentsUpdate_observable()150     public void testContentsUpdate_observable() throws Exception {
151         CountDownLatch latch = new CountDownLatch(1);
152         Runnable callback = () -> {
153             latch.countDown();
154             mContentChanged = true;
155         };
156         mLoader.setObserver(new LockingContentObserver(new ContentLock(), callback));
157 
158         final DocumentInfo doc = mEnv.model.createFile("freddy.jpg");
159         doc.lastModified = System.currentTimeMillis();
160         mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
161                 .setNextRecentDocumentsReturns(doc);
162 
163         mLoader.loadInBackground();
164 
165         TestCursor c = (TestCursor) mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
166                 .queryRecentDocuments(null, null);
167         c.mockOnChange();
168 
169         latch.await(1, TimeUnit.SECONDS);
170         assertTrue(mContentChanged);
171     }
172 
173     @Test
testLoaderOnUserWithoutPermission()174     public void testLoaderOnUserWithoutPermission() {
175         mEnv.state.canShareAcrossProfile = false;
176         mLoader = new RecentsLoader(mActivity, mEnv.providers, mEnv.state,
177                 TestImmediateExecutor.createLookup(), new TestFileTypeLookup(),
178                 TestProvidersAccess.OtherUser.USER_ID);
179         final DirectoryResult result = mLoader.loadInBackground();
180 
181         assertThat(result.getCursor()).isNull();
182         assertThat(result.exception).isInstanceOf(CrossProfileNoPermissionException.class);
183     }
184 
185     @Test
testLoaderOnUser_quietMode()186     public void testLoaderOnUser_quietMode() {
187         when(mActivity.userManager.isQuietModeEnabled(any())).thenReturn(true);
188         final DirectoryResult result = mLoader.loadInBackground();
189 
190         assertThat(result.getCursor()).isNull();
191         assertThat(result.exception).isInstanceOf(CrossProfileQuietModeException.class);
192     }
193 }
194