Home | History | Annotate | Download | only in settingslib
      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.settingslib;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.ActivityInfo;
     23 import android.content.pm.ApplicationInfo;
     24 import android.content.pm.PackageManager;
     25 import android.content.pm.ResolveInfo;
     26 import android.content.res.Resources;
     27 import android.content.res.TypedArray;
     28 import android.provider.Settings;
     29 import android.view.MenuItem;
     30 import com.android.internal.R;
     31 
     32 import org.junit.Before;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 import org.mockito.Answers;
     36 import org.mockito.Mock;
     37 import org.mockito.MockitoAnnotations;
     38 import org.robolectric.RobolectricTestRunner;
     39 import org.robolectric.RuntimeEnvironment;
     40 import org.robolectric.annotation.Config;
     41 
     42 import static com.google.common.truth.Truth.assertThat;
     43 import static org.mockito.Matchers.any;
     44 import static org.mockito.Matchers.anyInt;
     45 import static org.mockito.Mockito.mock;
     46 import static org.mockito.Mockito.verify;
     47 import static org.mockito.Mockito.when;
     48 
     49 /**
     50  * Tests for {@link HelpUtils}.
     51  */
     52 @RunWith(RobolectricTestRunner.class)
     53 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     54 public class HelpUtilsTest {
     55     private static final String TEST_HELP_URL = "intent:#Intent;action=com.android.test;end";
     56     private static final String PACKAGE_NAME_KEY = "package-name-key";
     57     private static final String PACKAGE_NAME_VALUE = "package-name-value";
     58     private static final String HELP_INTENT_EXTRA_KEY = "help-intent-extra";
     59     private static final String HELP_INTENT_NAME_KEY = "help-intent-name";
     60     private static final String FEEDBACK_INTENT_EXTRA_KEY = "feedback-intent-extra";
     61     private static final String FEEDBACK_INTENT_NAME_KEY = "feedback-intent-name";
     62 
     63     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     64     private Context mContext;
     65     @Mock
     66     private Activity mActivity;
     67     @Mock
     68     private PackageManager mPackageManager;
     69 
     70 
     71     @Before
     72     public void setUp() {
     73         MockitoAnnotations.initMocks(this);
     74         when(mContext.getResources().getString(R.string.config_helpPackageNameKey))
     75                 .thenReturn(PACKAGE_NAME_KEY);
     76         when(mContext.getResources().getString(R.string.config_helpPackageNameValue))
     77                 .thenReturn(PACKAGE_NAME_VALUE);
     78         when(mContext.getResources().getString(R.string.config_helpIntentExtraKey))
     79                 .thenReturn(HELP_INTENT_EXTRA_KEY);
     80         when(mContext.getResources().getString(R.string.config_helpIntentNameKey))
     81                 .thenReturn(HELP_INTENT_NAME_KEY);
     82         when(mContext.getResources().getString(R.string.config_feedbackIntentExtraKey))
     83                 .thenReturn(FEEDBACK_INTENT_EXTRA_KEY);
     84         when(mContext.getResources().getString(R.string.config_feedbackIntentNameKey))
     85                 .thenReturn(FEEDBACK_INTENT_NAME_KEY);
     86         when(mActivity.getPackageManager()).thenReturn(mPackageManager);
     87 
     88 
     89     }
     90 
     91     @Test
     92     public void addIntentParameters_configTrue_argumentTrue() {
     93         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(true);
     94         Intent intent = new Intent();
     95 
     96         HelpUtils.addIntentParameters(
     97                 mContext, intent, null /* backupContext */, true /* sendPackageName */);
     98 
     99         assertThat(intent.getStringArrayExtra(HELP_INTENT_EXTRA_KEY)).asList()
    100                 .containsExactly(PACKAGE_NAME_KEY);
    101         assertThat(intent.getStringArrayExtra(HELP_INTENT_NAME_KEY)).asList()
    102                 .containsExactly(PACKAGE_NAME_VALUE);
    103         assertThat(intent.getStringArrayExtra(FEEDBACK_INTENT_EXTRA_KEY)).asList()
    104                 .containsExactly(PACKAGE_NAME_KEY);
    105         assertThat(intent.getStringArrayExtra(FEEDBACK_INTENT_NAME_KEY)).asList()
    106                 .containsExactly(PACKAGE_NAME_VALUE);
    107     }
    108 
    109     @Test
    110     public void addIntentParameters_configTrue_argumentFalse() {
    111         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(true);
    112         Intent intent = new Intent();
    113 
    114         HelpUtils.addIntentParameters(
    115                 mContext, intent, null /* backupContext */, false /* sendPackageName */);
    116 
    117         assertThat(intent.hasExtra(HELP_INTENT_EXTRA_KEY)).isFalse();
    118         assertThat(intent.hasExtra(HELP_INTENT_NAME_KEY)).isFalse();
    119         assertThat(intent.hasExtra(FEEDBACK_INTENT_EXTRA_KEY)).isFalse();
    120         assertThat(intent.hasExtra(FEEDBACK_INTENT_NAME_KEY)).isFalse();
    121     }
    122 
    123     @Test
    124     public void addIntentParameters_configFalse_argumentTrue() {
    125         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(false);
    126         Intent intent = new Intent();
    127 
    128         HelpUtils.addIntentParameters(
    129                 mContext, intent, null /* backupContext */, true /* sendPackageName */);
    130 
    131         assertThat(intent.hasExtra(HELP_INTENT_EXTRA_KEY)).isFalse();
    132         assertThat(intent.hasExtra(HELP_INTENT_NAME_KEY)).isFalse();
    133         assertThat(intent.hasExtra(FEEDBACK_INTENT_EXTRA_KEY)).isFalse();
    134         assertThat(intent.hasExtra(FEEDBACK_INTENT_NAME_KEY)).isFalse();
    135     }
    136 
    137     @Test
    138     public void addIntentParameters_configFalse_argumentFalse() {
    139         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(false);
    140         Intent intent = new Intent();
    141 
    142         HelpUtils.addIntentParameters(
    143                 mContext, intent, null /* backupContext */, false /* sendPackageName */);
    144 
    145         assertThat(intent.hasExtra(HELP_INTENT_EXTRA_KEY)).isFalse();
    146         assertThat(intent.hasExtra(HELP_INTENT_NAME_KEY)).isFalse();
    147         assertThat(intent.hasExtra(FEEDBACK_INTENT_EXTRA_KEY)).isFalse();
    148         assertThat(intent.hasExtra(FEEDBACK_INTENT_NAME_KEY)).isFalse();
    149     }
    150 
    151     @Test
    152     public void prepareHelpMenuItem_shouldShowIcon() {
    153         Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(),
    154                 Settings.Global.DEVICE_PROVISIONED, 1);
    155         final Resources res = mock(Resources.class);
    156         final ResolveInfo resolveInfo = new ResolveInfo();
    157         resolveInfo.activityInfo = new ActivityInfo();
    158         resolveInfo.activityInfo.applicationInfo = new ApplicationInfo();
    159         resolveInfo.activityInfo.applicationInfo.packageName = "pkg";
    160         resolveInfo.activityInfo.name = "name";
    161         final MenuItem item = mock(MenuItem.class);
    162 
    163 
    164         when(mActivity.getContentResolver())
    165                 .thenReturn(RuntimeEnvironment.application.getContentResolver());
    166         when(mActivity.getResources()).thenReturn(res);
    167         when(mActivity.obtainStyledAttributes(any(int[].class)))
    168                 .thenReturn(mock(TypedArray.class));
    169         when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
    170                 .thenReturn(resolveInfo);
    171 
    172         HelpUtils.prepareHelpMenuItem(mActivity, item, TEST_HELP_URL, "backup_url");
    173 
    174         verify(item).setVisible(true);
    175         verify(item).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    176     }
    177 }