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 "DisplayTransactionTestHelpers.h"
21
22 #include <ftl/fake_guard.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25
26 namespace android {
27 namespace {
28
29 using hal::RenderIntent;
30
31 using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
32
33 class SetDisplayBrightnessTest : public DisplayTransactionTest {
34 public:
getDisplayDevice()35 sp<DisplayDevice> getDisplayDevice() { return injectDefaultInternalDisplay({}); }
36 };
37
TEST_F(SetDisplayBrightnessTest,persistDisplayBrightnessNoComposite)38 TEST_F(SetDisplayBrightnessTest, persistDisplayBrightnessNoComposite) {
39 ftl::FakeGuard guard(kMainThreadContext);
40 sp<DisplayDevice> displayDevice = getDisplayDevice();
41
42 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
43
44 constexpr float kDisplayBrightness = 0.5f;
45 displayDevice->stageBrightness(kDisplayBrightness);
46
47 EXPECT_EQ(0.5f, displayDevice->getStagedBrightness());
48
49 displayDevice->persistBrightness(false);
50
51 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
52 EXPECT_EQ(std::nullopt, displayDevice->getCompositionDisplay()->getState().displayBrightness);
53 }
54
TEST_F(SetDisplayBrightnessTest,persistDisplayBrightnessWithComposite)55 TEST_F(SetDisplayBrightnessTest, persistDisplayBrightnessWithComposite) {
56 ftl::FakeGuard guard(kMainThreadContext);
57 sp<DisplayDevice> displayDevice = getDisplayDevice();
58
59 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
60
61 constexpr float kDisplayBrightness = 0.5f;
62 displayDevice->stageBrightness(kDisplayBrightness);
63
64 EXPECT_EQ(0.5f, displayDevice->getStagedBrightness());
65
66 displayDevice->persistBrightness(true);
67
68 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
69 EXPECT_EQ(kDisplayBrightness,
70 displayDevice->getCompositionDisplay()->getState().displayBrightness);
71 }
72
TEST_F(SetDisplayBrightnessTest,persistDisplayBrightnessWithCompositeShortCircuitsOnNoOp)73 TEST_F(SetDisplayBrightnessTest, persistDisplayBrightnessWithCompositeShortCircuitsOnNoOp) {
74 ftl::FakeGuard guard(kMainThreadContext);
75 sp<DisplayDevice> displayDevice = getDisplayDevice();
76
77 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
78
79 constexpr float kDisplayBrightness = 0.5f;
80 displayDevice->stageBrightness(kDisplayBrightness);
81
82 EXPECT_EQ(0.5f, displayDevice->getStagedBrightness());
83
84 displayDevice->persistBrightness(true);
85
86 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
87 EXPECT_EQ(kDisplayBrightness,
88 displayDevice->getCompositionDisplay()->getState().displayBrightness);
89 displayDevice->getCompositionDisplay()->editState().displayBrightness = std::nullopt;
90
91 displayDevice->stageBrightness(kDisplayBrightness);
92 EXPECT_EQ(0.5f, displayDevice->getStagedBrightness());
93 displayDevice->persistBrightness(true);
94
95 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
96 EXPECT_EQ(std::nullopt, displayDevice->getCompositionDisplay()->getState().displayBrightness);
97 }
98
TEST_F(SetDisplayBrightnessTest,firstDisplayBrightnessWithComposite)99 TEST_F(SetDisplayBrightnessTest, firstDisplayBrightnessWithComposite) {
100 ftl::FakeGuard guard(kMainThreadContext);
101 sp<DisplayDevice> displayDevice = getDisplayDevice();
102
103 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
104
105 constexpr float kDisplayBrightness = -1.0f;
106 displayDevice->stageBrightness(kDisplayBrightness);
107
108 EXPECT_EQ(-1.0f, displayDevice->getStagedBrightness());
109
110 displayDevice->persistBrightness(true);
111
112 EXPECT_EQ(std::nullopt, displayDevice->getStagedBrightness());
113 EXPECT_EQ(kDisplayBrightness,
114 displayDevice->getCompositionDisplay()->getState().displayBrightness);
115 }
116
117 } // namespace
118 } // namespace android
119