Merge "Fix contents overlap to top bar"
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..98c2fc8
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+ "postsubmit": [
+ {
+ "name": "DocumentsUIGoogleTests"
+ }
+ ]
+}
diff --git a/perf-tests/Android.bp b/perf-tests/Android.bp
index 963a6da..e745fdf 100644
--- a/perf-tests/Android.bp
+++ b/perf-tests/Android.bp
@@ -20,6 +20,7 @@
static_libs: [
"androidx.legacy_legacy-support-v4",
+ "androidx.test.rules",
"androidx.test.espresso.core",
"mockito-target",
"ub-janktesthelper",
diff --git a/res/drawable/ic_root_bugreport.xml b/res/drawable/ic_root_bugreport.xml
new file mode 100644
index 0000000..bc6fc3a
--- /dev/null
+++ b/res/drawable/ic_root_bugreport.xml
@@ -0,0 +1,25 @@
+<!--
+ ~ Copyright (C) 2019 The Android Open Source Project
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp"
+ android:height="24dp"
+ android:viewportWidth="24.0"
+ android:viewportHeight="24.0">
+ <path
+ android:fillColor="#5F6368"
+ android:pathData="M20 10V8h-2.81c-.45-.78-1.07-1.46-1.82-1.96L17 4.41 15.59 3l-2.17 2.17c-.03-.01-.05-.01-.08-.01-.16-.04-.32-.06-.49-.09l-.17-.03C12.46 5.02 12.23 5 12 5c-.49 0-.97.07-1.42.18l.02-.01L8.41 3 7 4.41l1.62 1.63h.01c-.75.5-1.37 1.18-1.82 1.96H4v2h2.09c-.06.33-.09.66-.09 1v1H4v2h2v1c0 .34.04.67.09 1H4v2h2.81c1.04 1.79 2.97 3 5.19 3s4.15-1.21 5.19-3H20v-2h-2.09c.05-.33.09-.66.09-1v-1h2v-2h-2v-1c0-.34-.04-.67-.09-1H20zm-4 5c0 2.21-1.79 4-4 4s-4-1.79-4-4v-4c0-2.21 1.79-4 4-4s4 1.79 4 4v4zm-6-1h4v2h-4zm0-4h4v2h-4z" />
+</vector>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 10f9b05..2409ed6 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -91,8 +91,14 @@
<item name="buttonBarNegativeButtonStyle">@style/DialogTextButton</item>
</style>
+ <style name="MaterialAlertDialogStyle" parent="@style/MaterialAlertDialog.MaterialComponents">
+ <item name="backgroundInsetTop">12dp</item>
+ <item name="backgroundInsetBottom">12dp</item>
+ </style>
+
<style name="MaterialAlertDialogTheme" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered">
<item name="android:dialogCornerRadius">@dimen/grid_item_radius</item>
+ <item name="alertDialogStyle">@style/MaterialAlertDialogStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/DialogTextButton</item>
<item name="buttonBarNegativeButtonStyle">@style/DialogTextButton</item>
<item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialogTitleStyle</item>
diff --git a/src/com/android/documentsui/AbstractActionHandler.java b/src/com/android/documentsui/AbstractActionHandler.java
index 94b4d42..803aae5 100644
--- a/src/com/android/documentsui/AbstractActionHandler.java
+++ b/src/com/android/documentsui/AbstractActionHandler.java
@@ -594,11 +594,15 @@
Context context = mActivity;
if (mState.stack.isRecents()) {
+ final LockingContentObserver observer = new LockingContentObserver(
+ mContentLock, AbstractActionHandler.this::loadDocumentsForCurrentStack);
+ MultiRootDocumentsLoader loader;
+
if (mSearchMgr.isSearching()) {
if (DEBUG) {
Log.d(TAG, "Creating new GlobalSearchLoader.");
}
- return new GlobalSearchLoader(
+ loader = new GlobalSearchLoader(
context,
mProviders,
mState,
@@ -609,13 +613,15 @@
if (DEBUG) {
Log.d(TAG, "Creating new loader recents.");
}
- return new RecentsLoader(
+ loader = new RecentsLoader(
context,
mProviders,
mState,
mExecutors,
mInjector.fileTypeLookup);
}
+ loader.setObserver(observer);
+ return loader;
} else {
Uri contentsUri = mSearchMgr.isSearching()
? DocumentsContract.buildSearchDocumentsUri(
diff --git a/src/com/android/documentsui/CreateDirectoryFragment.java b/src/com/android/documentsui/CreateDirectoryFragment.java
index 8828d65..0d4aa4f 100644
--- a/src/com/android/documentsui/CreateDirectoryFragment.java
+++ b/src/com/android/documentsui/CreateDirectoryFragment.java
@@ -68,7 +68,6 @@
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
- final ContentResolver resolver = context.getContentResolver();
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
diff --git a/src/com/android/documentsui/DirectoryLoader.java b/src/com/android/documentsui/DirectoryLoader.java
index d65b4c3..63c3bf6 100644
--- a/src/com/android/documentsui/DirectoryLoader.java
+++ b/src/com/android/documentsui/DirectoryLoader.java
@@ -16,20 +16,16 @@
package com.android.documentsui;
-import static com.android.documentsui.base.SharedMinimal.DEBUG;
import static com.android.documentsui.base.SharedMinimal.VERBOSE;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
-import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.FileUtils;
-import android.os.Handler;
-import android.os.Looper;
import android.os.OperationCanceledException;
import android.os.RemoteException;
import android.provider.DocumentsContract.Document;
@@ -241,28 +237,4 @@
getContext().getContentResolver().unregisterContentObserver(mObserver);
}
-
- private static final class LockingContentObserver extends ContentObserver {
- private final ContentLock mLock;
- private final Runnable mContentChangedCallback;
-
- public LockingContentObserver(ContentLock lock, Runnable contentChangedCallback) {
- super(new Handler(Looper.getMainLooper()));
- mLock = lock;
- mContentChangedCallback = contentChangedCallback;
- }
-
- @Override
- public boolean deliverSelfNotifications() {
- return true;
- }
-
- @Override
- public void onChange(boolean selfChange) {
- if (DEBUG) {
- Log.d(TAG, "Directory content updated.");
- }
- mLock.runWhenUnlocked(mContentChangedCallback);
- }
- }
}
diff --git a/src/com/android/documentsui/LockingContentObserver.java b/src/com/android/documentsui/LockingContentObserver.java
new file mode 100644
index 0000000..bbd7a98
--- /dev/null
+++ b/src/com/android/documentsui/LockingContentObserver.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui;
+
+import static com.android.documentsui.base.SharedMinimal.DEBUG;
+import static com.android.documentsui.base.SharedMinimal.TAG;
+
+import android.database.ContentObserver;
+import android.os.Handler;
+import android.os.Looper;
+import android.util.Log;
+
+/**
+ * A custom {@link ContentObserver} which constructed by a {@link ContentLock}
+ * and a {@link Runnable} callback. It will callback when it's onChange and ContentLock is unlock.
+ */
+public final class LockingContentObserver extends ContentObserver {
+ private final ContentLock mLock;
+ private final Runnable mContentChangedCallback;
+
+ public LockingContentObserver(ContentLock lock, Runnable contentChangedCallback) {
+ super(new Handler(Looper.getMainLooper()));
+ mLock = lock;
+ mContentChangedCallback = contentChangedCallback;
+ }
+
+ @Override
+ public boolean deliverSelfNotifications() {
+ return true;
+ }
+
+ @Override
+ public void onChange(boolean selfChange) {
+ if (DEBUG) {
+ Log.d(TAG, "Content updated.");
+ }
+ mLock.runWhenUnlocked(mContentChangedCallback);
+ }
+}
diff --git a/src/com/android/documentsui/MultiRootDocumentsLoader.java b/src/com/android/documentsui/MultiRootDocumentsLoader.java
index 777efe0..11208c1 100644
--- a/src/com/android/documentsui/MultiRootDocumentsLoader.java
+++ b/src/com/android/documentsui/MultiRootDocumentsLoader.java
@@ -17,7 +17,6 @@
package com.android.documentsui;
import static com.android.documentsui.base.SharedMinimal.DEBUG;
-import static com.android.documentsui.base.SharedMinimal.TAG;
import android.app.ActivityManager;
import android.content.ContentProviderClient;
@@ -65,6 +64,9 @@
* and return the combined result.
*/
public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<DirectoryResult> {
+
+ private static final String TAG = "MultiRootDocsLoader";
+
// TODO: clean up cursor ownership so background thread doesn't traverse
// previously returned cursors for filtering/sorting; this currently races
// with the UI thread.
@@ -83,6 +85,7 @@
private final ProvidersAccess mProviders;
private final Lookup<String, Executor> mExecutors;
private final Lookup<String, String> mFileTypeMap;
+ private LockingContentObserver mObserver;
@GuardedBy("mTasks")
/** A authority -> QueryTask map */
@@ -101,6 +104,8 @@
* @param state current state
* @param executors the executors of authorities
* @param fileTypeMap the map of mime types and file types.
+ * @param lock the selection lock
+ * @param contentChangedCallback callback when content changed
*/
public MultiRootDocumentsLoader(Context context, ProvidersAccess providers, State state,
Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap) {
@@ -126,6 +131,10 @@
}
}
+ public void setObserver(LockingContentObserver observer) {
+ mObserver = observer;
+ }
+
private DirectoryResult loadInBackgroundLocked() {
if (mFirstPassLatch == null) {
// First time through we kick off all the recent tasks, and wait
@@ -322,6 +331,10 @@
FileUtils.closeQuietly(mResult);
mResult = null;
+
+ if (mObserver != null) {
+ getContext().getContentResolver().unregisterContentObserver(mObserver);
+ }
}
// TODO: create better transfer of ownership around cursor to ensure its
@@ -406,6 +419,9 @@
mState.sortModel.addQuerySortArgs(queryArgs);
addQueryArgs(queryArgs);
res[i] = client.query(uri, null, queryArgs, null);
+ if (mObserver != null) {
+ res[i].registerContentObserver(mObserver);
+ }
mCursors[i] = generateResultCursor(rootInfos.get(i), res[i]);
} catch (Exception e) {
Log.w(TAG, "Failed to load " + authority + ", " + rootInfos.get(i).rootId,
diff --git a/src/com/android/documentsui/base/Providers.java b/src/com/android/documentsui/base/Providers.java
index 586440c..1729d2e 100644
--- a/src/com/android/documentsui/base/Providers.java
+++ b/src/com/android/documentsui/base/Providers.java
@@ -41,6 +41,7 @@
public static final String ROOT_ID_AUDIO = "audio_root";
public static final String AUTHORITY_MTP = "com.android.mtp.documents";
+ public static final String AUTHORITY_BUGREPORT = "com.android.shell.documents";
private static final String DOCSUI_PACKAGE = "com.android.documentsui";
private static final Set<String> SYSTEM_AUTHORITIES = new HashSet<String>() {{
diff --git a/src/com/android/documentsui/base/RootInfo.java b/src/com/android/documentsui/base/RootInfo.java
index dd82077..e69c0f8 100644
--- a/src/com/android/documentsui/base/RootInfo.java
+++ b/src/com/android/documentsui/base/RootInfo.java
@@ -21,10 +21,8 @@
import static com.android.documentsui.base.DocumentInfo.getCursorInt;
import static com.android.documentsui.base.DocumentInfo.getCursorLong;
import static com.android.documentsui.base.DocumentInfo.getCursorString;
-import static com.android.documentsui.base.SharedMinimal.VERBOSE;
import static com.android.documentsui.base.Shared.compareToIgnoreCaseNullable;
-
-import androidx.annotation.IntDef;
+import static com.android.documentsui.base.SharedMinimal.VERBOSE;
import android.content.Context;
import android.database.Cursor;
@@ -37,6 +35,8 @@
import android.text.TextUtils;
import android.util.Log;
+import androidx.annotation.IntDef;
+
import com.android.documentsui.IconUtils;
import com.android.documentsui.R;
@@ -237,6 +237,9 @@
derivedIcon = LOAD_FROM_CONTENT_RESOLVER;
} else if (isRecents()) {
derivedType = TYPE_RECENTS;
+ } else if (isBugReport()) {
+ derivedType = TYPE_OTHER;
+ derivedIcon = R.drawable.ic_root_bugreport;
} else {
derivedType = TYPE_OTHER;
}
@@ -248,6 +251,10 @@
return DocumentsContract.buildRootUri(authority, rootId);
}
+ public boolean isBugReport() {
+ return Providers.AUTHORITY_BUGREPORT.equals(authority);
+ }
+
public boolean isRecents() {
return authority == null && rootId == null;
}
diff --git a/src/com/android/documentsui/dirlist/RenameDocumentFragment.java b/src/com/android/documentsui/dirlist/RenameDocumentFragment.java
index 8fae42e..0a25395 100644
--- a/src/com/android/documentsui/dirlist/RenameDocumentFragment.java
+++ b/src/com/android/documentsui/dirlist/RenameDocumentFragment.java
@@ -170,7 +170,7 @@
*/
private void selectFileName(EditText editText) {
String text = editText.getText().toString();
- int separatorIndex = text.indexOf(".");
+ int separatorIndex = text.lastIndexOf(".");
editText.setSelection(0,
(separatorIndex == -1 || mDocument.isDirectory()) ? text.length() : separatorIndex);
}
diff --git a/tests/common/com/android/documentsui/DialogFragmentTest.java b/tests/common/com/android/documentsui/DialogFragmentTest.java
new file mode 100644
index 0000000..05ad5fb
--- /dev/null
+++ b/tests/common/com/android/documentsui/DialogFragmentTest.java
@@ -0,0 +1,320 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import android.app.Activity;
+import android.app.Instrumentation;
+import android.app.UiAutomation;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.graphics.Paint;
+import android.os.Build;
+import android.os.ParcelFileDescriptor;
+
+import androidx.fragment.app.FragmentManager;
+import androidx.test.filters.MediumTest;
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.rule.ActivityTestRule;
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.documentsui.files.FilesActivity;
+
+import com.google.android.material.textfield.TextInputEditText;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+
+@MediumTest
+@RunWith(AndroidJUnit4.class)
+public class DialogFragmentTest {
+
+ private static final String CREATE_FRAGEMENT_TAG = "create_directory";
+ CreateDirectoryFragment mCreateDirectoryFragment;
+ FragmentManager mFragmentManager;
+ ScreenDensitySession mScreenDensitySession;
+ Intent mFileActivityIntent;
+
+ @Rule
+ public ActivityTestRule<FilesActivity> mActivityTestRule = new ActivityTestRule<>(
+ FilesActivity.class);
+
+ @Before
+ public void setup() {
+ mFileActivityIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
+ mFragmentManager = mActivityTestRule.getActivity().getSupportFragmentManager();
+ mScreenDensitySession = new ScreenDensitySession();
+ }
+
+ @After
+ public void tearDown() {
+ mScreenDensitySession.close();
+ mCreateDirectoryFragment = null;
+ }
+
+ @Test
+ public void testCreateDialogShows() throws Throwable {
+ mActivityTestRule.runOnUiThread(() -> CreateDirectoryFragment.show(mFragmentManager));
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ mCreateDirectoryFragment =
+ (CreateDirectoryFragment) mFragmentManager.findFragmentByTag(CREATE_FRAGEMENT_TAG);
+
+ assertNotNull("Dialog was null", mCreateDirectoryFragment.getDialog());
+ assertTrue("Dialog was not being shown", mCreateDirectoryFragment.getDialog().isShowing());
+ }
+
+ @Test
+ public void testCreateDialogShowsDismiss() throws Throwable {
+ mActivityTestRule.runOnUiThread(() -> CreateDirectoryFragment.show(mFragmentManager));
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ mCreateDirectoryFragment =
+ (CreateDirectoryFragment) mFragmentManager.findFragmentByTag(CREATE_FRAGEMENT_TAG);
+
+ assertNotNull("Dialog was null", mCreateDirectoryFragment.getDialog());
+ assertTrue("Dialog was not being shown", mCreateDirectoryFragment.getDialog().isShowing());
+
+ mActivityTestRule.runOnUiThread(() -> mCreateDirectoryFragment.dismiss());
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+
+ assertNull("Dialog should be null after dismiss()", mCreateDirectoryFragment.getDialog());
+ }
+
+ @Test
+ public void testCreateDialogShows_textInputEditText_shouldNotTruncateOnPortrait()
+ throws Throwable {
+ mActivityTestRule.runOnUiThread(() -> CreateDirectoryFragment.show(mFragmentManager));
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ mCreateDirectoryFragment =
+ (CreateDirectoryFragment) mFragmentManager.findFragmentByTag(CREATE_FRAGEMENT_TAG);
+
+ final TextInputEditText inputView =
+ mCreateDirectoryFragment.getDialog().findViewById(android.R.id.text1);
+
+ assertTrue(inputView.getHeight() > getInputTextHeight(inputView));
+ }
+
+ @Test
+ public void testCreateDialog_textInputEditText_shouldNotTruncateOnLargeDensity()
+ throws Throwable {
+
+ mScreenDensitySession.setLargeDensity();
+ mActivityTestRule.finishActivity();
+ mActivityTestRule.launchActivity(mFileActivityIntent);
+ mFragmentManager = mActivityTestRule.getActivity().getSupportFragmentManager();
+
+ mActivityTestRule.runOnUiThread(() -> CreateDirectoryFragment.show(mFragmentManager));
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ mCreateDirectoryFragment =
+ (CreateDirectoryFragment) mFragmentManager.findFragmentByTag(CREATE_FRAGEMENT_TAG);
+
+ final TextInputEditText inputView =
+ mCreateDirectoryFragment.getDialog().getWindow().findViewById(android.R.id.text1);
+
+ assertTrue(inputView.getHeight() > getInputTextHeight(inputView));
+
+ }
+
+ @Test
+ public void testCreateDialog_textInputEditText_shouldNotTruncateOnLargerDensity()
+ throws Throwable {
+
+ mScreenDensitySession.setLargerDensity();
+ mActivityTestRule.finishActivity();
+ mActivityTestRule.launchActivity(mFileActivityIntent);
+ mFragmentManager = mActivityTestRule.getActivity().getSupportFragmentManager();
+
+ mActivityTestRule.runOnUiThread(() -> CreateDirectoryFragment.show(mFragmentManager));
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ mCreateDirectoryFragment =
+ (CreateDirectoryFragment) mFragmentManager.findFragmentByTag(CREATE_FRAGEMENT_TAG);
+
+ final TextInputEditText inputView =
+ mCreateDirectoryFragment.getDialog().getWindow().findViewById(android.R.id.text1);
+
+ assertTrue(inputView.getHeight() > getInputTextHeight(inputView));
+ }
+
+ @Test
+ public void testCreateDialog_textInputEditText_shouldNotTruncateOnLargestDensity()
+ throws Throwable {
+
+ mScreenDensitySession.setLargestDensity();
+ mActivityTestRule.finishActivity();
+ mActivityTestRule.launchActivity(mFileActivityIntent);
+ mFragmentManager = mActivityTestRule.getActivity().getSupportFragmentManager();
+
+ mActivityTestRule.runOnUiThread(() -> CreateDirectoryFragment.show(mFragmentManager));
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ mCreateDirectoryFragment =
+ (CreateDirectoryFragment) mFragmentManager.findFragmentByTag(CREATE_FRAGEMENT_TAG);
+
+ final TextInputEditText inputView =
+ mCreateDirectoryFragment.getDialog().getWindow().findViewById(android.R.id.text1);
+
+ assertTrue(inputView.getHeight() > getInputTextHeight(inputView));
+ }
+
+ @Test
+ public void testCreateDirectoryFragmentShows_textInputEditText_shouldNotTruncateOnLandscape()
+ throws Throwable {
+ switchOrientation(mActivityTestRule.getActivity());
+ mScreenDensitySession.setLargestDensity();
+ mActivityTestRule.finishActivity();
+ mActivityTestRule.launchActivity(mFileActivityIntent);
+ mFragmentManager = mActivityTestRule.getActivity().getSupportFragmentManager();
+
+ mActivityTestRule.runOnUiThread(() -> CreateDirectoryFragment.show(mFragmentManager));
+ InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+ mCreateDirectoryFragment =
+ (CreateDirectoryFragment) mFragmentManager.findFragmentByTag(CREATE_FRAGEMENT_TAG);
+
+ final TextInputEditText inputView =
+ mCreateDirectoryFragment.getDialog().getWindow().findViewById(android.R.id.text1);
+ Paint paint = inputView.getPaint();
+ final float textSize = paint.getTextSize();
+
+ assertTrue(inputView.getHeight() > Math.round(textSize));
+
+ switchOrientation(mActivityTestRule.getActivity());
+ }
+
+ private static int getInputTextHeight(TextInputEditText v) {
+ Paint paint = v.getPaint();
+ final float textSize = paint.getTextSize();
+ final float textSpace = paint.getFontSpacing();
+ return Math.round(textSize + textSpace);
+ }
+
+ private static void switchOrientation(final Activity activity) {
+ final int[] orientations = getOrientations(activity);
+ activity.setRequestedOrientation(orientations[1]);
+ }
+
+ private static int[] getOrientations(final Activity activity) {
+ final int originalOrientation = activity.getResources().getConfiguration().orientation;
+ final int newOrientation = originalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
+ ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
+ : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+ return new int[]{originalOrientation, newOrientation};
+ }
+
+ private static class ScreenDensitySession implements AutoCloseable {
+ private static final String DENSITY_PROP_DEVICE = "ro.sf.lcd_density";
+ private static final String DENSITY_PROP_EMULATOR = "qemu.sf.lcd_density";
+
+ private static final float DENSITY_DEFAULT = 1f;
+ private static final float DENSITY_LARGE = 1.09f;
+ private static final float DENSITY_LARGER = 1.19f;
+ private static final float DENSITY_LARGEST = 1.29f;
+
+ void setDefaultDensity() {
+ final int stableDensity = getStableDensity();
+ final int targetDensity = (int) (stableDensity * DENSITY_DEFAULT);
+ setDensity(targetDensity);
+ }
+
+ void setLargeDensity() {
+ final int stableDensity = getStableDensity();
+ final int targetDensity = (int) (stableDensity * DENSITY_LARGE);
+ setDensity(targetDensity);
+ }
+
+ void setLargerDensity() {
+ final int stableDensity = getStableDensity();
+ final int targetDensity = (int) (stableDensity * DENSITY_LARGER);
+ setDensity(targetDensity);
+ }
+
+ void setLargestDensity() {
+ final int stableDensity = getStableDensity();
+ final int targetDensity = (int) (stableDensity * DENSITY_LARGEST);
+ setDensity(targetDensity);
+ }
+
+ @Override
+ public void close() {
+ resetDensity();
+ }
+
+ private int getStableDensity() {
+ final String densityProp;
+ if (Build.IS_EMULATOR) {
+ densityProp = DENSITY_PROP_EMULATOR;
+ } else {
+ densityProp = DENSITY_PROP_DEVICE;
+ }
+
+ return Integer.parseInt(executeShellCommand("getprop " + densityProp).trim());
+ }
+
+ private void setDensity(int targetDensity) {
+ executeShellCommand("wm density " + targetDensity);
+
+ // Verify that the density is changed.
+ final String output = executeShellCommand("wm density");
+ final boolean success = output.contains("Override density: " + targetDensity);
+
+ assertTrue("Failed to set density to " + targetDensity, success);
+ }
+
+ private void resetDensity() {
+ executeShellCommand("wm density reset");
+ }
+ }
+
+ public static String executeShellCommand(String cmd) {
+ try {
+ return runShellCommand(InstrumentationRegistry.getInstrumentation(), cmd);
+ } catch (IOException e) {
+ fail("Failed reading command output: " + e);
+ return "";
+ }
+ }
+
+ public static String runShellCommand(Instrumentation instrumentation, String cmd)
+ throws IOException {
+ return runShellCommand(instrumentation.getUiAutomation(), cmd);
+ }
+
+ public static String runShellCommand(UiAutomation automation, String cmd)
+ throws IOException {
+ if (cmd.startsWith("pm grant ") || cmd.startsWith("pm revoke ")) {
+ throw new UnsupportedOperationException("Use UiAutomation.grantRuntimePermission() "
+ + "or revokeRuntimePermission() directly, which are more robust.");
+ }
+ ParcelFileDescriptor pfd = automation.executeShellCommand(cmd);
+ byte[] buf = new byte[512];
+ int bytesRead;
+ FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
+ StringBuffer stdout = new StringBuffer();
+ while ((bytesRead = fis.read(buf)) != -1) {
+ stdout.append(new String(buf, 0, bytesRead));
+ }
+ fis.close();
+ return stdout.toString();
+ }
+}
diff --git a/tests/common/com/android/documentsui/testing/TestCursor.java b/tests/common/com/android/documentsui/testing/TestCursor.java
new file mode 100644
index 0000000..56f4269
--- /dev/null
+++ b/tests/common/com/android/documentsui/testing/TestCursor.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui.testing;
+
+import android.database.MatrixCursor;
+
+public class TestCursor extends MatrixCursor {
+
+ public TestCursor(String[] columnNames) {
+ super(columnNames);
+ }
+
+ public void mockOnChange() {
+ onChange(false);
+ }
+}
diff --git a/tests/common/com/android/documentsui/testing/TestDocumentsProvider.java b/tests/common/com/android/documentsui/testing/TestDocumentsProvider.java
index 794d1c9..3e4dffd 100644
--- a/tests/common/com/android/documentsui/testing/TestDocumentsProvider.java
+++ b/tests/common/com/android/documentsui/testing/TestDocumentsProvider.java
@@ -117,7 +117,7 @@
}
private Cursor createDocumentsCursor(DocumentInfo... docs) {
- MatrixCursor cursor = new MatrixCursor(DOCUMENTS_PROJECTION);
+ TestCursor cursor = new TestCursor(DOCUMENTS_PROJECTION);
for (DocumentInfo doc : docs) {
cursor.newRow()
.add(Document.COLUMN_DOCUMENT_ID, doc.documentId)
diff --git a/tests/functional/com/android/documentsui/FilesActivityUiTest.java b/tests/functional/com/android/documentsui/FilesActivityUiTest.java
index 16dc087..2da8309 100644
--- a/tests/functional/com/android/documentsui/FilesActivityUiTest.java
+++ b/tests/functional/com/android/documentsui/FilesActivityUiTest.java
@@ -57,7 +57,14 @@
// to be able to click on it.
public void testClickRecent() throws Exception {
bots.roots.openRoot("Recent");
- bots.main.assertSearchBarShow();
+
+ boolean showSearchBar =
+ context.getResources().getBoolean(R.bool.show_search_bar);
+ if (showSearchBar) {
+ bots.main.assertSearchBarShow();
+ } else {
+ bots.main.assertWindowTitle("Recent");
+ }
}
public void testRootClick_SetsWindowTitle() throws Exception {
diff --git a/tests/unit/com/android/documentsui/RecentsLoaderTests.java b/tests/unit/com/android/documentsui/RecentsLoaderTests.java
index f044b92..73ba36f 100644
--- a/tests/unit/com/android/documentsui/RecentsLoaderTests.java
+++ b/tests/unit/com/android/documentsui/RecentsLoaderTests.java
@@ -29,6 +29,7 @@
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.State;
import com.android.documentsui.testing.ActivityManagers;
+import com.android.documentsui.testing.TestCursor;
import com.android.documentsui.testing.TestEnv;
import com.android.documentsui.testing.TestFileTypeLookup;
import com.android.documentsui.testing.TestImmediateExecutor;
@@ -38,6 +39,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
@RunWith(AndroidJUnit4.class)
@MediumTest
public class RecentsLoaderTests {
@@ -45,6 +49,7 @@
private TestEnv mEnv;
private TestActivity mActivity;
private RecentsLoader mLoader;
+ private boolean mContentChanged;
@Before
public void setUp() {
@@ -105,4 +110,28 @@
assertEquals(0, flags & Document.FLAG_SUPPORTS_MOVE);
}
}
+
+ @Test
+ public void testContentsUpdate_observable() throws Exception {
+ CountDownLatch latch = new CountDownLatch(1);
+ Runnable callback = () -> {
+ latch.countDown();
+ mContentChanged = true;
+ };
+ mLoader.setObserver(new LockingContentObserver(new ContentLock(), callback));
+
+ final DocumentInfo doc = mEnv.model.createFile("freddy.jpg");
+ doc.lastModified = System.currentTimeMillis();
+ mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
+ .setNextRecentDocumentsReturns(doc);
+
+ mLoader.loadInBackground();
+
+ TestCursor c = (TestCursor) mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
+ .queryRecentDocuments(null, null);
+ c.mockOnChange();
+
+ latch.await(1, TimeUnit.SECONDS);
+ assertTrue(mContentChanged);
+ }
}