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.example.sampleleanbacklauncher.apps; 18 19 import android.content.Context; 20 import android.content.pm.ActivityInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.graphics.drawable.Drawable; 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import static junit.framework.Assert.assertEquals; 30 import static junit.framework.Assert.assertNotNull; 31 import static junit.framework.Assert.assertTrue; 32 import static org.mockito.Mockito.any; 33 import static org.mockito.Mockito.mock; 34 import static org.mockito.Mockito.when; 35 36 @RunWith(AndroidJUnit4.class) 37 public class LaunchItemTest { 38 createTestLaunchItem(String packageName, String name, Drawable banner, Drawable icon, CharSequence label, long priority)39 private LaunchItem createTestLaunchItem(String packageName, String name, Drawable banner, 40 Drawable icon, CharSequence label, long priority) { 41 final ActivityInfo activityInfo = mock(ActivityInfo.class); 42 when(activityInfo.loadBanner(any(PackageManager.class))).thenReturn(banner); 43 activityInfo.packageName = packageName; 44 activityInfo.name = name; 45 46 final ResolveInfo resolveInfo = mock(ResolveInfo.class); 47 when(resolveInfo.loadIcon(any(PackageManager.class))).thenReturn(icon); 48 when(resolveInfo.loadLabel(any(PackageManager.class))).thenReturn(label); 49 resolveInfo.activityInfo = activityInfo; 50 51 final Context context = mock(Context.class); 52 when(context.getPackageManager()).thenReturn(null); 53 54 return new LaunchItem(context, resolveInfo, priority); 55 } 56 57 @Test testGetIntent()58 public void testGetIntent() throws Exception { 59 final LaunchItem item = 60 createTestLaunchItem("com.example.testPackage", "com.example.testName", 61 null, null, null, 0); 62 assertNotNull(item.getIntent()); 63 assertNotNull(item.getIntent().getComponent()); 64 assertEquals("com.example.testPackage", item.getIntent().getComponent().getPackageName()); 65 assertEquals("com.example.testName", item.getIntent().getComponent().getClassName()); 66 } 67 68 @Test testCompareTo()69 public void testCompareTo() throws Exception { 70 final LaunchItem item = createTestLaunchItem("", "", null, null, "A label", 0); 71 final LaunchItem priorityItem = createTestLaunchItem("", "", null, null, "Z label", 1); 72 final LaunchItem alphaItem = createTestLaunchItem("", "", null, null, "Z label", 0); 73 74 assertTrue("priorityItem should sort before item", item.compareTo(priorityItem) > 0); 75 assertTrue("alphaItem should sort after item", item.compareTo(alphaItem) < 0); 76 assertTrue("item should sort equal to item", item.compareTo(item) == 0); 77 } 78 } 79