1 /*
2  * Copyright (C) 2023 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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doNothing;
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.content.Context;
27 
28 import androidx.fragment.app.FragmentActivity;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Spy;
39 import org.mockito.junit.MockitoJUnit;
40 import org.mockito.junit.MockitoRule;
41 import org.robolectric.Robolectric;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.annotation.Config;
44 
45 /** Tests for {@link ViewAllBluetoothDevicesPreferenceController}. */
46 @RunWith(RobolectricTestRunner.class)
47 @Config(shadows = {
48         com.android.settings.testutils.shadow.ShadowFragment.class,
49 })
50 public class ViewAllBluetoothDevicesPreferenceControllerTest {
51 
52     @Rule
53     public MockitoRule mocks = MockitoJUnit.rule();
54     private final Context mContext = ApplicationProvider.getApplicationContext();
55     private final Preference mPreference = new Preference(mContext);
56     private final String TEST_KEY = "test_key";
57 
58     @Spy
59     private HearingDevicePairingFragment mFragment = new HearingDevicePairingFragment();
60     private FragmentActivity mActivity;
61     @Mock
62     private PreferenceScreen mScreen;
63     private ViewAllBluetoothDevicesPreferenceController mController;
64 
65     @Before
setUp()66     public void setUp() {
67         mActivity = Robolectric.setupActivity(FragmentActivity.class);
68         when(mFragment.getContext()).thenReturn(mContext);
69         when(mFragment.getActivity()).thenReturn(mActivity);
70 
71         mController = spy(new ViewAllBluetoothDevicesPreferenceController(mActivity, TEST_KEY));
72         mController.init(mFragment);
73         mController.displayPreference(mScreen);
74     }
75 
76     @Test
handlePreferenceTreeClick_expectedPreference_launchConnectedDevicePage()77     public void handlePreferenceTreeClick_expectedPreference_launchConnectedDevicePage() {
78         doNothing().when(mController).launchConnectedDevicePage();
79         mPreference.setKey(TEST_KEY);
80 
81         boolean status = mController.handlePreferenceTreeClick(mPreference);
82 
83         verify(mController).launchConnectedDevicePage();
84         assertThat(status).isTrue();
85     }
86 }
87