• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 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 package com.android.settings.bluetooth
17 
18 import android.content.Context
19 import androidx.fragment.app.FragmentActivity
20 import androidx.fragment.app.testing.EmptyFragmentActivity
21 import androidx.preference.Preference
22 import androidx.test.core.app.ActivityScenario
23 import androidx.test.core.app.ApplicationProvider
24 import com.google.common.truth.Truth.assertThat
25 import org.junit.Before
26 import org.junit.Rule
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.mockito.Mockito.spy
30 import org.mockito.junit.MockitoJUnit
31 import org.mockito.junit.MockitoRule
32 import org.robolectric.RobolectricTestRunner
33 
34 @RunWith(RobolectricTestRunner::class)
35 class BluetoothDetailsFragmentTest {
36     @get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule()
37 
38     private lateinit var activity: FragmentActivity
39     private lateinit var fragment: TestConfigurableFragment
40     private lateinit var context: Context
41 
42     @Before
43     fun setUp() {
44         context = spy(ApplicationProvider.getApplicationContext<Context>())
45     }
46 
47     @Test
48     fun setPreferenceDisplayOrder_null_unchanged() = buildFragment {
49         fragment.preferenceScreen.addPreference(Preference(context).apply { key = "key1" })
50         fragment.preferenceScreen.addPreference(Preference(context).apply { key = "key2" })
51 
52         fragment.setPreferenceDisplayOrder(null)
53 
54         assertThat(this.displayedKeys).containsExactly("key1", "key2")
55     }
56 
57     @Test
58     fun setPreferenceDisplayOrder_hideItem() = buildFragment {
59         fragment.preferenceScreen.addPreference(Preference(context).apply { key = "key1" })
60         fragment.preferenceScreen.addPreference(Preference(context).apply { key = "key2" })
61 
62         fragment.setPreferenceDisplayOrder(mutableListOf("key2"))
63 
64         assertThat(this.displayedKeys).containsExactly("key2")
65     }
66 
67     @Test
68     fun setPreferenceDisplayOrder_hideAndReShownItem() = buildFragment {
69         fragment.preferenceScreen.addPreference(Preference(context).apply { key = "key1" })
70         fragment.preferenceScreen.addPreference(Preference(context).apply { key = "key2" })
71 
72         fragment.setPreferenceDisplayOrder(mutableListOf("key2"))
73         fragment.setPreferenceDisplayOrder(mutableListOf("key2", "key1"))
74 
75         assertThat(this.displayedKeys).containsExactly("key2", "key1")
76     }
77 
78     private fun buildFragment(r: (() -> Unit)) {
79         ActivityScenario.launch(EmptyFragmentActivity::class.java).use { activityScenario ->
80             activityScenario.onActivity { activity: EmptyFragmentActivity ->
81                 this@BluetoothDetailsFragmentTest.activity = activity
82                 fragment = TestConfigurableFragment()
83                 activity.supportFragmentManager.beginTransaction().add(fragment, null).commitNow()
84                 fragment.setPreferenceScreen(
85                     fragment.preferenceManager.createPreferenceScreen(context)
86                 )
87                 r.invoke()
88             }
89         }
90     }
91 
92     private val displayedKeys: List<String>
93         get() {
94             val keys: MutableList<String> = mutableListOf()
95             for (i in 0..<fragment.preferenceScreen.preferenceCount) {
96                 if (fragment.preferenceScreen.getPreference(i).isVisible) {
97                     keys.add(fragment.preferenceScreen.getPreference(i).key)
98                 }
99             }
100             return keys
101         }
102 
103     class TestConfigurableFragment : BluetoothDetailsConfigurableFragment() {
104         protected override fun getPreferenceScreenResId(): Int {
105             return 0
106         }
107 
108         override fun getLogTag(): String {
109             return "TAG"
110         }
111 
112         override fun getMetricsCategory(): Int {
113             return 0
114         }
115     }
116 }
117