Snap for 4448085 from f1744af64eb2460238b68009ce24aa9d63f43c61 to oc-m3-release

Change-Id: Ib9804f5d6f640495ce6e3ae69745cb93c40dd3fb
diff --git a/libraries/aupt-lib/src/android/support/test/aupt/AuptTestRunner.java b/libraries/aupt-lib/src/android/support/test/aupt/AuptTestRunner.java
index 2603abb..1cf66cd 100644
--- a/libraries/aupt-lib/src/android/support/test/aupt/AuptTestRunner.java
+++ b/libraries/aupt-lib/src/android/support/test/aupt/AuptTestRunner.java
@@ -204,7 +204,11 @@
                     injectInstrumentation(test);
                 }
 
-                super.runTest(result);
+                try {
+                    super.runTest(result);
+                } finally {
+                    mDataCollector.stop();
+                }
             }
         };
 
@@ -228,11 +232,6 @@
         super.onCreate(params);
     }
 
-    @Override
-    public void onDestroy() {
-        mDataCollector.stop();
-    }
-
     /* Option-parsing helpers */
 
     private long parseLongParam(String key, long alternative) throws NumberFormatException {
diff --git a/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java b/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java
index 99dddd0..6f9ad53 100644
--- a/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java
+++ b/libraries/launcher-helper/src/android/support/test/launcherhelper/TvLauncherStrategy.java
@@ -18,7 +18,9 @@
 
 import android.app.Instrumentation;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.graphics.Point;
 import android.os.RemoteException;
 import android.os.SystemClock;
@@ -43,9 +45,11 @@
     private static final String LOG_TAG = TvLauncherStrategy.class.getSimpleName();
     private static final String PACKAGE_LAUNCHER = "com.google.android.tvlauncher";
     private static final String PACKAGE_SETTINGS = "com.android.tv.settings";
-
     private static final String CHANNEL_TITLE_WATCH_NEXT = "Watch Next";
 
+    // Build version
+    private static final int BUILD_INT_BANDGAP = 1010100000;
+
     // Wait time
     private static final int UI_APP_LAUNCH_WAIT_TIME_MS = 10000;
     private static final int UI_WAIT_TIME_MS = 5000;
@@ -215,6 +219,31 @@
     }
 
     /**
+     * Get the launcher's version code.
+     * @return the version code. -1 if the launcher package is not found.
+     */
+    public int getVersionCode() {
+        String pkg = getSupportedLauncherPackage();
+        if (null == pkg || pkg.isEmpty()) {
+            throw new RuntimeException("Can't find version of empty package");
+        }
+        if (mInstrumentation == null) {
+            Log.w(LOG_TAG, "Instrumentation is null. setInstrumentation should be called "
+                    + "to get the version code");
+            return -1;
+        }
+        PackageManager pm = mInstrumentation.getContext().getPackageManager();
+        PackageInfo pInfo = null;
+        try {
+            pInfo = pm.getPackageInfo(pkg, 0);
+            return pInfo.versionCode;
+        } catch (NameNotFoundException e) {
+            Log.w(LOG_TAG, String.format("package name is not found: %s", pkg));
+            return -1;
+        }
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
@@ -278,6 +307,10 @@
      * Returns a {@link BySelector} describing a given app in Apps View
      */
     public BySelector getAppInAppsViewSelector(String appName) {
+        if (getVersionCode() > BUILD_INT_BANDGAP) {
+            // bandgap or higher
+            return By.res(getSupportedLauncherPackage(), "banner_image").desc(appName);
+        }
         return By.res(getSupportedLauncherPackage(), "app_title").text(appName);
     }
 
@@ -553,11 +586,11 @@
      * down to the next row like a zigzag pattern until the app is found.
      */
     protected UiObject2 selectAppInAllApps(BySelector appSelector, String packageName) {
-        openAllApps(true);
+        Assert.assertTrue(mDevice.hasObject(getAllAppsSelector()));
 
         // Assume that the focus always starts at the top left of the Apps view.
         final int maxScrollAttempts = 20;
-        final int margin = 10;
+        final int margin = 30;
         int attempts = 0;
         UiObject2 focused = null;
         UiObject2 expected = null;
@@ -568,8 +601,8 @@
             if (expected == null) {
                 mDPadUtil.pressDPadDown();
                 continue;
-            } else if (focused.getVisibleCenter().equals(expected.getVisibleCenter())) {
-                // The app icon is on the screen, and selected.
+            } else if (focused.hasObject(appSelector)) {
+                // The app icon is selected.
                 Log.i(LOG_TAG, String.format("The app %s is selected", packageName));
                 break;
             } else {
@@ -579,6 +612,7 @@
                 Point targetPosition = expected.getVisibleCenter();
                 int dx = targetPosition.x - currentPosition.x;
                 int dy = targetPosition.y - currentPosition.y;
+                Log.d(LOG_TAG, String.format("selectAppInAllApps: [dx,dx][%d,%d]", dx, dy));
                 if (dy > margin) {
                     mDPadUtil.pressDPadDown();
                     continue;
@@ -608,8 +642,8 @@
      * Search from left to right, and down to the next row, from right to left, and
      * down to the next row like a zigzag pattern until it founds a given app.
      */
-    public UiObject2 selectAppInAllAppsZigZag(BySelector appSelector, String packageName) {
-        openAllApps(true);
+    protected UiObject2 selectAppInAllAppsZigZag(BySelector appSelector, String packageName) {
+        Assert.assertTrue(mDevice.hasObject(getAllAppsSelector()));
         Direction direction = Direction.RIGHT;
         UiObject2 app = select(appSelector, direction, UI_TRANSITION_WAIT_TIME_MS);
         while (app == null && move(Direction.DOWN)) {
@@ -623,13 +657,31 @@
     }
 
     /**
+     * Select the given app in All Apps using the versioned BySelector for the app
+     */
+    public UiObject2 selectAppInAllApps(String appName, String packageName) {
+        UiObject2 app = null;
+        int versionCode = getVersionCode();
+        if (versionCode > BUILD_INT_BANDGAP) {
+            // bandgap or higher
+            Log.i(LOG_TAG,
+                    String.format("selectAppInAllApps: app banner has app name [versionCode]%d",
+                            versionCode));
+            app = selectAppInAllApps(getAppInAppsViewSelector(appName), packageName);
+        } else {
+            app = selectAppInAllAppsZigZag(getAppInAppsViewSelector(appName), packageName);
+        }
+        return app;
+    }
+
+    /**
      * Launch the given app in the Apps view.
      */
     public boolean launchAppInAppsView(String appName, String packageName) {
         Log.d(LOG_TAG, String.format("launching in apps view [appName]%s [packageName]%s",
                 appName, packageName));
         openAllApps(true);
-        UiObject2 app = selectAppInAllAppsZigZag(getAppInAppsViewSelector(appName), packageName);
+        UiObject2 app = selectAppInAllApps(appName, packageName);
         if (app == null) {
             throw new RuntimeException(
                 "Failed to navigate to the app icon in the Apps view: " + packageName);
@@ -667,9 +719,7 @@
             app = selectBidirect(By.copy(favAppSelector).focused(true), Direction.RIGHT);
         } else {
             openAllApps(true);
-            // Find app in Apps View in zigzag mode with app selector for Apps View
-            // because the app title no longer appears until focused.
-            app = selectAppInAllAppsZigZag(getAppInAppsViewSelector(appName), packageName);
+            app = selectAppInAllApps(appName, packageName);
         }
         if (app == null) {
             throw new RuntimeException(
@@ -739,7 +789,7 @@
                 isGame = (appInfo.metaData != null && appInfo.metaData.getBoolean("isGame", false))
                         || ((appInfo.flags & ApplicationInfo.FLAG_IS_GAME) != 0);
                 Log.i(LOG_TAG, String.format("The package %s isGame: %b", packageName, isGame));
-            } catch (PackageManager.NameNotFoundException e) {
+            } catch (NameNotFoundException e) {
                 Log.w(LOG_TAG,
                         String.format("No package found: %s, error:%s", packageName, e.toString()));
                 return false;
diff --git a/tests/functional/notificationtests/src/com/android/notification/functional/NotificationHelper.java b/tests/functional/notificationtests/src/com/android/notification/functional/NotificationHelper.java
index dc9aeb4..12d3468 100644
--- a/tests/functional/notificationtests/src/com/android/notification/functional/NotificationHelper.java
+++ b/tests/functional/notificationtests/src/com/android/notification/functional/NotificationHelper.java
@@ -16,6 +16,9 @@
 
 package com.android.notification.functional;
 
+import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
+import static android.app.NotificationManager.IMPORTANCE_LOW;
+
 import android.app.Instrumentation;
 import android.app.IntentService;
 import android.app.KeyguardManager;
@@ -27,7 +30,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Typeface;
-import android.net.Uri;
 import android.os.Handler;
 import android.os.RemoteException;
 import android.provider.Settings;
@@ -65,6 +67,10 @@
     public static final String FIRST_ACTION = "FIRST ACTION";
     public static final String SECOND_ACTION = "SECOND ACTION";
     public static final String CONTENT_TITLE = "THIS IS A NOTIFICATION";
+    private static final String BUZZY_CHANNEL_ID = "com.android.notification.functional.buzzy";
+    private static final String QUIET_CHANNEL_ID = "com.android.notification.functional.quiet";
+    private NotificationChannel mBuzzyChannel;
+    private NotificationChannel mQuietChannel;
 
     private UiDevice mDevice;
     private Instrumentation mInst;
@@ -76,6 +82,9 @@
         mInst = inst;
         mNotificationManager = nm;
         mContext = inst.getContext();
+        // create the channels we need
+        mBuzzyChannel = getChannel(true);
+        mQuietChannel = getChannel(false);
     }
 
     public void sleepAndWakeUpDevice() throws RemoteException, InterruptedException {
@@ -126,7 +135,7 @@
 
     public boolean removeScreenLock(int pin, String mode) throws Exception {
         navigateToScreenLock();
-        if (new UiObject(new UiSelector().text("Confirm your PIN")).exists()) {
+        if (new UiObject(new UiSelector().text("Re-enter your PIN")).exists()) {
             UiObject pinField = new UiObject(new UiSelector().className(EditText.class.getName()));
             pinField.setText(String.format("%04d", pin));
             mDevice.pressEnter();
@@ -186,7 +195,7 @@
                 .setContentText(subtitle)
                 .setContentIntent(pendingIntent)
                 .setVisibility(visibility)
-                .setPriority(Notification.PRIORITY_HIGH)
+                .setChannelId(buzz ? BUZZY_CHANNEL_ID : QUIET_CHANNEL_ID)
                 .addAction(new Notification.Action.Builder(R.drawable.stat_notify_email,
                         FIRST_ACTION, emptyIntent)
                         .build())
@@ -194,9 +203,6 @@
                         SECOND_ACTION, emptyIntent)
                         .build())
                 .setAutoCancel(false);
-        if (buzz) {
-            notification.setDefaults(Notification.DEFAULT_VIBRATE);
-        }
         mNotificationManager.notify(id, notification.build());
         Thread.sleep(LONG_TIMEOUT);
     }
@@ -361,6 +367,26 @@
         return mNotificationManager.getNotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID);
     }
 
+    public NotificationChannel getChannel(boolean buzz) {
+        String id = (buzz ? BUZZY_CHANNEL_ID : QUIET_CHANNEL_ID);
+        String name = (buzz ? "This channel is buzzy" : "This channel is quiet");
+        int importance = (buzz ? IMPORTANCE_DEFAULT : IMPORTANCE_LOW);
+
+        NotificationChannel channel = (buzz ? mBuzzyChannel : mQuietChannel);
+        if (channel == null) {
+            channel = mNotificationManager.getNotificationChannel(id);
+        }
+        if (channel == null){
+            channel = new NotificationChannel(id, name, importance);
+            if (buzz) {
+                channel.enableVibration(true);
+                channel.setSound(null, null);
+            }
+            mNotificationManager.createNotificationChannel(channel);
+        }
+        return channel;
+    }
+
     public static class ToastService extends IntentService {
         private static final String TAG = "ToastService";
         private static final String ACTION_TOAST = "toast";
diff --git a/tests/functional/notificationtests/src/com/android/notification/functional/NotificationInteractionTests.java b/tests/functional/notificationtests/src/com/android/notification/functional/NotificationInteractionTests.java
index 53b9c69..d5b4f58 100644
--- a/tests/functional/notificationtests/src/com/android/notification/functional/NotificationInteractionTests.java
+++ b/tests/functional/notificationtests/src/com/android/notification/functional/NotificationInteractionTests.java
@@ -237,7 +237,7 @@
                 new LogMaker(MetricsEvent.NOTIFICATION_ALERT)
                         .setType(MetricsEvent.TYPE_OPEN)
                         .addTaggedData(MetricsEvent.NOTIFICATION_ID, id)
-                        .setSubtype(1) // 1: BUZZ, nop BEEP, nop BLINK
+                        .setSubtype(MetricsEvent.ALERT_BUZZ) // no BEEP or BLINK
                         .setPackageName(mContext.getPackageName()));
         MetricsAsserts.assertHasLog("missing notification action 0 click log", mMetricsReader,
                 new LogMaker(MetricsEvent.NOTIFICATION_ITEM_ACTION)
diff --git a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchLeanbackJankTests.java b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchLeanbackJankTests.java
index c671730..d03f1b2 100644
--- a/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchLeanbackJankTests.java
+++ b/tests/jank/uibench/src/com/android/uibench/janktests/UiBenchLeanbackJankTests.java
@@ -25,7 +25,7 @@
 import android.text.TextUtils;
 import android.view.KeyEvent;
 
-import static com.android.uibench.janktests.UiBenchJankTestsHelper.EXPECTED_FRAMES;
+import static com.android.uibench.janktests.UiBenchJankTestsHelper.SHORT_EXPECTED_FRAMES;
 import static com.android.uibench.janktests.UiBenchJankTestsHelper.PACKAGE_NAME;
 
 import java.lang.annotation.ElementType;
@@ -125,7 +125,7 @@
     /**
      * Vertically scroll BrowseFragment in the fast lane
      */
-    @JankTest(expectedFrames = EXPECTED_FRAMES)
+    @JankTest(expectedFrames = SHORT_EXPECTED_FRAMES)
     @Option(activity = "leanback.BrowseActivity")
     @GfxMonitor(processName = PACKAGE_NAME)
     @GfxFrameStatsMonitor(processName = PACKAGE_NAME)
@@ -136,7 +136,7 @@
     /**
      * Vertically scroll BrowseFragment in the content (fast lane closed)
      */
-    @JankTest(expectedFrames = EXPECTED_FRAMES)
+    @JankTest(expectedFrames = SHORT_EXPECTED_FRAMES)
     @Option(activity = "leanback.BrowseActivity", postLaunch = "focusToBrowseContent")
     @GfxMonitor(processName = PACKAGE_NAME)
     @GfxFrameStatsMonitor(processName = PACKAGE_NAME)
@@ -148,7 +148,7 @@
      * Vertically scroll BrowseFragment in the fast lane
      * option: no bitmap upload
      */
-    @JankTest(expectedFrames = EXPECTED_FRAMES)
+    @JankTest(expectedFrames = SHORT_EXPECTED_FRAMES)
     @Option(activity = "leanback.BrowseActivity", extras = "noBitmapUpload")
     @GfxMonitor(processName = PACKAGE_NAME)
     @GfxFrameStatsMonitor(processName = PACKAGE_NAME)
@@ -160,7 +160,7 @@
      * Vertically scroll BrowseFragment in the content (fast lane closed)
      * option: no bitmap upload
      */
-    @JankTest(expectedFrames = EXPECTED_FRAMES)
+    @JankTest(expectedFrames = SHORT_EXPECTED_FRAMES)
     @Option(activity = "leanback.BrowseActivity", extras = "noBitmapUpload",
             postLaunch = "focusToBrowseContent")
     @GfxMonitor(processName = PACKAGE_NAME)