1 /*
2 * Copyright (c) 2022 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 #include <fcntl.h>
16 #include <gtest/gtest.h>
17
18 #include "display_manager.h"
19 #include "window_dumper.h"
20 #include "window_manager_service.h"
21 #include "window_impl.h"
22 #include "window_agent.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 class WindowDumperTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35 static const std::string dumpFile_;
36 };
37
38 const std::string WindowDumperTest::dumpFile_ = "data/window_dump_test.txt";
39
SetUpTestCase()40 void WindowDumperTest::SetUpTestCase()
41 {
42 auto display = DisplayManager::GetInstance().GetDefaultDisplay();
43 ASSERT_TRUE((display != nullptr));
44 sptr<DisplayInfo> displayInfo = display->GetDisplayInfo();
45 ASSERT_TRUE((displayInfo != nullptr));
46
47 displayInfo->SetScreenGroupId(0);
48 WindowManagerService::GetInstance().windowRoot_->CreateWindowNodeContainer(0, displayInfo);
49 }
50
TearDownTestCase()51 void WindowDumperTest::TearDownTestCase()
52 {
53 unlink(dumpFile_.c_str());
54 }
55
SetUp()56 void WindowDumperTest::SetUp()
57 {
58 }
59
TearDown()60 void WindowDumperTest::TearDown()
61 {
62 }
63
64 namespace {
65 /**
66 * @tc.name: Dump01
67 * @tc.desc: Dump
68 * @tc.type: FUNC
69 */
70 HWTEST_F(WindowDumperTest, Dump01, Function | SmallTest | Level1)
71 {
72 sptr<WindowDumper> windowDumper;
73 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
74 int fd = open(dumpFile_.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
75 if (fd == -1) {
76 return;
77 }
78 std::vector<std::u16string> args;
79 WMError ret = windowDumper->Dump(fd, args);
80 ASSERT_EQ(ret, WMError::WM_OK);
81 close(fd);
82 }
83
84 /**
85 * @tc.name: Dump02
86 * @tc.desc: Dump fd less 0
87 * @tc.type: FUNC
88 */
89 HWTEST_F(WindowDumperTest, Dump02, Function | SmallTest | Level1)
90 {
91 sptr<WindowDumper> windowDumper;
92 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
93 int fd = -1;
94 std::vector<std::u16string> args;
95 WMError ret = windowDumper->Dump(fd, args);
96 ASSERT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
97 }
98
99 /**
100 * @tc.name: Dump04
101 * @tc.desc: Dump one param with '-x'
102 * @tc.type: FUNC
103 */
104 HWTEST_F(WindowDumperTest, Dump04, Function | SmallTest | Level1)
105 {
106 sptr<WindowDumper> windowDumper;
107 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
108 int fd = 2;
109 std::vector<std::u16string> args;
110 const std::u16string DUMP_HELP = u"-x";
111 args.emplace_back(DUMP_HELP);
112 WMError ret = windowDumper->Dump(fd, args);
113 ASSERT_EQ(ret, WMError::WM_OK);
114 }
115
116 /**
117 * @tc.name: Dump05
118 * @tc.desc: Dump two param with '-a'
119 * @tc.type: FUNC
120 */
121 HWTEST_F(WindowDumperTest, Dump05, Function | SmallTest | Level1)
122 {
123 sptr<WindowDumper> windowDumper;
124 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
125 int fd = 3;
126 std::vector<std::u16string> args;
127 const std::u16string DUMP_ALL = u"-a";
128 args.emplace_back(DUMP_ALL);
129 WMError ret = windowDumper->Dump(fd, args);
130 ASSERT_TRUE(ret == WMError::WM_OK || ret == WMError::WM_ERROR_INVALID_OPERATION);
131 }
132
133 /**
134 * @tc.name: Dump06
135 * @tc.desc: Dump two param with '-w 1'
136 * @tc.type: FUNC
137 */
138 HWTEST_F(WindowDumperTest, Dump06, Function | SmallTest | Level1)
139 {
140 sptr<WindowDumper> windowDumper;
141 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
142 int fd = 4;
143 std::vector<std::u16string> args;
144 const std::u16string DUMP_WINDOW = u"-w";
145 const std::u16string DUMP_WINDOW_ID = u"3";
146 args.emplace_back(DUMP_WINDOW);
147 args.emplace_back(DUMP_WINDOW_ID);
148 WMError ret = windowDumper->Dump(fd, args);
149 ASSERT_TRUE(ret == WMError::WM_OK || ret == WMError::WM_ERROR_INVALID_OPERATION);
150 }
151
152 /**
153 * @tc.name: Dump07
154 * @tc.desc: Dump two param with '-w n'
155 * @tc.type: FUNC
156 */
157 HWTEST_F(WindowDumperTest, Dump07, Function | SmallTest | Level1)
158 {
159 sptr<WindowDumper> windowDumper;
160 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
161 int fd = 5;
162 std::vector<std::u16string> args;
163 const std::u16string DUMP_WINDOW = u"-w";
164 const std::u16string DUMP_WINDOW_ID = u"n";
165 args.emplace_back(DUMP_WINDOW);
166 args.emplace_back(DUMP_WINDOW_ID);
167 WMError ret = windowDumper->Dump(fd, args);
168 ASSERT_EQ(ret, WMError::WM_ERROR_INVALID_OPERATION);
169 }
170
171 /**
172 * @tc.name: ShowAceDumpHelp01
173 * @tc.desc: ShowAceDumpHelp
174 * @tc.type: FUNC
175 */
176 HWTEST_F(WindowDumperTest, ShowAceDumpHelp01, Function | SmallTest | Level1)
177 {
178 sptr<WindowDumper> windowDumper;
179 sptr<WindowNode> node = new WindowNode();
180 uint32_t id = 101;
181 node->property_->SetWindowId(id);
182 node->property_->SetWindowType(WindowType::WINDOW_TYPE_KEYGUARD);
183 sptr<WindowOption> windowOption = new WindowOption();
184 sptr<WindowImpl> windowImpl = new WindowImpl(windowOption);
185 sptr<IWindow> window = new WindowAgent(windowImpl);
186 node->SetWindowToken(window);
187 WindowManagerService::GetInstance().windowRoot_->windowNodeMap_.insert(std::make_pair(id, node));
188 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
189 std::string dumpInfo;
190 windowDumper->ShowAceDumpHelp(dumpInfo);
191 WindowManagerService::GetInstance().windowRoot_->windowNodeMap_.clear();
192 ASSERT_TRUE(dumpInfo.empty());
193 }
194
195 /**
196 * @tc.name: ShowAceDumpHelp02
197 * @tc.desc: ShowAceDumpHelp
198 * @tc.type: FUNC
199 */
200 HWTEST_F(WindowDumperTest, ShowAceDumpHelp02, Function | SmallTest | Level1)
201 {
202 sptr<WindowDumper> windowDumper;
203 sptr<WindowNode> node = new WindowNode();
204 uint32_t id = 102;
205 node->property_->SetWindowId(id);
206 node->property_->SetWindowType(WindowType::WINDOW_TYPE_KEYGUARD);
207 WindowManagerService::GetInstance().windowRoot_->windowNodeMap_.insert(std::make_pair(id, node));
208 windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
209 std::string dumpInfo;
210 windowDumper->ShowAceDumpHelp(dumpInfo);
211 WindowManagerService::GetInstance().windowRoot_->windowNodeMap_.clear();
212 ASSERT_TRUE(dumpInfo.empty());
213 }
214 }
215 }
216 }