Added GoogleNow tests and fixed a few ABVT tests

Bug: 30898195
Bug: 30897930
Bug: 30913861
Bug: 30972062
Change-Id: I3c22a9a085145440bc5f2c475175ed6d1e0fe9d1
diff --git a/tests/androidbvt/Android.mk b/tests/androidbvt/Android.mk
index 6dd5df2..3b3271b 100644
--- a/tests/androidbvt/Android.mk
+++ b/tests/androidbvt/Android.mk
@@ -24,6 +24,7 @@
 LOCAL_STATIC_JAVA_LIBRARIES := android-support-test \
     first-party-app-helpers \
     launcher-helper-lib \
+    settings-app-helper \
     platform-test-annotations \
     ub-uiautomator
 
diff --git a/tests/androidbvt/src/com/android/androidbvt/PlayStoreDownloadTests.java b/tests/androidbvt/src/com/android/androidbvt/PlayStoreDownloadTests.java
index 5d3ae40..1861cc9 100644
--- a/tests/androidbvt/src/com/android/androidbvt/PlayStoreDownloadTests.java
+++ b/tests/androidbvt/src/com/android/androidbvt/PlayStoreDownloadTests.java
@@ -90,6 +90,12 @@
         mDevice.wait(Until.findObject(By.res(PLAYSTORE_PKG, "buy_button").text("INSTALL")),
                 mABvtHelper.LONG_TIMEOUT)
                 .clickAndWait(Until.newWindow(), 2 * mABvtHelper.LONG_TIMEOUT);
+        UiObject2 continueBtn = mDevice.wait(
+                Until.findObject(By.res(PLAYSTORE_PKG, "positive_button")),
+                mABvtHelper.LONG_TIMEOUT);
+        if (continueBtn != null) {
+            continueBtn.click();
+        }
     }
 
     public void uninstallFromPlayStore(String pkgName) throws Exception {
diff --git a/tests/androidbvt/src/com/android/androidbvt/SysUIGSATests.java b/tests/androidbvt/src/com/android/androidbvt/SysUIGSATests.java
index 80364e3..935e169 100644
--- a/tests/androidbvt/src/com/android/androidbvt/SysUIGSATests.java
+++ b/tests/androidbvt/src/com/android/androidbvt/SysUIGSATests.java
@@ -135,16 +135,12 @@
             mDevice.wait(Until.findObject(By.res(NEXUS_LAUNCHER_PKG, "g_icon")),
                     mABvtHelper.LONG_TIMEOUT).click();
             Thread.sleep(2000);
-            mDevice.wait(Until.findObject(By.res(QSB_PKG, "navigation_viewport")),
-                    mABvtHelper.LONG_TIMEOUT).click();
         } else {
             mDevice.wait(Until.findObject(By.res(QSB_PKG, "search_plate")),
                     mABvtHelper.LONG_TIMEOUT).click();
         }
         // Search for Paris and click on first suggested text
-        mDevice.wait(Until.findObject(By.res(QSB_PKG, "text_container")),
-                mABvtHelper.LONG_TIMEOUT).click();
-        mDevice.wait(Until.findObject(By.res(QSB_PKG, "search_box")),
+        mDevice.wait(Until.findObject(By.clazz("android.widget.EditText")),
                 mABvtHelper.LONG_TIMEOUT).setText("Paris");
         Thread.sleep(mABvtHelper.LONG_TIMEOUT);
         List<UiObject2> suggestedTexts = null;
@@ -165,7 +161,6 @@
         Thread.sleep(mABvtHelper.LONG_TIMEOUT);
         // Now long press home to load assist layer
         mDevice.pressKeyCode(KeyEvent.KEYCODE_ASSIST);
-
         // Ensure some cards are loaded
         // Note card's content isn't verified
         counter = 5;
diff --git a/tests/androidbvt/src/com/android/androidbvt/SysUILauncherTests.java b/tests/androidbvt/src/com/android/androidbvt/SysUILauncherTests.java
index 652588f..07e4af9 100644
--- a/tests/androidbvt/src/com/android/androidbvt/SysUILauncherTests.java
+++ b/tests/androidbvt/src/com/android/androidbvt/SysUILauncherTests.java
@@ -17,10 +17,15 @@
 package com.android.androidbvt;
 
 import android.app.WallpaperManager;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.Intent;
 import android.graphics.Point;
 import android.graphics.drawable.Drawable;
 import android.platform.test.annotations.HermeticTest;
+import android.platform.test.helpers.SettingsHelperImpl;
+import android.provider.Settings;
+import android.provider.Telephony.Threads;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.launcherhelper.ILauncherStrategy;
 import android.support.test.launcherhelper.LauncherStrategyFactory;
@@ -37,17 +42,19 @@
 @HermeticTest
 public class SysUILauncherTests extends TestCase {
     private static final int LONG_TIMEOUT = 5000;
-    private static final String APP_NAME = "Calendar";
+    private static final String APP_NAME = "Calculator";
     private static final String PKG_NAME = "com.google.android.deskclock";
     private static final String WIDGET_PREVIEW = "widget_preview";
     private static final String APP_WIDGET_VIEW = "android.appwidget.AppWidgetHostView";
     private static final String WIDGET_TEXT_VIEW = "android.widget.TextView";
     private static final String WALLPAPER_PKG = "com.google.android.apps.wallpaper";
     private static final String GOOGLE_SEARCH_PKG = "com.google.android.googlequicksearchbox";
+    private static final String GOOGLE_NOW_HEADER = "now_header_hamburger";
     private UiDevice mDevice = null;
     private Context mContext;
     private ILauncherStrategy mLauncherStrategy = null;
     private AndroidBvtHelper mABvtHelper = null;
+    private SettingsHelperImpl mHelper;
     private boolean mIsMr1Device = false;
 
     @Override
@@ -57,9 +64,10 @@
         mContext = InstrumentationRegistry.getTargetContext();
         mDevice.setOrientationNatural();
         mLauncherStrategy = LauncherStrategyFactory.getInstance(mDevice).getLauncherStrategy();
-        mABvtHelper = AndroidBvtHelper.getInstance(mDevice, mContext, 
+        mABvtHelper = AndroidBvtHelper.getInstance(mDevice, mContext,
                 InstrumentationRegistry.getInstrumentation().getUiAutomation());
         mIsMr1Device = mABvtHelper.isNexusExperienceDevice();
+        mHelper =  new SettingsHelperImpl(InstrumentationRegistry.getInstrumentation());
     }
 
     @Override
@@ -180,4 +188,75 @@
             homeScreen.click();
         }
     }
