1 /*
2  * Copyright (c) 2021-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 <gtest/gtest.h>
17 #include "display_manager.h"
18 #include "pixel_map.h"
19 
20 #include <securec.h>
21 
22 #include "mock_display_manager_adapter.h"
23 #include "singleton_mocker.h"
24 #include "common_test_utils.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace Rosen {
31 using Mocker = SingletonMocker<DisplayManagerAdapter, MockDisplayManagerAdapter>;
32 class ScreenshotTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     virtual void SetUp() override;
37     virtual void TearDown() override;
38 };
SetUpTestCase()39 void ScreenshotTest::SetUpTestCase()
40 {
41     CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0);
42     const char** perms = new const char *[1];
43     perms[0] = "ohos.permission.CAPTURE_SCREEN";
44     CommonTestUtils::SetAceessTokenPermission("DisplayManagerServiceTest", perms, 1);
45 }
46 
TearDownTestCase()47 void ScreenshotTest::TearDownTestCase()
48 {
49 }
50 
SetUp()51 void ScreenshotTest::SetUp()
52 {
53 }
54 
TearDown()55 void ScreenshotTest::TearDown()
56 {
57 }
58 
59 namespace {
60 /**
61  * @tc.name: GetScreenshot_default
62  * @tc.desc: SetWindowRect/GetWindowRect
63  * @tc.type: FUNC
64  */
65 HWTEST_F(ScreenshotTest, GetScreenshot_default, Function | SmallTest | Level2)
66 {
67     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
68 
69     EXPECT_CALL(m->Mock(), GetDefaultDisplayInfo()).Times(1).WillOnce(Return(nullptr));
70 
71     DisplayManager::GetInstance().GetDefaultDisplayId();
72 
73     EXPECT_CALL(m->Mock(), GetDisplaySnapshot(_, _, _)).Times(1).WillOnce(Return(nullptr));
74 
75     ASSERT_EQ(nullptr, DisplayManager::GetInstance().GetScreenshot(0));
76 }
77 
78 HWTEST_F(ScreenshotTest, GetScreenshot_01, Function | MediumTest | Level2)
79 {
80     std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
81 
82     EXPECT_CALL(m->Mock(), GetDefaultDisplayInfo()).Times(1).WillOnce(Return(nullptr));
83     DisplayManager::GetInstance().GetDefaultDisplayId();
84 
85     EXPECT_CALL(m->Mock(), GetDisplaySnapshot(_, _, _)).Times(1).WillOnce(Return(CommonTestUtils::CreatePixelMap()));
86     auto screenshot = DisplayManager::GetInstance().GetScreenshot(0);
87     ASSERT_NE(nullptr, screenshot);
88 
89     uint32_t width = screenshot->GetWidth();
90     uint32_t height = screenshot->GetHeight();
91     ASSERT_EQ(width, CommonTestUtils::TEST_IMAGE_WIDTH);
92     ASSERT_EQ(height, CommonTestUtils::TEST_IMAGE_HEIGHT);
93 }
94 }
95 } // namespace Rosen
96 } // namespace OHOS
97