• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ShortcutInfo;
24 
25 import android.content.pm.ShortcutManager;
26 
27 import java.util.Collections;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.MockitoAnnotations;
32 import org.robolectric.RobolectricTestRunner;
33 import org.robolectric.RuntimeEnvironment;
34 
35 import java.util.List;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public class SettingsInitializeTest {
39 
40     private Context mContext;
41     private SettingsInitialize mSettingsInitialize;
42     private ShortcutManager mShortcutManager;
43 
44     @Before
setUp()45     public void setUp() {
46         MockitoAnnotations.initMocks(this);
47 
48         mContext = RuntimeEnvironment.application;
49         mSettingsInitialize = new SettingsInitialize();
50         mShortcutManager = (ShortcutManager) mContext.getSystemService(Context.SHORTCUT_SERVICE);
51     }
52 
53     @Test
refreshExistingShortcuts_shouldUpdateLaunchIntentFlagsForExistingShortcut()54     public void refreshExistingShortcuts_shouldUpdateLaunchIntentFlagsForExistingShortcut() {
55         final String id = "test_shortcut_id";
56         final Intent intent = new Intent(Intent.ACTION_DEFAULT);
57         intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
58         final ShortcutInfo info = new ShortcutInfo.Builder(mContext, id)
59                 .setShortLabel("test123")
60                 .setIntent(intent)
61                 .build();
62 
63         mShortcutManager.addDynamicShortcuts(Collections.singletonList(info));
64         mShortcutManager.requestPinShortcut(info, null);
65 
66         mSettingsInitialize.refreshExistingShortcuts(mContext);
67 
68         final List<ShortcutInfo> updatedShortcuts = mShortcutManager.getPinnedShortcuts();
69         assertThat(updatedShortcuts).hasSize(1);
70         final ShortcutInfo updateInfo = updatedShortcuts.get(0);
71         assertThat(updateInfo.getId()).isEqualTo(id);
72         final int flags = updateInfo.getIntent().getFlags();
73         // The original flag should be removed
74         assertThat(flags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED).isEqualTo(0);
75         // The new flags should be set
76         assertThat(flags & Intent.FLAG_ACTIVITY_NEW_TASK).isEqualTo(Intent.FLAG_ACTIVITY_NEW_TASK);
77         assertThat(flags & Intent.FLAG_ACTIVITY_CLEAR_TOP).isEqualTo(
78                 Intent.FLAG_ACTIVITY_CLEAR_TOP);
79     }
80 
81     @Test
refreshExistingShortcuts_shouldNotUpdateImmutableShortcut()82     public void refreshExistingShortcuts_shouldNotUpdateImmutableShortcut() {
83         final String id = "test_shortcut_id";
84         final ShortcutInfo info = new ShortcutInfo.Builder(mContext, id)
85             .setShortLabel("test123")
86             .setIntent(new Intent(Intent.ACTION_DEFAULT))
87             .build();
88         info.addFlags(ShortcutInfo.FLAG_IMMUTABLE);
89 
90         mShortcutManager.addDynamicShortcuts(Collections.singletonList(info));
91         mShortcutManager.requestPinShortcut(info, null);
92 
93         mSettingsInitialize.refreshExistingShortcuts(mContext);
94 
95         final List<ShortcutInfo> updatedShortcuts = mShortcutManager.getPinnedShortcuts();
96         assertThat(updatedShortcuts).hasSize(1);
97         assertThat(updatedShortcuts.get(0)).isSameAs(info);
98     }
99 }
100