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 androidx.core.content.pm;
18
19import static androidx.core.graphics.drawable.IconCompatTest.verifyBadgeBitmap;
20
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertNull;
24import static org.mockito.ArgumentMatchers.any;
25import static org.mockito.Mockito.doReturn;
26import static org.mockito.Mockito.spy;
27
28import android.content.ComponentName;
29import android.content.Context;
30import android.content.ContextWrapper;
31import android.content.Intent;
32import android.content.pm.ApplicationInfo;
33import android.content.pm.PackageManager;
34import android.support.test.InstrumentationRegistry;
35import android.support.test.filters.SmallTest;
36import android.support.test.runner.AndroidJUnit4;
37
38import androidx.core.app.TestActivity;
39import androidx.core.content.ContextCompat;
40import androidx.core.graphics.drawable.IconCompat;
41import androidx.core.test.R;
42
43import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46
47@SmallTest
48@RunWith(AndroidJUnit4.class)
49public class ShortcutInfoCompatTest {
50
51    private Intent mAction;
52
53    private Context mContext;
54    private ShortcutInfoCompat.Builder mBuilder;
55
56    @Before
57    public void setup() {
58        mContext = spy(new ContextWrapper(InstrumentationRegistry.getContext()));
59        mAction = new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName());
60
61        mBuilder = new ShortcutInfoCompat.Builder(mContext, "test-shortcut")
62                .setIntent(mAction)
63                .setShortLabel("Test shortcut")
64                .setIcon(IconCompat.createWithResource(mContext, R.drawable.test_drawable_red));
65    }
66
67    @Test
68    public void testAddToIntent_noBadge() {
69        Intent intent = new Intent();
70        mBuilder.setActivity(new ComponentName(mContext, TestActivity.class))
71                .build()
72                .addToIntent(intent);
73
74        assertEquals(mAction, intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT));
75        assertNotNull(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE));
76        assertNull(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON));
77    }
78
79    @Test
80    public void testAddToIntent_badgeActivity() {
81        Intent intent = new Intent();
82        mBuilder.setActivity(new ComponentName(mContext, TestActivity.class))
83                .setAlwaysBadged()
84                .build()
85                .addToIntent(intent);
86
87        assertEquals(mAction, intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT));
88        assertNull(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE));
89
90        verifyBadgeBitmap(intent, ContextCompat.getColor(mContext, R.color.test_red),
91                ContextCompat.getColor(mContext, R.color.test_blue));
92    }
93
94    @Test
95    public void testAddToIntent_badgeApplication() {
96        ApplicationInfo appInfo = spy(mContext.getApplicationInfo());
97        doReturn(ContextCompat.getDrawable(mContext, R.drawable.test_drawable_green))
98                .when(appInfo).loadIcon(any(PackageManager.class));
99        doReturn(appInfo).when(mContext).getApplicationInfo();
100
101        Intent intent = new Intent();
102        mBuilder.setAlwaysBadged()
103                .build()
104                .addToIntent(intent);
105
106        assertEquals(mAction, intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT));
107        assertNull(intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE));
108
109        verifyBadgeBitmap(intent, ContextCompat.getColor(mContext, R.color.test_red),
110                ContextCompat.getColor(mContext, R.color.test_green));
111    }
112}
113