Added Two tests(Playstore Download and Power key test) and fix one
test(SysUIMultiWindowTests)

Bug: 29415751
Bug: 29398222
Change-Id: I5d8a60543a7ddbfb5e797cbca8c24ce13df1892c
diff --git a/tests/androidbvt/AndroidManifest.xml b/tests/androidbvt/AndroidManifest.xml
index c38bc88..ef7d102 100644
--- a/tests/androidbvt/AndroidManifest.xml
+++ b/tests/androidbvt/AndroidManifest.xml
@@ -49,7 +49,13 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-
+        <receiver android:name="com.android.androidbvt.PackageAddedBroadcastReceiver" >
+            <intent-filter android:priority="100" >
+                <action android:name="android.intent.action.PACKAGE_INSTALL" />
+                <action android:name="android.intent.action.PACKAGE_ADDED" />
+                <data android:scheme="package" />
+            </intent-filter>
+        </receiver>
     </application>
     <instrumentation
             android:name="android.support.test.runner.AndroidJUnitRunner"
diff --git a/tests/androidbvt/src/com/android/androidbvt/PackageAddedBroadcastReceiver.java b/tests/androidbvt/src/com/android/androidbvt/PackageAddedBroadcastReceiver.java
new file mode 100644
index 0000000..c351675
--- /dev/null
+++ b/tests/androidbvt/src/com/android/androidbvt/PackageAddedBroadcastReceiver.java
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+
+package com.android.androidbvt;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+import android.widget.Toast;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * BroadcastReceiver that listens for package-added events.
+ */
+public class PackageAddedBroadcastReceiver extends BroadcastReceiver {
+
+    private static final String TEST_TAG = "AndroidBVT";
+    public static AtomicBoolean isInstalled = new AtomicBoolean(false);
+
+    @Override
+    public void onReceive(Context context, Intent arg1) {
+        Log.v(TEST_TAG, "there is a broadcast for app installation");
+        isInstalled.compareAndSet(false, true);
+    }
+
+    // Tries 10 times/10 secs for install to be completed
+    public boolean isInstallCompleted()
+            throws InterruptedException {
+        int counter = 10;
+        while (--counter > 0) {
+            if (isInstalled.get()) {
+                return true;
+            }
+            Thread.sleep(1000);
+        }
+        return false;
+    }
+}
\ No newline at end of file
diff --git a/tests/androidbvt/src/com/android/androidbvt/PlayStoreDownloadTests.java b/tests/androidbvt/src/com/android/androidbvt/PlayStoreDownloadTests.java
new file mode 100644
index 0000000..5d3ae40
--- /dev/null
+++ b/tests/androidbvt/src/com/android/androidbvt/PlayStoreDownloadTests.java
@@ -0,0 +1,99 @@
+/*
+ * 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.
+ */
+
+package com.android.androidbvt;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+import android.database.Cursor;
+import android.os.RemoteException;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.By;
+import android.support.test.uiautomator.Direction;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.UiObject2;
+import android.support.test.uiautomator.Until;
+import android.test.suitebuilder.annotation.LargeTest;
+import android.util.Log;
+import android.widget.Toast;
+import android.platform.test.helpers.PlayStoreHelperImpl;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.List;
+
+/*
+ * Basic test for installing apk from playstore
+ */
+public class PlayStoreDownloadTests extends TestCase {
+    private static final String TEST_TAG = "AndroidBVT";
+    //Test apk uploaded in PlayStore for testing purpose only
+    private static final String TEST_APK_NAME = "w35location1";
+    private static final String TEST_PKG_NAME = "com.test.w35location1";
+    private static final String PLAYSTORE_PKG = "com.android.vending";
+    private AndroidBvtHelper mABvtHelper = null;
+    private UiDevice mDevice;
+    private Context mContext = null;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+        mDevice.freezeRotation();
+        mContext = InstrumentationRegistry.getTargetContext();
+        mABvtHelper = AndroidBvtHelper.getInstance(mDevice, mContext,
+                InstrumentationRegistry.getInstrumentation().getUiAutomation());
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        mDevice.unfreezeRotation();
+        mDevice.pressMenu();
+        mDevice.waitForIdle();
+        super.tearDown();
+    }
+
+    @LargeTest
+    public void testPlayStoreDownload() throws Exception {
+        installFromPlayStore(TEST_APK_NAME);
+        PackageAddedBroadcastReceiver pBroadcastReceiver = new PackageAddedBroadcastReceiver();
+        assertTrue("The apk has not been installed from playstore", pBroadcastReceiver.isInstallCompleted());
+        Thread.sleep(mABvtHelper.LONG_TIMEOUT);
+        uninstallFromPlayStore(TEST_PKG_NAME);
+    }
+
+    public void installFromPlayStore(String appName) throws Exception {
+        PlayStoreHelperImpl mHelper = new PlayStoreHelperImpl(
+                InstrumentationRegistry.getInstrumentation());
+        mHelper.open();
+        mHelper.doSearch(appName);
+        mHelper.selectFirstResult();
+        mDevice.wait(Until.findObject(By.res(PLAYSTORE_PKG, "buy_button").text("INSTALL")),
+                mABvtHelper.LONG_TIMEOUT)
+                .clickAndWait(Until.newWindow(), 2 * mABvtHelper.LONG_TIMEOUT);
+    }
+
+    public void uninstallFromPlayStore(String pkgName) throws Exception {
+        String cmd = " pm uninstall " + pkgName;
+        mABvtHelper.executeShellCommand(cmd);
+    }
+}
diff --git a/tests/androidbvt/src/com/android/androidbvt/SysPowerTests.java b/tests/androidbvt/src/com/android/androidbvt/SysPowerTests.java
new file mode 100644
index 0000000..ce9032f
--- /dev/null
+++ b/tests/androidbvt/src/com/android/androidbvt/SysPowerTests.java
@@ -0,0 +1,79 @@
+/*
+ * 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.
+ */
+
+package com.android.androidbvt;
+
+import android.app.UiAutomation;
+import android.content.Context;
+import android.os.PowerManager;
+import android.os.RemoteException;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.uiautomator.By;
+import android.support.test.uiautomator.Direction;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.UiObject2;
+import android.support.test.uiautomator.Until;
+import android.test.suitebuilder.annotation.LargeTest;
+import android.view.KeyEvent;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+
+/**
+ * Basic tests for Power On/Off
+ */
+public class SysPowerTests extends TestCase {
+    private static final int SLEEP_TIMEOUT = 30000;
+    private UiAutomation mUiAutomation = null;
+    private UiDevice mDevice;
+    private Context mContext = null;
+    private PowerManager mPowerManager;
+    private AndroidBvtHelper mABvtHelper = null;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+        mDevice.setOrientationNatural();
+        mContext = InstrumentationRegistry.getTargetContext();
+        mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        mPowerManager = (PowerManager) mContext.getSystemService(mContext.POWER_SERVICE);
+        mABvtHelper = AndroidBvtHelper.getInstance(mDevice, mContext, mUiAutomation);
+        mDevice.pressMenu();
+        mDevice.pressHome();
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        mDevice.pressHome();
+        mDevice.unfreezeRotation();
+        super.tearDown();
+    }
+
+    public void testPowerOnOff() throws InterruptedException, RemoteException {
+        mDevice.pressKeyCode(KeyEvent.KEYCODE_POWER);
+        Thread.sleep(SLEEP_TIMEOUT);
+        assertFalse("Screen is still on", mPowerManager.isInteractive());
+        Thread.sleep(SLEEP_TIMEOUT);
+        assertFalse("Screen is still on", mPowerManager.isInteractive());
+        mDevice.pressKeyCode(KeyEvent.KEYCODE_POWER);
+        mDevice.pressMenu();
+        mDevice.pressHome();
+        UiObject2 hotseat = mDevice.findObject(By.res(mDevice.getLauncherPackageName(), "hotseat"));
+        assertNotNull("Not on home screen", hotseat);
+    }
+}
diff --git a/tests/androidbvt/src/com/android/androidbvt/SysUIMultiWindowTests.java b/tests/androidbvt/src/com/android/androidbvt/SysUIMultiWindowTests.java
index 1ac6d75..e9eb0ba 100644
--- a/tests/androidbvt/src/com/android/androidbvt/SysUIMultiWindowTests.java
+++ b/tests/androidbvt/src/com/android/androidbvt/SysUIMultiWindowTests.java
@@ -84,11 +84,11 @@
         // Convert calculator to multiwindow mode
         mUiAutomation.executeShellCommand(
                 String.format("am stack movetask %d %d true", taskId, SPLITSCREEN));
-        Thread.sleep(mABvtHelper.SHORT_TIMEOUT * 2);
+        Thread.sleep(mABvtHelper.LONG_TIMEOUT * 2);
         // Launch Settings
         launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(SETTINGS_PACKAGE);
         mContext.startActivity(launchIntent);
-        Thread.sleep(mABvtHelper.SHORT_TIMEOUT * 2);
+        Thread.sleep(mABvtHelper.LONG_TIMEOUT * 2);
         // Ensure settings is active window
         List<AccessibilityWindowInfo> windows = mUiAutomation.getWindows();
         AccessibilityWindowInfo window = windows.get(windows.size() - 1);