• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.launcher3.ui.widget;
17 
18 import android.support.test.filters.LargeTest;
19 import android.support.test.runner.AndroidJUnit4;
20 import android.support.test.uiautomator.By;
21 import android.support.test.uiautomator.UiObject2;
22 import android.view.View;
23 
24 import com.android.launcher3.ItemInfo;
25 import com.android.launcher3.LauncherAppWidgetInfo;
26 import com.android.launcher3.LauncherAppWidgetProviderInfo;
27 import com.android.launcher3.Workspace.ItemOperator;
28 import com.android.launcher3.ui.AbstractLauncherUiTest;
29 import com.android.launcher3.util.Condition;
30 import com.android.launcher3.util.Wait;
31 import com.android.launcher3.util.rule.LauncherActivityRule;
32 import com.android.launcher3.util.rule.ShellCommandRule;
33 import com.android.launcher3.widget.WidgetCell;
34 
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 import static org.junit.Assert.assertTrue;
40 
41 /**
42  * Test to add widget from widget tray
43  */
44 @LargeTest
45 @RunWith(AndroidJUnit4.class)
46 public class AddWidgetTest extends AbstractLauncherUiTest {
47 
48     @Rule public LauncherActivityRule mActivityMonitor = new LauncherActivityRule();
49     @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grandWidgetBind();
50 
51     @Test
testDragIcon_portrait()52     public void testDragIcon_portrait() throws Throwable {
53         lockRotation(true);
54         performTest();
55     }
56 
57     @Test
testDragIcon_landscape()58     public void testDragIcon_landscape() throws Throwable {
59         lockRotation(false);
60         performTest();
61     }
62 
performTest()63     private void performTest() throws Throwable {
64         clearHomescreen();
65         mActivityMonitor.startLauncher();
66 
67         final LauncherAppWidgetProviderInfo widgetInfo =
68                 findWidgetProvider(false /* hasConfigureScreen */);
69 
70         // Open widget tray and wait for load complete.
71         final UiObject2 widgetContainer = openWidgetsTray();
72         assertTrue(Wait.atMost(Condition.minChildCount(widgetContainer, 2), DEFAULT_UI_TIMEOUT));
73 
74         // Drag widget to homescreen
75         UiObject2 widget = scrollAndFind(widgetContainer, By.clazz(WidgetCell.class)
76                 .hasDescendant(By.text(widgetInfo.getLabel(mTargetContext.getPackageManager()))));
77         dragToWorkspace(widget, false);
78 
79         assertTrue(mActivityMonitor.itemExists(new ItemOperator() {
80             @Override
81             public boolean evaluate(ItemInfo info, View view) {
82                 return info instanceof LauncherAppWidgetInfo &&
83                         ((LauncherAppWidgetInfo) info).providerName.equals(widgetInfo.provider);
84             }
85         }).call());
86     }
87 }
88