Home | History | Annotate | Download | only in shortcut
      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.shortcut;
     18 
     19 import static android.support.test.espresso.Espresso.onView;
     20 import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
     21 import static org.junit.Assert.assertEquals;
     22 import static org.junit.Assert.assertNotNull;
     23 import static org.mockito.Mockito.any;
     24 import static org.mockito.Mockito.doReturn;
     25 import static org.mockito.Mockito.eq;
     26 import static org.mockito.Mockito.spy;
     27 import static org.mockito.Mockito.times;
     28 import static org.mockito.Mockito.verify;
     29 import static org.mockito.Mockito.when;
     30 
     31 import android.app.Instrumentation;
     32 import android.content.ComponentName;
     33 import android.content.Context;
     34 import android.content.ContextWrapper;
     35 import android.content.Intent;
     36 import android.content.pm.ResolveInfo;
     37 import android.content.pm.ShortcutInfo;
     38 import android.content.pm.ShortcutManager;
     39 import android.support.test.InstrumentationRegistry;
     40 import android.support.test.espresso.matcher.ViewMatchers;
     41 import android.support.test.filters.SmallTest;
     42 import android.support.test.runner.AndroidJUnit4;
     43 
     44 import com.android.settings.R;
     45 import com.android.settings.Settings;
     46 
     47 import org.junit.Before;
     48 import org.junit.Test;
     49 import org.junit.runner.RunWith;
     50 import org.mockito.ArgumentCaptor;
     51 import org.mockito.Captor;
     52 import org.mockito.Mock;
     53 import org.mockito.MockitoAnnotations;
     54 
     55 import java.util.Arrays;
     56 import java.util.List;
     57 
     58 /**
     59  * Tests for {@link CreateShortcutTest}
     60  */
     61 @RunWith(AndroidJUnit4.class)
     62 @SmallTest
     63 public class CreateShortcutTest {
     64 
     65     private static final String SHORTCUT_ID_PREFIX = CreateShortcut.SHORTCUT_ID_PREFIX;
     66 
     67     private Instrumentation mInstrumentation;
     68     private Context mContext;
     69 
     70     @Mock
     71     ShortcutManager mShortcutManager;
     72     @Captor
     73     ArgumentCaptor<List<ShortcutInfo>> mListCaptor;
     74 
     75     @Before
     76     public void setup() {
     77         MockitoAnnotations.initMocks(this);
     78         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     79         mContext = mInstrumentation.getTargetContext();
     80     }
     81 
     82     @Test
     83     public void test_layoutDoesNotHaveCancelButton() {
     84         mInstrumentation.startActivitySync(new Intent(Intent.ACTION_CREATE_SHORTCUT)
     85                 .setClassName(mContext, CreateShortcut.class.getName())
     86                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
     87         onView(ViewMatchers.withText(R.string.cancel)).check(doesNotExist());
     88     }
     89 
     90     @Test
     91     public void createResultIntent() {
     92         CreateShortcut orgActivity = (CreateShortcut) mInstrumentation.startActivitySync(
     93                 new Intent(Intent.ACTION_CREATE_SHORTCUT)
     94                         .setClassName(mContext, CreateShortcut.class.getName())
     95                         .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
     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
    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 
    137     private ShortcutInfo makeShortcut(Class<?> className) {
    138         ComponentName cn = new ComponentName(mContext, className);
    139         return makeShortcut(SHORTCUT_ID_PREFIX + cn.flattenToShortString());
    140     }
    141 
    142     private ShortcutInfo makeShortcut(String id) {
    143         return new ShortcutInfo.Builder(mContext, id).build();
    144     }
    145 }
    146