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 <cinttypes>
17 #include <cstdlib>
18 #include <gtest/gtest.h>
19
20 #include "pixel_map.h"
21
22 #include "snapshot_utils.h"
23 #include "common_test_utils.h"
24 #include "window_manager_hilog.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_DISPLAY, "SnapshotDisplayTest"};
33 }
34
35 class SnapshotDisplayTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 virtual void SetUp() override;
40 virtual void TearDown() override;
41 static DisplayId defaultId_;
42 DisplayId invalidId_ = DISPLAY_ID_INVALID;
43 const std::string defaultCmd_ = "/system/bin/snapshot_display";
44 const int testTimeCount_ = 2;
45 };
46
47 DisplayId SnapshotDisplayTest::defaultId_ = DISPLAY_ID_INVALID;
48
SetUpTestCase()49 void SnapshotDisplayTest::SetUpTestCase()
50 {
51 auto display = DisplayManager::GetInstance().GetDefaultDisplay();
52 if (display == nullptr) {
53 WLOGFE("GetDefaultDisplay: failed!\n");
54 return;
55 }
56 WLOGFI("GetDefaultDisplay: id %" PRIu64", w %d, h %d, fps %u\n", display->GetId(), display->GetWidth(),
57 display->GetHeight(), display->GetRefreshRate());
58
59 defaultId_ = display->GetId();
60
61 CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0);
62 }
63
TearDownTestCase()64 void SnapshotDisplayTest::TearDownTestCase()
65 {
66 }
67
SetUp()68 void SnapshotDisplayTest::SetUp()
69 {
70 }
71
TearDown()72 void SnapshotDisplayTest::TearDown()
73 {
74 }
75
CheckFileExist(const std::string & fPath)76 bool CheckFileExist(const std::string& fPath)
77 {
78 if (!fPath.empty()) {
79 FILE* fp = fopen(fPath.c_str(), "r");
80 if (fp != nullptr) {
81 fclose(fp);
82 return true;
83 }
84 }
85 return false;
86 }
87
TakeScreenshotBySpecifiedParam(std::string exec,std::string imgPath,std::string extraParam)88 bool TakeScreenshotBySpecifiedParam(std::string exec, std::string imgPath, std::string extraParam)
89 {
90 if (CheckFileExist(imgPath)) {
91 remove(imgPath.c_str());
92 }
93 const std::string cmd = exec + " -f " + imgPath + " " + extraParam;
94 (void)system(cmd.c_str());
95 bool isExist = CheckFileExist(imgPath);
96 if (isExist) {
97 remove(imgPath.c_str());
98 }
99 return isExist;
100 }
101
102 namespace {
103 /**
104 * @tc.name: ScreenShotCmdValid
105 * @tc.desc: Call screenshot default cmd and check if it saves image in default path
106 * @tc.type: FUNC
107 */
108 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid01, Function | MediumTest | Level2)
109 {
110 std::string imgPath[testTimeCount_];
111 int i;
112
113 for (i = 0; i < testTimeCount_; i++) {
114 imgPath[i] = SnapShotUtils::GenerateFileName(i);
115 if (CheckFileExist(imgPath[i])) {
116 remove(imgPath[i].c_str());
117 }
118 }
119
120 (void)system(defaultCmd_.c_str());
121
122 for (i = 0; i < testTimeCount_; i++) {
123 if (CheckFileExist(imgPath[i])) { // ok
124 remove(imgPath[i].c_str());
125 ASSERT_TRUE(true);
126 return;
127 }
128 }
129 ADD_FAILURE(); // fail, can't find snapshot file
130 }
131
132 /**
133 * @tc.name: ScreenShotCmdValid
134 * @tc.desc: Call screenshot with default displayID and default path
135 * @tc.type: FUNC
136 */
137 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid02, Function | MediumTest | Level2)
138 {
139 std::string imgPath[testTimeCount_];
140 int i;
141
142 for (i = 0; i < testTimeCount_; i++) {
143 imgPath[i] = SnapShotUtils::GenerateFileName(i);
144 if (CheckFileExist(imgPath[i])) {
145 remove(imgPath[i].c_str());
146 }
147 }
148
149 const std::string cmd = defaultCmd_ + " -i " + std::to_string(defaultId_);
150 (void)system(cmd.c_str());
151
152 for (i = 0; i < testTimeCount_; i++) {
153 if (CheckFileExist(imgPath[i])) { // ok
154 remove(imgPath[i].c_str());
155 ASSERT_TRUE(true);
156 return;
157 }
158 }
159 ADD_FAILURE(); // fail, can't find snapshot file
160 }
161
162 /**
163 * @tc.name: ScreenShotCmdValid
164 * @tc.desc: Call screenshot with default displayID and custom path
165 * @tc.type: FUNC
166 */
167 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid03, Function | MediumTest | Level2)
168 {
169 const std::string imgPath = "/data/snapshot_display_test.jpeg";
170 std::string extraParam = "-i " + std::to_string(defaultId_);
171 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
172 }
173
174 /**
175 * @tc.name: ScreenShotCmdValid
176 * @tc.desc: Call screenshot with valid width/height
177 * @tc.type: FUNC
178 */
179 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid04, Function | MediumTest | Level2)
180 {
181 const std::string imgPath = "/data/snapshot_display_test.jpeg";
182 std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 100 -h 100";
183 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
184 }
185
186 /**
187 * @tc.name: ScreenShotCmdValid
188 * @tc.desc: Call screenshot with valid width
189 * @tc.type: FUNC
190 */
191 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid05, Function | MediumTest | Level2)
192 {
193 const std::string imgPath = "/data/snapshot_display_test.jpeg";
194 std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 100";
195 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
196 }
197
198 /**
199 * @tc.name: ScreenShotCmdValid
200 * @tc.desc: Call screenshot with valid height
201 * @tc.type: FUNC
202 */
203 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid06, Function | MediumTest | Level2)
204 {
205 const std::string imgPath = "/data/snapshot_display_test.jpeg";
206 std::string extraParam = "-i " + std::to_string(defaultId_) + " -h 100";
207 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
208 }
209
210 /**
211 * @tc.name: ScreenShotCmdValid
212 * @tc.desc: Call screenshot with invalid width/height
213 * @tc.type: FUNC
214 */
215 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid07, Function | MediumTest | Level2)
216 {
217 const std::string imgPath = "/data/snapshot_display_test.jpeg";
218 std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 10000 -h 10000";
219 ASSERT_EQ(false, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
220 }
221
222 /**
223 * @tc.name: ScreenShotCmdValid
224 * @tc.desc: Call screenshot with -m
225 * @tc.type: FUNC
226 */
227 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid08, Function | MediumTest | Level2)
228 {
229 const std::string imgPath = "/data/snapshot_display_test.jpeg";
230 std::string extraParam = "-i " + std::to_string(defaultId_) + " -m";
231 ASSERT_EQ(false, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
232 }
233 } // namespace
234 } // namespace Rosen
235 } // namespace OHOS