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 android.server.am;
18 
19 import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
20 import static android.server.am.ComponentNameUtils.getLogTag;
21 import static android.server.am.Components.LOG_CONFIGURATION_ACTIVITY;
22 import static android.server.am.StateLogger.log;
23 import static android.server.am.StateLogger.logAlways;
24 import static android.view.Surface.ROTATION_0;
25 import static android.view.Surface.ROTATION_180;
26 
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assume.assumeTrue;
30 
31 import android.content.ComponentName;
32 import android.os.SystemClock;
33 
34 import org.junit.Test;
35 
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 
39 /**
40  * Build/Install/Run:
41  *     atest CtsActivityManagerDeviceTestCases:ActivityAndWindowManagerOverrideConfigTests
42  */
43 public class ActivityAndWindowManagerOverrideConfigTests extends ActivityManagerTestBase {
44 
45     private static class ConfigurationChangeObserver {
46         private static final Pattern CONFIGURATION_CHANGED_PATTERN =
47             Pattern.compile("(.+)Configuration changed: (\\d+),(\\d+)");
48 
ConfigurationChangeObserver()49         private ConfigurationChangeObserver() {
50         }
51 
findConfigurationChange( ComponentName activityName, LogSeparator logSeparator)52         private boolean findConfigurationChange(
53                 ComponentName activityName, LogSeparator logSeparator) {
54             for (int retry = 1; retry <= 5; retry++) {
55                 final String[] lines =
56                         getDeviceLogsForComponents(logSeparator, getLogTag(activityName));
57                 log("Looking at logcat");
58                 for (int i = lines.length - 1; i >= 0; i--) {
59                     final String line = lines[i].trim();
60                     log(line);
61                     Matcher matcher = CONFIGURATION_CHANGED_PATTERN.matcher(line);
62                     if (matcher.matches()) {
63                         return true;
64                     }
65                 }
66                 logAlways("***Waiting configuration change of " + getLogTag(activityName)
67                         + " retry=" + retry);
68                 SystemClock.sleep(500);
69             }
70             return false;
71         }
72     }
73 
74     @Test
testReceiveOverrideConfigFromRelayout()75     public void testReceiveOverrideConfigFromRelayout() throws Exception {
76         assumeTrue("Device doesn't support freeform. Skipping test.", supportsFreeform());
77 
78         launchActivity(LOG_CONFIGURATION_ACTIVITY, WINDOWING_MODE_FREEFORM);
79 
80         try (final RotationSession rotationSession = new RotationSession()) {
81             rotationSession.set(ROTATION_0);
82             LogSeparator logSeparator = separateLogs();
83             resizeActivityTask(LOG_CONFIGURATION_ACTIVITY, 0, 0, 100, 100);
84             ConfigurationChangeObserver c = new ConfigurationChangeObserver();
85             final boolean reportedSizeAfterResize = c.findConfigurationChange(
86                     LOG_CONFIGURATION_ACTIVITY, logSeparator);
87             assertTrue("Expected to observe configuration change when resizing",
88                     reportedSizeAfterResize);
89 
90             logSeparator = separateLogs();
91             rotationSession.set(ROTATION_180);
92             final boolean reportedSizeAfterRotation = c.findConfigurationChange(
93                     LOG_CONFIGURATION_ACTIVITY, logSeparator);
94             assertFalse("Not expected to observe configuration change after flip rotation",
95                     reportedSizeAfterRotation);
96         }
97     }
98 }
99 
100