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
26using namespace testing;
27using namespace testing::ext;
28
29namespace OHOS {
30namespace Rosen {
31namespace {
32constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SnapshotDisplayTest"};
33}
34
35class SnapshotDisplayTest : public testing::Test {
36public:
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
47DisplayId SnapshotDisplayTest::defaultId_ = DISPLAY_ID_INVALID;
48
49void SnapshotDisplayTest::SetUpTestCase()
50{
51    auto display = DisplayManager::GetInstance().GetDefaultDisplay();
52    if (display == nullptr) {
53        WLOGFE("GetDefaultDisplay: failed!\n");
54        return;
55    }
56    WLOGI("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    const char** perms = new const char *[1];
63    perms[0] = "ohos.permission.CAPTURE_SCREEN";
64    CommonTestUtils::SetAceessTokenPermission("DisplayManagerServiceTest", perms, 1);
65}
66
67void SnapshotDisplayTest::TearDownTestCase()
68{
69}
70
71void SnapshotDisplayTest::SetUp()
72{
73}
74
75void SnapshotDisplayTest::TearDown()
76{
77}
78
79bool CheckFileExist(const std::string& fPath)
80{
81    if (!fPath.empty()) {
82        FILE* fp = fopen(fPath.c_str(), "r");
83        if (fp != nullptr) {
84            fclose(fp);
85            return true;
86        }
87    }
88    return false;
89}
90
91bool TakeScreenshotBySpecifiedParam(std::string exec, std::string imgPath, std::string extraParam)
92{
93    if (CheckFileExist(imgPath)) {
94        remove(imgPath.c_str());
95    }
96    const std::string cmd = exec + " -f " + imgPath +  " " + extraParam;
97    (void)system(cmd.c_str());
98    bool isExist = CheckFileExist(imgPath);
99    if (isExist) {
100        remove(imgPath.c_str());
101    }
102    return isExist;
103}
104
105namespace {
106/**
107 * @tc.name: ScreenShotCmdValid
108 * @tc.desc: Call screenshot default cmd and check if it saves image in default path
109 * @tc.type: FUNC
110 */
111HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid01, Function | MediumTest | Level2)
112{
113    std::string imgPath[testTimeCount_];
114    int i;
115
116    for (i = 0; i < testTimeCount_; i++) {
117        imgPath[i] = SnapShotUtils::GenerateFileName("jpeg", i);
118        if (CheckFileExist(imgPath[i])) {
119            remove(imgPath[i].c_str());
120        }
121    }
122
123    (void)system(defaultCmd_.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 */
140HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid02, Function | MediumTest | Level2)
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_);
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 */
170HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid03, Function | MediumTest | Level2)
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 */
182HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid04, Function | MediumTest | Level2)
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 */
194HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid05, Function | MediumTest | Level2)
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 */
206HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid06, Function | MediumTest | Level2)
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 */
218HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid07, Function | MediumTest | Level2)
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 */
230HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid08, Function | MediumTest | Level2)
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 */
242HWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid09, Function | MediumTest | Level2)
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_ + " -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