1 package com.android.launcher3.ui;
2 
3 import android.util.Log;
4 import android.view.Surface;
5 
6 import com.android.launcher3.Launcher;
7 import com.android.launcher3.tapl.TestHelpers;
8 import com.android.launcher3.util.rule.FailureWatcher;
9 
10 import org.junit.rules.TestRule;
11 import org.junit.runner.Description;
12 import org.junit.runners.model.Statement;
13 
14 import java.lang.annotation.ElementType;
15 import java.lang.annotation.Retention;
16 import java.lang.annotation.RetentionPolicy;
17 import java.lang.annotation.Target;
18 import java.util.Objects;
19 import java.util.concurrent.TimeUnit;
20 
21 public class PortraitLandscapeRunner<LAUNCHER_TYPE extends Launcher> implements TestRule {
22     private static final String TAG = "PortraitLandscapeRunner";
23     private AbstractLauncherUiTest<LAUNCHER_TYPE> mTest;
24 
25     // Annotation for tests that need to be run in portrait and landscape modes.
26     @Retention(RetentionPolicy.RUNTIME)
27     @Target(ElementType.METHOD)
28     public @interface PortraitLandscape {
29     }
30 
PortraitLandscapeRunner(AbstractLauncherUiTest<LAUNCHER_TYPE> test)31     public PortraitLandscapeRunner(AbstractLauncherUiTest<LAUNCHER_TYPE> test) {
32         mTest = test;
33     }
34 
35     @Override
apply(Statement base, Description description)36     public Statement apply(Statement base, Description description) {
37         if (!TestHelpers.isInLauncherProcess()
38                 || description.getAnnotation(PortraitLandscape.class) == null) {
39             return base;
40         }
41 
42         return new Statement() {
43             @Override
44             public void evaluate() throws Throwable {
45                 try {
46                     try {
47                         // we expect to begin unlocked...
48                         AbstractLauncherUiTest.verifyKeyguardInvisible();
49 
50                         mTest.mDevice.pressHome();
51                         mTest.waitForLauncherCondition("Launcher activity wasn't created",
52                                 Objects::nonNull,
53                                 TimeUnit.SECONDS.toMillis(20));
54 
55                         mTest.executeOnLauncher(launcher ->
56                                 launcher.getRotationHelper().forceAllowRotationForTesting(
57                                         true));
58 
59                     } catch (Throwable e) {
60                         FailureWatcher.onError(mTest.mLauncher, description);
61                         throw e;
62                     }
63 
64                     evaluateInPortrait();
65                     evaluateInLandscape();
66                 } catch (Throwable e) {
67                     Log.e(TAG, "Error", e);
68                     throw e;
69                 } finally {
70                     mTest.mDevice.setOrientationNatural();
71                     mTest.executeOnLauncher(launcher ->
72                     {
73                         if (launcher != null) {
74                             launcher.getRotationHelper().forceAllowRotationForTesting(false);
75                         }
76                     });
77                     mTest.mLauncher.setExpectedRotation(Surface.ROTATION_0);
78 
79                     // and end unlocked...
80                     AbstractLauncherUiTest.verifyKeyguardInvisible();
81                 }
82             }
83 
84             private void evaluateInPortrait() throws Throwable {
85                 mTest.mDevice.setOrientationNatural();
86                 mTest.mLauncher.setExpectedRotation(Surface.ROTATION_0);
87                 AbstractLauncherUiTest.checkDetectedLeaks(mTest.mLauncher, true);
88                 base.evaluate();
89                 mTest.getDevice().pressHome();
90             }
91 
92             private void evaluateInLandscape() throws Throwable {
93                 mTest.mDevice.setOrientationLeft();
94                 mTest.mLauncher.setExpectedRotation(Surface.ROTATION_90);
95                 AbstractLauncherUiTest.checkDetectedLeaks(mTest.mLauncher, true);
96                 base.evaluate();
97                 mTest.getDevice().pressHome();
98             }
99         };
100     }
101 }
102