ShortcutManagerTest9.java revision 87a563e0707bb7e2be034c195e9827dfe3451cfd
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().provider.getClassName());
87    }
88
89    public void testNotForeground() {
90        setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_1, USER_0));
91
92        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
93            makeCallerBackground();
94
95            assertExpectException(IllegalStateException.class, "foreground activity", () -> {
96                mInternal.requestPinAppWidget(CALLING_PACKAGE_1, makeProviderInfo("dummy"),
97            /* resultIntent= */ null, USER_P0);
98            });
99
100            verify(mServiceContext, times(0)).sendIntentSender(any(IntentSender.class));
101            verify(mServiceContext, times(0)).startActivityAsUser(
102                    any(Intent.class), any(UserHandle.class));
103        });
104    }
105
106    private void checkRequestPinAppWidget(@Nullable PendingIntent resultIntent) {
107        setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_1, USER_0));
108        setDefaultLauncher(USER_10, mMainActivityFetcher.apply(LAUNCHER_2, USER_10));
109
110        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
111            AppWidgetProviderInfo info = makeProviderInfo("c1");
112
113            assertTrue(mInternal.requestPinAppWidget(CALLING_PACKAGE_1, info,
114                    resultIntent == null ? null : resultIntent.getIntentSender(), USER_P0));
115
116            verify(mServiceContext, times(0)).sendIntentSender(any(IntentSender.class));
117        });
118
119        runWithCaller(LAUNCHER_1, USER_0, () -> {
120            // Check the intent passed to startActivityAsUser().
121            final ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class);
122
123            verify(mServiceContext).startActivityAsUser(intent.capture(), eq(HANDLE_USER_0));
124
125            assertPinItemRequestIntent(intent.getValue(), mInjectedClientPackage);
126
127            // Check the request object.
128            final PinItemRequest request = mLauncherApps.getPinItemRequest(intent.getValue());
129            assertPinItemRequest(request, "c1");
130
131            // Accept the request.
132            assertTrue(request.accept());
133        });
134
135        // This method is always called, even with PI == null.
136        if (resultIntent == null) {
137            verify(mServiceContext, times(1)).sendIntentSender(eq(null));
138        } else {
139            verify(mServiceContext, times(1)).sendIntentSender(any(IntentSender.class));
140        }
141    }
142
143    public void testRequestPinAppWidget() {
144        checkRequestPinAppWidget(/* resultIntent=*/ null);
145    }
146
147    public void testRequestPinAppWidget_withCallback() {
148        final PendingIntent resultIntent =
149                PendingIntent.getActivity(getTestContext(), 0, new Intent(), 0);
150
151        checkRequestPinAppWidget(resultIntent);
152    }
153}
154