2017-01-25 11:19:59 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
|
* the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
|
* the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3.ui.widget;
|
|
|
|
|
|
2019-06-21 10:58:03 -07:00
|
|
|
import static com.android.launcher3.ui.TaplTestsLauncher3.getAppPackageName;
|
|
|
|
|
|
2018-08-10 20:01:24 -07:00
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
|
import static org.junit.Assert.assertNotSame;
|
|
|
|
|
|
2017-01-25 11:19:59 -08:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.content.Intent;
|
2017-03-08 14:49:55 -08:00
|
|
|
import android.graphics.Color;
|
2019-05-16 16:32:25 -07:00
|
|
|
import android.view.View;
|
|
|
|
|
|
2018-08-15 15:28:47 -07:00
|
|
|
import androidx.test.filters.LargeTest;
|
|
|
|
|
import androidx.test.runner.AndroidJUnit4;
|
2017-01-25 11:19:59 -08:00
|
|
|
|
|
|
|
|
import com.android.launcher3.ItemInfo;
|
|
|
|
|
import com.android.launcher3.LauncherAppWidgetInfo;
|
|
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
|
|
|
|
import com.android.launcher3.Workspace.ItemOperator;
|
2019-05-16 16:32:25 -07:00
|
|
|
import com.android.launcher3.WorkspaceItemInfo;
|
2017-01-25 11:19:59 -08:00
|
|
|
import com.android.launcher3.shortcuts.ShortcutKey;
|
2019-06-25 19:57:13 -07:00
|
|
|
import com.android.launcher3.tapl.AddToHomeScreenPrompt;
|
2017-01-25 11:19:59 -08:00
|
|
|
import com.android.launcher3.testcomponent.AppWidgetNoConfig;
|
|
|
|
|
import com.android.launcher3.testcomponent.AppWidgetWithConfig;
|
|
|
|
|
import com.android.launcher3.testcomponent.RequestPinItemActivity;
|
2017-07-31 10:59:52 -07:00
|
|
|
import com.android.launcher3.ui.AbstractLauncherUiTest;
|
2017-01-25 11:19:59 -08:00
|
|
|
import com.android.launcher3.util.Condition;
|
|
|
|
|
import com.android.launcher3.util.Wait;
|
2017-07-31 10:59:52 -07:00
|
|
|
import com.android.launcher3.util.rule.ShellCommandRule;
|
2017-01-25 11:19:59 -08:00
|
|
|
|
2017-07-31 10:59:52 -07:00
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
2017-01-25 11:19:59 -08:00
|
|
|
import java.util.UUID;
|
2017-07-31 10:59:52 -07:00
|
|
|
|
2017-01-25 11:19:59 -08:00
|
|
|
/**
|
|
|
|
|
* Test to verify pin item request flow.
|
|
|
|
|
*/
|
|
|
|
|
@LargeTest
|
2017-07-31 10:59:52 -07:00
|
|
|
@RunWith(AndroidJUnit4.class)
|
2018-10-05 16:19:38 -07:00
|
|
|
public class RequestPinItemTest extends AbstractLauncherUiTest {
|
2017-01-25 11:19:59 -08:00
|
|
|
|
2019-11-04 14:50:22 -08:00
|
|
|
@Rule
|
|
|
|
|
public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
|
2017-01-25 11:19:59 -08:00
|
|
|
|
|
|
|
|
private String mCallbackAction;
|
|
|
|
|
private String mShortcutId;
|
|
|
|
|
private int mAppWidgetId;
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-07-31 10:59:52 -07:00
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
2017-01-25 11:19:59 -08:00
|
|
|
super.setUp();
|
|
|
|
|
mCallbackAction = UUID.randomUUID().toString();
|
|
|
|
|
mShortcutId = UUID.randomUUID().toString();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 10:59:52 -07:00
|
|
|
@Test
|
2018-06-18 11:36:20 -07:00
|
|
|
public void testEmpty() throws Throwable { /* needed while the broken tests are being fixed */ }
|
|
|
|
|
|
2019-05-03 12:56:13 -07:00
|
|
|
@Test
|
2017-01-25 11:19:59 -08:00
|
|
|
public void testPinWidgetNoConfig() throws Throwable {
|
2019-06-25 18:34:48 -07:00
|
|
|
runTest("pinWidgetNoConfig", true, (info, view) -> info instanceof LauncherAppWidgetInfo &&
|
|
|
|
|
((LauncherAppWidgetInfo) info).appWidgetId == mAppWidgetId &&
|
|
|
|
|
((LauncherAppWidgetInfo) info).providerName.getClassName()
|
|
|
|
|
.equals(AppWidgetNoConfig.class.getName()));
|
2017-01-25 11:19:59 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-04 14:50:22 -08:00
|
|
|
@Test
|
2017-03-08 14:49:55 -08:00
|
|
|
public void testPinWidgetNoConfig_customPreview() throws Throwable {
|
|
|
|
|
// Command to set custom preview
|
2019-11-04 14:50:22 -08:00
|
|
|
Intent command = RequestPinItemActivity.getCommandIntent(
|
2017-03-08 14:49:55 -08:00
|
|
|
RequestPinItemActivity.class, "setRemoteViewColor").putExtra(
|
|
|
|
|
RequestPinItemActivity.EXTRA_PARAM + "0", Color.RED);
|
|
|
|
|
|
2019-06-25 18:34:48 -07:00
|
|
|
runTest("pinWidgetNoConfig", true, (info, view) -> info instanceof LauncherAppWidgetInfo &&
|
|
|
|
|
((LauncherAppWidgetInfo) info).appWidgetId == mAppWidgetId &&
|
|
|
|
|
((LauncherAppWidgetInfo) info).providerName.getClassName()
|
|
|
|
|
.equals(AppWidgetNoConfig.class.getName()), command);
|
2017-03-08 14:49:55 -08:00
|
|
|
}
|
|
|
|
|
|
2019-05-03 12:56:13 -07:00
|
|
|
@Test
|
2017-01-25 11:19:59 -08:00
|
|
|
public void testPinWidgetWithConfig() throws Throwable {
|
2019-06-25 18:34:48 -07:00
|
|
|
runTest("pinWidgetWithConfig", true,
|
|
|
|
|
(info, view) -> info instanceof LauncherAppWidgetInfo &&
|
2017-01-25 11:19:59 -08:00
|
|
|
((LauncherAppWidgetInfo) info).appWidgetId == mAppWidgetId &&
|
|
|
|
|
((LauncherAppWidgetInfo) info).providerName.getClassName()
|
2019-06-25 18:34:48 -07:00
|
|
|
.equals(AppWidgetWithConfig.class.getName()));
|
2017-01-25 11:19:59 -08:00
|
|
|
}
|
|
|
|
|
|
2019-05-03 12:56:13 -07:00
|
|
|
@Test
|
2017-03-08 14:49:55 -08:00
|
|
|
public void testPinShortcut() throws Throwable {
|
|
|
|
|
// Command to set the shortcut id
|
|
|
|
|
Intent command = RequestPinItemActivity.getCommandIntent(
|
|
|
|
|
RequestPinItemActivity.class, "setShortcutId").putExtra(
|
|
|
|
|
RequestPinItemActivity.EXTRA_PARAM + "0", mShortcutId);
|
2017-01-25 11:19:59 -08:00
|
|
|
|
|
|
|
|
runTest("pinShortcut", false, new ItemOperator() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean evaluate(ItemInfo info, View view) {
|
2019-03-27 16:03:06 -07:00
|
|
|
return info instanceof WorkspaceItemInfo &&
|
2017-01-25 11:19:59 -08:00
|
|
|
info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
|
|
|
|
|
ShortcutKey.fromItemInfo(info).getId().equals(mShortcutId);
|
|
|
|
|
}
|
2017-03-08 14:49:55 -08:00
|
|
|
}, command);
|
2017-01-25 11:19:59 -08:00
|
|
|
}
|
|
|
|
|
|
2017-03-08 14:49:55 -08:00
|
|
|
private void runTest(String activityMethod, boolean isWidget, ItemOperator itemMatcher,
|
|
|
|
|
Intent... commandIntents) throws Throwable {
|
2017-09-11 11:18:03 -07:00
|
|
|
if (!Utilities.ATLEAST_OREO) {
|
2017-01-25 11:19:59 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
clearHomescreen();
|
2019-07-12 12:26:08 -07:00
|
|
|
mDevice.pressHome();
|
2017-01-25 11:19:59 -08:00
|
|
|
|
|
|
|
|
// Open Pin item activity
|
|
|
|
|
BlockingBroadcastReceiver openMonitor = new BlockingBroadcastReceiver(
|
|
|
|
|
RequestPinItemActivity.class.getName());
|
2019-06-20 17:52:16 -07:00
|
|
|
mLauncher.
|
|
|
|
|
getWorkspace().
|
|
|
|
|
switchToAllApps().
|
|
|
|
|
getAppIcon("Test Pin Item").
|
2019-06-21 10:58:03 -07:00
|
|
|
launch(getAppPackageName());
|
2017-01-25 11:19:59 -08:00
|
|
|
assertNotNull(openMonitor.blockingGetExtraIntent());
|
|
|
|
|
|
|
|
|
|
// Set callback
|
|
|
|
|
PendingIntent callback = PendingIntent.getBroadcast(mTargetContext, 0,
|
|
|
|
|
new Intent(mCallbackAction), PendingIntent.FLAG_ONE_SHOT);
|
|
|
|
|
mTargetContext.sendBroadcast(RequestPinItemActivity.getCommandIntent(
|
|
|
|
|
RequestPinItemActivity.class, "setCallback").putExtra(
|
|
|
|
|
RequestPinItemActivity.EXTRA_PARAM + "0", callback));
|
|
|
|
|
|
2017-03-08 14:49:55 -08:00
|
|
|
for (Intent command : commandIntents) {
|
|
|
|
|
mTargetContext.sendBroadcast(command);
|
2017-01-25 11:19:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// call the requested method to start the flow
|
|
|
|
|
mTargetContext.sendBroadcast(RequestPinItemActivity.getCommandIntent(
|
|
|
|
|
RequestPinItemActivity.class, activityMethod));
|
2019-06-25 19:57:13 -07:00
|
|
|
final AddToHomeScreenPrompt addToHomeScreenPrompt = mLauncher.getAddToHomeScreenPrompt();
|
2017-01-25 11:19:59 -08:00
|
|
|
|
|
|
|
|
// Accept confirmation:
|
|
|
|
|
BlockingBroadcastReceiver resultReceiver = new BlockingBroadcastReceiver(mCallbackAction);
|
2019-06-25 19:57:13 -07:00
|
|
|
addToHomeScreenPrompt.addAutomatically();
|
2017-01-25 11:19:59 -08:00
|
|
|
Intent result = resultReceiver.blockingGetIntent();
|
|
|
|
|
assertNotNull(result);
|
|
|
|
|
mAppWidgetId = result.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
|
|
|
|
|
if (isWidget) {
|
|
|
|
|
assertNotSame(-1, mAppWidgetId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Go back to home
|
2019-06-11 19:15:14 -07:00
|
|
|
mLauncher.pressHome();
|
2019-12-12 14:18:19 -08:00
|
|
|
Wait.atMost("", new ItemSearchCondition(itemMatcher), DEFAULT_ACTIVITY_TIMEOUT,
|
2019-11-04 14:50:22 -08:00
|
|
|
mLauncher);
|
2017-01-25 11:19:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Condition for for an item
|
|
|
|
|
*/
|
2018-08-14 10:27:29 -07:00
|
|
|
private class ItemSearchCondition implements Condition {
|
2017-01-25 11:19:59 -08:00
|
|
|
|
|
|
|
|
private final ItemOperator mOp;
|
|
|
|
|
|
|
|
|
|
ItemSearchCondition(ItemOperator op) {
|
|
|
|
|
mOp = op;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isTrue() throws Throwable {
|
2017-07-31 10:59:52 -07:00
|
|
|
return mMainThreadExecutor.submit(mActivityMonitor.itemExists(mOp)).get();
|
2017-01-25 11:19:59 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|