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 static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNotSame;
21 import static org.junit.Assert.assertNull;
22 
23 import android.appwidget.AppWidgetManager;
24 import android.content.Intent;
25 
26 import androidx.test.filters.LargeTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
30 import com.android.launcher3.tapl.Widget;
31 import com.android.launcher3.tapl.WidgetResizeFrame;
32 import com.android.launcher3.tapl.Widgets;
33 import com.android.launcher3.testcomponent.WidgetConfigActivity;
34 import com.android.launcher3.ui.AbstractLauncherUiTest;
35 import com.android.launcher3.ui.TestViewHelpers;
36 import com.android.launcher3.util.rule.ShellCommandRule;
37 import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
38 
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 
44 /**
45  * Test to verify widget configuration is properly shown.
46  */
47 @LargeTest
48 @RunWith(AndroidJUnit4.class)
49 public class AddConfigWidgetTest extends AbstractLauncherUiTest {
50 
51     @Rule
52     public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
53 
54     private LauncherAppWidgetProviderInfo mWidgetInfo;
55     private AppWidgetManager mAppWidgetManager;
56 
57     private int mWidgetId;
58 
59     @Override
60     @Before
setUp()61     public void setUp() throws Exception {
62         super.setUp();
63         mWidgetInfo = TestViewHelpers.findWidgetProvider(this, true /* hasConfigureScreen */);
64         mAppWidgetManager = AppWidgetManager.getInstance(mTargetContext);
65     }
66 
67     @Test
68     @PortraitLandscape
testWidgetConfig()69     public void testWidgetConfig() throws Throwable {
70         runTest(true);
71     }
72 
73     @Test
74     @PortraitLandscape
testConfigCancelled()75     public void testConfigCancelled() throws Throwable {
76         runTest(false);
77     }
78 
79 
80     /**
81      * @param acceptConfig accept the config activity
82      */
runTest(boolean acceptConfig)83     private void runTest(boolean acceptConfig) throws Throwable {
84         clearHomescreen();
85         mDevice.pressHome();
86 
87         final Widgets widgets = mLauncher.getWorkspace().openAllWidgets();
88 
89         // Drag widget to homescreen
90         WidgetConfigStartupMonitor monitor = new WidgetConfigStartupMonitor();
91         WidgetResizeFrame resizeFrame =
92                 widgets.getWidget(mWidgetInfo.getLabel(mTargetContext.getPackageManager()))
93                         .dragConfigWidgetToWorkspace(acceptConfig);
94         // Widget id for which the config activity was opened
95         mWidgetId = monitor.getWidgetId();
96 
97         // Verify that the widget id is valid and bound
98         assertNotNull(mAppWidgetManager.getAppWidgetInfo(mWidgetId));
99 
100         if (acceptConfig) {
101             assertNotNull("Widget resize frame not shown after widget added", resizeFrame);
102             resizeFrame.dismiss();
103 
104             final Widget widget =
105                     mLauncher.getWorkspace().tryGetWidget(mWidgetInfo.label, DEFAULT_UI_TIMEOUT);
106             assertNotNull("Widget not found on the workspace", widget);
107         } else {
108             final Widget widget =
109                     mLauncher.getWorkspace().tryGetWidget(mWidgetInfo.label, DEFAULT_UI_TIMEOUT);
110             assertNull("Widget unexpectedly found on the workspace", widget);
111         }
112     }
113 
114     /**
115      * Broadcast receiver for receiving widget config activity status.
116      */
117     private class WidgetConfigStartupMonitor extends BlockingBroadcastReceiver {
118 
WidgetConfigStartupMonitor()119         public WidgetConfigStartupMonitor() {
120             super(WidgetConfigActivity.class.getName());
121         }
122 
getWidgetId()123         public int getWidgetId() throws InterruptedException {
124             Intent intent = blockingGetExtraIntent();
125             assertNotNull(intent);
126             assertEquals(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE, intent.getAction());
127             int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
128                     LauncherAppWidgetInfo.NO_ID);
129             assertNotSame(widgetId, LauncherAppWidgetInfo.NO_ID);
130             return widgetId;
131         }
132     }
133 }
134