1 /*
2  * Copyright (C) 2016 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 com.android.androidbvt;
18 
19 import android.app.UiAutomation;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.RemoteException;
23 import android.support.test.InstrumentationRegistry;
24 import android.support.test.uiautomator.UiDevice;
25 import android.test.suitebuilder.annotation.LargeTest;
26 import android.view.accessibility.AccessibilityWindowInfo;
27 
28 import junit.framework.TestCase;
29 
30 import java.util.List;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import android.util.Log;
34 public class SysUIMultiWindowTests extends TestCase {
35     private static final String CALCULATOR_PACKAGE = "com.google.android.calculator";
36     private static final String CALCULATOR_ACTIVITY = "com.android.calculator2.Calculator";
37     private static final String SETTINGS_PACKAGE = "com.android.settings";
38     private static final int FULLSCREEN = 1;
39     private static final int SPLITSCREEN = 3;
40     private UiAutomation mUiAutomation = null;
41     private UiDevice mDevice;
42     private Context mContext = null;
43     private AndroidBvtHelper mABvtHelper = null;
44 
45     @Override
setUp()46     public void setUp() throws Exception {
47         super.setUp();
48         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
49         mDevice.setOrientationNatural();
50         mContext = InstrumentationRegistry.getTargetContext();
51         mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
52         mABvtHelper = AndroidBvtHelper.getInstance(mDevice, mContext, mUiAutomation);
53     }
54 
55     @Override
tearDown()56     public void tearDown() throws Exception {
57         mDevice.unfreezeRotation();
58         super.tearDown();
59     }
60 
61     /**
62      * Following test ensures any app can be docked from full-screen to split-screen, another can be
63      * launched to multiwindow mode and finally, initial app can be brought back to full-screen
64      */
65     @LargeTest
testLaunchInMultiwindow()66     public void testLaunchInMultiwindow() throws InterruptedException, RemoteException {
67         // Launch calculator in full screen
68         Intent launchIntent = mContext.getPackageManager()
69                 .getLaunchIntentForPackage(CALCULATOR_PACKAGE);
70         mContext.startActivity(launchIntent);
71         Thread.sleep(mABvtHelper.SHORT_TIMEOUT);
72         int taskId = -1;
73         // Find task id for launched Calculator package
74         List<String> cmdOut = mABvtHelper.executeShellCommand("am stack list");
75         for (String line : cmdOut) {
76             Pattern pattern = Pattern.compile(String.format(".*taskId=([0-9]+): %s/%s.*",CALCULATOR_PACKAGE, CALCULATOR_ACTIVITY));
77             Matcher matcher = pattern.matcher(line);
78             if (matcher.find()) {
79                 taskId = Integer.parseInt(matcher.group(1));
80                 break;
81             }
82         }
83         assertTrue("Taskid hasn't been found", taskId != -1);
84         // Convert calculator to multiwindow mode
85         mUiAutomation.executeShellCommand(
86                 String.format("am stack movetask %d %d true", taskId, SPLITSCREEN));
87         Thread.sleep(mABvtHelper.SHORT_TIMEOUT * 2);
88         // Launch Settings
89         launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(SETTINGS_PACKAGE);
90         mContext.startActivity(launchIntent);
91         Thread.sleep(mABvtHelper.SHORT_TIMEOUT * 2);
92         // Ensure settings is active window
93         List<AccessibilityWindowInfo> windows = mUiAutomation.getWindows();
94         AccessibilityWindowInfo window = windows.get(windows.size() - 1);
95         assertTrue("Settings isn't active window",
96                 window.getRoot().getPackageName().equals(SETTINGS_PACKAGE));
97         // Calculate midpoint for Calculator window and click
98         mDevice.click(mDevice.getDisplayHeight() / 4, mDevice.getDisplayWidth() / 2);
99         Thread.sleep(mABvtHelper.SHORT_TIMEOUT);
100         windows = mUiAutomation.getWindows();
101         window = windows.get(windows.size() - 2);
102         assertTrue("Calcualtor isn't active window",
103                 window.getRoot().getPackageName().equals(CALCULATOR_PACKAGE));
104         // Make Calculator FullWindow again and ensure Settings package isn't found on window
105         mUiAutomation.executeShellCommand(
106                 String.format("am stack movetask %d %d true", taskId, FULLSCREEN));
107         Thread.sleep(mABvtHelper.SHORT_TIMEOUT * 2);
108         windows = mUiAutomation.getWindows();
109         for(int i = 0; i < windows.size() && windows.get(i).getRoot() != null; ++i) {
110             assertFalse("Settings have been found",
111                     windows.get(i).getRoot().getPackageName().equals(SETTINGS_PACKAGE));
112         }
113         mDevice.pressHome();
114     }
115 }
116