1 /*
2  * Copyright (c) 2023 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 <cstdint>
17 #include <gtest/gtest.h>
18 #include "display_manager.h"
19 #include "window_agent.h"
20 #include "window_group_mgr.h"
21 #include "window_impl.h"
22 #include "window_property.h"
23 #include "window_root.h"
24 #include "wm_common.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowGroupMgrTest"};
33 }
34 
35 class WindowGroupMgrTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41     static sptr<WindowGroupMgr> windowGroupMgr_;
42     static sptr<WindowRoot> windowRoot_;
43     static DisplayId defalutDisplayId_;
44     static int missionCount_;
45 };
46 
47 
48 sptr<WindowGroupMgr> WindowGroupMgrTest::windowGroupMgr_ = nullptr;
49 sptr<WindowRoot> WindowGroupMgrTest::windowRoot_;
50 DisplayId WindowGroupMgrTest::defalutDisplayId_ = -1;
51 int WindowGroupMgrTest::missionCount_ = 3;
52 
SetUpTestCase()53 void WindowGroupMgrTest::SetUpTestCase()
54 {
55     WLOGI("SetUpTestCase");
56     windowRoot_ = new WindowRoot(nullptr);
57     windowGroupMgr_ = new WindowGroupMgr(windowRoot_);
58 
59     auto display = DisplayManager::GetInstance().GetDefaultDisplay();
60     ASSERT_TRUE((display != nullptr));
61     sptr<DisplayInfo> displayInfo = display->GetDisplayInfo();
62     ASSERT_TRUE((displayInfo != nullptr));
63     auto container = windowRoot_->CreateWindowNodeContainer(0, displayInfo);
64     defalutDisplayId_ = display->GetId();
65 
66     for (int i = 0; i < missionCount_; i++) {
67         int32_t missindId =  i + 1;
68         sptr<WindowProperty> property = new WindowProperty();
69         AbilityInfo abilityInfo({"bundle_test" + std::to_string(missindId), "ability_test" + std::to_string(missindId),
70             missindId});
71         property->SetAbilityInfo(abilityInfo);
72         property->SetWindowId(static_cast<uint32_t>(missindId));
73         property->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
74         property->SetDisplayId(defalutDisplayId_);
75         sptr<WindowNode> node = new WindowNode(property);
76         node->SetWindowProperty(property);
77         node->abilityInfo_ = abilityInfo;
78         sptr<WindowOption> windowOption = new WindowOption();
79         sptr<WindowImpl> windowImpl = new WindowImpl(windowOption);
80         windowImpl->SetWindowState(WindowState::STATE_SHOWN);
81         sptr<IWindow> window = new WindowAgent(windowImpl);
82         node->SetWindowToken(window);
83         WLOGFI("windowId: %{public}u, missionId: %{public}d", node->GetWindowId(), node->abilityInfo_.missionId_);
84         windowRoot_->SaveWindow(node);
85     }
86 }
87 
TearDownTestCase()88 void WindowGroupMgrTest::TearDownTestCase()
89 {
90     windowGroupMgr_ = nullptr;
91     windowRoot_ = nullptr;
92     defalutDisplayId_ = -1;
93 }
94 
SetUp()95 void WindowGroupMgrTest::SetUp()
96 {
97 }
98 
TearDown()99 void WindowGroupMgrTest::TearDown()
100 {
101 }
102 
103 namespace {
104 /**
105  * @tc.name: MoveMissionsToForeground01
106  * @tc.desc: move missions to foreground
107  * @tc.type: FUNC
108  */
109 HWTEST_F(WindowGroupMgrTest, MoveMissionsToForeground01, Function | SmallTest | Level2)
110 {
111     WLOGI("MoveMissionsToForeground01");
112     auto rs = windowGroupMgr_->MoveMissionsToForeground({1, 2, 3}, 2);
113     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, rs);
114     rs = windowGroupMgr_->MoveMissionsToForeground({1, 2, 3}, -1);
115     ASSERT_EQ(WMError::WM_ERROR_NULLPTR, rs);
116 }
117 
118 /**
119  * @tc.name: MoveMissionsToBackground01
120  * @tc.desc: move missions to background
121  * @tc.type: FUNC
122  */
123 HWTEST_F(WindowGroupMgrTest, MoveMissionsToBackground01, Function | SmallTest | Level2)
124 {
125     std::vector<int32_t> moveRs;
126     auto rs = windowGroupMgr_->MoveMissionsToBackground({1, 2, 3}, moveRs);
127     ASSERT_EQ(WMError::WM_OK, rs);
128     ASSERT_EQ(0, moveRs.size());
129 }
130 
131 /**
132  * @tc.name: OnWindowDestroyed01
133  * @tc.desc: OnWindowDestroyed test
134  * @tc.type: FUNC
135  */
136 HWTEST_F(WindowGroupMgrTest, OnWindowDestroyed01, Function | SmallTest | Level2)
137 {
138     windowGroupMgr_->OnWindowDestroyed(1);
139     ASSERT_EQ(0, windowGroupMgr_->backupWindowModes_.count(1));
140 }
141 }
142 }
143 }