summaryrefslogtreecommitdiff
path: root/tests/tests/systemui/src/android/systemui/cts/MediaOutputDialogTest.java
blob: b74678bc3aff2e80ca7f0fb89acf21ec6db4d26c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) 2022 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.systemui.cts;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.server.wm.WindowManagerStateHelper;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.compatibility.common.util.SystemUtil;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests related MediaOutputDialog:
 *
 * atest MediaDialogTest
 */
@RunWith(AndroidJUnit4.class)
public class MediaOutputDialogTest {

    private static final int TIMEOUT = 5000;
    private static final String ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG =
            "com.android.systemui.action.LAUNCH_MEDIA_OUTPUT_DIALOG";
    private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui";
    public static final String EXTRA_PACKAGE_NAME = "package_name";
    public static final String TEST_PACKAGE_NAME = "com.android.package.test";
    private static final BySelector MEDIA_DIALOG_SELECTOR = By.res(SYSTEMUI_PACKAGE_NAME,
            "media_output_dialog");
    private WindowManagerStateHelper mWmState = new WindowManagerStateHelper();

    private Context mContext;
    private UiDevice mDevice;
    private String mLauncherPackage;
    private boolean mHasTouchScreen;
    private ActivityManager mActivityManager;
    private static final int DURATION_SCREEN_TOGGLE = 2000;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getTargetContext();
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        final PackageManager packageManager = mContext.getPackageManager();

        mHasTouchScreen = packageManager.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
                || packageManager.hasSystemFeature(PackageManager.FEATURE_FAKETOUCH);

        Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
        launcherIntent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo resolveInfo = packageManager.resolveActivity(launcherIntent,
                PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY));
        assumeFalse("Skipping test: can't get resolve info", resolveInfo == null);
        assumeFalse("Skipping test: not supported on automotive yet",
                packageManager.hasSystemFeature(
                        PackageManager.FEATURE_AUTOMOTIVE));
        mLauncherPackage = resolveInfo.activityInfo.packageName;
        mActivityManager = mContext.getSystemService(ActivityManager.class);
    }

    @Test
    public void mediaOutputDialog_correctDialog() throws Exception {
        prepareDevice();
        assumeTrue(mHasTouchScreen);
        try {
            if (mActivityManager.isLowRamDevice()) {
                SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(),
                        "appops set " + SYSTEMUI_PACKAGE_NAME + " SYSTEM_ALERT_WINDOW allow");
            }

            launchMediaOutputDialog();

            assertThat(mDevice.wait(Until.hasObject(MEDIA_DIALOG_SELECTOR), TIMEOUT)).isTrue();
        } finally {
            if (mActivityManager.isLowRamDevice()) {
                SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(),
                        "appops set " + SYSTEMUI_PACKAGE_NAME + " SYSTEM_ALERT_WINDOW ignore");
            }
        }
    }

    private void prepareDevice() throws Exception {
        mDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP");
        mDevice.executeShellCommand("wm dismiss-keyguard");
        // Since the screen on/off intent is ordered, they will not be sent right now.
        mWmState.waitForKeyguardGone();
    }

    private void launchMediaOutputDialog() {
        mDevice.pressHome();
        mDevice.wait(Until.hasObject(By.pkg(mLauncherPackage).depth(0)), TIMEOUT);

        Intent intent = new Intent();
        intent.setPackage(SYSTEMUI_PACKAGE_NAME)
                .setAction(ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG)
                .putExtra(EXTRA_PACKAGE_NAME, TEST_PACKAGE_NAME);

        mContext.sendBroadcast(intent);
    }

}