TV YouTube functional tests

Add functional test cases for TV YouTube app

Bug: 30299617
Change-Id: Ia711d5ac595ee7cebc5954118560d75aec2a20ce
diff --git a/tests/functional/tv/YouTubeTests/Android.mk b/tests/functional/tv/YouTubeTests/Android.mk
new file mode 100644
index 0000000..ecf13ce
--- /dev/null
+++ b/tests/functional/tv/YouTubeTests/Android.mk
@@ -0,0 +1,27 @@
+# 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.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+# ------------------------------------------------
+# build a test apk for YouTube functional testing
+
+LOCAL_PACKAGE_NAME := YouTubeFuncTests
+LOCAL_MODULE_TAGS := tests
+LOCAL_STATIC_JAVA_LIBRARIES := ub-uiautomator leanback-app-helpers android-support-test
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+include $(BUILD_PACKAGE)
+
diff --git a/tests/functional/tv/YouTubeTests/AndroidManifest.xml b/tests/functional/tv/YouTubeTests/AndroidManifest.xml
new file mode 100644
index 0000000..1c3a2ed
--- /dev/null
+++ b/tests/functional/tv/YouTubeTests/AndroidManifest.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.test.functional.tv.youtube">
+
+    <uses-sdk android:minSdkVersion="21"
+          android:targetSdkVersion="24"/>
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation
+            android:name="android.support.test.runner.AndroidJUnitRunner"
+            android:targetPackage="android.test.functional.tv.youtube"
+            android:label="TV YouTube Functional Tests" />
+
+</manifest>
+
+
diff --git a/tests/functional/tv/YouTubeTests/src/android/test/functional/tv/youtube/YouTubeTests.java b/tests/functional/tv/YouTubeTests/src/android/test/functional/tv/youtube/YouTubeTests.java
new file mode 100644
index 0000000..196e4d5
--- /dev/null
+++ b/tests/functional/tv/YouTubeTests/src/android/test/functional/tv/youtube/YouTubeTests.java
@@ -0,0 +1,146 @@
+/*
+ * 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 android.test.functional.tv.youtube;
+
+import android.app.Instrumentation;
+import android.content.Context;
+import android.os.Bundle;
+import android.os.SystemClock;
+import android.platform.test.helpers.tv.YouTubeHelperImpl;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.launcherhelper.ILeanbackLauncherStrategy;
+import android.support.test.launcherhelper.LauncherStrategyFactory;
+import android.support.test.launcherhelper.LeanbackLauncherStrategy;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.test.uiautomator.UiDevice;
+import android.util.Log;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Functional verification tests for YouTube on TV.
+ *
+ * adb shell am instrument -w -r \
+ *    -e class android.test.functional.tv.youtube.YouTubeTests \
+ *    android.test.functional.tv.youtube/android.support.test.runner.AndroidJUnitRunner
+ */
+@RunWith(AndroidJUnit4.class)
+public class YouTubeTests {
+
+    private static final String TAG = YouTubeTests.class.getSimpleName();
+    private static final String DEFAULT_SEARCH_QUERY = "never gonna give you up";
+    private static final long DEFAULT_SEARCH_PLAY_DURATION_MS = 30 * 1000;  // 30 seconds
+
+    private UiDevice mDevice;
+    private Instrumentation mInstrumentation;
+    private Context mContext;
+    private Bundle mArguments;
+
+    private LeanbackLauncherStrategy mLauncherStrategy;
+    private YouTubeHelperImpl mYouTubeHelper;
+
+
+    public YouTubeTests() {
+        initialize(InstrumentationRegistry.getInstrumentation());
+    }
+
+    private void initialize(Instrumentation instrumentation) {
+        // Initialize instances of testing support library
+        mInstrumentation = instrumentation;
+        mContext = getInstrumentation().getContext();
+        mDevice = UiDevice.getInstance(getInstrumentation());
+        mArguments = InstrumentationRegistry.getArguments();
+
+        // Initialize instances of leanback and app helpers
+        ILeanbackLauncherStrategy launcherStrategy = LauncherStrategyFactory.getInstance(
+                mDevice).getLeanbackLauncherStrategy();
+        if (launcherStrategy instanceof LeanbackLauncherStrategy) {
+            mLauncherStrategy = (LeanbackLauncherStrategy) launcherStrategy;
+        }
+        mYouTubeHelper = new YouTubeHelperImpl(getInstrumentation());
+    }
+
+    protected Instrumentation getInstrumentation() {
+        return mInstrumentation;
+    }
+
+    @Before
+    public void setUp() {
+        mLauncherStrategy.open();
+    }
+
+    @After
+    public void tearDown() {
+        mYouTubeHelper.exit();
+    }
+
+
+    /**
+     * Objective: Able to play the first video on Home section.
+     */
+    @Test
+    public void testPlayVideoAtHome() {
+        mYouTubeHelper.open();
+        mYouTubeHelper.openHome();
+
+        Log.i(TAG, "found a video: " + mYouTubeHelper.getFocusedVideoTitleText());
+        long durationMs = mYouTubeHelper.getFocusedVideoDuration();
+        if (durationMs > DEFAULT_SEARCH_PLAY_DURATION_MS) {
+            durationMs = DEFAULT_SEARCH_PLAY_DURATION_MS;
+        }
+        Assert.assertTrue(mYouTubeHelper.playFocusedVideo(durationMs));
+    }
+
+    /**
+     * Objective: Able to search for videos and play.
+     */
+    @Test
+    public void testSearchVideoAndPlay() {
+        // Search for a video
+        mYouTubeHelper.open();
+        mYouTubeHelper.search(DEFAULT_SEARCH_QUERY);
+
+        long durationMs = mYouTubeHelper.getFocusedVideoDuration();
+        if (durationMs > DEFAULT_SEARCH_PLAY_DURATION_MS) {
+            durationMs = DEFAULT_SEARCH_PLAY_DURATION_MS;
+        }
+
+        // Select the first video in the search results and play
+        mYouTubeHelper.openFirstSearchResult();
+
+        // Play the video for a given period of time
+        SystemClock.sleep(durationMs);
+        Assert.assertTrue(mYouTubeHelper.isInVideoPlayback());
+    }
+
+    /**
+     * Objective: Able to launch YouTube video in the Notification row
+     */
+    @Test
+    public void testLaunchVideoInNotificationRow() {
+        Assert.assertTrue(mLauncherStrategy.launchNotification(mYouTubeHelper.getLauncherName()));
+
+        // Play the video for a given period of time
+        SystemClock.sleep(5000);
+        Assert.assertTrue(mYouTubeHelper.isInVideoPlayback());
+    }
+}
+