1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#include <cinttypes>
17e0dac50fSopenharmony_ci#include <gtest/gtest.h>
18e0dac50fSopenharmony_ci
19e0dac50fSopenharmony_ci#include "common_test_utils.h"
20e0dac50fSopenharmony_ci#include "display_test_utils.h"
21e0dac50fSopenharmony_ci#include "display_manager_proxy.h"
22e0dac50fSopenharmony_ci#include "future.h"
23e0dac50fSopenharmony_ci#include "pixel_map.h"
24e0dac50fSopenharmony_ci#include "screenshot_listener_future.h"
25e0dac50fSopenharmony_ci
26e0dac50fSopenharmony_ciusing namespace testing;
27e0dac50fSopenharmony_ciusing namespace testing::ext;
28e0dac50fSopenharmony_ci
29e0dac50fSopenharmony_cinamespace OHOS {
30e0dac50fSopenharmony_cinamespace Rosen {
31e0dac50fSopenharmony_cinamespace {
32e0dac50fSopenharmony_ciconstexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "ScreenshotTest"};
33e0dac50fSopenharmony_ci}
34e0dac50fSopenharmony_ciusing Utils = DisplayTestUtils;
35e0dac50fSopenharmony_ciclass ScreenshotListener;
36e0dac50fSopenharmony_ciclass ScreenshotTest : public testing::Test {
37e0dac50fSopenharmony_cipublic:
38e0dac50fSopenharmony_ci    static void SetUpTestCase();
39e0dac50fSopenharmony_ci    static void TearDownTestCase();
40e0dac50fSopenharmony_ci    virtual void SetUp() override;
41e0dac50fSopenharmony_ci    virtual void TearDown() override;
42e0dac50fSopenharmony_ci    static DisplayId defaultId_;
43e0dac50fSopenharmony_ci    static Media::Rect defaultScreen_;
44e0dac50fSopenharmony_ci    static Media::Size defaultImage_;
45e0dac50fSopenharmony_ci    DisplayId invalidId_ = DISPLAY_ID_INVALID;
46e0dac50fSopenharmony_ci    Media::Rect invalidRect_ = {-1, -1, -1, -1};
47e0dac50fSopenharmony_ci    uint32_t defaultRot_ = 0;
48e0dac50fSopenharmony_ci};
49e0dac50fSopenharmony_ci
50e0dac50fSopenharmony_ciDisplayId ScreenshotTest::defaultId_ = DISPLAY_ID_INVALID;
51e0dac50fSopenharmony_ciMedia::Rect ScreenshotTest::defaultScreen_ = {0, 0, 0, 0};
52e0dac50fSopenharmony_ciMedia::Size ScreenshotTest::defaultImage_ = {0, 0};
53e0dac50fSopenharmony_ci
54e0dac50fSopenharmony_civoid ScreenshotTest::SetUpTestCase()
55e0dac50fSopenharmony_ci{
56e0dac50fSopenharmony_ci    CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0);
57e0dac50fSopenharmony_ci    const char** perms = new const char *[1];
58e0dac50fSopenharmony_ci    perms[0] = "ohos.permission.CAPTURE_SCREEN";
59e0dac50fSopenharmony_ci    CommonTestUtils::SetAceessTokenPermission("ScreenshotTest", perms, 1);
60e0dac50fSopenharmony_ci
61e0dac50fSopenharmony_ci    auto display = DisplayManager::GetInstance().GetDefaultDisplay();
62e0dac50fSopenharmony_ci    if (display == nullptr) {
63e0dac50fSopenharmony_ci        WLOGFE("GetDefaultDisplay: failed!\n");
64e0dac50fSopenharmony_ci        return;
65e0dac50fSopenharmony_ci    }
66e0dac50fSopenharmony_ci    WLOGI("GetDefaultDisplay: id %" PRIu64", w %d, h %d, fps %u\n", display->GetId(), display->GetWidth(),
67e0dac50fSopenharmony_ci        display->GetHeight(), display->GetRefreshRate());
68e0dac50fSopenharmony_ci
69e0dac50fSopenharmony_ci    defaultId_ = display->GetId();
70e0dac50fSopenharmony_ci    defaultScreen_ = {0, 0, display->GetWidth(), display->GetHeight()};
71e0dac50fSopenharmony_ci    defaultImage_ = {display->GetWidth(), display->GetHeight()};
72e0dac50fSopenharmony_ci}
73e0dac50fSopenharmony_ci
74e0dac50fSopenharmony_civoid ScreenshotTest::TearDownTestCase()
75e0dac50fSopenharmony_ci{
76e0dac50fSopenharmony_ci}
77e0dac50fSopenharmony_ci
78e0dac50fSopenharmony_civoid ScreenshotTest::SetUp()
79e0dac50fSopenharmony_ci{
80e0dac50fSopenharmony_ci}
81e0dac50fSopenharmony_ci
82e0dac50fSopenharmony_civoid ScreenshotTest::TearDown()
83e0dac50fSopenharmony_ci{
84e0dac50fSopenharmony_ci}
85e0dac50fSopenharmony_ci
86e0dac50fSopenharmony_cinamespace {
87e0dac50fSopenharmony_ci/**
88e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
89e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot of default display's ID is valid
90e0dac50fSopenharmony_ci * @tc.type: FUNC
91e0dac50fSopenharmony_ci */
92e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShotValid01, Function | MediumTest | Level2)
93e0dac50fSopenharmony_ci{
94e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, DisplayManager::GetInstance().GetScreenshot(defaultId_));
95e0dac50fSopenharmony_ci}
96e0dac50fSopenharmony_ci
97e0dac50fSopenharmony_ci/**
98e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
99e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot of invalid display's ID is nullptr
100e0dac50fSopenharmony_ci * @tc.type: FUNC
101e0dac50fSopenharmony_ci */
102e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShotValid02, Function | MediumTest | Level2)
103e0dac50fSopenharmony_ci{
104e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, DisplayManager::GetInstance().GetScreenshot(invalidId_));
105e0dac50fSopenharmony_ci}
106e0dac50fSopenharmony_ci
107e0dac50fSopenharmony_ci/**
108e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
109e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot of default display's ID match default display's Media::Size
110e0dac50fSopenharmony_ci * @tc.type: FUNC
111e0dac50fSopenharmony_ci */
112e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShotValid03, Function | MediumTest | Level2)
113e0dac50fSopenharmony_ci{
114e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
115e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(defaultId_);
116e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, screenshot);
117e0dac50fSopenharmony_ci
118e0dac50fSopenharmony_ci    Media::Size screenSize = {screenshot->GetWidth(), screenshot->GetHeight()};
119e0dac50fSopenharmony_ci    ASSERT_TRUE(Utils::SizeEqualToDisplay(dm.GetDefaultDisplay(), screenSize));
120e0dac50fSopenharmony_ci}
121e0dac50fSopenharmony_ci
122e0dac50fSopenharmony_ci/**
123e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
124e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot created by default parameters is valid
125e0dac50fSopenharmony_ci * @tc.type: FUNC
126e0dac50fSopenharmony_ci */
127e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShotValid04, Function | MediumTest | Level2)
128e0dac50fSopenharmony_ci{
129e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
130e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(defaultId_, defaultScreen_,
131e0dac50fSopenharmony_ci                                                                   defaultImage_, defaultRot_);
132e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, screenshot);
133e0dac50fSopenharmony_ci}
134e0dac50fSopenharmony_ci
135e0dac50fSopenharmony_ci/**
136e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
137e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot match default imageSize
138e0dac50fSopenharmony_ci * @tc.type: FUNC
139e0dac50fSopenharmony_ci */
140e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShotValid05, Function | MediumTest | Level2)
141e0dac50fSopenharmony_ci{
142e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
143e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(defaultId_, defaultScreen_,
144e0dac50fSopenharmony_ci                                                                   defaultImage_, defaultRot_);
145e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, screenshot);
146e0dac50fSopenharmony_ci    Media::Size screenSize = {screenshot->GetWidth(), screenshot->GetHeight()};
147e0dac50fSopenharmony_ci    ASSERT_TRUE(Utils::SizeEqual(defaultImage_, screenSize));
148e0dac50fSopenharmony_ci
149e0dac50fSopenharmony_ci    Media::Size halfDefault_ = {defaultImage_.width / 2, defaultImage_.height / 2};
150e0dac50fSopenharmony_ci    Media::Rect halfRect = {defaultScreen_.left, defaultScreen_.top, halfDefault_.width, halfDefault_.height};
151e0dac50fSopenharmony_ci    screenshot = dm.GetScreenshot(defaultId_, halfRect, defaultImage_, defaultRot_);
152e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, screenshot);
153e0dac50fSopenharmony_ci    screenSize = {screenshot->GetWidth(), screenshot->GetHeight()};
154e0dac50fSopenharmony_ci    ASSERT_TRUE(Utils::SizeEqual(defaultImage_, screenSize));
155e0dac50fSopenharmony_ci
156e0dac50fSopenharmony_ci    Media::Size halfSize = {halfDefault_.width, halfDefault_.height};
157e0dac50fSopenharmony_ci    screenshot = dm.GetScreenshot(defaultId_, defaultScreen_, halfSize, defaultRot_);
158e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, screenshot);
159e0dac50fSopenharmony_ci    screenSize = {screenshot->GetWidth(), screenshot->GetHeight()};
160e0dac50fSopenharmony_ci    ASSERT_TRUE(Utils::SizeEqual(halfSize, screenSize));
161e0dac50fSopenharmony_ci}
162e0dac50fSopenharmony_ci
163e0dac50fSopenharmony_ci/**
164e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
165e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot created by default parameters match default display's Media::Size
166e0dac50fSopenharmony_ci * @tc.type: FUNC
167e0dac50fSopenharmony_ci */
168e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShotValid06, Function | MediumTest | Level2)
169e0dac50fSopenharmony_ci{
170e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
171e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(defaultId_, defaultScreen_,
172e0dac50fSopenharmony_ci                                                                   defaultImage_, defaultRot_);
173e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, screenshot);
174e0dac50fSopenharmony_ci    Media::Size screenSize = {screenshot->GetWidth(), screenshot->GetHeight()};
175e0dac50fSopenharmony_ci    ASSERT_TRUE(Utils::SizeEqualToDisplay(dm.GetDefaultDisplay(), screenSize));
176e0dac50fSopenharmony_ci}
177e0dac50fSopenharmony_ci
178e0dac50fSopenharmony_ci/**
179e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
180e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot created by invalid display ID is nullptr
181e0dac50fSopenharmony_ci * @tc.type: FUNC
182e0dac50fSopenharmony_ci */
183e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShot07, Function | MediumTest | Level2)
184e0dac50fSopenharmony_ci{
185e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
186e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(invalidId_, defaultScreen_,
187e0dac50fSopenharmony_ci                                                                   defaultImage_, defaultRot_);
188e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, screenshot);
189e0dac50fSopenharmony_ci}
190e0dac50fSopenharmony_ci
191e0dac50fSopenharmony_ci/**
192e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
193e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot created by invalid screenRect is nullptr
194e0dac50fSopenharmony_ci * @tc.type: FUNC
195e0dac50fSopenharmony_ci */
196e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShot08, Function | MediumTest | Level2)
197e0dac50fSopenharmony_ci{
198e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
199e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(defaultId_, invalidRect_,
200e0dac50fSopenharmony_ci                                                                   defaultImage_, defaultRot_);
201e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, screenshot);
202e0dac50fSopenharmony_ci    Media::Rect invalidScreen = {invalidRect_.left, defaultScreen_.top, defaultScreen_.width, defaultScreen_.height};
203e0dac50fSopenharmony_ci    screenshot = dm.GetScreenshot(defaultId_, invalidScreen, defaultImage_, defaultRot_);
204e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, screenshot);
205e0dac50fSopenharmony_ci    invalidScreen = {defaultScreen_.left, defaultScreen_.top, invalidRect_.width, defaultScreen_.height};
206e0dac50fSopenharmony_ci    screenshot = dm.GetScreenshot(defaultId_, invalidScreen, defaultImage_, defaultRot_);
207e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, screenshot);
208e0dac50fSopenharmony_ci}
209e0dac50fSopenharmony_ci
210e0dac50fSopenharmony_ci/**
211e0dac50fSopenharmony_ci * @tc.name: ScreenShotValid
212e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot created by invalid imageSize is nullptr
213e0dac50fSopenharmony_ci * @tc.type: FUNC
214e0dac50fSopenharmony_ci */
215e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShot09, Function | MediumTest | Level2)
216e0dac50fSopenharmony_ci{
217e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
218e0dac50fSopenharmony_ci    Media::Size invalidSize = {invalidRect_.width, invalidRect_.height};
219e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(defaultId_, defaultScreen_,
220e0dac50fSopenharmony_ci                                                                   invalidSize, defaultRot_);
221e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, screenshot);
222e0dac50fSopenharmony_ci    invalidSize = {invalidRect_.width, defaultScreen_.height};
223e0dac50fSopenharmony_ci    screenshot = dm.GetScreenshot(defaultId_, defaultScreen_, invalidSize, defaultRot_);
224e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, screenshot);
225e0dac50fSopenharmony_ci    invalidSize = {defaultScreen_.width, invalidRect_.height};
226e0dac50fSopenharmony_ci    screenshot = dm.GetScreenshot(defaultId_, defaultScreen_, invalidSize, defaultRot_);
227e0dac50fSopenharmony_ci    ASSERT_EQ(nullptr, screenshot);
228e0dac50fSopenharmony_ci}
229e0dac50fSopenharmony_ci
230e0dac50fSopenharmony_ci/**
231e0dac50fSopenharmony_ci * @tc.name: ScreenShotListener01
232e0dac50fSopenharmony_ci * @tc.desc: Check if screenshot listener info valid
233e0dac50fSopenharmony_ci * @tc.type: FUNC
234e0dac50fSopenharmony_ci * @tc.require: issueI5G62O
235e0dac50fSopenharmony_ci */
236e0dac50fSopenharmony_ciHWTEST_F(ScreenshotTest, ScreenShotListener01, Function | MediumTest | Level2)
237e0dac50fSopenharmony_ci{
238e0dac50fSopenharmony_ci    sptr<ScreenshotListenerFuture> screenShotListener = new ScreenshotListenerFuture();
239e0dac50fSopenharmony_ci
240e0dac50fSopenharmony_ci    auto& dm = DisplayManager::GetInstance();
241e0dac50fSopenharmony_ci    dm.RegisterScreenshotListener(screenShotListener);
242e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> screenshot = dm.GetScreenshot(defaultId_);
243e0dac50fSopenharmony_ci    auto info = screenShotListener->future_.GetResult(1000);
244e0dac50fSopenharmony_ci
245e0dac50fSopenharmony_ci    ASSERT_NE(nullptr, screenshot);
246e0dac50fSopenharmony_ci    ASSERT_EQ(info.GetDisplayId(), defaultId_);
247e0dac50fSopenharmony_ci    ASSERT_GT(info.GetTrigger().size(), 0UL);
248e0dac50fSopenharmony_ci
249e0dac50fSopenharmony_ci    dm.UnregisterScreenshotListener(screenShotListener);
250e0dac50fSopenharmony_ci}
251e0dac50fSopenharmony_ci} // namespace
252e0dac50fSopenharmony_ci} // namespace Rosen
253e0dac50fSopenharmony_ci} // namespace OHOS