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 android.app.Activity; 20 import android.content.Context; 21 import android.os.UserManager; 22 import android.provider.Settings; 23 24 import com.android.settings.SettingsRobolectricTestRunner; 25 import com.android.settings.TestConfig; 26 import com.android.settings.applications.PackageManagerWrapper; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Answers; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 import org.robolectric.util.ReflectionHelpers; 37 38 import static com.google.common.truth.Truth.assertThat; 39 import static org.mockito.Mockito.doReturn; 40 import static org.mockito.Mockito.spy; 41 import static org.mockito.Mockito.when; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 45 public class DefaultEmergencyPickerTest { 46 47 private static final String TEST_APP_KEY = "test_app"; 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private Activity mActivity; 51 @Mock 52 private UserManager mUserManager; 53 @Mock 54 private PackageManagerWrapper mPackageManager; 55 56 private DefaultEmergencyPicker mPicker; 57 58 @Before 59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 62 63 mPicker = spy(new DefaultEmergencyPicker()); 64 mPicker.onAttach((Context) mActivity); 65 66 ReflectionHelpers.setField(mPicker, "mPm", mPackageManager); 67 68 doReturn(RuntimeEnvironment.application).when(mPicker).getContext(); 69 } 70 71 @Test 72 public void setDefaultAppKey_shouldUpdateDefault() { 73 assertThat(mPicker.setDefaultKey(TEST_APP_KEY)).isTrue(); 74 assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY); 75 } 76 77 @Test 78 public void getDefaultAppKey_shouldReturnDefault() { 79 Settings.Secure.putString(RuntimeEnvironment.application.getContentResolver(), 80 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION, 81 TEST_APP_KEY); 82 83 assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY); 84 } 85 } 86