+
+    public void testGoogleNowShowInGel() throws Exception {
+        mDevice.pressHome();
+        swipeToLeftScreen();
+        // Enable Now cards if GEL is not enabled.
+        if (!isOnGoogleNowPage()) {
+            // enable Now Cards
+            enableNowCardsSetting();
+            // swipe left to Screen -1
+            swipeToLeftScreen();
+        }
+        // verify GoogleNow shows up in Screen -1
+        assertTrue("Not on Google now page!", isOnGoogleNowPage());
+    }
+
+    private void enableNowCardsSetting()  throws Exception {
+        mABvtHelper.launchIntent(Settings.ACTION_SETTINGS);
+        UiObject2 obj = launchSettingItems("Google");
+        obj.click();
+        obj = launchSettingItems("Search & Now");
+        obj.click();
+        obj = launchSettingItems("Now cards");
+        obj.click();
+        obj = (mDevice.wait(Until.findObject(By.res("android","list")), mABvtHelper.LONG_TIMEOUT)).getChildren().get(0);
+        if (obj.hasObject(By.text("OFF"))){
+            obj.click();
+            //click setup
+            UiObject2 setUp = mDevice.wait(Until.findObject(By.res(GOOGLE_SEARCH_PKG,"accept_button")),
+                    mABvtHelper.SHORT_TIMEOUT);
+            if (setUp != null) {
+                setUp.clickAndWait(Until.newWindow(), mABvtHelper.SHORT_TIMEOUT);
+                mDevice.wait(Until.findObject(By.res(GOOGLE_SEARCH_PKG, "text_container")),
+                        mABvtHelper.SHORT_TIMEOUT).swipe(Direction.UP, 1.0f);
+                mDevice.wait(Until.findObject(By.res(GOOGLE_SEARCH_PKG,"accept_button")),
+                        mABvtHelper.SHORT_TIMEOUT)
+                        .clickAndWait(Until.newWindow(), mABvtHelper.SHORT_TIMEOUT);
+            }
+        }
+        Thread.sleep(mABvtHelper.LONG_TIMEOUT);
+        mDevice.pressHome();
+        Thread.sleep( mABvtHelper.SHORT_TIMEOUT);
+    }
+
+    private UiObject2 launchSettingItems(String title) throws Exception {
+        int maxAttempt = 5;
+        UiObject2 item = null;
+        UiObject2 view = null;
+        while (maxAttempt-- > 0) {
+            item = mDevice.wait(Until.findObject(By.res("android:id/title").text(title)),
+                    mABvtHelper.LONG_TIMEOUT);
+            if (item == null) {
+                mHelper.scrollVert(false);
+            } else {
+                return item;
+            }
+        }
+        assertNotNull(String.format("Can't find %s", title), item);
+        return null;
+    }
+
+    private boolean isOnGoogleNowPage(){
+        return mDevice.wait(Until.findObject(By.res(GOOGLE_SEARCH_PKG, GOOGLE_NOW_HEADER)),
+                mABvtHelper.LONG_TIMEOUT) != null;
+    }
+
+    //swipe to left screen
+    private void swipeToLeftScreen() throws Exception{
+        mDevice.swipe(0, mDevice.getDisplayHeight() / 2, mDevice.getDisplayWidth() - 50,
+                mDevice.getDisplayHeight() / 2, 30);
+        Thread.sleep(mABvtHelper.SHORT_TIMEOUT);
+    }
 }