1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "modifier_render_thread/rs_modifiers_draw_thread.h"
19 #include "session/host/include/session_coordinate_helper.h"
20
21 #include "screen_session_manager_client/include/screen_session_manager_client.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28
29 class SessionCoordinateHelperTest : public Test {
30 public:
SessionCoordinateHelperTest()31 SessionCoordinateHelperTest() : ssmClient_(ScreenSessionManagerClient::GetInstance()) {}
32
33 static void SetUpTestCase();
34 static void TearDownTestCase();
SetUp()35 void SetUp() override {}
TearDown()36 void TearDown() override
37 {
38 std::lock_guard<std::mutex> lock(ssmClient_.screenSessionMapMutex_);
39 ssmClient_.screenSessionMap_.clear();
40 }
41
42 protected:
43 ScreenSessionManagerClient& ssmClient_;
44 };
45
SetUpTestCase()46 void SessionCoordinateHelperTest::SetUpTestCase() {}
47
TearDownTestCase()48 void SessionCoordinateHelperTest::TearDownTestCase()
49 {
50 #ifdef RS_ENABLE_VK
51 RSModifiersDrawThread::Destroy();
52 #endif
53 }
54
55 /**
56 * @tc.name: TestRelativeToGlobalDisplayRect
57 * @tc.desc: Verify RelativeToGlobalDisplayRect correctly converts relative rect to global rect
58 * @tc.type: FUNC
59 */
60 HWTEST_F(SessionCoordinateHelperTest, TestRelativeToGlobalDisplayRect, TestSize.Level1)
61 {
62 constexpr ScreenId invalidScreenId = 9999;
63 WSRect relativeRect { 50, 60, 300, 200 };
64 WSRect result = SessionCoordinateHelper::RelativeToGlobalDisplayRect(invalidScreenId, relativeRect);
65 EXPECT_EQ(result, relativeRect);
66
67 constexpr ScreenId screenId = 1001;
68 auto screenProperty = ScreenProperty();
69 screenProperty.SetX(500);
70 screenProperty.SetY(600);
71 auto screenSession = sptr<ScreenSession>::MakeSptr(screenId, screenProperty, screenId);
72 {
73 std::lock_guard<std::mutex> lock(ssmClient_.screenSessionMapMutex_);
74 ssmClient_.screenSessionMap_[screenId] = screenSession;
75 }
76 result = SessionCoordinateHelper::RelativeToGlobalDisplayRect(screenId, relativeRect);
77 WSRect expectedRect { 550, 660, 300, 200 };
78 EXPECT_EQ(result, expectedRect);
79 }
80
81 /**
82 * @tc.name: TestGlobalToScreenRelativeRect
83 * @tc.desc: Verify GlobalToScreenRelativeRect correctly converts global rect to relative rect
84 * @tc.type: FUNC
85 */
86 HWTEST_F(SessionCoordinateHelperTest, TestGlobalToScreenRelativeRect, TestSize.Level1)
87 {
88 constexpr ScreenId invalidScreenId = 9999;
89 WSRect globalRect { 600, 600, 100, 100 };
90 auto result = SessionCoordinateHelper::GlobalToScreenRelativeRect(invalidScreenId, globalRect);
91 EXPECT_EQ(result.screenId, MAIN_SCREEN_ID_DEFAULT);
92 EXPECT_EQ(result.rect, globalRect);
93
94 ScreenProperty propA;
95 propA.SetX(0);
96 propA.SetY(0);
97 RRect boundsA;
98 boundsA.rect_ = { 0, 0, 500, 500 };
99 propA.SetBounds(boundsA);
100 constexpr ScreenId screenIdOfA = 1;
101 auto screenA = sptr<ScreenSession>::MakeSptr(screenIdOfA, propA, screenIdOfA);
102
103 ScreenProperty propB;
104 propB.SetX(600);
105 propB.SetY(600);
106 RRect boundsB;
107 boundsB.rect_ = { 600, 600, 400, 400 };
108 propB.SetBounds(boundsB);
109 constexpr ScreenId screenIdOfB = 2;
110 auto screenB = sptr<ScreenSession>::MakeSptr(screenIdOfB, propB, screenIdOfB);
111 {
112 std::lock_guard<std::mutex> lock(ssmClient_.screenSessionMapMutex_);
113 ssmClient_.screenSessionMap_[screenIdOfA] = screenA;
114 ssmClient_.screenSessionMap_[screenIdOfB] = screenB;
115 }
116
117 screenA->property_.SetVirtualPixelRatio(0.0f);
118 result = SessionCoordinateHelper::GlobalToScreenRelativeRect(screenIdOfA, globalRect);
119 EXPECT_EQ(result.screenId, MAIN_SCREEN_ID_DEFAULT);
120 EXPECT_EQ(result.rect, globalRect);
121
122 screenA->property_.SetVirtualPixelRatio(1.0f);
123 screenB->property_.SetVirtualPixelRatio(1.0f);
124 result = SessionCoordinateHelper::GlobalToScreenRelativeRect(screenIdOfA, globalRect);
125 EXPECT_EQ(result.screenId, screenIdOfB);
126 WSRect expectedRect { 0, 0, 100, 100 }; // relative to screen B
127 EXPECT_EQ(result.rect, expectedRect);
128 }
129 } // namespace Rosen
130 } // namespace OHOS