Make DragAndDropTest work reliably on smaller screens

Instead of creating multiple widgets corresponding to specific
View.DRAG_FLAG* combinations, create a single widget and pass the
required flag combination via an intent extra.

Bug: 28508640
Change-Id: Ifcaf947713fe7590d2a08babfc45547a9e2dd537
diff --git a/tests/tests/draganddrop/dragsource/res/layout/main_activity.xml b/tests/tests/draganddrop/dragsource/res/layout/main_activity.xml
index cac6a082..98c8202 100644
--- a/tests/tests/draganddrop/dragsource/res/layout/main_activity.xml
+++ b/tests/tests/draganddrop/dragsource/res/layout/main_activity.xml
@@ -21,91 +21,11 @@
         android:orientation="vertical">
 
     <TextView
-            android:id="@+id/dont_grant"
+            android:id="@+id/source"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#ddd"
-            android:text="@string/dont_grant_label">
-    </TextView>
-
-    <TextView
-            android:id="@+id/grant_read"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#ddd"
-            android:text="@string/grant_read_label">
-    </TextView>
-
-    <TextView
-            android:id="@+id/grant_write"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#ddd"
-            android:text="@string/grant_write_label">
-    </TextView>
-
-    <TextView
-            android:id="@+id/grant_read_prefix"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#ddd"
-            android:text="@string/grant_read_prefix_label">
-    </TextView>
-
-    <TextView
-            android:id="@+id/grant_read_noprefix"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#ddd"
-            android:text="@string/grant_read_noprefix_label">
-    </TextView>
-
-    <TextView
-            android:id="@+id/grant_read_persistable"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#ddd"
-            android:text="@string/grant_read_persistable_label">
-    </TextView>
-
-    <TextView
-            android:id="@+id/disallow_global"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#aaa"
-            android:text="@string/disallow_global_label">
-    </TextView>
-
-    <TextView
-            android:id="@+id/cancel_soon"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#aaa"
-            android:text="@string/cancel_soon_label">
+            android:background="#ddd">
     </TextView>
 
 </LinearLayout>
\ No newline at end of file
diff --git a/tests/tests/draganddrop/dragsource/res/values/dimen.xml b/tests/tests/draganddrop/dragsource/res/values/dimen.xml
deleted file mode 100644
index 4923a32..0000000
--- a/tests/tests/draganddrop/dragsource/res/values/dimen.xml
+++ /dev/null
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2016 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.
--->
-
-<resources>
-    <dimen name="item_margin">6dp</dimen>
-    <dimen name="item_padding">6dp</dimen>
-</resources>
diff --git a/tests/tests/draganddrop/dragsource/res/values/strings.xml b/tests/tests/draganddrop/dragsource/res/values/strings.xml
deleted file mode 100644
index 28cd792..0000000
--- a/tests/tests/draganddrop/dragsource/res/values/strings.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2016 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.
--->
-
-<resources>
-
-     <string name="dont_grant_label">Do not grant permissions</string>
-     <string name="disallow_global_label">Disallow global drag</string>
-     <string name="cancel_soon_label">Cancel soon after start</string>
-     <string name="grant_read_label">Grant read permissions</string>
-     <string name="grant_write_label">Grant write permissions</string>
-     <string name="grant_read_prefix_label">Grant read+prefix permissions</string>
-     <string name="grant_read_noprefix_label">Grant read, no prefix permissions</string>
-     <string name="grant_read_persistable_label">Grant read+persistable</string>
-
-</resources>
diff --git a/tests/tests/draganddrop/dragsource/src/android/dnd/cts/dragsource/DragSource.java b/tests/tests/draganddrop/dragsource/src/android/dnd/cts/dragsource/DragSource.java
index bbba70d..df93852 100644
--- a/tests/tests/draganddrop/dragsource/src/android/dnd/cts/dragsource/DragSource.java
+++ b/tests/tests/draganddrop/dragsource/src/android/dnd/cts/dragsource/DragSource.java
@@ -24,6 +24,7 @@
 import android.os.Handler;
 import android.os.PersistableBundle;
 import android.view.View;
