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 defaultId_ = display->GetId();
57 WLOGI("GetDefaultDisplay: id[%{public}" PRIu64"], w:[%{public}d], h[%{public}d], fps[%{public}u]",
58 defaultId_, display->GetWidth(), display->GetHeight(), display->GetRefreshRate());
59
60 CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0);
61 const char** perms = new const char *[1];
62 perms[0] = "ohos.permission.CAPTURE_SCREEN";
63 CommonTestUtils::SetAceessTokenPermission("DisplayManagerServiceTest", perms, 1);
64 }
65
TearDownTestCase()66 void SnapshotDisplayTest::TearDownTestCase()
67 {
68 }
69
SetUp()70 void SnapshotDisplayTest::SetUp()
71 {
72 }
73
TearDown()74 void SnapshotDisplayTest::TearDown()
75 {
76 }
77
CheckFileExist(const std::string & fPath)78 bool CheckFileExist(const std::string& fPath)
79 {
80 if (!fPath.empty()) {
81 FILE* fp = fopen(fPath.c_str(), "r");
82 if (fp != nullptr) {
83 fclose(fp);
84 return true;
85 }
86 }
87 return false;
88 }
89
TakeScreenshotBySpecifiedParam(std::string exec,std::string imgPath,std::string extraParam)90 bool TakeScreenshotBySpecifiedParam(std::string exec, std::string imgPath, std::string extraParam)
91 {
92 if (CheckFileExist(imgPath)) {
93 remove(imgPath.c_str());
94 }
95 const std::string cmd = exec + " -f " + imgPath + " " + extraParam;
96 (void)system(cmd.c_str());
97 bool isExist = CheckFileExist(imgPath);
98 if (isExist) {
99 remove(imgPath.c_str());
100 }
101 return isExist;
102 }
103
104 namespace {
105 /**
106 * @tc.name: ScreenShotCmdValid
107 * @tc.desc: Call screenshot default cmd and check if it saves image in default path
108 * @tc.type: FUNC
109 */
110 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid01, TestSize.Level1)
111 {
112 std::string imgPath[testTimeCount_];
113 int i;
114
115 for (i = 0; i < testTimeCount_; i++) {
116 imgPath[i] = SnapShotUtils::GenerateFileName("jpeg", i);
117 if (CheckFileExist(imgPath[i])) {
118 remove(imgPath[i].c_str());
119 }
120 }
121
122 const std::string cmd = defaultCmd_ + " -i " + std::to_string(defaultId_) + " -f " + imgPath[0];
123 (void)system(cmd.c_str());
124
125 for (i = 0; i < testTimeCount_; i++) {
126 if (CheckFileExist(imgPath[i])) { // ok
127 remove(imgPath[i].c_str());
128 ASSERT_TRUE(true);
129 return;
130 }
131 }
132 ADD_FAILURE(); // fail, can't find snapshot file
133 }
134
135 /**
136 * @tc.name: ScreenShotCmdValid
137 * @tc.desc: Call screenshot with default displayID and default path
138 * @tc.type: FUNC
139 */
140 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid02, TestSize.Level1)
141 {
142 std::string imgPath[testTimeCount_];
143 int i;
144
145 for (i = 0; i < testTimeCount_; i++) {
146 imgPath[i] = SnapShotUtils::GenerateFileName("jpeg", i);
147 if (CheckFileExist(imgPath[i])) {
148 remove(imgPath[i].c_str());
149 }
150 }
151
152 const std::string cmd = defaultCmd_ + " -i " + std::to_string(defaultId_) + " -f " + imgPath[0];
153 (void)system(cmd.c_str());
154
155 for (i = 0; i < testTimeCount_; i++) {
156 if (CheckFileExist(imgPath[i])) { // ok
157 remove(imgPath[i].c_str());
158 ASSERT_TRUE(true);
159 return;
160 }
161 }
162 ADD_FAILURE(); // fail, can't find snapshot file
163 }
164
165 /**
166 * @tc.name: ScreenShotCmdValid
167 * @tc.desc: Call screenshot with default displayID and custom path
168 * @tc.type: FUNC
169 */
170 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid03, TestSize.Level1)
171 {
172 const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg";
173 std::string extraParam = "-i " + std::to_string(defaultId_);
174 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
175 }
176
177 /**
178 * @tc.name: ScreenShotCmdValid
179 * @tc.desc: Call screenshot with valid width/height
180 * @tc.type: FUNC
181 */
182 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid04, TestSize.Level1)
183 {
184 const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg";
185 std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 100 -h 100";
186 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
187 }
188
189 /**
190 * @tc.name: ScreenShotCmdValid
191 * @tc.desc: Call screenshot with valid width
192 * @tc.type: FUNC
193 */
194 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid05, TestSize.Level1)
195 {
196 const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg";
197 std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 100";
198 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
199 }
200
201 /**
202 * @tc.name: ScreenShotCmdValid
203 * @tc.desc: Call screenshot with valid height
204 * @tc.type: FUNC
205 */
206 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid06, TestSize.Level1)
207 {
208 const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg";
209 std::string extraParam = "-i " + std::to_string(defaultId_) + " -h 100";
210 ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
211 }
212
213 /**
214 * @tc.name: ScreenShotCmdValid
215 * @tc.desc: Call screenshot with invalid width/height
216 * @tc.type: FUNC
217 */
218 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid07, TestSize.Level1)
219 {
220 const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg";
221 std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 10000 -h 10000";
222 ASSERT_EQ(false, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
223 }
224
225 /**
226 * @tc.name: ScreenShotCmdValid
227 * @tc.desc: Call screenshot with -m
228 * @tc.type: FUNC
229 */
230 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid08, TestSize.Level1)
231 {
232 const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg";
233 std::string extraParam = "-i " + std::to_string(defaultId_) + " -m";
234 ASSERT_EQ(false, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam));
235 }
236
237 /**
238 * @tc.name: ScreenShotCmdValid
239 * @tc.desc: screenshot png type
240 * @tc.type: FUNC
241 */
242 HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid09, TestSize.Level1)
243 {
244 std::string imgPath[testTimeCount_];
245 int i;
246
247 for (i = 0; i < testTimeCount_; i++) {
248 imgPath[i] = SnapShotUtils::GenerateFileName("png", i);
249 if (CheckFileExist(imgPath[i])) {
250 remove(imgPath[i].c_str());
251 }
252 }
253
254 const std::string cmd = defaultCmd_ + " -f " + imgPath[0] + " -t png";
255 (void)system(cmd.c_str());
256
257 for (i = 0; i < testTimeCount_; i++) {
258 if (CheckFileExist(imgPath[i])) { // ok
259 remove(imgPath[i].c_str());
260 ASSERT_TRUE(true);
261 return;
262 }
263 }
264 ADD_FAILURE(); // fail, can't find snapshot file
265 }
266 } // namespace
267 } // namespace Rosen
268 } // namespace OHOS