Home | History | Annotate | Download | only in defaultapps
      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.applications.defaultapps;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Matchers.any;
     21 import static org.mockito.Matchers.anyInt;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.pm.ResolveInfo;
     29 import android.os.UserManager;
     30 import android.support.v7.preference.Preference;
     31 
     32 import com.android.settings.R;
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 import com.android.settingslib.wrapper.PackageManagerWrapper;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.mockito.Answers;
     40 import org.mockito.Mock;
     41 import org.mockito.MockitoAnnotations;
     42 import org.robolectric.util.ReflectionHelpers;
     43 
     44 import java.util.Collections;
     45 
     46 @RunWith(SettingsRobolectricTestRunner.class)
     47 public class DefaultBrowserPreferenceControllerTest {
     48 
     49     @Mock
     50     private Context mContext;
     51     @Mock
     52     private UserManager mUserManager;
     53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     54     private PackageManagerWrapper mPackageManager;
     55 
     56     private DefaultBrowserPreferenceController mController;
     57 
     58     @Before
     59     public void setUp() {
     60         MockitoAnnotations.initMocks(this);
     61         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
     62 
     63         mController = new DefaultBrowserPreferenceController(mContext);
     64         ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
     65     }
     66 
     67     @Test
     68     public void isAvailable_noBrowser_shouldReturnFalse() {
     69         when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
     70                 .thenReturn(null);
     71         assertThat(mController.isAvailable()).isFalse();
     72     }
     73 
     74     @Test
     75     public void isAvailable_hasBrowser_shouldReturnTrue() {
     76         when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
     77             .thenReturn(Collections.singletonList(new ResolveInfo()));
     78         assertThat(mController.isAvailable()).isTrue();
     79     }
     80 
     81     @Test
     82     public void getSoleAppLabel_hasNoApp_shouldNotReturnLabel() {
     83         when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
     84                 .thenReturn(null);
     85         final Preference pref = mock(Preference.class);
     86 
     87         mController.updateState(pref);
     88         verify(pref).setSummary(R.string.app_list_preference_none);
     89     }
     90 
     91     @Test
     92     public void getDefaultApp_shouldGetDefaultBrowserPackage() {
     93         mController.getDefaultAppInfo();
     94 
     95         verify(mPackageManager).getDefaultBrowserPackageNameAsUser(anyInt());
     96     }
     97 
     98     @Test
     99     public void isBrowserDefault_onlyApp_shouldReturnTrue() {
    100         when(mPackageManager.getDefaultBrowserPackageNameAsUser(anyInt())).thenReturn(null);
    101         when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
    102                 .thenReturn(Collections.singletonList(new ResolveInfo()));
    103 
    104         assertThat(mController.isBrowserDefault("pkg", 0)).isTrue();
    105     }
    106 }
    107