Home | History | Annotate | Download | only in common
      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.managedprovisioning.common;
     18 
     19 import static org.mockito.Mockito.when;
     20 
     21 import android.content.Context;
     22 import android.content.pm.ApplicationInfo;
     23 import android.content.pm.PackageManager;
     24 import android.content.pm.PackageManager.NameNotFoundException;
     25 import android.graphics.drawable.Drawable;
     26 import android.graphics.drawable.ColorDrawable;
     27 import android.test.AndroidTestCase;
     28 import android.test.suitebuilder.annotation.SmallTest;
     29 
     30 import org.mockito.Mock;
     31 import org.mockito.MockitoAnnotations;
     32 
     33 @SmallTest
     34 public class MdmPackageInfoTest extends AndroidTestCase {
     35     private final static Drawable TEST_DRAWABLE = new ColorDrawable(0);
     36     private final static String TEST_PACKAGE_NAME = "com.test.mdm";
     37     private final static String TEST_LABEL = "Test app";
     38 
     39     @Mock private Context mockContext;
     40     @Mock private PackageManager mockPackageManager;
     41 
     42     private final ApplicationInfo mApplicationInfo = new ApplicationInfo();
     43 
     44     @Override
     45     public void setUp() throws Exception {
     46         // this is necessary for mockito to work
     47         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
     48 
     49         MockitoAnnotations.initMocks(this);
     50 
     51         when(mockContext.getPackageManager()).thenReturn(mockPackageManager);
     52         when(mockPackageManager.getApplicationInfo(TEST_PACKAGE_NAME, 0))
     53                 .thenReturn(mApplicationInfo);
     54         when(mockPackageManager.getApplicationIcon(TEST_PACKAGE_NAME)).thenReturn(TEST_DRAWABLE);
     55         when(mockPackageManager.getApplicationLabel(mApplicationInfo)).thenReturn(TEST_LABEL);
     56     }
     57 
     58     public void testConstructor() {
     59         // GIVEN an app icon and an app label
     60         // WHEN MdmPackageInfo is constructed
     61         MdmPackageInfo mdmInfo = new MdmPackageInfo(TEST_DRAWABLE, TEST_LABEL);
     62         // THEN the app icon and app label are stored in the MdmPackageInfo object
     63         assertSame(TEST_DRAWABLE, mdmInfo.packageIcon);
     64         assertEquals(TEST_LABEL, mdmInfo.appLabel);
     65     }
     66 
     67     public void testCreateFromPackageName() {
     68         // GIVEN a package name
     69         // WHEN MdmPackageInfo is created from package name
     70         MdmPackageInfo mdmInfo = MdmPackageInfo.createFromPackageName(mockContext,
     71                 TEST_PACKAGE_NAME);
     72         // THEN the app icon and app label are loaded correctly from the package manager
     73         assertSame(TEST_DRAWABLE, mdmInfo.packageIcon);
     74         assertEquals(TEST_LABEL, mdmInfo.appLabel);
     75     }
     76 
     77     public void testCreateFromPackageName_NameNotFoundException() throws Exception {
     78         // GIVEN that the package does not exist on the device
     79         // WHEN MdmPackageInfo is created from package name
     80         when(mockPackageManager.getApplicationInfo(TEST_PACKAGE_NAME, 0))
     81                 .thenThrow(new NameNotFoundException());
     82         // THEN null is returned
     83         assertNull(MdmPackageInfo.createFromPackageName(mockContext, TEST_PACKAGE_NAME));
     84     }
     85 }
     86