Home | History | Annotate | Download | only in appinfo
      1 /*
      2  * Copyright (C) 2018 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.appinfo;
     18 
     19 import static android.content.Intent.EXTRA_PACKAGE_NAME;
     20 import static com.google.common.truth.Truth.assertThat;
     21 import static org.mockito.Mockito.when;
     22 
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.pm.ActivityInfo;
     26 import android.content.pm.ApplicationInfo;
     27 import android.content.pm.ResolveInfo;
     28 import android.support.v7.preference.Preference;
     29 import android.support.v7.preference.PreferenceScreen;
     30 
     31 import com.android.settings.core.BasePreferenceController;
     32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.RuntimeEnvironment;
     40 import org.robolectric.Shadows;
     41 import org.robolectric.shadows.ShadowPackageManager;
     42 
     43 @RunWith(SettingsRobolectricTestRunner.class)
     44 public class TimeSpentInAppPreferenceControllerTest {
     45 
     46     private static final String TEST_KEY = "test_tey";
     47     private static final Intent TEST_INTENT = new Intent(
     48             TimeSpentInAppPreferenceController.SEE_TIME_IN_APP_TEMPLATE)
     49             .putExtra(EXTRA_PACKAGE_NAME, "com.android.settings");
     50 
     51     @Mock
     52     private PreferenceScreen mScreen;
     53 
     54     private Context mContext;
     55     private ShadowPackageManager mPackageManager;
     56     private TimeSpentInAppPreferenceController mController;
     57     private Preference mPreference;
     58 
     59     @Before
     60     public void setUp() {
     61         MockitoAnnotations.initMocks(this);
     62         mContext = RuntimeEnvironment.application;
     63         mPackageManager = Shadows.shadowOf(mContext.getPackageManager());
     64         mController = new TimeSpentInAppPreferenceController(mContext, TEST_KEY);
     65         mPreference = new Preference(mContext);
     66         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     67     }
     68 
     69     @Test
     70     public void noPackageName_shouldBeDisabled() {
     71         mController.setPackageName(null);
     72 
     73         assertThat(mController.getAvailabilityStatus())
     74                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
     75     }
     76 
     77     @Test
     78     public void noIntentHandler_shouldBeDisabled() {
     79         mController.setPackageName(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
     80 
     81         assertThat(mController.getAvailabilityStatus())
     82                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
     83     }
     84 
     85     @Test
     86     public void hasIntentHandler_notSystemApp_shouldBeDisabled() {
     87         mPackageManager.addResolveInfoForIntent(TEST_INTENT, new ResolveInfo());
     88         mController.setPackageName(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
     89 
     90         assertThat(mController.getAvailabilityStatus())
     91                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
     92 
     93     }
     94 
     95     @Test
     96     public void hasIntentHandler_resolvedToSystemApp_shouldBeAvailable() {
     97         final ResolveInfo info = new ResolveInfo();
     98         info.activityInfo = new ActivityInfo();
     99         info.activityInfo.applicationInfo = new ApplicationInfo();
    100         info.activityInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
    101         mPackageManager.addResolveInfoForIntent(TEST_INTENT, info);
    102         mController.setPackageName(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
    103 
    104         assertThat(mController.getAvailabilityStatus())
    105                 .isEqualTo(BasePreferenceController.AVAILABLE);
    106         mController.displayPreference(mScreen);
    107 
    108         final Intent intent = mPreference.getIntent();
    109         assertThat(intent.getAction()).isEqualTo(TEST_INTENT.getAction());
    110         assertThat(intent.getStringExtra(EXTRA_PACKAGE_NAME))
    111                 .isEqualTo(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
    112     }
    113 }
    114