• 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 static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
19 
20 import static com.android.launcher3.util.LauncherBindableItemsContainer.ItemOperator;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNotSame;
25 
26 import android.appwidget.AppWidgetManager;
27 import android.content.Intent;
28 import android.view.View;
29 
30 import androidx.test.filters.LargeTest;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import com.android.launcher3.Launcher;
34 import com.android.launcher3.celllayout.FavoriteItemsTransaction;
35 import com.android.launcher3.model.data.ItemInfo;
36 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
37 import com.android.launcher3.testcomponent.WidgetConfigActivity;
38 import com.android.launcher3.ui.AbstractLauncherUiTest;
39 import com.android.launcher3.ui.PortraitLandscapeRunner.PortraitLandscape;
40 import com.android.launcher3.ui.TestViewHelpers;
41 import com.android.launcher3.util.Wait;
42 import com.android.launcher3.util.rule.ShellCommandRule;
43 import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
44 
45 import org.junit.Before;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 
50 /**
51  * Test to verify widget configuration is properly shown.
52  */
53 @LargeTest
54 @RunWith(AndroidJUnit4.class)
55 public class AddConfigWidgetTest extends AbstractLauncherUiTest {
56 
57     @Rule
58     public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
59 
60     private LauncherAppWidgetProviderInfo mWidgetInfo;
61     private AppWidgetManager mAppWidgetManager;
62 
63     private int mWidgetId;
64 
65     @Override
66     @Before
setUp()67     public void setUp() throws Exception {
68         super.setUp();
69         mWidgetInfo = TestViewHelpers.findWidgetProvider(true /* hasConfigureScreen */);
70         mAppWidgetManager = AppWidgetManager.getInstance(mTargetContext);
71     }
72 
73     @Test
74     @PortraitLandscape
testWidgetConfig()75     public void testWidgetConfig() throws Throwable {
76         runTest(true);
77     }
78 
79     @Test
80     @PortraitLandscape
testConfigCancelled()81     public void testConfigCancelled() throws Throwable {
82         runTest(false);
83     }
84 
85 
86     /**
87      * @param acceptConfig accept the config activity
88      */
runTest(boolean acceptConfig)89     private void runTest(boolean acceptConfig) throws Throwable {
90         new FavoriteItemsTransaction(mTargetContext).commitAndLoadHome(mLauncher);
91 
92         // Drag widget to homescreen
93         WidgetConfigStartupMonitor monitor = new WidgetConfigStartupMonitor();
94         mLauncher.getWorkspace()
95                 .openAllWidgets()
96                 .getWidget(mWidgetInfo.getLabel(mTargetContext.getPackageManager()))
97                 .dragToWorkspace(true, false);
98         // Widget id for which the config activity was opened
99         mWidgetId = monitor.getWidgetId();
100 
101         // Verify that the widget id is valid and bound
102         assertNotNull(mAppWidgetManager.getAppWidgetInfo(mWidgetId));
103 
104         setResult(acceptConfig);
105         if (acceptConfig) {
106             Wait.atMost("", new WidgetSearchCondition(), DEFAULT_ACTIVITY_TIMEOUT, mLauncher);
107             assertNotNull(mAppWidgetManager.getAppWidgetInfo(mWidgetId));
108         } else {
109             // Verify that the widget id is deleted.
110             Wait.atMost("", () -> mAppWidgetManager.getAppWidgetInfo(mWidgetId) == null,
111                     DEFAULT_ACTIVITY_TIMEOUT, mLauncher);
112         }
113     }
114 
setResult(boolean success)115     private void setResult(boolean success) {
116         getInstrumentation().getTargetContext().sendBroadcast(
117                 WidgetConfigActivity.getCommandIntent(WidgetConfigActivity.class,
118                         success ? "clickOK" : "clickCancel"));
119     }
120 
121     /**
122      * Condition for searching widget id
123      */
124     private class WidgetSearchCondition implements Wait.Condition, ItemOperator {
125 
126         @Override
isTrue()127         public boolean isTrue() throws Throwable {
128             return mMainThreadExecutor.submit(() -> {
129                 Launcher l = Launcher.ACTIVITY_TRACKER.getCreatedActivity();
130                 return l != null && l.getWorkspace().getFirstMatch(this) != null;
131             }).get();
132         }
133 
134         @Override
evaluate(ItemInfo info, View view)135         public boolean evaluate(ItemInfo info, View view) {
136             return info instanceof LauncherAppWidgetInfo
137                     && ((LauncherAppWidgetInfo) info).providerName.getClassName().equals(
138                             mWidgetInfo.provider.getClassName())
139                     && ((LauncherAppWidgetInfo) info).appWidgetId == mWidgetId;
140         }
141     }
142 
143     /**
144      * Broadcast receiver for receiving widget config activity status.
145      */
146     private class WidgetConfigStartupMonitor extends BlockingBroadcastReceiver {
147 
WidgetConfigStartupMonitor()148         public WidgetConfigStartupMonitor() {
149             super(WidgetConfigActivity.class.getName());
150         }
151 
getWidgetId()152         public int getWidgetId() throws InterruptedException {
153             Intent intent = blockingGetExtraIntent();
154             assertNotNull(intent);
155             assertEquals(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE, intent.getAction());
156             int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
157                     LauncherAppWidgetInfo.NO_ID);
158             assertNotSame(widgetId, LauncherAppWidgetInfo.NO_ID);
159             return widgetId;
160         }
161     }
162 }
163