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 import com.android.settings.testutils.SettingsRobolectricTestRunner; 25 import com.android.settings.testutils.shadow.ShadowShortcutManager; 26 import java.util.ArrayList; 27 import java.util.List; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.annotation.Config; 35 36 @RunWith(SettingsRobolectricTestRunner.class) 37 @Config(shadows = {ShadowShortcutManager.class}) 38 public class SettingsInitializeTest { 39 40 private Context mContext; 41 private SettingsInitialize mSettingsInitialize; 42 43 @Before setUp()44 public void setUp() { 45 MockitoAnnotations.initMocks(this); 46 47 mContext = RuntimeEnvironment.application; 48 mSettingsInitialize = new SettingsInitialize(); 49 } 50 51 @Test refreshExistingShortcuts_shouldUpdateLaunchIntentFlagsForExistingShortcut()52 public void refreshExistingShortcuts_shouldUpdateLaunchIntentFlagsForExistingShortcut() { 53 final String id = "test_shortcut_id"; 54 final Intent intent = new Intent(Intent.ACTION_DEFAULT); 55 intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 56 final ShortcutInfo info = new ShortcutInfo.Builder(mContext, id) 57 .setShortLabel("test123") 58 .setIntent(intent) 59 .build(); 60 final List<ShortcutInfo> shortcuts = new ArrayList<>(); 61 shortcuts.add(info); 62 ShadowShortcutManager.get().setPinnedShortcuts(shortcuts); 63 64 mSettingsInitialize.refreshExistingShortcuts(mContext); 65 66 final List<ShortcutInfo> updatedShortcuts = 67 ShadowShortcutManager.get().getLastUpdatedShortcuts(); 68 assertThat(updatedShortcuts).hasSize(1); 69 final ShortcutInfo updateInfo = updatedShortcuts.get(0); 70 assertThat(updateInfo.getId()).isEqualTo(id); 71 final int flags = updateInfo.getIntent().getFlags(); 72 // The original flag should be removed 73 assertThat(flags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED).isEqualTo(0); 74 // The new flags should be set 75 assertThat(flags & Intent.FLAG_ACTIVITY_NEW_TASK).isEqualTo(Intent.FLAG_ACTIVITY_NEW_TASK); 76 assertThat(flags & Intent.FLAG_ACTIVITY_CLEAR_TOP).isEqualTo( 77 Intent.FLAG_ACTIVITY_CLEAR_TOP); 78 } 79 } 80