1 /*
2  * Copyright (C) 2021 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.server.wm;
18 
19 import android.annotation.NonNull;
20 import android.view.DisplayInfo;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 
25 /**
26  * In-memory DisplayWindowSettingsProvider used in tests. Ensures no settings are read from or
27  * written to device-specific display settings files.
28  */
29 public final class TestDisplayWindowSettingsProvider extends DisplayWindowSettingsProvider {
30 
31     private final Map<String, SettingsEntry> mOverrideSettingsMap = new HashMap<>();
32 
33     @Override
34     @NonNull
getSettings(@onNull DisplayInfo info)35     public SettingsEntry getSettings(@NonNull DisplayInfo info) {
36         // Because no settings are read from settings files, there is no need to store base
37         // settings. Only override settings are necessary to track because they can be modified
38         // during tests (e.g. display size, ignore orientation requests).
39         return getOverrideSettings(info);
40     }
41 
42     @Override
43     @NonNull
getOverrideSettings(@onNull DisplayInfo info)44     public SettingsEntry getOverrideSettings(@NonNull DisplayInfo info) {
45         return new SettingsEntry(getOrCreateOverrideSettingsEntry(info));
46     }
47 
48     @Override
updateOverrideSettings(@onNull DisplayInfo info, @NonNull SettingsEntry overrides)49     public void updateOverrideSettings(@NonNull DisplayInfo info,
50             @NonNull SettingsEntry overrides) {
51         final SettingsEntry overrideSettings = getOrCreateOverrideSettingsEntry(info);
52         overrideSettings.setTo(overrides);
53     }
54 
55     @NonNull
getOrCreateOverrideSettingsEntry(DisplayInfo info)56     private SettingsEntry getOrCreateOverrideSettingsEntry(DisplayInfo info) {
57         final String identifier = getIdentifier(info);
58         SettingsEntry settings;
59         if ((settings = mOverrideSettingsMap.get(identifier)) != null) {
60             return settings;
61         }
62         settings = new SettingsEntry();
63         mOverrideSettingsMap.put(identifier, settings);
64         return settings;
65     }
66 
67     /**
68      * In {@link TestDisplayWindowSettingsProvider}, always use uniqueId as the identifier.
69      */
getIdentifier(DisplayInfo displayInfo)70     private static String getIdentifier(DisplayInfo displayInfo) {
71         return displayInfo.uniqueId;
72     }
73 }
74