Bringup System app Jank tests for clockwork.
Updated MIN frames and some code clean up.

Change-Id: I0ad615be14df79a27269a2ef9695cba04bb1c3e3
diff --git a/tests/jank/sysapp_wear/Android.mk b/tests/jank/sysapp_wear/Android.mk
new file mode 100644
index 0000000..a8c27cd
--- /dev/null
+++ b/tests/jank/sysapp_wear/Android.mk
@@ -0,0 +1,26 @@
+# Copyright 2015 Google Inc. All Rights Reserved.
+#
+# 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_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_PACKAGE_NAME := SysAppJankTestsWear
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_STATIC_JAVA_LIBRARIES := ub-uiautomator ub-janktesthelper
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/tests/jank/sysapp_wear/AndroidManifest.xml b/tests/jank/sysapp_wear/AndroidManifest.xml
new file mode 100644
index 0000000..78d3567
--- /dev/null
+++ b/tests/jank/sysapp_wear/AndroidManifest.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2015 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.wearable.sysapp.janktests">
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation
+            android:name="android.test.InstrumentationTestRunner"
+            android:targetPackage="com.android.wearable.sysapp.janktests"
+            android:label="Wearable Platform System Apps Jank Tests" />
+</manifest>
diff --git a/tests/jank/sysapp_wear/src/com/android/wearable/sysapp/janktests/CardsJankTest.java b/tests/jank/sysapp_wear/src/com/android/wearable/sysapp/janktests/CardsJankTest.java
new file mode 100644
index 0000000..10f3bfb
--- /dev/null
+++ b/tests/jank/sysapp_wear/src/com/android/wearable/sysapp/janktests/CardsJankTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2015 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.wearable.sysapp.janktests;
+
+import android.os.Bundle;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.support.test.jank.GfxMonitor;
+import android.support.test.jank.JankTest;
+import android.support.test.jank.JankTestBase;
+import android.support.test.uiautomator.UiDevice;
+
+/**
+ * Janks tests for scrolling & swiping off notification cards on wear
+ */
+public class CardsJankTest extends JankTestBase {
+
+    private UiDevice mDevice;
+    private SysAppTestHelper mHelper;
+
+    /*
+     * (non-Javadoc)
+     * @see junit.framework.TestCase#setUp()
+     */
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mDevice = UiDevice.getInstance(getInstrumentation());
+        mHelper = SysAppTestHelper.getInstance(mDevice, this.getInstrumentation().getContext());
+        mDevice.wakeUp();
+        SystemClock.sleep(SysAppTestHelper.SHORT_TIMEOUT);
+    }
+
+    // Prepare device to start scrolling by tapping on the screen
+    // As this is done using demo cards a tap on screen will stop animation and show
+    // home screen
+    public void openScrollCard() throws Exception {
+        mHelper.goBackHome();
+        mHelper.hasDemoCards();
+        SystemClock.sleep(SysAppTestHelper.SHORT_TIMEOUT);
+    }
+
+    // Measure card scroll jank
+
+    @JankTest(beforeTest = "openScrollCard", afterTest = "goBackHome",
+            expectedFrames = SysAppTestHelper.MIN_FRAMES)
+    @GfxMonitor(processName = "com.google.android.wearable.app")
+    public void testScrollCard() {
+        mHelper.swipeUp();
+    }
+
+    // Preparing the cards to full view before dismissing them
+
+    public void openSwipeCard() throws Exception {
+        mHelper.hasDemoCards();
+        mHelper.swipeUp();
+        mHelper.swipeUp();
+        SystemClock.sleep(SysAppTestHelper.SHORT_TIMEOUT);
+    }
+
+    // Measure jank when dismissing a card
+
+    @JankTest(beforeTest = "openSwipeCard", afterTest = "goBackHome",
+            expectedFrames = SysAppTestHelper.MIN_FRAMES)
+    @GfxMonitor(processName = "com.google.android.wearable.app")
+    public void testSwipeCard() {
+        mHelper.swipeRight();
+    }
+
+    // Ensuring that we head back to the first screen before launching the app again
+    public void goBackHome(Bundle metrics) throws RemoteException {
+        mHelper.goBackHome();
+        super.afterTest(metrics);
+        SystemClock.sleep(SysAppTestHelper.LONG_TIMEOUT);
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see android.test.InstrumentationTestCase#tearDown()
+     */
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+}
diff --git a/tests/jank/sysapp_wear/src/com/android/wearable/sysapp/janktests/SysAppTestHelper.java b/tests/jank/sysapp_wear/src/com/android/wearable/sysapp/janktests/SysAppTestHelper.java
new file mode 100644
index 0000000..5421602
--- /dev/null
+++ b/tests/jank/sysapp_wear/src/com/android/wearable/sysapp/janktests/SysAppTestHelper.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2015 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.wearable.sysapp.janktests;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.UiObject;
+import android.support.test.uiautomator.UiSelector;
+
+import junit.framework.Assert;
+
+/**
+ * Helper for all they system apps tests
+ */
+public class SysAppTestHelper {
+
+    public static final int MIN_FRAMES = 20;
+    public static final int LONG_TIMEOUT = 5000;
+    public static final int SHORT_TIMEOUT = 500;
+    private static final long NEW_CARD_TIMEOUT_MS = 10 * 1000; // 10s
+    private static final int CARD_SWIPE_STEPS = 20;
+
+    // Demo card selectors
+    private static final UiSelector CARD_SELECTOR = new UiSelector()
+            .resourceId("com.google.android.wearable.app:id/snippet");
+    private static final UiSelector TITLE_SELECTOR = new UiSelector()
+            .resourceId("com.google.android.wearable.app:id/title");
+    private static final UiSelector CLOCK_SELECTOR = new UiSelector()
+            .resourceId("com.google.android.wearable.app:id/clock_bar");
+
+    private UiDevice mDevice = null;
+    private Context mContext = null; // Currently not used but for further tests may be useful.
+    private UiObject mCard = null;
+    private UiObject mTitle = null;
+    private UiObject mClock = null;
+    private Intent mIntent = null;
+    private static SysAppTestHelper sysAppTestHelperInstance;
+
+    /**
+     * @param mDevice
+     * @param mContext
+     */
+    private SysAppTestHelper(UiDevice mDevice, Context mContext) {
+        super();
+        this.mDevice = mDevice;
+        this.mContext = mContext;
+        mIntent = new Intent();
+    }
+
+    public static SysAppTestHelper getInstance(UiDevice device, Context context) {
+        if (sysAppTestHelperInstance == null) {
+            sysAppTestHelperInstance = new SysAppTestHelper(device, context);
+        }
+        return sysAppTestHelperInstance;
+    }
+
+    public void swipeRight() {
+        mDevice.swipe(50,
+                mDevice.getDisplayHeight() / 2, mDevice.getDisplayWidth() - 25,
+                mDevice.getDisplayHeight() / 2, 30); // slow speed
+        SystemClock.sleep(SHORT_TIMEOUT);
+    }
+
+    public void swipeLeft() {
+        mDevice.swipe(mDevice.getDisplayWidth() - 50, mDevice.getDisplayHeight() / 2, 50,
+                mDevice.getDisplayHeight() / 2, 30); // slow speed
+        SystemClock.sleep(SHORT_TIMEOUT);
+    }
+
+    public void swipeUp() {
+        mDevice.swipe(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight() / 2 + 50,
+                mDevice.getDisplayWidth() / 2, 0, 30); // slow speed
+        SystemClock.sleep(SHORT_TIMEOUT);
+    }
+
+    public void swipeDown() {
+        mDevice.swipe(mDevice.getDisplayWidth() / 2, 0, mDevice.getDisplayWidth() / 2,
+                mDevice.getDisplayHeight() / 2 + 50, 30); // slow speed
+        SystemClock.sleep(SHORT_TIMEOUT);
+    }
+
+    public void flingUp() {
+        mDevice.swipe(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight() / 2 + 50,
+                mDevice.getDisplayWidth() / 2, 0, 5); // fast speed
+    }
+
+    public void flingDown() {
+        mDevice.swipe(mDevice.getDisplayWidth() / 2, 0, mDevice.getDisplayWidth() / 2,
+                mDevice.getDisplayHeight() / 2 + 50, 5); // fast speed
+        SystemClock.sleep(SHORT_TIMEOUT);
+    }
+
+    // Helper method to go back to home screen
+    public void goBackHome() throws RemoteException {
+        int count = 0;
+        mClock = null;
+        while (mClock == null && count++ < 5) {
+            mDevice.sleep();
+            SystemClock.sleep(LONG_TIMEOUT);
+            mDevice.wakeUp();
+            SystemClock.sleep(SHORT_TIMEOUT + SHORT_TIMEOUT);
+            mClock = mDevice.findObject(CLOCK_SELECTOR); // Ensure device is really on Home screen
+        }
+    }
+
+    // Helper method to verify if there are any Demo cards.
+
+    // TODO: Allow user to pass in how many cards are expected to find cause some tests may require
+    // more than one card.
+    public void hasDemoCards() throws Exception {
+        // Device should be pre-loaded with demo cards.
+        // Start the intent to go to home screen
+        mCard = mDevice.findObject(CARD_SELECTOR);
+        mTitle = mDevice.findObject(TITLE_SELECTOR);
+        mClock = mDevice.findObject(CLOCK_SELECTOR);
+
+        if (mClock.waitForExists(NEW_CARD_TIMEOUT_MS)) {
+            mClock.swipeUp(CARD_SWIPE_STEPS);
+        }
+
+        // First card from the pre-loaded demo cards could be either in peek view
+        // or in full view(e.g Dory) or no peek view(Sturgeon). Ensure to check for demo cards
+        // existence in
+        // both cases.
+        Assert.assertTrue("no cards available for testing",
+                (mCard.waitForExists(NEW_CARD_TIMEOUT_MS)
+                        || mTitle.waitForExists(NEW_CARD_TIMEOUT_MS)));
+    }
+
+    public void launchActivity(String appPackage, String activityToLaunch) {
+        mIntent.setAction("android.intent.action.MAIN");
+        mIntent.setComponent(new ComponentName(appPackage, activityToLaunch));
+        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        mContext.startActivity(mIntent);
+    }
+
+}