1 /*
2 * Copyright 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 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <gui/LayerMetadata.h>
23 #include <gui/SurfaceComposerClient.h>
24 #include <log/log.h>
25
26 #include "TestableSurfaceFlinger.h"
27 #include "mock/DisplayHardware/MockComposer.h"
28
29 namespace android {
30
31 using testing::_;
32 using testing::Mock;
33 using testing::Return;
34
35 using gui::GameMode;
36 using gui::LayerMetadata;
37
38 class GameModeTest : public testing::Test {
39 public:
GameModeTest()40 GameModeTest() {
41 const ::testing::TestInfo* const test_info =
42 ::testing::UnitTest::GetInstance()->current_test_info();
43 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
44 mFlinger.setupMockScheduler();
45 setupComposer();
46 }
47
~GameModeTest()48 ~GameModeTest() {
49 const ::testing::TestInfo* const test_info =
50 ::testing::UnitTest::GetInstance()->current_test_info();
51 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
52 }
53
createLayer()54 sp<Layer> createLayer() {
55 sp<Client> client;
56 LayerCreationArgs args(mFlinger.flinger(), client, "layer", 0, LayerMetadata());
57 return sp<Layer>::make(args);
58 }
59
setupComposer()60 void setupComposer() {
61 mComposer = new Hwc2::mock::Composer();
62 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
63
64 Mock::VerifyAndClear(mComposer);
65 }
66
67 // Mocks the behavior of applying a transaction from WMShell
setGameModeMetadata(sp<Layer> layer,GameMode gameMode)68 void setGameModeMetadata(sp<Layer> layer, GameMode gameMode) {
69 mLayerMetadata.setInt32(gui::METADATA_GAME_MODE, static_cast<int32_t>(gameMode));
70 layer->setMetadata(mLayerMetadata);
71 layer->setGameModeForTree(gameMode);
72 }
73
74 TestableSurfaceFlinger mFlinger;
75 Hwc2::mock::Composer* mComposer = nullptr;
76 client_cache_t mClientCache;
77 LayerMetadata mLayerMetadata;
78 };
79
TEST_F(GameModeTest,SetGameModeSetsForAllCurrentChildren)80 TEST_F(GameModeTest, SetGameModeSetsForAllCurrentChildren) {
81 sp<Layer> rootLayer = createLayer();
82 sp<Layer> childLayer1 = createLayer();
83 sp<Layer> childLayer2 = createLayer();
84 rootLayer->addChild(childLayer1);
85 rootLayer->addChild(childLayer2);
86 rootLayer->setGameModeForTree(GameMode::Performance);
87
88 EXPECT_EQ(rootLayer->getGameMode(), GameMode::Performance);
89 EXPECT_EQ(childLayer1->getGameMode(), GameMode::Performance);
90 EXPECT_EQ(childLayer2->getGameMode(), GameMode::Performance);
91 }
92
TEST_F(GameModeTest,AddChildAppliesGameModeFromParent)93 TEST_F(GameModeTest, AddChildAppliesGameModeFromParent) {
94 sp<Layer> rootLayer = createLayer();
95 sp<Layer> childLayer = createLayer();
96 rootLayer->setGameModeForTree(GameMode::Performance);
97 rootLayer->addChild(childLayer);
98
99 EXPECT_EQ(rootLayer->getGameMode(), GameMode::Performance);
100 EXPECT_EQ(childLayer->getGameMode(), GameMode::Performance);
101 }
102
TEST_F(GameModeTest,RemoveChildResetsGameMode)103 TEST_F(GameModeTest, RemoveChildResetsGameMode) {
104 sp<Layer> rootLayer = createLayer();
105 sp<Layer> childLayer = createLayer();
106 rootLayer->setGameModeForTree(GameMode::Performance);
107 rootLayer->addChild(childLayer);
108
109 EXPECT_EQ(rootLayer->getGameMode(), GameMode::Performance);
110 EXPECT_EQ(childLayer->getGameMode(), GameMode::Performance);
111
112 rootLayer->removeChild(childLayer);
113 EXPECT_EQ(childLayer->getGameMode(), GameMode::Unsupported);
114 }
115
TEST_F(GameModeTest,ReparentingDoesNotOverrideMetadata)116 TEST_F(GameModeTest, ReparentingDoesNotOverrideMetadata) {
117 sp<Layer> rootLayer = createLayer();
118 sp<Layer> childLayer1 = createLayer();
119 sp<Layer> childLayer2 = createLayer();
120 rootLayer->setGameModeForTree(GameMode::Standard);
121 rootLayer->addChild(childLayer1);
122
123 setGameModeMetadata(childLayer2, GameMode::Performance);
124 rootLayer->addChild(childLayer2);
125
126 EXPECT_EQ(rootLayer->getGameMode(), GameMode::Standard);
127 EXPECT_EQ(childLayer1->getGameMode(), GameMode::Standard);
128 EXPECT_EQ(childLayer2->getGameMode(), GameMode::Performance);
129
130 rootLayer->removeChild(childLayer2);
131 EXPECT_EQ(childLayer2->getGameMode(), GameMode::Performance);
132 }
133
134 } // namespace android
135