1 package com.android.launcher3.ui;
2 
3 import android.util.Log;
4 import android.view.Surface;
5 
6 import com.android.launcher3.tapl.TestHelpers;
7 import com.android.launcher3.testing.TestProtocol;
8 
9 import org.junit.rules.TestRule;
10 import org.junit.runner.Description;
11 import org.junit.runners.model.Statement;
12 
13 class PortraitLandscapeRunner implements TestRule {
14     private static final String TAG = "PortraitLandscapeRunner";
15     private AbstractLauncherUiTest mTest;
16 
PortraitLandscapeRunner(AbstractLauncherUiTest test)17     public PortraitLandscapeRunner(AbstractLauncherUiTest test) {
18         mTest = test;
19     }
20 
21     @Override
apply(Statement base, Description description)22     public Statement apply(Statement base, Description description) {
23         if (!TestHelpers.isInLauncherProcess() ||
24                 description.getAnnotation(AbstractLauncherUiTest.PortraitLandscape.class) == null) {
25             return base;
26         }
27 
28         return new Statement() {
29             @Override
30             public void evaluate() throws Throwable {
31                 try {
32                     mTest.mDevice.pressHome();
33                     mTest.waitForLauncherCondition("Launcher activity wasn't created",
34                             launcher -> launcher != null);
35 
36                     mTest.executeOnLauncher(launcher ->
37                             launcher.getRotationHelper().forceAllowRotationForTesting(
38                                     true));
39 
40                     evaluateInPortrait();
41                     evaluateInLandscape();
42                 } catch (Throwable e) {
43                     Log.e(TAG, "Error", e);
44                     throw e;
45                 } finally {
46                     mTest.mDevice.setOrientationNatural();
47                     mTest.executeOnLauncher(launcher ->
48                     {
49                         if (launcher != null) {
50                             launcher.getRotationHelper().forceAllowRotationForTesting(false);
51                         }
52                     });
53                     mTest.mLauncher.setExpectedRotation(Surface.ROTATION_0);
54                 }
55             }
56 
57             private void evaluateInPortrait() throws Throwable {
58                 mTest.mDevice.setOrientationNatural();
59                 mTest.mLauncher.setExpectedRotation(Surface.ROTATION_0);
60                 AbstractLauncherUiTest.checkDetectedLeaks(mTest.mLauncher);
61                 base.evaluate();
62                 mTest.getDevice().pressHome();
63             }
64 
65             private void evaluateInLandscape() throws Throwable {
66                 mTest.mDevice.setOrientationLeft();
67                 mTest.mLauncher.setExpectedRotation(Surface.ROTATION_90);
68                 AbstractLauncherUiTest.checkDetectedLeaks(mTest.mLauncher);
69                 base.evaluate();
70                 mTest.getDevice().pressHome();
71             }
72         };
73     }
74 }
75