ShortcutManagerTest9.java revision 970d4b462652cab66ce34da551d089f385bf39d8
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.pm;
18
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
20
21import static org.mockito.Matchers.any;
22import static org.mockito.Matchers.eq;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25
26import android.annotation.Nullable;
27import android.app.PendingIntent;
28import android.appwidget.AppWidgetProviderInfo;
29import android.content.ComponentName;
30import android.content.Intent;
31import android.content.IntentSender;
32import android.content.pm.LauncherApps;
33import android.content.pm.LauncherApps.PinItemRequest;
34import android.os.UserHandle;
35import android.test.suitebuilder.annotation.SmallTest;
36
37import org.mockito.ArgumentCaptor;
38
39/**
40 * Tests for {@link android.content.pm.ShortcutServiceInternal#requestPinAppWidget}
41 * and relevant APIs.
42 *
43 m FrameworksServicesTests &&
44 adb install \
45 -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
46 adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest9 \
47 -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
48 */
49@SmallTest
50public class ShortcutManagerTest9 extends BaseShortcutManagerTest {
51
52    private ShortcutRequestPinProcessor mProcessor;
53
54    @Override
55    protected void initService() {
56        super.initService();
57        mProcessor = mService.getShortcutRequestPinProcessorForTest();
58    }
59
60    @Override
61    protected void setCaller(String packageName, int userId) {
62        super.setCaller(packageName, userId);
63
64        // Note during this test, assume all callers are in the foreground by default.
65        makeCallerForeground();
66    }
67
68    private AppWidgetProviderInfo makeProviderInfo(String className) {
69        AppWidgetProviderInfo info = new AppWidgetProviderInfo();
70        info.provider = new ComponentName(CALLING_PACKAGE_3, className);
71        return info;
72    }
73
74    private void assertPinItemRequestIntent(Intent actualIntent, String expectedPackage) {
75        assertEquals(LauncherApps.ACTION_CONFIRM_PIN_ITEM, actualIntent.getAction());
76        assertEquals(expectedPackage, actualIntent.getComponent().getPackageName());
77        assertEquals(PIN_CONFIRM_ACTIVITY_CLASS,
78                actualIntent.getComponent().getClassName());
79        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK,
80                actualIntent.getFlags());
81    }
82
83    private void assertPinItemRequest(PinItemRequest actualRequest, String className) {
84        assertNotNull(actualRequest);
85        assertEquals(PinItemRequest.REQUEST_TYPE_APPWIDGET, actualRequest.getRequestType());
86        assertEquals(className, actualRequest.getAppWidgetProviderInfo(getTestContext())
87                .provider.getClassName());
88    }
89
90    public void testNotForeground() {
91        setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_1, USER_0));
92
93        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
94            makeCallerBackground();
95
96            assertExpectException(IllegalStateException.class, "foreground activity", () -> {
97                mInternal.requestPinAppWidget(CALLING_PACKAGE_1, makeProviderInfo("dummy"),
98            /* resultIntent= */ null, USER_P0);
99            });
100
101            verify(mServiceContext, times(0)).sendIntentSender(any(IntentSender.class));
102            verify(mServiceContext, times(0)).startActivityAsUser(
103                    any(Intent.class), any(UserHandle.class));
104        });
105    }
106
107    private void checkRequestPinAppWidget(@Nullable PendingIntent resultIntent) {
108        setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_1, USER_0));
109        setDefaultLauncher(USER_10, mMainActivityFetcher.apply(LAUNCHER_2, USER_10));
110
111        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
112            AppWidgetProviderInfo info = makeProviderInfo("c1");
113
114            assertTrue(mInternal.requestPinAppWidget(CALLING_PACKAGE_1, info,
115                    resultIntent == null ? null : resultIntent.getIntentSender(), USER_P0));
116
117            verify(mServiceContext, times(0)).sendIntentSender(any(IntentSender.class));
118        });
119
120        runWithCaller(LAUNCHER_1, USER_0, () -> {
121            // Check the intent passed to startActivityAsUser().
122            final ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class);
123
124            verify(mServiceContext).startActivityAsUser(intent.capture(), eq(HANDLE_USER_0));
125
126            assertPinItemRequestIntent(intent.getValue(), mInjectedClientPackage);
127
128            // Check the request object.
129            final PinItemRequest request = mLauncherApps.getPinItemRequest(intent.getValue());
130            assertPinItemRequest(request, "c1");
131
132            // Accept the request.
133            assertTrue(request.accept());
134        });
135
136        // This method is always called, even with PI == null.
137        if (resultIntent == null) {
138            verify(mServiceContext, times(1)).sendIntentSender(eq(null));
139        } else {
140            verify(mServiceContext, times(1)).sendIntentSender(any(IntentSender.class));
141        }
142    }
143
144    public void testRequestPinAppWidget() {
145        checkRequestPinAppWidget(/* resultIntent=*/ null);
146    }
147
148    public void testRequestPinAppWidget_withCallback() {
149        final PendingIntent resultIntent =
150                PendingIntent.getActivity(getTestContext(), 0, new Intent(), 0);
151
152        checkRequestPinAppWidget(resultIntent);
153    }
154}
155