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.uiautomator.By; 19 import android.support.test.uiautomator.UiObject2; 20 import android.test.suitebuilder.annotation.LargeTest; 21 import android.view.View; 22 23 import com.android.launcher3.ItemInfo; 24 import com.android.launcher3.Launcher; 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.LauncherInstrumentationTestCase; 29 import com.android.launcher3.util.Condition; 30 import com.android.launcher3.util.Wait; 31 import com.android.launcher3.widget.WidgetCell; 32 33 /** 34 * Test to add widget from widget tray 35 */ 36 @LargeTest 37 public class AddWidgetTest extends LauncherInstrumentationTestCase { 38 39 private LauncherAppWidgetProviderInfo widgetInfo; 40 41 @Override setUp()42 protected void setUp() throws Exception { 43 super.setUp(); 44 grantWidgetPermission(); 45 46 widgetInfo = findWidgetProvider(false /* hasConfigureScreen */); 47 } 48 testDragIcon_portrait()49 public void testDragIcon_portrait() throws Throwable { 50 lockRotation(true); 51 performTest(); 52 } 53 testDragIcon_landscape()54 public void testDragIcon_landscape() throws Throwable { 55 lockRotation(false); 56 performTest(); 57 } 58 performTest()59 private void performTest() throws Throwable { 60 clearHomescreen(); 61 Launcher launcher = startLauncher(); 62 63 // Open widget tray and wait for load complete. 64 final UiObject2 widgetContainer = openWidgetsTray(); 65 assertTrue(Wait.atMost(Condition.minChildCount(widgetContainer, 2), DEFAULT_UI_TIMEOUT)); 66 67 // Drag widget to homescreen 68 UiObject2 widget = scrollAndFind(widgetContainer, By.clazz(WidgetCell.class) 69 .hasDescendant(By.text(widgetInfo.getLabel(mTargetContext.getPackageManager())))); 70 dragToWorkspace(widget, false); 71 72 assertNotNull(launcher.getWorkspace().getFirstMatch(new ItemOperator() { 73 @Override 74 public boolean evaluate(ItemInfo info, View view) { 75 return info instanceof LauncherAppWidgetInfo && 76 ((LauncherAppWidgetInfo) info).providerName.equals(widgetInfo.provider); 77 } 78 })); 79 } 80 } 81