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.widget;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.Activity;
26 import android.content.Context;
27 import android.os.UserManager;
28 import android.support.v7.preference.PreferenceScreen;
29 
30 import com.android.settings.SettingsRobolectricTestRunner;
31 import com.android.settings.TestConfig;
32 import com.android.settings.applications.defaultapps.DefaultAppInfo;
33 import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Answers;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.annotation.Config;
43 
44 import java.util.ArrayList;
45 import java.util.List;
46 
47 @RunWith(SettingsRobolectricTestRunner.class)
48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
49 public class RadioButtonPickerFragmentTest {
50 
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 TestFragment mFragment;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         mFragment = spy(new TestFragment());
65 
66         when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
67         doReturn(mActivity).when(mFragment).getContext();
68         doReturn(mScreen).when(mFragment).getPreferenceScreen();
69     }
70 
71     @Test
onAttach_userIsInitialized()72     public void onAttach_userIsInitialized() {
73         mFragment.onAttach((Context) mActivity);
74 
75         verify(mActivity).getPackageManager();
76         verify(mActivity).getSystemService(Context.USER_SERVICE);
77     }
78 
79     @Test
displaySingleOption_shouldSelectRadioButton()80     public void displaySingleOption_shouldSelectRadioButton() {
81         final RadioButtonPreference pref =
82                 new RadioButtonPreference(RuntimeEnvironment.application);
83         when(mScreen.getPreferenceCount()).thenReturn(1);
84         when(mScreen.getPreference(0)).thenReturn(pref);
85 
86         mFragment.mayCheckOnlyRadioButton();
87 
88         assertThat(pref.isChecked()).isTrue();
89     }
90 
91     @Test
clickPreference_shouldConfirm()92     public void clickPreference_shouldConfirm() {
93         final RadioButtonPreference pref =
94                 new RadioButtonPreference(RuntimeEnvironment.application);
95         pref.setKey("TEST");
96 
97         mFragment.onRadioButtonClicked(pref);
98 
99         assertThat(mFragment.setDefaultKeyCalled).isTrue();
100     }
101 
102     public static class TestFragment extends DefaultAppPickerFragment {
103 
104         boolean setDefaultKeyCalled;
105 
106         @Override
getMetricsCategory()107         public int getMetricsCategory() {
108             return 0;
109         }
110 
111         @Override
getCandidates()112         protected List<DefaultAppInfo> getCandidates() {
113             return new ArrayList<>();
114         }
115 
116         @Override
getDefaultKey()117         protected String getDefaultKey() {
118             return null;
119         }
120 
121         @Override
setDefaultKey(String key)122         protected boolean setDefaultKey(String key) {
123             setDefaultKeyCalled = true;
124             return true;
125         }
126 
127         @Override
getContext()128         public Context getContext() {
129             return RuntimeEnvironment.application;
130         }
131     }
132 }
133