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 
17 package com.android.launcher3.tapl;
18 
19 import android.graphics.Point;
20 import android.graphics.Rect;
21 import android.os.SystemClock;
22 import android.view.MotionEvent;
23 
24 import androidx.annotation.NonNull;
25 import androidx.test.uiautomator.UiObject2;
26 
27 public class Folder {
28 
29     protected static final String FOLDER_CONTENT_RES_ID = "folder_content";
30 
31     private final UiObject2 mContainer;
32     private final LauncherInstrumentation mLauncher;
33 
Folder(LauncherInstrumentation launcher)34     Folder(LauncherInstrumentation launcher) {
35         this.mLauncher = launcher;
36         this.mContainer = launcher.waitForLauncherObject(FOLDER_CONTENT_RES_ID);
37     }
38 
39     /**
40      * Find an app icon with given name or raise assertion error.
41      */
42     @NonNull
getAppIcon(String appName)43     public AppIcon getAppIcon(String appName) {
44         try (LauncherInstrumentation.Closable ignored = mLauncher.addContextLayer(
45                 "Want to get app icon in folder")) {
46             return new AppIcon(mLauncher,
47                     mLauncher.waitForObjectInContainer(
48                             mContainer,
49                             AppIcon.getAppIconSelector(appName, mLauncher)));
50         }
51     }
52 
touchOutsideFolder()53     private void touchOutsideFolder() {
54         Rect containerBounds = mLauncher.getVisibleBounds(this.mContainer);
55         final long downTime = SystemClock.uptimeMillis();
56         Point containerLeftTopCorner = new Point(containerBounds.left - 1, containerBounds.top - 1);
57         mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_DOWN,
58                 containerLeftTopCorner, LauncherInstrumentation.GestureScope.INSIDE);
59         mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_UP,
60                 containerLeftTopCorner, LauncherInstrumentation.GestureScope.INSIDE);
61     }
62 
63     /**
64      * CLose opened folder if possible. It throws assertion error if the folder is already closed.
65      */
close()66     public Workspace close() {
67         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
68              LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
69                      "Want to close opened folder")) {
70             mLauncher.waitForLauncherObject(FOLDER_CONTENT_RES_ID);
71             touchOutsideFolder();
72             mLauncher.waitUntilLauncherObjectGone(FOLDER_CONTENT_RES_ID);
73             return mLauncher.getWorkspace();
74         }
75     }
76 }
77