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