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 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 class WindowSnapshotTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31     virtual void SetUp() override;
32     virtual void TearDown() override;
33 };
34 
SetUpTestCase()35 void WindowSnapshotTest::SetUpTestCase()
36 {
37 }
38 
TearDownTestCase()39 void WindowSnapshotTest::TearDownTestCase()
40 {
41 }
42 
SetUp()43 void WindowSnapshotTest::SetUp()
44 {
45 }
46 
TearDown()47 void WindowSnapshotTest::TearDown()
48 {
49 }
50 
51 namespace {
52 /**
53  * @tc.name: GetSnapshot
54  * @tc.desc: GetSnapshot when parameter abilityToken is nullptr
55  * @tc.type: FUNC
56  */
57 HWTEST_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  */
71 HWTEST_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  */
88 HWTEST_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  */
106 HWTEST_F(WindowSnapshotTest, GetSnapshot04, Function | SmallTest | Level3)
107 {
108     auto runner = AppExecFwk::EventRunner::Create("TestRunner");
109     auto handler = std::make_shared<AppExecFwk::EventHandler>(runner);
110 
__anonad4868ae0202(Event event, const sptr<IRemoteObject>& remoteObject) 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 /**
131  * @tc.name: GetSnapshot
132  * @tc.desc: GetSnapshot when parameter abilityToken is nullptr
133  * @tc.type: FUNC
134  */
135 HWTEST_F(WindowSnapshotTest, GetSnapshot05, Function | SmallTest | Level3)
136 {
137     auto runner = AppExecFwk::EventRunner::Create("TestRunner");
138     auto handler = std::make_shared<AppExecFwk::EventHandler>(runner);
139 
__anonad4868ae0302(Event event, const sptr<IRemoteObject>& remoteObject) 140     sptr<WindowRoot> root = new WindowRoot([](Event event, const sptr<IRemoteObject>& remoteObject) {});
141     sptr<WindowNode> node = new WindowNode();
142     sptr<WindowProperty> property = sptr<WindowProperty>::MakeSptr();
143     EXPECT_NE(property, nullptr);
144     node->SetWindowProperty(property);
145     root->windowNodeMap_.insert(std::make_pair(0, node));
146 
147     sptr<IRemoteObject> iRemoteObjectMocker = new IRemoteObjectMocker();
148     node->abilityToken_ = iRemoteObjectMocker;
149     node->SetSnapshot(CommonTestUtils::CreatePixelMap());
150 
151     sptr<SnapshotController> snapshotController = new SnapshotController(root, handler);
152     AAFwk::Snapshot snapshot;
153     ASSERT_EQ(static_cast<int32_t>(WMError::WM_OK), snapshotController->GetSnapshot(iRemoteObjectMocker, snapshot));
154 }
155 }
156 } // namespace Rosen
157 } // namespace OHOS
158