1 /*
2  * Copyright (C) 2011 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 package android.media.cts;
17 
18 import static org.junit.Assert.assertNotNull;
19 
20 import android.app.Instrumentation;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.os.ConditionVariable;
24 
25 import androidx.annotation.CallSuper;
26 import androidx.test.core.app.ActivityScenario;
27 import androidx.test.platform.app.InstrumentationRegistry;
28 
29 import com.android.compatibility.common.util.MediaUtils;
30 
31 import java.util.logging.Logger;
32 
33 /**
34  * Base class for media tests that need UI thread access.
35  */
36 public class MediaTestBase {
37     private static final Logger LOG = Logger.getLogger(MediaTestBase.class.getName());
38 
39     protected static final int SLEEP_TIME = 1000;
40     protected static final int LONG_SLEEP_TIME = 6000;
41     protected static final int STREAM_RETRIES = 20;
42     protected static boolean sUseScaleToFitMode = false;
43 
44     protected Context mContext;
45 
46     protected ActivityScenario<MediaStubActivity> mActivityScenario;
47     protected MediaStubActivity mActivity;
48 
49     @CallSuper
setUp()50     protected void setUp() throws Throwable {
51         mActivityScenario = ActivityScenario.launch(MediaStubActivity.class);
52         ConditionVariable activityReferenceObtained = new ConditionVariable();
53         mActivityScenario.onActivity(activity -> {
54             mActivity = activity;
55             activityReferenceObtained.open();
56         });
57         activityReferenceObtained.block(/* timeoutMs= */ 10000);
58         assertNotNull("Failed to acquire activity reference.", mActivity);
59 
60         mContext = getInstrumentation().getTargetContext();
61         getInstrumentation().waitForIdleSync();
62     }
63 
64     @CallSuper
tearDown()65     protected void tearDown() {
66         if (mActivityScenario != null) {
67             mActivityScenario.close();
68         }
69         mActivity = null;
70     }
71 
getInstrumentation()72     protected Instrumentation getInstrumentation() {
73         return InstrumentationRegistry.getInstrumentation();
74     }
75 
getActivity()76     protected MediaStubActivity getActivity() {
77         return mActivity;
78     }
79 
isTv()80     public boolean isTv() {
81         PackageManager pm = getInstrumentation().getTargetContext().getPackageManager();
82         return pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
83                 && pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
84     }
85 
checkTv()86     public boolean checkTv() {
87         return MediaUtils.check(isTv(), "not a TV");
88     }
89 
90 }
91