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 android.app; 18 19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 20 21 import static junit.framework.Assert.assertEquals; 22 import static junit.framework.Assert.assertNotNull; 23 import static junit.framework.Assert.assertNull; 24 25 import static org.junit.Assume.assumeNotNull; 26 27 import android.content.Context; 28 import android.content.pm.PackageManager; 29 import android.platform.test.annotations.Presubmit; 30 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 /** 39 * Unit tests for {@link android.app.GameManager}. 40 */ 41 @RunWith(AndroidJUnit4.class) 42 @SmallTest 43 @Presubmit 44 public final class GameManagerTests { 45 protected Context mContext; 46 private GameManager mGameManager; 47 private String mPackageName; 48 49 @Before setUp()50 public void setUp() { 51 mContext = getInstrumentation().getContext(); 52 mGameManager = mContext.getSystemService(GameManager.class); 53 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) { 54 assumeNotNull(mGameManager); 55 } 56 mPackageName = mContext.getPackageName(); 57 58 // Reset the Game Mode for the test app, since it persists across invocations. 59 mGameManager.setGameMode(mPackageName, GameManager.GAME_MODE_STANDARD); 60 } 61 62 @Test testPublicApiGameModeGetterSetter()63 public void testPublicApiGameModeGetterSetter() { 64 assertEquals(GameManager.GAME_MODE_STANDARD, 65 mGameManager.getGameMode()); 66 67 mGameManager.setGameMode(mPackageName, GameManager.GAME_MODE_PERFORMANCE); 68 assertEquals(GameManager.GAME_MODE_PERFORMANCE, 69 mGameManager.getGameMode()); 70 71 mGameManager.setGameMode(mPackageName, GameManager.GAME_MODE_CUSTOM); 72 assertEquals(GameManager.GAME_MODE_CUSTOM, 73 mGameManager.getGameMode()); 74 } 75 76 @Test testPrivilegedGameModeGetterSetter()77 public void testPrivilegedGameModeGetterSetter() { 78 assertEquals(GameManager.GAME_MODE_STANDARD, 79 mGameManager.getGameMode(mPackageName)); 80 81 mGameManager.setGameMode(mPackageName, GameManager.GAME_MODE_PERFORMANCE); 82 assertEquals(GameManager.GAME_MODE_PERFORMANCE, 83 mGameManager.getGameMode(mPackageName)); 84 85 mGameManager.setGameMode(mPackageName, GameManager.GAME_MODE_CUSTOM); 86 assertEquals(GameManager.GAME_MODE_CUSTOM, 87 mGameManager.getGameMode(mPackageName)); 88 } 89 90 @Test testUpdateCustomGameModeConfiguration()91 public void testUpdateCustomGameModeConfiguration() { 92 GameModeInfo gameModeInfo = mGameManager.getGameModeInfo(mPackageName); 93 assertNotNull(gameModeInfo); 94 assertNull(gameModeInfo.getGameModeConfiguration(GameManager.GAME_MODE_CUSTOM)); 95 96 GameModeConfiguration supportedFpsConfig = 97 new GameModeConfiguration.Builder().setFpsOverride( 98 60).setScalingFactor(0.5f).build(); 99 mGameManager.updateCustomGameModeConfiguration(mPackageName, supportedFpsConfig); 100 gameModeInfo = mGameManager.getGameModeInfo(mPackageName); 101 assertNotNull(gameModeInfo); 102 assertEquals(supportedFpsConfig, 103 gameModeInfo.getGameModeConfiguration(GameManager.GAME_MODE_CUSTOM)); 104 } 105 } 106