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.server.wm;
18 
19 import static com.android.server.wm.testing.Assert.assertThrows;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.res.Resources;
25 import android.platform.test.annotations.Presubmit;
26 
27 import org.hamcrest.Matchers;
28 import org.junit.Assert;
29 import org.junit.Test;
30 
31 /**
32  * Build/Install/Run:
33  *  atest WmTests:DisplayAreaProviderTest
34  */
35 @Presubmit
36 public class DisplayAreaProviderTest {
37 
38     @Test
testFromResources_emptyProvider()39     public void testFromResources_emptyProvider() {
40         Assert.assertThat(DisplayAreaPolicy.Provider.fromResources(resourcesWithProvider("")),
41                 Matchers.instanceOf(DisplayAreaPolicy.DefaultProvider.class));
42     }
43 
44     @Test
testFromResources_nullProvider()45     public void testFromResources_nullProvider() {
46         Assert.assertThat(DisplayAreaPolicy.Provider.fromResources(resourcesWithProvider(null)),
47                 Matchers.instanceOf(DisplayAreaPolicy.DefaultProvider.class));
48     }
49 
50     @Test
testFromResources_customProvider()51     public void testFromResources_customProvider() {
52         Assert.assertThat(DisplayAreaPolicy.Provider.fromResources(resourcesWithProvider(
53                 TestProvider.class.getName())), Matchers.instanceOf(TestProvider.class));
54     }
55 
56     @Test
testFromResources_badProvider_notImplementingProviderInterface()57     public void testFromResources_badProvider_notImplementingProviderInterface() {
58         assertThrows(IllegalStateException.class, () -> {
59             DisplayAreaPolicy.Provider.fromResources(resourcesWithProvider(
60                     Object.class.getName()));
61         });
62     }
63 
64     @Test
testFromResources_badProvider_doesntExist()65     public void testFromResources_badProvider_doesntExist() {
66         assertThrows(IllegalStateException.class, () -> {
67             DisplayAreaPolicy.Provider.fromResources(resourcesWithProvider(
68                     "com.android.wmtests.nonexistent.Provider"));
69         });
70     }
71 
resourcesWithProvider(String provider)72     private static Resources resourcesWithProvider(String provider) {
73         Resources mock = mock(Resources.class);
74         when(mock.getString(
75                 com.android.internal.R.string.config_deviceSpecificDisplayAreaPolicyProvider))
76                 .thenReturn(provider);
77         return mock;
78     }
79 
80     static class TestProvider implements DisplayAreaPolicy.Provider {
81 
82         @Override
instantiate(WindowManagerService wmService, DisplayContent content, RootDisplayArea root, DisplayArea.Tokens imeContainer)83         public DisplayAreaPolicy instantiate(WindowManagerService wmService, DisplayContent content,
84                 RootDisplayArea root, DisplayArea.Tokens imeContainer) {
85             throw new RuntimeException("test stub");
86         }
87     }
88 }
89