1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.tapl;
17 
18 import static com.android.launcher3.tapl.Launchable.DEFAULT_DRAG_STEPS;
19 
20 import static org.junit.Assert.assertTrue;
21 
22 import android.graphics.Point;
23 import android.graphics.Rect;
24 import android.os.SystemClock;
25 import android.view.MotionEvent;
26 
27 import androidx.test.uiautomator.UiObject2;
28 
29 /** The resize frame that is shown for a widget on the workspace. */
30 public class WidgetResizeFrame {
31 
32     private final LauncherInstrumentation mLauncher;
33 
WidgetResizeFrame(LauncherInstrumentation launcher)34     WidgetResizeFrame(LauncherInstrumentation launcher) {
35         mLauncher = launcher;
36         launcher.waitForLauncherObject("widget_resize_frame");
37     }
38 
39     /** Dismisses the resize frame. */
dismiss()40     public void dismiss() {
41         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
42              LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
43                      "want to dismiss widget resize frame")) {
44             // Dismiss the resize frame by pressing the home button.
45             mLauncher.getDevice().pressHome();
46         }
47     }
48 
49     /** Resizes the widget to double its height, and returns the resize frame. */
resize()50     public WidgetResizeFrame resize() {
51         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
52              LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
53                      "want to resize the widget frame.")) {
54             UiObject2 widget = mLauncher.waitForLauncherObject("widget_resize_frame");
55             UiObject2 bottomResizeHandle =
56                     mLauncher.waitForLauncherObject("widget_resize_bottom_handle");
57             Rect originalWidgetSize = widget.getVisibleBounds();
58             Point targetStart = bottomResizeHandle.getVisibleCenter();
59             Point targetDest = bottomResizeHandle.getVisibleCenter();
60             targetDest.offset(0,
61                     originalWidgetSize.height() + mLauncher.getCellLayoutBoarderHeight());
62 
63             final long downTime = SystemClock.uptimeMillis();
64             mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_DOWN, targetStart,
65                     LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
66             try {
67                 mLauncher.movePointer(targetStart, targetDest, DEFAULT_DRAG_STEPS,
68                         true, downTime, downTime, true,
69                         LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
70             } finally {
71                 mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_UP, targetDest,
72                         LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
73             }
74 
75             try (LauncherInstrumentation.Closable c2 = mLauncher.addContextLayer(
76                          "want to return resized widget resize frame")) {
77                 float newHeight = mLauncher.waitForLauncherObject(
78                         "widget_resize_frame").getVisibleBounds().height();
79                 assertTrue("Widget not resized.", newHeight >= originalWidgetSize.height() * 2);
80                 return this;
81             }
82         }
83     }
84 }
85