1 /*
2  * Copyright (C) 2020 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.car.settings.common;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import androidx.preference.Preference;
22 
23 public class PreferenceControllerTestUtil {
24 
25     /**
26      * Associates a PreferenceController with its Preference.
27      */
assignPreference(PreferenceController controller, Preference preference)28     public static void assignPreference(PreferenceController controller, Preference preference) {
29         controller.setPreference(preference);
30     }
31 
32     /**
33      * Asserts that the test availability status matches the expected result
34      *
35      * @param actualValue availability returned by the test
36      * @param expectedValue expected availability
37      */
assertAvailability(int actualValue, int expectedValue)38     public static void assertAvailability(int actualValue, int expectedValue) {
39         assertWithMessage("controller availability (%s=%s, %s=%s)",
40                 actualValue, availabilityToString(actualValue),
41                 expectedValue, availabilityToString(expectedValue))
42                 .that(actualValue).isEqualTo(expectedValue);
43     }
44 
availabilityToString(int value)45     private static String availabilityToString(int value) {
46         switch (value) {
47             case PreferenceController.AVAILABLE:
48                 return "AVAILABLE";
49             case PreferenceController.AVAILABLE_FOR_VIEWING:
50                 return "AVAILABLE_FOR_VIEWING";
51             case PreferenceController.CONDITIONALLY_UNAVAILABLE:
52                 return "CONDITIONALLY_UNAVAILABLE";
53             case PreferenceController.DISABLED_FOR_PROFILE:
54                 return "DISABLED_FOR_PROFILE";
55             case PreferenceController.UNSUPPORTED_ON_DEVICE:
56                 return "UNSUPPORTED_ON_DEVICE";
57             default:
58                 return "INVALID-" + value;
59         }
60     }
61 }
62