Home | History | Annotate | Download | only in widget
      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 android.app.Activity;
     20 import android.content.Context;
     21 import android.os.UserManager;
     22 import android.support.v7.preference.PreferenceScreen;
     23 
     24 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     25 import com.android.settings.TestConfig;
     26 import com.android.settings.applications.defaultapps.DefaultAppInfo;
     27 import com.android.settings.testutils.FakeFeatureFactory;
     28 
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.mockito.Answers;
     33 import org.mockito.Mock;
     34 import org.mockito.MockitoAnnotations;
     35 import org.robolectric.RuntimeEnvironment;
     36 import org.robolectric.annotation.Config;
     37 
     38 import java.util.ArrayList;
     39 import java.util.List;
     40 
     41 import static com.google.common.truth.Truth.assertThat;
     42 import static org.mockito.Mockito.doReturn;
     43 import static org.mockito.Mockito.spy;
     44 import static org.mockito.Mockito.verify;
     45 import static org.mockito.Mockito.when;
     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
     62     public void setUp() {
     63         MockitoAnnotations.initMocks(this);
     64         FakeFeatureFactory.setupForTest(mActivity);
     65         mFragment = spy(new TestFragment());
     66 
     67         when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
     68         doReturn(mActivity).when(mFragment).getContext();
     69         doReturn(mScreen).when(mFragment).getPreferenceScreen();
     70     }
     71 
     72     @Test
     73     public void onAttach_userIsInitialized() {
     74         mFragment.onAttach((Context) mActivity);
     75 
     76         verify(mActivity).getSystemService(Context.USER_SERVICE);
     77     }
     78 
     79     @Test
     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
     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 RadioButtonPickerFragment {
    103 
    104         boolean setDefaultKeyCalled;
    105 
    106         @Override
    107         public int getMetricsCategory() {
    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             setDefaultKeyCalled = true;
    124             return true;
    125         }
    126 
    127         @Override
    128         public Context getContext() {
    129             return RuntimeEnvironment.application;
    130         }
    131     }
    132 }
    133