add API to get screen size in dp, and a skeleton test

As a replacement for UiDevice#isPhone, a better way to determine
the layout in use is to look at screen size, as most apps are
doing to adjust layout, more specifically, the width in dp

The skeleton test is meant to be used as a prototype for
spawning new UI tests. It has basic setup to be built in the
source tree, and has one single test case.

Change-Id: I1c4738eba4aca24fcb960dfd873c51107777789e
diff --git a/uiautomator/library/src/com/android/uiautomator/core/UiDevice.java b/uiautomator/library/src/com/android/uiautomator/core/UiDevice.java
index 2aedd9d..08016fb 100644
--- a/uiautomator/library/src/com/android/uiautomator/core/UiDevice.java
+++ b/uiautomator/library/src/com/android/uiautomator/core/UiDevice.java
@@ -18,10 +18,12 @@
 
 import android.content.Context;
 import android.graphics.Point;
+import android.os.Build;
 import android.os.Environment;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemClock;
+import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.Display;
 import android.view.IWindowManager;
@@ -93,6 +95,38 @@
     }
 
     /**
+     * Returns the display size in dp (device-independent pixel)
+     *
+     * The returned display size is adjusted per screen rotation
+     *
+     * @return
+     */
+    public Point getDisplaySizeDp() {
+        Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
+        Point p = new Point();
+        display.getSize(p);
+        DisplayMetrics metrics = new DisplayMetrics();
+        display.getMetrics(metrics);
+        float dpx = p.x / metrics.density;
+        float dpy = p.y / metrics.density;
+        p.x = Math.round(dpx);
+        p.y = Math.round(dpy);
+        return p;
+    }
+
+    /**
+     * Returns the product name of the device
+     *
+     * This provides info on what type of device that the test is running on. However, for the
+     * purpose of adapting to different styles of UI, test should favor
+     * {@link UiDevice#getDisplaySizeDp()} over this method, and only use product name as a fallback
+     * mechanism
+     */
+    public String getProductName() {
+        return Build.PRODUCT;
+    }
+
+    /**
      * This method returns the text from the last UI traversal event received.
      * This is helpful in WebView when the test performs directional arrow presses to focus
      * on different elements inside the WebView. The accessibility fires events
diff --git a/uiautomator/samples/SkeletonTest/Android.mk b/uiautomator/samples/SkeletonTest/Android.mk
new file mode 100644
index 0000000..a4a776f
--- /dev/null
+++ b/uiautomator/samples/SkeletonTest/Android.mk
@@ -0,0 +1,30 @@
+#Copyright (C) 2012 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.
+
+local_target_dir := $(TARGET_OUT_DATA)/local/tmp
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE := uiautomator.skeletontest
+
+LOCAL_JAVA_LIBRARIES := uiautomator.core
+
+LOCAL_MODULE_PATH := $(local_target_dir)
+
+include $(BUILD_JAVA_LIBRARY)
diff --git a/uiautomator/samples/SkeletonTest/README b/uiautomator/samples/SkeletonTest/README
new file mode 100644
index 0000000..b402039
--- /dev/null
+++ b/uiautomator/samples/SkeletonTest/README
@@ -0,0 +1,19 @@
+This is an (almost) empty test, it serves as a prototype to create new UI
+Automator tests that can be compiled inside the Android build tree. The single
+test case included performs a key press, and send out some information about
+the test device.
+
+Steps to run this test:
+* have a fully built Android source tree
+* build the test:
+  mmm frameworks/testing/uiautomator/samples/SkeletonTest
+* deploy the test:
+  adb push ${OUT}/data/local/tmp/uiautomator.skeletontest.jar /data/local/tmp/
+* run the test:
+  adb shell uiautomator runtest uiautomator.skeletontest.jar \
+    -e class com.android.uiautomator.samples.skeleton.DemoTestCase
+
+Steps to create new tests off it:
+* cp -r frameworks/testing/uiautomator/samples/SkeletonTest /new/location
+* modify Android.mk, replace LOCAL_MODULE_NAME, change LOCAL_MODULE_TAGS if
+  necessary, add new dependecies if needed
diff --git a/uiautomator/samples/SkeletonTest/src/com/android/uiautomator/samples/skeleton/DemoTestCase.java b/uiautomator/samples/SkeletonTest/src/com/android/uiautomator/samples/skeleton/DemoTestCase.java
new file mode 100644
index 0000000..1abc25a
--- /dev/null
+++ b/uiautomator/samples/SkeletonTest/src/com/android/uiautomator/samples/skeleton/DemoTestCase.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2012 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.uiautomator.samples.skeleton;
+
+import android.app.Activity;
+import android.graphics.Point;
+import android.os.Bundle;
+
+import com.android.uiautomator.testrunner.UiAutomatorTestCase;
+
+public class DemoTestCase extends UiAutomatorTestCase {
+
+    public void testDemo() {
+        assertTrue(getUiDevice().pressHome());
+        Bundle status = new Bundle();
+        status.putString("msg", "This is a demo test and I just pressed HOME");
+        status.putString("product", getUiDevice().getProductName());
+        Point p = getUiDevice().getDisplaySizeDp();
+        status.putInt("dp-width", p.x);
+        status.putInt("dp-height", p.y);
+        getAutomationSupport().sendStatus(Activity.RESULT_OK, status);
+    }
+}