1 /*
2  * Copyright 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 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19 
20 #include "DisplayTransactionTestHelpers.h"
21 
22 namespace android {
23 namespace {
24 
25 class OnInitializeDisplaysTest : public DisplayTransactionTest {};
26 
TEST_F(OnInitializeDisplaysTest,onInitializeDisplaysSetsUpPrimaryDisplay)27 TEST_F(OnInitializeDisplaysTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
28     using Case = SimplePrimaryDisplayCase;
29 
30     // --------------------------------------------------------------------
31     // Preconditions
32 
33     // A primary display is set up
34     Case::Display::injectHwcDisplay(this);
35     auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
36     primaryDisplay.inject();
37 
38     // --------------------------------------------------------------------
39     // Call Expectations
40 
41     // We expect the surface interceptor to possibly be used, but we treat it as
42     // disabled since it is called as a side effect rather than directly by this
43     // function.
44     EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
45 
46     // We expect a call to get the active display config.
47     Case::Display::setupHwcGetActiveConfigCallExpectations(this);
48 
49     // We expect invalidate() to be invoked once to trigger display transaction
50     // processing.
51     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
52 
53     EXPECT_CALL(*mVSyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
54 
55     // --------------------------------------------------------------------
56     // Invocation
57 
58     mFlinger.onInitializeDisplays();
59 
60     // --------------------------------------------------------------------
61     // Postconditions
62 
63     // The primary display should have a current state
64     ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
65     const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
66     // The layer stack state should be set to zero
67     EXPECT_EQ(0u, primaryDisplayState.layerStack);
68     // The orientation state should be set to zero
69     EXPECT_EQ(ui::ROTATION_0, primaryDisplayState.orientation);
70 
71     // The orientedDisplaySpaceRect state should be set to INVALID
72     EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.orientedDisplaySpaceRect);
73 
74     // The layerStackSpaceRect state should be set to INVALID
75     EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.layerStackSpaceRect);
76 
77     // The width and height should both be zero
78     EXPECT_EQ(0u, primaryDisplayState.width);
79     EXPECT_EQ(0u, primaryDisplayState.height);
80 
81     // The display should be set to PowerMode::ON
82     ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
83     auto displayDevice = primaryDisplay.mutableDisplayDevice();
84     EXPECT_EQ(PowerMode::ON, displayDevice->getPowerMode());
85 
86     // The display refresh period should be set in the orientedDisplaySpaceRect tracker.
87     FrameStats stats;
88     mFlinger.getAnimFrameTracker().getStats(&stats);
89     EXPECT_EQ(DEFAULT_VSYNC_PERIOD, stats.refreshPeriodNano);
90 
91     // The display transaction needed flag should be set.
92     EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
93 
94     // The compositor timing should be set to default values
95     const auto& compositorTiming = mFlinger.getCompositorTiming();
96     EXPECT_EQ(-DEFAULT_VSYNC_PERIOD, compositorTiming.deadline);
97     EXPECT_EQ(DEFAULT_VSYNC_PERIOD, compositorTiming.interval);
98     EXPECT_EQ(DEFAULT_VSYNC_PERIOD, compositorTiming.presentLatency);
99 }
100 
101 } // namespace
102 } // namespace android