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;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.car.Car;
22 import android.car.CarFeatures;
23 import android.hardware.automotive.vehicle.VehicleProperty;
24 import android.util.Log;
25 
26 import androidx.test.annotation.UiThreadTest;
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 import androidx.test.filters.MediumTest;
29 
30 import com.android.car.hal.test.AidlVehiclePropValueBuilder;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 import java.util.List;
36 
37 @RunWith(AndroidJUnit4.class)
38 @MediumTest
39 public class CarFeatureControllerTest extends MockedCarTestBase {
40     private static final String TAG = CarFeatureControllerTest.class.getSimpleName();
41     private static final String[] ENABLED_OPTIONAL_FEATURES = {
42             CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE,
43             Car.STORAGE_MONITORING_SERVICE
44     };
45     private String mDisabledOptionalFeatures = "";
46 
47     @Override
configureMockedHal()48     protected void configureMockedHal() {
49         Log.i(TAG, "mDisabledOptionalFeatures:" + mDisabledOptionalFeatures);
50         addAidlProperty(VehicleProperty.DISABLED_OPTIONAL_FEATURES,
51                 AidlVehiclePropValueBuilder.newBuilder(VehicleProperty.DISABLED_OPTIONAL_FEATURES)
52                         .setStringValue(mDisabledOptionalFeatures)
53                         .build());
54     }
55 
56     @Override
configureResourceOverrides(MockResources resources)57     protected void configureResourceOverrides(MockResources resources) {
58         super.configureResourceOverrides(resources);
59         resources.overrideResource(com.android.car.R.array.config_allowed_optional_car_features,
60                 ENABLED_OPTIONAL_FEATURES);
61     }
62 
63     @Override
setUp()64     public void setUp() throws Exception {
65         // Do nothing so that we can call super.setUp in test itself.
66     }
67 
68     @Test
69     @UiThreadTest
testParsingVhalEmptyList()70     public void testParsingVhalEmptyList() throws Exception {
71         super.setUp();
72         CarFeatureController featureController = CarLocalServices.getService(
73                 CarFeatureController.class);
74         assertThat(featureController).isNotNull();
75         List<String> disabledFeatures = featureController.getDisabledFeaturesFromVhal();
76         assertThat(disabledFeatures).isEmpty();
77     }
78 
79     @Test
80     @UiThreadTest
testParsingVhalMultipleEntries()81     public void testParsingVhalMultipleEntries() throws Exception {
82         String[] disabledFeaturesExpected = {"com.aaa", "com.bbb"};
83         mDisabledOptionalFeatures = String.join(",", disabledFeaturesExpected);
84         super.setUp();
85         CarFeatureController featureController = CarLocalServices.getService(
86                 CarFeatureController.class);
87         assertThat(featureController).isNotNull();
88         List<String> disabledFeatures = featureController.getDisabledFeaturesFromVhal();
89         assertThat(disabledFeatures).hasSize(disabledFeaturesExpected.length);
90         for (String feature: disabledFeaturesExpected) {
91             assertThat(disabledFeatures).contains(feature);
92         }
93     }
94 
95     @Test
96     @UiThreadTest
testUserNoticeDisabledFromVhal()97     public void testUserNoticeDisabledFromVhal() throws Exception {
98         mDisabledOptionalFeatures = CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE;
99         super.setUp();
100         CarFeatureController featureController = CarLocalServices.getService(
101                 CarFeatureController.class);
102         assertThat(featureController).isNotNull();
103         List<String> disabledFeatures = featureController.getDisabledFeaturesFromVhal();
104         assertThat(disabledFeatures).contains(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE);
105         assertThat(featureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE))
106                 .isFalse();
107         assertThat(featureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)).isTrue();
108     }
109 }
110