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
24using namespace testing;
25using namespace testing::ext;
26
27namespace OHOS {
28namespace Rosen {
29class WindowDumperTest : public testing::Test {
30public:
31    static void SetUpTestCase();
32    static void TearDownTestCase();
33    void SetUp() override;
34    void TearDown() override;
35    static const std::string dumpFile_;
36};
37
38const std::string WindowDumperTest::dumpFile_ = "data/window_dump_test.txt";
39
40void 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
51void WindowDumperTest::TearDownTestCase()
52{
53    unlink(dumpFile_.c_str());
54}
55
56void WindowDumperTest::SetUp()
57{
58}
59
60void WindowDumperTest::TearDown()
61{
62}
63
64namespace {
65/**
66 * @tc.name: Dump01
67 * @tc.desc: Dump
68 * @tc.type: FUNC
69 */
70HWTEST_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 */
89HWTEST_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 */
104HWTEST_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 */
121HWTEST_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 */
138HWTEST_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 */
157HWTEST_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 */
176HWTEST_F(WindowDumperTest, ShowAceDumpHelp01, Function | SmallTest | Level1)
177{
178    sptr<WindowDumper> windowDumper;
179    windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
180    std::string dumpInfo;
181    windowDumper->ShowAceDumpHelp(dumpInfo);
182    WindowManagerService::GetInstance().windowRoot_->windowNodeMap_.clear();
183    ASSERT_TRUE(dumpInfo.empty());
184}
185
186/**
187 * @tc.name: ShowAceDumpHelp02
188 * @tc.desc: ShowAceDumpHelp
189 * @tc.type: FUNC
190 */
191HWTEST_F(WindowDumperTest, ShowAceDumpHelp02, Function | SmallTest | Level1)
192{
193    sptr<WindowDumper> windowDumper;
194    windowDumper = new WindowDumper(WindowManagerService::GetInstance().windowRoot_);
195    std::string dumpInfo;
196    windowDumper->ShowAceDumpHelp(dumpInfo);
197    WindowManagerService::GetInstance().windowRoot_->windowNodeMap_.clear();
198    ASSERT_TRUE(dumpInfo.empty());
199}
200}
201}
202}