+import android.widget.TextView;
 
 public class DragSource extends Activity{
     private static final String URI_PREFIX =
@@ -41,30 +42,35 @@
 
         final Uri plainUri = Uri.parse(URI_PREFIX + "/" + MAGIC_VALUE);
 
-        setUpDragSource(R.id.disallow_global, plainUri, 0);
-        setUpDragSource(R.id.cancel_soon, plainUri, View.DRAG_FLAG_GLOBAL);
+        setUpDragSource("disallow_global", plainUri, 0);
+        setUpDragSource("cancel_soon", plainUri, View.DRAG_FLAG_GLOBAL);
 
-        setUpDragSource(R.id.dont_grant, plainUri, View.DRAG_FLAG_GLOBAL);
-        setUpDragSource(R.id.grant_read, plainUri,
+        setUpDragSource("dont_grant", plainUri, View.DRAG_FLAG_GLOBAL);
+        setUpDragSource("grant_read", plainUri,
                 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ);
-        setUpDragSource(R.id.grant_write, plainUri,
+        setUpDragSource("grant_write", plainUri,
                 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_WRITE);
-        setUpDragSource(R.id.grant_read_persistable, plainUri,
+        setUpDragSource("grant_read_persistable", plainUri,
                 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
                         View.DRAG_FLAG_GLOBAL_PERSISTABLE_URI_PERMISSION);
 
         final Uri prefixUri = Uri.parse(URI_PREFIX);
 
-        setUpDragSource(R.id.grant_read_prefix, prefixUri,
+        setUpDragSource("grant_read_prefix", prefixUri,
                 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ |
                         View.DRAG_FLAG_GLOBAL_PREFIX_URI_PERMISSION);
-        setUpDragSource(R.id.grant_read_noprefix, prefixUri,
+        setUpDragSource("grant_read_noprefix", prefixUri,
                 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ);
 
     }
 
-    private void setUpDragSource(final int resourceId, final Uri uri, final int flags) {
-        findViewById(resourceId).setOnLongClickListener(new View.OnLongClickListener() {
+    private void setUpDragSource(String mode, final Uri uri, final int flags) {
+        if (!mode.equals(getIntent().getStringExtra("mode"))) {
+            return;
+        }
+        final View source = findViewById(R.id.source);
+        ((TextView) source).setText(mode);
+        source.setOnLongClickListener(new View.OnLongClickListener() {
             @Override
             public boolean onLongClick(final View v) {
                 final ClipDescription clipDescription = new ClipDescription("", new String[] {
@@ -78,7 +84,7 @@
                         new View.DragShadowBuilder(v),
                         null,
                         flags);
-                if (resourceId == R.id.cancel_soon) {
+                if (mode.equals("cancel_soon")) {
                     new Handler().postDelayed(new Runnable() {
                         @Override
                         public void run() {
diff --git a/tests/tests/draganddrop/droptarget/res/layout/main_activity.xml b/tests/tests/draganddrop/droptarget/res/layout/main_activity.xml
index 4a5a5b1..509debc 100644
--- a/tests/tests/draganddrop/droptarget/res/layout/main_activity.xml
+++ b/tests/tests/draganddrop/droptarget/res/layout/main_activity.xml
@@ -21,58 +21,11 @@
         android:orientation="vertical">
 
     <TextView
-            android:id="@+id/dont_request"
+            android:id="@+id/target"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:gravity="center"
-            android:layout_margin="@dimen/item_margin"
-            android:padding="@dimen/item_padding"
-            android:background="#ddd"
-            android:text="@string/dont_request_label">
-    </TextView>
-
-    <TextView
-        android:id="@+id/request_read"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:gravity="center"
-        android:layout_margin="@dimen/item_margin"
-        android:padding="@dimen/item_padding"
-        android:background="#ddd"
-        android:text="@string/request_read_label">
-    </TextView>
-
-    <TextView
-        android:id="@+id/request_write"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:gravity="center"
-        android:layout_margin="@dimen/item_margin"
-        android:padding="@dimen/item_padding"
-        android:background="#ddd"
-        android:text="@string/request_write_label">
-    </TextView>
-
-    <TextView
-        android:id="@+id/request_read_nested"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:gravity="center"
-        android:layout_margin="@dimen/item_margin"
-        android:padding="@dimen/item_padding"
-        android:background="#ddd"
-        android:text="@string/request_read_nested_label">
-    </TextView>
-
-    <TextView
-        android:id="@+id/request_take_persistable"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:gravity="center"
-        android:layout_margin="@dimen/item_margin"
-        android:padding="@dimen/item_padding"
-        android:background="#ddd"
-        android:text="@string/request_take_persistable_label">
+            android:background="#ddd">
     </TextView>
 
     <TextView
diff --git a/tests/tests/draganddrop/droptarget/res/values/dimen.xml b/tests/tests/draganddrop/droptarget/res/values/dimen.xml
deleted file mode 100644
index 4923a32..0000000
--- a/tests/tests/draganddrop/droptarget/res/values/dimen.xml
+++ /dev/null
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2016 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.
--->
-
-<resources>
-    <dimen name="item_margin">6dp</dimen>
-    <dimen name="item_padding">6dp</dimen>
-</resources>
diff --git a/tests/tests/draganddrop/droptarget/res/values/strings.xml b/tests/tests/draganddrop/droptarget/res/values/strings.xml
index 9fa4619..ea0cc34 100644
--- a/tests/tests/draganddrop/droptarget/res/values/strings.xml
+++ b/tests/tests/draganddrop/droptarget/res/values/strings.xml
@@ -15,12 +15,5 @@
 -->
 
 <resources>
-
-     <string name="dont_request_label">Do not request permissions, read</string>
-     <string name="request_read_label">Request permissions, read</string>
-     <string name="request_write_label">Request permissions, write</string>
-     <string name="request_read_nested_label">Request permissions, read nested</string>
-     <string name="request_take_persistable_label">Request permissions, take persistable</string>
      <string name="not_available_label">N/A</string>
-
 </resources>
diff --git a/tests/tests/draganddrop/droptarget/src/android/dnd/cts/droptarget/DropTarget.java b/tests/tests/draganddrop/droptarget/src/android/dnd/cts/droptarget/DropTarget.java
index 7c157b5..62c7d16 100644
--- a/tests/tests/draganddrop/droptarget/src/android/dnd/cts/droptarget/DropTarget.java
+++ b/tests/tests/draganddrop/droptarget/src/android/dnd/cts/droptarget/DropTarget.java
@@ -40,15 +40,20 @@
         View view = getLayoutInflater().inflate(R.layout.main_activity, null);
         setContentView(view);
 
-        setUpDropTarget(R.id.dont_request, new OnDragUriReadListener(false));
-        setUpDropTarget(R.id.request_read, new OnDragUriReadListener());
-        setUpDropTarget(R.id.request_write, new OnDragUriWriteListener());
-        setUpDropTarget(R.id.request_read_nested, new OnDragUriReadPrefixListener());
-        setUpDropTarget(R.id.request_take_persistable, new OnDragUriTakePersistableListener());
+        setUpDropTarget("dont_request", new OnDragUriReadListener(false));
+        setUpDropTarget("request_read", new OnDragUriReadListener());
+        setUpDropTarget("request_write", new OnDragUriWriteListener());
+        setUpDropTarget("request_read_nested", new OnDragUriReadPrefixListener());
+        setUpDropTarget("request_take_persistable", new OnDragUriTakePersistableListener());
     }
 
-    private void setUpDropTarget(final int targetResourceId, OnDragUriListener listener) {
-        findViewById(targetResourceId).setOnDragListener(listener);
+    private void setUpDropTarget(String mode, OnDragUriListener listener) {
+        if (!mode.equals(getIntent().getStringExtra("mode"))) {
+            return;
+        }
+        final View target = findViewById(R.id.target);
+        ((TextView) target).setText(mode);
+        target.setOnDragListener(listener);
     }
 
     private String checkExtraValue(DragEvent event) {
diff --git a/tests/tests/draganddrop/src/android/dnd/cts/DragAndDropTest.java b/tests/tests/draganddrop/src/android/dnd/cts/DragAndDropTest.java
index 9924839..e91e9c9 100644
--- a/tests/tests/draganddrop/src/android/dnd/cts/DragAndDropTest.java
+++ b/tests/tests/draganddrop/src/android/dnd/cts/DragAndDropTest.java
@@ -46,10 +46,11 @@
         mDevice = UiDevice.getInstance(getInstrumentation());
     }
 
-    private void startAppInStack(String packageName, int stackId) {
+    private void startAppInStack(String packageName, int stackId, String mode) {
         Context context = getInstrumentation().getContext();
         Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
+        intent.putExtra("mode", mode);
 
         ActivityOptions options = ActivityOptions.makeBasic();
         options.setLaunchStackId(stackId);
@@ -71,12 +72,12 @@
         mDevice.drag(srcPosition.x, srcPosition.y, tgtPosition.x, tgtPosition.y, 100);
     }
 
-    private void doCrossAppDrag(String sourceViewId, String targetViewId, String expectedResult) {
-        startAppInStack(DRAG_SOURCE_PKG, DOCKED_STACK_ID);
-        Point srcPosition = getVisibleCenter(DRAG_SOURCE_PKG, sourceViewId);
+    private void doCrossAppDrag(String sourceMode, String targetMode, String expectedResult) {
+        startAppInStack(DRAG_SOURCE_PKG, DOCKED_STACK_ID, sourceMode);
+        Point srcPosition = getVisibleCenter(DRAG_SOURCE_PKG, "source");
 
-        startAppInStack(DROP_TARGET_PKG, FREEFORM_WORKSPACE_STACK_ID);
-        Point tgtPosition = getVisibleCenter(DROP_TARGET_PKG, targetViewId);
+        startAppInStack(DROP_TARGET_PKG, FREEFORM_WORKSPACE_STACK_ID, targetMode);
+        Point tgtPosition = getVisibleCenter(DROP_TARGET_PKG, "target");
 
         drag(srcPosition, tgtPosition);