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 org.mockito.Matchers.any; 20 import static org.mockito.Matchers.eq; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.app.Activity; 27 import android.content.Context; 28 import android.os.UserManager; 29 import android.support.v7.preference.PreferenceScreen; 30 import android.util.Pair; 31 32 import com.android.internal.logging.nano.MetricsProto; 33 import com.android.settings.testutils.FakeFeatureFactory; 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 import com.android.settings.widget.RadioButtonPreference; 36 import com.android.settingslib.applications.DefaultAppInfo; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Answers; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RuntimeEnvironment; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 public class DefaultAppPickerFragmentTest { 51 52 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 53 private Activity mActivity; 54 @Mock 55 private PreferenceScreen mScreen; 56 @Mock 57 private UserManager mUserManager; 58 59 private FakeFeatureFactory mFeatureFactory; 60 private TestFragment mFragment; 61 62 @Before 63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mFeatureFactory = FakeFeatureFactory.setupForTest(); 66 mFragment = spy(new TestFragment()); 67 68 when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 69 doReturn(mActivity).when(mFragment).getContext(); 70 doReturn(mScreen).when(mFragment).getPreferenceScreen(); 71 } 72 73 @Test 74 public void clickPreference_hasConfirmation_shouldShowConfirmation() { 75 mFragment.onAttach((Context) mActivity); 76 final RadioButtonPreference pref = 77 new RadioButtonPreference(RuntimeEnvironment.application); 78 pref.setKey("TEST"); 79 doReturn("confirmation_text").when(mFragment) 80 .getConfirmationMessage(any(DefaultAppInfo.class)); 81 doReturn(mActivity).when(mFragment).getActivity(); 82 83 mFragment.onRadioButtonClicked(pref); 84 } 85 86 @Test 87 public void onRadioButtonConfirmed_shouldLog() { 88 mFragment.onAttach((Context) mActivity); 89 mFragment.onRadioButtonConfirmed("test_pkg"); 90 91 verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class), 92 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_UPDATE_DEFAULT_APP), 93 eq("test_pkg"), 94 any(Pair.class)); 95 } 96 97 public static class TestFragment extends DefaultAppPickerFragment { 98 99 boolean setDefaultAppKeyCalled; 100 101 @Override 102 public int getMetricsCategory() { 103 return 0; 104 } 105 106 @Override 107 protected int getPreferenceScreenResId() { 108 return 0; 109 } 110 111 @Override 112 protected List<DefaultAppInfo> getCandidates() { 113 return new ArrayList<>(); 114 } 115 116 @Override 117 protected String getDefaultKey() { 118 return null; 119 } 120 121 @Override 122 protected boolean setDefaultKey(String key) { 123 setDefaultAppKeyCalled = true; 124 return true; 125 } 126 127 @Override 128 public Context getContext() { 129 return RuntimeEnvironment.application; 130 } 131 } 132 } 133