• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.platform.test.flag.junit.host;
18 
19 import android.platform.test.flag.junit.CheckFlagsRule;
20 import android.platform.test.flag.junit.IFlagsValueProvider;
21 import android.platform.test.flag.util.FlagReadException;
22 
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.log.LogUtil;
25 
26 import com.google.common.cache.CacheBuilder;
27 import com.google.common.cache.CacheLoader;
28 import com.google.common.cache.LoadingCache;
29 
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.concurrent.ExecutionException;
33 import java.util.function.Supplier;
34 
35 /** A {@code IFlagsValueProvider} which provides flag values from host side. */
36 public class HostFlagsValueProvider implements IFlagsValueProvider {
37     /** The key is the device serial number. */
38     private static final LoadingCache<String, DeviceFlags> CACHED_DEVICE_FLAGS;
39 
40     /** The key is the device serial number. */
41     private static final Map<String, ITestDevice> TEST_DEVICES = new HashMap<>();
42 
43     static {
44         CacheLoader<String, DeviceFlags> cacheLoader =
45                 new CacheLoader<String, DeviceFlags>() {
46                     @Override
47                     public DeviceFlags load(String deviceSerial) throws FlagReadException {
48                         if (!TEST_DEVICES.containsKey(deviceSerial)) {
49                             throw new IllegalStateException(
50                                     String.format(
51                                             "No ITestDevice found for serial %s.", deviceSerial));
52                         }
53                         return DeviceFlags.createDeviceFlags(TEST_DEVICES.get(deviceSerial));
54                     }
55                 };
56 
57         CACHED_DEVICE_FLAGS = CacheBuilder.newBuilder().build(cacheLoader);
58     }
59 
60     private final Supplier<ITestDevice> mTestDeviceSupplier;
61     private DeviceFlags mDeviceFlags;
62 
HostFlagsValueProvider(Supplier<ITestDevice> testDeviceSupplier)63     HostFlagsValueProvider(Supplier<ITestDevice> testDeviceSupplier) {
64         mTestDeviceSupplier = testDeviceSupplier;
65     }
66 
createCheckFlagsRule(Supplier<ITestDevice> testDeviceSupplier)67     public static CheckFlagsRule createCheckFlagsRule(Supplier<ITestDevice> testDeviceSupplier) {
68         return new CheckFlagsRule(new HostFlagsValueProvider(testDeviceSupplier));
69     }
70 
71     /**
72      * Refreshes all flag values of a given device. Must be called when the device flags have been
73      * changed.
74      */
refreshFlagsCache(String serial)75     public static void refreshFlagsCache(String serial) throws FlagReadException {
76         Map<String, DeviceFlags> cachedDeviceFlagsMap = CACHED_DEVICE_FLAGS.asMap();
77         if (cachedDeviceFlagsMap.containsKey(serial)) {
78             cachedDeviceFlagsMap.get(serial).init(TEST_DEVICES.get(serial));
79         }
80     }
81 
82     @Override
setUp()83     public void setUp() throws FlagReadException {
84         ITestDevice testDevice = mTestDeviceSupplier.get();
85         TEST_DEVICES.put(testDevice.getSerialNumber(), testDevice);
86 
87         try {
88             mDeviceFlags = CACHED_DEVICE_FLAGS.get(testDevice.getSerialNumber());
89         } catch (ExecutionException e) {
90             throw new FlagReadException("ALL_FLAGS", e);
91         }
92     }
93 
94     @Override
getBoolean(String flag)95     public boolean getBoolean(String flag) throws FlagReadException {
96         String value = mDeviceFlags.getFlagValue(flag);
97 
98         if (value == null) {
99             LogUtil.CLog.i("Flag %s is not found, treating as false.", flag);
100             return false;
101         }
102 
103         if (!IFlagsValueProvider.isBooleanValue(value)) {
104             throw new FlagReadException(
105                     flag, String.format("Flag value %s is not a boolean", value));
106         }
107         return Boolean.valueOf(value);
108     }
109 }
110