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 "snapshot_controller.h"
18#include "wm_common.h"
19#include "iremote_object_mocker.h"
20#include "common_test_utils.h"
21
22using namespace testing;
23using namespace testing::ext;
24
25namespace OHOS {
26namespace Rosen {
27class WindowSnapshotTest : public testing::Test {
28public:
29    static void SetUpTestCase();
30    static void TearDownTestCase();
31    virtual void SetUp() override;
32    virtual void TearDown() override;
33};
34
35void WindowSnapshotTest::SetUpTestCase()
36{
37}
38
39void WindowSnapshotTest::TearDownTestCase()
40{
41}
42
43void WindowSnapshotTest::SetUp()
44{
45}
46
47void WindowSnapshotTest::TearDown()
48{
49}
50
51namespace {
52/**
53 * @tc.name: GetSnapshot
54 * @tc.desc: GetSnapshot when parameter abilityToken is nullptr
55 * @tc.type: FUNC
56 */
57HWTEST_F(WindowSnapshotTest, GetSnapshot01, Function | SmallTest | Level3)
58{
59    sptr<WindowRoot> root = nullptr;
60    std::shared_ptr<AppExecFwk::EventHandler> handler = nullptr;
61    sptr<SnapshotController> snapshotController_ = new SnapshotController(root, handler);
62    AAFwk::Snapshot snapshot_;
63    ASSERT_EQ(static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), snapshotController_->GetSnapshot(nullptr, snapshot_));
64}
65
66/**
67 * @tc.name: GetSnapshot
68 * @tc.desc: GetSnapshot when parameter abilityToken is nullptr
69 * @tc.type: FUNC
70 */
71HWTEST_F(WindowSnapshotTest, GetSnapshot02, Function | SmallTest | Level3)
72{
73    sptr<WindowRoot> root = nullptr;
74    std::shared_ptr<AppExecFwk::EventHandler> handler = nullptr;
75    sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
76
77    sptr<SnapshotController> snapshotController_ = new SnapshotController(root, handler);
78    AAFwk::Snapshot snapshot_;
79    ASSERT_EQ(static_cast<int32_t>(WMError::WM_ERROR_NULLPTR),
80        snapshotController_->GetSnapshot(iRemoteObjectMocker, snapshot_));
81}
82
83/**
84 * @tc.name: GetSnapshot
85 * @tc.desc: GetSnapshot when parameter abilityToken is nullptr
86 * @tc.type: FUNC
87 */
88HWTEST_F(WindowSnapshotTest, GetSnapshot03, Function | SmallTest | Level3)
89{
90    auto runner = AppExecFwk::EventRunner::Create("TestRunner");
91    auto handler = std::make_shared<AppExecFwk::EventHandler>(runner);
92    sptr<WindowRoot> root = nullptr;
93    sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
94
95    sptr<SnapshotController> snapshotController_ = new SnapshotController(root, handler);
96    AAFwk::Snapshot snapshot_;
97    ASSERT_EQ(static_cast<int32_t>(WMError::WM_ERROR_NULLPTR),
98        snapshotController_->GetSnapshot(iRemoteObjectMocker, snapshot_));
99}
100
101/**
102 * @tc.name: GetSnapshot
103 * @tc.desc: GetSnapshot when parameter abilityToken is nullptr
104 * @tc.type: FUNC
105 */
106HWTEST_F(WindowSnapshotTest, GetSnapshot04, Function | SmallTest | Level3)
107{
108    auto runner = AppExecFwk::EventRunner::Create("TestRunner");
109    auto handler = std::make_shared<AppExecFwk::EventHandler>(runner);
110
111    sptr<WindowRoot> root = new WindowRoot([](Event event, const sptr<IRemoteObject>& remoteObject) {});
112    sptr<WindowProperty> property = new WindowProperty();
113    sptr<WindowNode> node = new WindowNode();
114    node->SetWindowProperty(property);
115    root->windowNodeMap_.insert(std::make_pair(0, node));
116
117    sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
118    node->abilityToken_ = iRemoteObjectMocker;
119
120    sptr<SnapshotController> snapshotController_ = new SnapshotController(root, handler);
121    AAFwk::Snapshot snapshot_;
122    ASSERT_EQ(static_cast<int32_t>(WMError::WM_ERROR_NULLPTR),
123        snapshotController_->GetSnapshot(iRemoteObjectMocker, snapshot_));
124
125    sptr<IRemoteObject> iRemoteObjectMockerInvalid = new IRemoteObjectMocker();
126    ASSERT_EQ(static_cast<int32_t>(WMError::WM_ERROR_NULLPTR),
127        snapshotController_->GetSnapshot(iRemoteObjectMockerInvalid, snapshot_));
128}
129}
130} // namespace Rosen
131} // namespace OHOS
132