mirror of
				https://github.com/ARM-software/workload-automation.git
				synced 2025-10-31 15:12:25 +00:00 
			
		
		
		
	Youtube: Codetidy to conform with the other workloads
This commit is contained in:
		| @@ -17,7 +17,6 @@ package com.arm.wlauto.uiauto.youtube; | ||||
|  | ||||
| import android.os.Bundle; | ||||
| import android.os.SystemClock; | ||||
| import android.util.Log; | ||||
|  | ||||
| // Import the uiautomator libraries | ||||
| import com.android.uiautomator.core.UiObject; | ||||
| @@ -32,54 +31,68 @@ import static com.arm.wlauto.uiauto.BaseUiAutomation.FindByCriteria.BY_DESC; | ||||
|  | ||||
| public class UiAutomation extends UxPerfUiAutomation { | ||||
|  | ||||
|     public static final String CLASS_BUTTON = "android.widget.Button"; | ||||
|     public static final String CLASS_TEXT_VIEW = "android.widget.TextView"; | ||||
|     public Bundle parameters; | ||||
|     public String packageName; | ||||
|     public String packageID; | ||||
|  | ||||
|     public static final int WAIT_TIMEOUT_1SEC = 1000; | ||||
|     public static final int VIDEO_SLEEP_SECONDS = 3; | ||||
|     public static final int LIST_SWIPE_COUNT = 5; | ||||
|     public static final String SOURCE_MY_VIDEOS = "my_videos"; | ||||
|     public static final String SOURCE_SEARCH = "search"; | ||||
|     public static final String SOURCE_TRENDING = "trending"; | ||||
|  | ||||
|     protected ActionLogger logger; | ||||
|     protected Bundle parameters; | ||||
|     protected String packageName; | ||||
|     protected String packageID; | ||||
|     protected String videoSource; | ||||
|     protected String searchTerm; | ||||
|     public static final int WAIT_TIMEOUT_1SEC = 1000; | ||||
|     public static final int VIDEO_SLEEP_SECONDS = 3; | ||||
|     public static final int LIST_SWIPE_COUNT = 5; | ||||
|  | ||||
|     public void runUiAutomation() throws Exception { | ||||
|         parameters = getParams(); | ||||
|         packageName = parameters.getString("package"); | ||||
|         packageID = packageName + ":id/"; | ||||
|         videoSource = parameters.getString("video_source"); | ||||
|         searchTerm = parameters.getString("search_term"); | ||||
|  | ||||
|         String videoSource = parameters.getString("video_source"); | ||||
|         String searchTerm = parameters.getString("search_term"); | ||||
|         if (searchTerm != null) { | ||||
|             searchTerm = searchTerm.replaceAll("0space0", " "); | ||||
|             searchTerm = searchTerm.replace("0space0", " "); | ||||
|         } | ||||
|  | ||||
|         setScreenOrientation(ScreenOrientation.NATURAL); | ||||
|  | ||||
|         clearFirstRunDialogues(); | ||||
|         disableAutoplay(); | ||||
|         testPlayVideo(videoSource, searchTerm); | ||||
|         dismissAdvert(); | ||||
|         checkPlayerError(); | ||||
|         pausePlayVideo(); | ||||
|         checkVideoInfo(); | ||||
|         scrollRelated(); | ||||
|  | ||||
|         unsetScreenOrientation(); | ||||
|     } | ||||
|  | ||||
|     public void clearFirstRunDialogues() throws Exception { | ||||
|         UiObject laterButton = new UiObject(new UiSelector().textContains("Later").className(CLASS_TEXT_VIEW)); | ||||
|         UiObject laterButton = | ||||
|             new UiObject(new UiSelector().textContains("Later") | ||||
|                                          .className("android.widget.TextView")); | ||||
|         if (laterButton.waitForExists(WAIT_TIMEOUT_1SEC)) { | ||||
|            laterButton.click(); | ||||
|         } | ||||
|         UiObject cancelButton = new UiObject(new UiSelector().textContains("Cancel").className(CLASS_BUTTON)); | ||||
|  | ||||
|         UiObject cancelButton = | ||||
|             new UiObject(new UiSelector().textContains("Cancel") | ||||
|                                          .className("android.widget.Button")); | ||||
|         if (cancelButton.waitForExists(WAIT_TIMEOUT_1SEC)) { | ||||
|             cancelButton.click(); | ||||
|         } | ||||
|         UiObject skipButton = new UiObject(new UiSelector().textContains("Skip").className(CLASS_TEXT_VIEW)); | ||||
|  | ||||
|         UiObject skipButton = | ||||
|             new UiObject(new UiSelector().textContains("Skip") | ||||
|                                          .className("android.widget.TextView")); | ||||
|         if (skipButton.waitForExists(WAIT_TIMEOUT_1SEC)) { | ||||
|             skipButton.click(); | ||||
|         } | ||||
|         UiObject gotItButton = new UiObject(new UiSelector().textContains("Got it").className(CLASS_BUTTON)); | ||||
|  | ||||
|         UiObject gotItButton = | ||||
|             new UiObject(new UiSelector().textContains("Got it") | ||||
|                                          .className("android.widget.Button")); | ||||
|         if (gotItButton.waitForExists(WAIT_TIMEOUT_1SEC)) { | ||||
|             gotItButton.click(); | ||||
|         } | ||||
| @@ -91,7 +104,8 @@ public class UiAutomation extends UxPerfUiAutomation { | ||||
|         clickUiObject(BY_TEXT, "General", true); | ||||
|  | ||||
|         // Don't fail fatally if autoplay toggle cannot be found | ||||
|         UiObject autoplayToggle = new UiObject(new UiSelector().textContains("Autoplay")); | ||||
|         UiObject autoplayToggle = | ||||
|             new UiObject(new UiSelector().textContains("Autoplay")); | ||||
|         if (autoplayToggle.waitForExists(WAIT_TIMEOUT_1SEC)) { | ||||
|             autoplayToggle.click(); | ||||
|         } | ||||
| @@ -99,64 +113,73 @@ public class UiAutomation extends UxPerfUiAutomation { | ||||
|  | ||||
|         // Tablet devices use a split with General in the left pane and Autoplay in the right so no | ||||
|         // need to click back twice | ||||
|         UiObject generalButton = new UiObject(new UiSelector().textContains("General").className(CLASS_TEXT_VIEW)); | ||||
|         UiObject generalButton = | ||||
|             new UiObject(new UiSelector().textContains("General") | ||||
|                                          .className("android.widget.TextView")); | ||||
|         if (generalButton.exists()) { | ||||
|             getUiDevice().pressBack(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void testPlayVideo(String source, String searchTerm) throws Exception { | ||||
|         String testTag = "play"; | ||||
|         ActionLogger logger = new ActionLogger(testTag + "_" + source, parameters); | ||||
|  | ||||
|         if (SOURCE_SEARCH.equalsIgnoreCase(source)) { | ||||
|             clickUiObject(BY_DESC, "Search"); | ||||
|             UiObject textField = getUiObjectByResourceId(packageID + "search_edit_text"); | ||||
|             textField.setText(searchTerm); | ||||
|             getUiDevice().pressEnter(); | ||||
|  | ||||
|             startMeasurements("play_search"); | ||||
|             // If a video exists whose title contains the exact search term, then play it | ||||
|             // Otherwise click the first video in the search results | ||||
|             UiObject thumbnail = new UiObject(new UiSelector().resourceId(packageID + "thumbnail")); | ||||
|             UiObject matchedVideo = thumbnail.getFromParent(new UiSelector().textContains(searchTerm)); | ||||
|             UiObject thumbnail = | ||||
|                 new UiObject(new UiSelector().resourceId(packageID + "thumbnail")); | ||||
|             UiObject matchedVideo = | ||||
|                 thumbnail.getFromParent(new UiSelector().textContains(searchTerm)); | ||||
|  | ||||
|             logger.start(); | ||||
|             if (matchedVideo.exists()) { | ||||
|                 matchedVideo.clickAndWaitForNewWindow(); | ||||
|             } else { | ||||
|                 thumbnail.clickAndWaitForNewWindow(); | ||||
|             } | ||||
|             endMeasurements(); | ||||
|             logger.stop(); | ||||
|  | ||||
|         } else if (SOURCE_MY_VIDEOS.equalsIgnoreCase(source)) { | ||||
|             clickUiObject(BY_DESC, "Account"); | ||||
|             clickUiObject(BY_TEXT, "My Videos", true); | ||||
|  | ||||
|             startMeasurements("play_myvideos"); | ||||
|             logger.start(); | ||||
|             clickUiObject(BY_ID, packageID + "thumbnail", true); | ||||
|             endMeasurements(); | ||||
|             logger.stop(); | ||||
|  | ||||
|         } else if (SOURCE_TRENDING.equalsIgnoreCase(source)) { | ||||
|             clickUiObject(BY_DESC, "Trending"); | ||||
|  | ||||
|             startMeasurements("play_trending"); | ||||
|             logger.start(); | ||||
|             clickUiObject(BY_ID, packageID + "thumbnail", true); | ||||
|             endMeasurements(); | ||||
|             logger.stop(); | ||||
|  | ||||
|         } else { // homepage videos | ||||
|             UiScrollable list = new UiScrollable(new UiSelector().resourceId(packageID + "results")); | ||||
|             UiScrollable list = | ||||
|                 new UiScrollable(new UiSelector().resourceId(packageID + "results")); | ||||
|             if (list.exists()) { | ||||
|                 list.scrollForward(); | ||||
|             } | ||||
|             startMeasurements("play_home"); | ||||
|             clickUiObject(BY_ID, packageID + "thumbnail", true); | ||||
|             endMeasurements(); | ||||
|         } | ||||
|  | ||||
|         dismissAdvert(); | ||||
|         checkPlayerError(); | ||||
|         pausePlayVideo(); | ||||
|         checkVideoInfo(); | ||||
|         scrollRelated(); | ||||
|             logger.start(); | ||||
|             clickUiObject(BY_ID, packageID + "thumbnail", true); | ||||
|             logger.stop(); | ||||
|  | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void dismissAdvert() throws Exception { | ||||
|         UiObject advert = new UiObject(new UiSelector().textContains("Visit advertiser")); | ||||
|         UiObject advert = | ||||
|             new UiObject(new UiSelector().textContains("Visit advertiser")); | ||||
|         if (advert.exists()) { | ||||
|             UiObject skip = new UiObject(new UiSelector().textContains("Skip ad")); | ||||
|             UiObject skip = | ||||
|                 new UiObject(new UiSelector().textContains("Skip ad")); | ||||
|             if (skip.waitForExists(WAIT_TIMEOUT_1SEC*5)) { | ||||
|                 skip.click(); | ||||
|                 sleep(VIDEO_SLEEP_SECONDS); | ||||
| @@ -165,8 +188,10 @@ public class UiAutomation extends UxPerfUiAutomation { | ||||
|     } | ||||
|  | ||||
|     public void checkPlayerError() throws Exception { | ||||
|         UiObject playerError = new UiObject(new UiSelector().resourceId(packageID + "player_error_view")); | ||||
|         UiObject tapToRetry = new UiObject(new UiSelector().textContains("Tap to retry")); | ||||
|         UiObject playerError = | ||||
|             new UiObject(new UiSelector().resourceId(packageID + "player_error_view")); | ||||
|         UiObject tapToRetry = | ||||
|             new UiObject(new UiSelector().textContains("Tap to retry")); | ||||
|         if (playerError.waitForExists(WAIT_TIMEOUT_1SEC) || tapToRetry.waitForExists(WAIT_TIMEOUT_1SEC)) { | ||||
|             throw new RuntimeException("Video player encountered an error and cannot continue."); | ||||
|         } | ||||
| @@ -182,7 +207,8 @@ public class UiAutomation extends UxPerfUiAutomation { | ||||
|     } | ||||
|  | ||||
|     public void checkVideoInfo() throws Exception { | ||||
|         UiObject expandButton = new UiObject(new UiSelector().resourceId(packageID + "expand_button")); | ||||
|         UiObject expandButton = | ||||
|             new UiObject(new UiSelector().resourceId(packageID + "expand_button")); | ||||
|         if (!expandButton.waitForExists(WAIT_TIMEOUT_1SEC)) { | ||||
|             return; | ||||
|         } | ||||
| @@ -193,28 +219,24 @@ public class UiAutomation extends UxPerfUiAutomation { | ||||
|     } | ||||
|  | ||||
|     public void scrollRelated() throws Exception { | ||||
|         // ListView of related videos and (maybe) comments | ||||
|         UiScrollable list = new UiScrollable(new UiSelector().resourceId(packageID + "watch_list")); | ||||
|         if (list.isScrollable()) { | ||||
|             startMeasurements("scroll_down"); | ||||
|             list.flingToEnd(LIST_SWIPE_COUNT); | ||||
|             endMeasurements(); | ||||
|         String testTag = "scroll"; | ||||
|  | ||||
|             startMeasurements("scroll_up"); | ||||
|         // ListView of related videos and (maybe) comments | ||||
|         UiScrollable list = | ||||
|             new UiScrollable(new UiSelector().resourceId(packageID + "watch_list")); | ||||
|         if (list.isScrollable()) { | ||||
|             ActionLogger logger = new ActionLogger(testTag + "_down", parameters); | ||||
|             logger.start(); | ||||
|             list.flingToEnd(LIST_SWIPE_COUNT); | ||||
|             logger.stop(); | ||||
|  | ||||
|             logger = new ActionLogger(testTag + "_up", parameters); | ||||
|             logger.start(); | ||||
|             list.flingToBeginning(LIST_SWIPE_COUNT); | ||||
|             endMeasurements(); | ||||
|             logger.stop(); | ||||
|         } | ||||
|         // After flinging, give the window enough time to settle down before | ||||
|         // the next step, or else UiAutomator fails to find views in time | ||||
|         sleep(VIDEO_SLEEP_SECONDS); | ||||
|     } | ||||
|  | ||||
|     protected void startMeasurements(String testTag) throws Exception { | ||||
|         logger = new ActionLogger(testTag, parameters); | ||||
|         logger.start(); | ||||
|     } | ||||
|  | ||||
|     protected void endMeasurements() throws Exception { | ||||
|         logger.stop(); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user