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 17 package com.android.settings.display; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.pm.ActivityInfo; 27 import android.content.pm.ApplicationInfo; 28 import android.content.pm.PackageManager; 29 import android.content.pm.ResolveInfo; 30 import android.graphics.drawable.Drawable; 31 import android.util.IconDrawableFactory; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 41 @RunWith(RobolectricTestRunner.class) 42 public class AppGridViewTest { 43 44 @Mock 45 private ResolveInfo mInfo; 46 @Mock 47 private ActivityInfo mActivityInfo; 48 @Mock 49 private ApplicationInfo mApplicationInfo; 50 @Mock 51 private Drawable mIcon; 52 @Mock 53 private PackageManager mPackageManager; 54 private Context mContext; 55 private IconDrawableFactory mIconFactory; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mInfo.activityInfo = mActivityInfo; 61 mInfo.activityInfo.applicationInfo = mApplicationInfo; 62 mContext = spy(RuntimeEnvironment.application); 63 doReturn(mPackageManager).when(mContext).getPackageManager(); 64 mIconFactory = IconDrawableFactory.newInstance(mContext); 65 } 66 67 @Test appEntry_shouldLoadIcon()68 public void appEntry_shouldLoadIcon() { 69 when(mPackageManager.loadUnbadgedItemIcon(mActivityInfo, mApplicationInfo)) 70 .thenReturn(mIcon); 71 final AppGridView.ActivityEntry activityEntry = 72 new AppGridView.ActivityEntry(mInfo, "label", mIconFactory); 73 74 assertThat(activityEntry.label).isEqualTo("label"); 75 assertThat(activityEntry.getIcon()).isNotNull(); 76 } 77 78 @Test appEntry_compare_shouldCompareIgnoreCase()79 public void appEntry_compare_shouldCompareIgnoreCase() { 80 final AppGridView.ActivityEntry entry1 = 81 new AppGridView.ActivityEntry(mInfo, "label", mIconFactory); 82 final AppGridView.ActivityEntry entry2 = 83 new AppGridView.ActivityEntry(mInfo, "LABEL", mIconFactory); 84 final AppGridView.ActivityEntry entry3 = 85 new AppGridView.ActivityEntry(mInfo, "label2", mIconFactory); 86 87 assertThat(entry1.compareTo(entry2)).isEqualTo(0); 88 assertThat(entry1.compareTo(entry3)).isNotEqualTo(0); 89 } 90 } 91