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 CreateDisplayTest : public DisplayTransactionTest {};
26 
TEST_F(CreateDisplayTest,createDisplaySetsCurrentStateForNonsecureDisplay)27 TEST_F(CreateDisplayTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
28     const String8 name("virtual.test");
29 
30     // --------------------------------------------------------------------
31     // Call Expectations
32 
33     // The call should notify the interceptor that a display was created.
34     EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
35 
36     // --------------------------------------------------------------------
37     // Invocation
38 
39     sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
40 
41     // --------------------------------------------------------------------
42     // Postconditions
43 
44     // The display should have been added to the current state
45     ASSERT_TRUE(hasCurrentDisplayState(displayToken));
46     const auto& display = getCurrentDisplayState(displayToken);
47     EXPECT_TRUE(display.isVirtual());
48     EXPECT_FALSE(display.isSecure);
49     EXPECT_EQ(name.string(), display.displayName);
50 
51     // --------------------------------------------------------------------
52     // Cleanup conditions
53 
54     // Destroying the display invalidates the display state.
55     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
56 }
57 
TEST_F(CreateDisplayTest,createDisplaySetsCurrentStateForSecureDisplay)58 TEST_F(CreateDisplayTest, createDisplaySetsCurrentStateForSecureDisplay) {
59     const String8 name("virtual.test");
60 
61     // --------------------------------------------------------------------
62     // Call Expectations
63 
64     // The call should notify the interceptor that a display was created.
65     EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
66 
67     // --------------------------------------------------------------------
68     // Invocation
69     int64_t oldId = IPCThreadState::self()->clearCallingIdentity();
70     // Set the calling identity to graphics so captureDisplay with secure is allowed.
71     IPCThreadState::self()->restoreCallingIdentity(static_cast<int64_t>(AID_GRAPHICS) << 32 |
72                                                    AID_GRAPHICS);
73     sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
74     IPCThreadState::self()->restoreCallingIdentity(oldId);
75 
76     // --------------------------------------------------------------------
77     // Postconditions
78 
79     // The display should have been added to the current state
80     ASSERT_TRUE(hasCurrentDisplayState(displayToken));
81     const auto& display = getCurrentDisplayState(displayToken);
82     EXPECT_TRUE(display.isVirtual());
83     EXPECT_TRUE(display.isSecure);
84     EXPECT_EQ(name.string(), display.displayName);
85 
86     // --------------------------------------------------------------------
87     // Cleanup conditions
88 
89     // Destroying the display invalidates the display state.
90     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
91 }
92 
93 } // namespace
94 } // namespace android