• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 import org.junit.rules.TestRule;
9 import org.junit.runner.Description;
10 import org.junit.runners.model.Statement;
11 
12 import java.lang.annotation.ElementType;
13 import java.lang.annotation.Retention;
14 import java.lang.annotation.RetentionPolicy;
15 import java.lang.annotation.Target;
16 
17 public class PortraitLandscapeRunner implements TestRule {
18     private static final String TAG = "PortraitLandscapeRunner";
19     private AbstractLauncherUiTest mTest;
20 
21     // Annotation for tests that need to be run in portrait and landscape modes.
22     @Retention(RetentionPolicy.RUNTIME)
23     @Target(ElementType.METHOD)
24     public @interface PortraitLandscape {
25     }
26 
PortraitLandscapeRunner(AbstractLauncherUiTest test)27     public PortraitLandscapeRunner(AbstractLauncherUiTest test) {
28         mTest = test;
29     }
30 
31     @Override
apply(Statement base, Description description)32     public Statement apply(Statement base, Description description) {
33         if (!TestHelpers.isInLauncherProcess() ||
34                 description.getAnnotation(PortraitLandscape.class) == null) {
35             return base;
36         }
37 
38         return new Statement() {
39             @Override
40             public void evaluate() throws Throwable {
41                 try {
42                     mTest.mDevice.pressHome();
43                     mTest.waitForLauncherCondition("Launcher activity wasn't created",
44                             launcher -> launcher != null);
45 
46                     mTest.executeOnLauncher(launcher ->
47                             launcher.getRotationHelper().forceAllowRotationForTesting(
48                                     true));
49 
50                     evaluateInPortrait();
51                     evaluateInLandscape();
52                 } catch (Throwable e) {
53                     Log.e(TAG, "Error", e);
54                     throw e;
55                 } finally {
56                     mTest.mDevice.setOrientationNatural();
57                     mTest.executeOnLauncher(launcher ->
58                     {
59                         if (launcher != null) {
60                             launcher.getRotationHelper().forceAllowRotationForTesting(false);
61                         }
62                     });
63                     mTest.mLauncher.setExpectedRotation(Surface.ROTATION_0);
64                 }
65             }
66 
67             private void evaluateInPortrait() throws Throwable {
68                 mTest.mDevice.setOrientationNatural();
69                 mTest.mLauncher.setExpectedRotation(Surface.ROTATION_0);
70                 AbstractLauncherUiTest.checkDetectedLeaks(mTest.mLauncher);
71                 base.evaluate();
72                 mTest.getDevice().pressHome();
73             }
74 
75             private void evaluateInLandscape() throws Throwable {
76                 mTest.mDevice.setOrientationLeft();
77                 mTest.mLauncher.setExpectedRotation(Surface.ROTATION_90);
78                 AbstractLauncherUiTest.checkDetectedLeaks(mTest.mLauncher);
79                 base.evaluate();
80                 mTest.getDevice().pressHome();
81             }
82         };
83     }
84 }
85