1 /* 2 * Copyright (C) 2016 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 android.support.test.espresso.Espresso.onView; 20 import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; 21 import static android.support.test.espresso.matcher.ViewMatchers.withText; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertNotNull; 25 import static org.mockito.Mockito.any; 26 import static org.mockito.Mockito.doReturn; 27 import static org.mockito.Mockito.eq; 28 import static org.mockito.Mockito.spy; 29 import static org.mockito.Mockito.times; 30 import static org.mockito.Mockito.verify; 31 import static org.mockito.Mockito.when; 32 33 import android.app.Instrumentation; 34 import android.content.ComponentName; 35 import android.content.Context; 36 import android.content.ContextWrapper; 37 import android.content.Intent; 38 import android.content.pm.ResolveInfo; 39 import android.content.pm.ShortcutInfo; 40 import android.content.pm.ShortcutManager; 41 import android.support.test.InstrumentationRegistry; 42 import android.support.test.filters.SmallTest; 43 import android.support.test.runner.AndroidJUnit4; 44 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.mockito.ArgumentCaptor; 49 import org.mockito.Captor; 50 import org.mockito.Mock; 51 import org.mockito.MockitoAnnotations; 52 53 import java.util.Arrays; 54 import java.util.List; 55 56 /** 57 * Tests for {@link CreateShortcutTest} 58 * 59 m SettingsTests && 60 adb install \ 61 -r -g ${ANDROID_PRODUCT_OUT}/data/app/SettingsTests/SettingsTests.apk && 62 adb shell am instrument -e class com.android.settings.CreateShortcutTest \ 63 -w com.android.settings.tests/android.support.test.runner.AndroidJUnitRunner 64 */ 65 @RunWith(AndroidJUnit4.class) 66 @SmallTest 67 public class CreateShortcutTest { 68 69 private static final String SHORTCUT_ID_PREFIX = CreateShortcut.SHORTCUT_ID_PREFIX; 70 71 private Instrumentation mInstrumentation; 72 private Context mContext; 73 74 @Mock ShortcutManager mShortcutManager; 75 @Captor ArgumentCaptor<List<ShortcutInfo>> mListCaptor; 76 77 @Before setup()78 public void setup() { 79 MockitoAnnotations.initMocks(this); 80 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 81 mContext = mInstrumentation.getTargetContext(); 82 } 83 84 @Test test_layoutDoesNotHaveCancelButton()85 public void test_layoutDoesNotHaveCancelButton() { 86 mInstrumentation.startActivitySync(new Intent(Intent.ACTION_CREATE_SHORTCUT) 87 .setClassName(mContext, CreateShortcut.class.getName())); 88 onView(withText(R.string.cancel)).check(doesNotExist()); 89 } 90 91 @Test createResultIntent()92 public void createResultIntent() { 93 CreateShortcut orgActivity = (CreateShortcut) mInstrumentation.startActivitySync( 94 new Intent(Intent.ACTION_CREATE_SHORTCUT) 95 .setClassName(mContext, CreateShortcut.class.getName())); 96 CreateShortcut activity = spy(orgActivity); 97 doReturn(mShortcutManager).when(activity).getSystemService(eq(Context.SHORTCUT_SERVICE)); 98 99 when(mShortcutManager.createShortcutResultIntent(any(ShortcutInfo.class))) 100 .thenReturn(new Intent().putExtra("d1", "d2")); 101 102 Intent intent = CreateShortcut.getBaseIntent() 103 .setClass(activity, Settings.ManageApplicationsActivity.class); 104 ResolveInfo ri = activity.getPackageManager().resolveActivity(intent, 0); 105 Intent result = activity.createResultIntent(intent, ri, "dummy"); 106 assertEquals("d2", result.getStringExtra("d1")); 107 assertNotNull(result.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT)); 108 109 ArgumentCaptor<ShortcutInfo> infoCaptor = ArgumentCaptor.forClass(ShortcutInfo.class); 110 verify(mShortcutManager, times(1)) 111 .createShortcutResultIntent(infoCaptor.capture()); 112 String expectedId = SHORTCUT_ID_PREFIX + intent.getComponent().flattenToShortString(); 113 assertEquals(expectedId, infoCaptor.getValue().getId()); 114 } 115 116 @Test shortcutsUpdateTask()117 public void shortcutsUpdateTask() { 118 mContext = spy(new ContextWrapper(mInstrumentation.getTargetContext())); 119 doReturn(mShortcutManager).when(mContext).getSystemService(eq(Context.SHORTCUT_SERVICE)); 120 121 List<ShortcutInfo> pinnedShortcuts = Arrays.asList( 122 makeShortcut("d1"), makeShortcut("d2"), 123 makeShortcut(Settings.ManageApplicationsActivity.class), 124 makeShortcut("d3"), 125 makeShortcut(Settings.SoundSettingsActivity.class)); 126 when(mShortcutManager.getPinnedShortcuts()).thenReturn(pinnedShortcuts); 127 new CreateShortcut.ShortcutsUpdateTask(mContext).doInBackground(); 128 129 verify(mShortcutManager, times(1)).updateShortcuts(mListCaptor.capture()); 130 131 List<ShortcutInfo> updates = mListCaptor.getValue(); 132 assertEquals(2, updates.size()); 133 assertEquals(pinnedShortcuts.get(2).getId(), updates.get(0).getId()); 134 assertEquals(pinnedShortcuts.get(4).getId(), updates.get(1).getId()); 135 } 136 makeShortcut(Class<?> className)137 private ShortcutInfo makeShortcut(Class<?> className) { 138 ComponentName cn = new ComponentName(mContext, className); 139 return makeShortcut(SHORTCUT_ID_PREFIX + cn.flattenToShortString()); 140 } 141 makeShortcut(String id)142 private ShortcutInfo makeShortcut(String id) { 143 return new ShortcutInfo.Builder(mContext, id).build(); 144 } 145 } 146