1 /*
2 * Copyright (c) 2024 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 #include <regex>
18 #include <pointer_event.h>
19 #include <ui/rs_surface_node.h>
20
21 #include "mock/mock_session_stage.h"
22 #include "mock/mock_window_event_channel.h"
23 #include "mock/mock_pattern_detach_callback.h"
24 #include "session/host/include/extension_session.h"
25 #include "session/host/include/move_drag_controller.h"
26 #include "session/host/include/scene_session.h"
27 #include "session_manager/include/scene_session_manager.h"
28 #include "session/host/include/session.h"
29 #include "session_info.h"
30 #include "key_event.h"
31 #include "wm_common.h"
32 #include "window_manager_hilog.h"
33
34 using namespace testing;
35 using namespace testing::ext;
36
37 namespace OHOS {
38 namespace Rosen {
39 namespace {
40 const std::string UNDEFINED = "undefined";
41 }
42
43 class SessionLayoutTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp() override;
48 void TearDown() override;
49 int32_t GetTaskCount();
50 sptr<SceneSessionManager> ssm_;
51
52 private:
53 RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
54 sptr<Session> session_ = nullptr;
55 static constexpr uint32_t WAIT_SYNC_IN_NS = 500000;
56 sptr<SessionStageMocker> mockSessionStage_ = nullptr;
57 sptr<WindowEventChannelMocker> mockEventChannel_ = nullptr;
58 };
59
SetUpTestCase()60 void SessionLayoutTest::SetUpTestCase()
61 {
62 }
63
TearDownTestCase()64 void SessionLayoutTest::TearDownTestCase()
65 {
66 }
67
SetUp()68 void SessionLayoutTest::SetUp()
69 {
70 SessionInfo info;
71 info.abilityName_ = "testSession1";
72 info.moduleName_ = "testSession2";
73 info.bundleName_ = "testSession3";
74 session_ = sptr<Session>::MakeSptr(info);
75 session_->surfaceNode_ = CreateRSSurfaceNode();
76 ssm_ = sptr<SceneSessionManager>::MakeSptr();
77 session_->SetEventHandler(ssm_->taskScheduler_->GetEventHandler(), ssm_->eventHandler_);
78 auto isScreenLockedCallback = [this]() {
79 return ssm_->IsScreenLocked();
80 };
81 session_->RegisterIsScreenLockedCallback(isScreenLockedCallback);
82 mockSessionStage_ = sptr<SessionStageMocker>::MakeSptr();
83 mockEventChannel_ = sptr<WindowEventChannelMocker>::MakeSptr(mockSessionStage_);
84 }
85
TearDown()86 void SessionLayoutTest::TearDown()
87 {
88 session_ = nullptr;
89 usleep(WAIT_SYNC_IN_NS);
90 }
91
CreateRSSurfaceNode()92 RSSurfaceNode::SharedPtr SessionLayoutTest::CreateRSSurfaceNode()
93 {
94 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
95 rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
96 auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
97 if (surfaceNode == nullptr) {
98 GTEST_LOG_(INFO) << "SessionLayoutTest::CreateRSSurfaceNode surfaceNode is nullptr";
99 }
100 return surfaceNode;
101 }
102
GetTaskCount()103 int32_t SessionLayoutTest::GetTaskCount()
104 {
105 std::string dumpInfo = session_->handler_->GetEventRunner()->GetEventQueue()->DumpCurrentQueueSize();
106 std::regex pattern("\\d+");
107 std::smatch matches;
108 int32_t taskNum = 0;
109 while (std::regex_search(dumpInfo, matches, pattern)) {
110 taskNum += std::stoi(matches.str());
111 dumpInfo = matches.suffix();
112 }
113 return taskNum;
114 }
115
116 namespace {
117 /**
118 * @tc.name: SetSingleHandTransform
119 * @tc.desc: SetSingleHandTransform
120 * @tc.type: FUNC
121 */
122 HWTEST_F(SessionLayoutTest, SetSingleHandTransform, Function | SmallTest | Level2)
123 {
124 SessionInfo info;
125 info.abilityName_ = "SetSingleHandTransform";
126 info.bundleName_ = "SetSingleHandTransform";
127 sptr<Session> session = sptr<Session>::MakeSptr(info);
128 SingleHandTransform transform;
129 session->SetSingleHandTransform(transform);
130 ASSERT_EQ(transform, session->GetSingleHandTransform());
131 }
132 }
133 } // namespace Rosen
134 } // namespace OHOS
135