1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.platform.helpers;
18 
19 import android.app.Instrumentation;
20 import android.os.SystemClock;
21 import android.platform.helpers.exceptions.UnknownUiException;
22 import android.support.test.uiautomator.UiObject2;
23 
24 import java.util.List;
25 
26 public class TestMediaAppHelperImpl extends AbstractAutoStandardAppHelper
27         implements IAutoTestMediaAppHelper {
28     // Wait Time
29     private static final int SHORT_RESPONSE_WAIT_MS = 1000;
30 
TestMediaAppHelperImpl(Instrumentation instr)31     public TestMediaAppHelperImpl(Instrumentation instr) {
32         super(instr);
33     }
34 
35     /**
36      * {@inheritDoc}
37      */
38     @Override
loadMediaInLocalMediaTestApp()39     public void loadMediaInLocalMediaTestApp() {
40         // Open Account type
41         clickAndWait(
42                 AutoConfigConstants.TEST_MEDIA_ACCOUNT_TYPE, "Account Type");
43         // Select Paid Account type
44         clickAndWait(
45                 AutoConfigConstants.TEST_MEDIA_ACCOUNT_TYPE_PAID, "Account Type: Paid");
46         // open Root node type
47         clickAndWait(
48                 AutoConfigConstants.TEST_MEDIA_ROOT_NODE_TYPE, "Root node type");
49         // select Browsable content
50         clickAndWait(AutoConfigConstants.TEST_MEDIA_ROOT_NODE_TYPE_BROWSABLE,
51                 "Browsable Content");
52         // close settings
53         clickAndWait(AutoConfigConstants.TEST_MEDIA_APP_CLOSE_SETTING, "Close Settings");
54         selectSongInTestMediaApp();
55     }
56 
selectSongInTestMediaApp()57     private void selectSongInTestMediaApp() {
58         List<UiObject2> songList = findUiObjects(getResourceFromConfig(
59                 AutoConfigConstants.MEDIA_CENTER,
60                 AutoConfigConstants.MEDIA_APP,
61                 AutoConfigConstants.MEDIA_SONGS_LIST));
62         if (songList.size() == 0) {
63             throw new UnknownUiException("Unable to find Songs in the Test Media App.");
64         }
65         clickAndWaitForIdleScreen(songList.get(1));
66         SystemClock.sleep(SHORT_RESPONSE_WAIT_MS);
67         // minimize songs
68         UiObject2 goBackToSongsList = findUiObject(getResourceFromConfig(
69                 AutoConfigConstants.MEDIA_CENTER,
70                 AutoConfigConstants.MEDIA_APP,
71                 AutoConfigConstants.MEDIA_APP_NAVIGATION_ICON));
72         clickAndWaitForIdleScreen(goBackToSongsList);
73         SystemClock.sleep(SHORT_RESPONSE_WAIT_MS);
74     }
75 
clickAndWait(String autoConfigConstants, String fieldName)76     private void clickAndWait(String autoConfigConstants, String fieldName) {
77         UiObject2 mediaTestAppField = findUiObject(getResourceFromConfig(
78                 AutoConfigConstants.MEDIA_CENTER,
79                 AutoConfigConstants.TEST_MEDIA_APP,
80                 autoConfigConstants));
81         if (mediaTestAppField == null) {
82             throw new UnknownUiException("Unable to find Test Media App field: " + fieldName);
83         }
84         clickAndWaitForIdleScreen(mediaTestAppField);
85         SystemClock.sleep(SHORT_RESPONSE_WAIT_MS);
86     }
87 }
88