1 /*
2  * Copyright (C) 2022 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.dx.mockito.inline.extended.ExtendedMockito.doReturn;
20 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.ArgumentMatchers.anyString;
25 
26 import android.app.GameManagerInternal;
27 import android.platform.test.annotations.Presubmit;
28 
29 import androidx.test.filters.SmallTest;
30 
31 import com.android.server.LocalServices;
32 
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 
37 /**
38  * Tests for the {@link CompatModePackages} class.
39  *
40  * Build/Install/Run:
41  * atest WmTests:CompatModePackagesTests
42  */
43 @SmallTest
44 @Presubmit
45 public class CompatModePackagesTests extends SystemServiceTestsBase {
46     ActivityTaskManagerService mAtm;
47     GameManagerInternal mGm;
48     static final String TEST_PACKAGE = "compat.mode.packages";
49     static final int TEST_USER_ID = 1;
50 
51     @Before
setUp()52     public void setUp() {
53         mAtm = mSystemServicesTestRule.getActivityTaskManagerService();
54         mGm = mock(GameManagerInternal.class);
55     }
56 
57     @After
tearDown()58     public void tearDown() {
59         LocalServices.removeServiceForTest(GameManagerInternal.class);
60     }
61 
62     @Test
testGetCompatScale_gameManagerReturnsPositive()63     public void testGetCompatScale_gameManagerReturnsPositive() {
64         LocalServices.addService(GameManagerInternal.class, mGm);
65         float scale = 0.25f;
66         doReturn(scale).when(mGm).getResolutionScalingFactor(anyString(), anyInt());
67         assertEquals(mAtm.mCompatModePackages.getCompatScale(TEST_PACKAGE, TEST_USER_ID), 1 / scale,
68                 0.01f);
69     }
70 
71     @Test
testGetCompatScale_gameManagerReturnsZero()72     public void testGetCompatScale_gameManagerReturnsZero() {
73         LocalServices.addService(GameManagerInternal.class, mGm);
74         float scale = 0f;
75         doReturn(scale).when(mGm).getResolutionScalingFactor(anyString(), anyInt());
76         assertEquals(mAtm.mCompatModePackages.getCompatScale(TEST_PACKAGE, TEST_USER_ID), 1f,
77                 0.01f);
78     }
79 
80     @Test
testGetCompatScale_gameManagerReturnsNegative()81     public void testGetCompatScale_gameManagerReturnsNegative() {
82         LocalServices.addService(GameManagerInternal.class, mGm);
83         float scale = -1f;
84         doReturn(scale).when(mGm).getResolutionScalingFactor(anyString(), anyInt());
85         assertEquals(mAtm.mCompatModePackages.getCompatScale(TEST_PACKAGE, TEST_USER_ID), 1f,
86                 0.01f);
87     }
88 
89     @Test
testGetCompatScale_noGameManager()90     public void testGetCompatScale_noGameManager() {
91         assertEquals(mAtm.mCompatModePackages.getCompatScale(TEST_PACKAGE, TEST_USER_ID), 1f,
92                 0.01f);
93     }
94 
95 }
96