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 "snapshot_test.h"
17 
18 #define private public
19 #include "mission/snapshot.h"
20 #undef private
21 #include "parcel_helper.h"
22 #include "test_log.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace DistributedSchedule {
29 namespace {
30 const std::string TAG = "Snapshot";
31 constexpr size_t TEST_PARCEL_WRITE_VALUE = 1;
32 }
SetUpTestCase()33 void SnapshotTest::SetUpTestCase()
34 {
35 }
36 
TearDownTestCase()37 void SnapshotTest::TearDownTestCase()
38 {
39 }
40 
SetUp()41 void SnapshotTest::SetUp()
42 {
43 }
44 
TearDown()45 void SnapshotTest::TearDown()
46 {
47 }
48 
49 /**
50  * @tc.name: testWriteToParcel001
51  * @tc.desc: write data to parcel
52  * @tc.type: FUNC
53  * @tc.require: I5O2P9
54  */
55 HWTEST_F(SnapshotTest, testWriteToParcel001, TestSize.Level1)
56 {
57     Snapshot snapshot;
58     MessageParcel data;
59     auto ret = snapshot.WriteToParcel(data);
60     EXPECT_EQ(ret, true);
61 }
62 
63 /**
64  * @tc.name: testWriteToParcel002
65  * @tc.desc: test WriteToParcel when rect_ is not nullptr
66  * @tc.type: FUNC
67  * @tc.require: I5Y2VH
68  */
69 HWTEST_F(SnapshotTest, testWriteToParcel002, TestSize.Level3)
70 {
71     DTEST_LOG << "SnapshotTest testWriteToParcel002 start" << std::endl;
72     Snapshot snapshot;
73     MessageParcel data;
74     snapshot.rect_ = std::make_unique<Rect>(0, 0, 0, 0);
75     bool ret = snapshot.WriteToParcel(data);
76     EXPECT_TRUE(ret);
77     DTEST_LOG << "SnapshotTest testWriteToParcel002 end" << std::endl;
78 }
79 
80 /**
81  * @tc.name: testWriteToParcel003
82  * @tc.desc: test WriteToParcel when windowBounds_ is not nullptr
83  * @tc.type: FUNC
84  * @tc.require: I5Y2VH
85  */
86 HWTEST_F(SnapshotTest, testWriteToParcel003, TestSize.Level3)
87 {
88     DTEST_LOG << "SnapshotTest testWriteToParcel003 start" << std::endl;
89     Snapshot snapshot;
90     MessageParcel data;
91     snapshot.rect_ = std::make_unique<Rect>(0, 0, 0, 0);
92     snapshot.windowBounds_ = std::make_unique<Rect>(0, 0, 0, 0);
93     bool ret = snapshot.WriteToParcel(data);
94     EXPECT_TRUE(ret);
95     DTEST_LOG << "SnapshotTest testWriteToParcel003 end" << std::endl;
96 }
97 
98 /**
99  * @tc.name: testWriteToParcel004
100  * @tc.desc: test WriteToParcel when pixelMap_ is not nullptr
101  * @tc.type: FUNC
102  * @tc.require: I5Y2VH
103  */
104 HWTEST_F(SnapshotTest, testWriteToParcel004, TestSize.Level3)
105 {
106     DTEST_LOG << "SnapshotTest testWriteToParcel004 start" << std::endl;
107     Snapshot snapshot;
108     MessageParcel data;
109     snapshot.rect_ = std::make_unique<Rect>(0, 0, 0, 0);
110     snapshot.windowBounds_ = std::make_unique<Rect>(0, 0, 0, 0);
111     uint8_t buffer = (uint8_t)TEST_PARCEL_WRITE_VALUE;
112     snapshot.pixelMap_ = snapshot.CreatePixelMap(&buffer, TEST_PARCEL_WRITE_VALUE);
113     /**
114      * @tc.steps: step1. WriteToParcel when pixelMap_ is not nullptr
115      */
116     bool ret = snapshot.WriteToParcel(data);
117     EXPECT_TRUE(ret);
118     /**
119      * @tc.steps: step2. FillSnapshot
120      */
121     std::unique_ptr<Snapshot> snapShotReturn = snapshot.FillSnapshot(data);
122     EXPECT_NE(nullptr, snapShotReturn);
123     /**
124      * @tc.steps: step3. CreatePixelMap when buffer == nullptr
125      */
126     std::unique_ptr<Media::PixelMap> pixelMap = snapshot.CreatePixelMap(nullptr, TEST_PARCEL_WRITE_VALUE);
127     EXPECT_EQ(nullptr, pixelMap);
128     DTEST_LOG << "SnapshotTest testWriteToParcel004 end" << std::endl;
129 }
130 
131 /**
132  * @tc.name: testFillSnapshot001
133  * @tc.desc: fill up a snapshot
134  * @tc.type: FUNC
135 * @tc.require: I5O2P9
136  */
137 HWTEST_F(SnapshotTest, testFillSnapshot001, TestSize.Level1)
138 {
139     Snapshot snapshot;
140     MessageParcel data;
141     auto ret = snapshot.FillSnapshot(data);
142     EXPECT_EQ(ret, nullptr);
143 }
144 
145 /**
146  * @tc.name: testWriteSnapshotInfo001
147  * @tc.desc: write a snapshot info
148  * @tc.type: FUNC
149 * @tc.require: I5O2P9
150  */
151 HWTEST_F(SnapshotTest, testWriteSnapshotInfo001, TestSize.Level1)
152 {
153     Snapshot snapshot;
154     MessageParcel data;
155     auto ret = snapshot.WriteSnapshotInfo(data);
156     EXPECT_EQ(ret, true);
157 }
158 
159 /**
160  * @tc.name: testCreate001
161  * @tc.desc: test Create when buffer is nullptr
162  * @tc.type: FUNC
163  * @tc.require: I5Y2VH
164  */
165 HWTEST_F(SnapshotTest, testCreate001, TestSize.Level3)
166 {
167     DTEST_LOG << "SnapshotTest testCreate001 start" << std::endl;
168     Snapshot snapshot;
169     std::vector<uint8_t> data;
170     /**
171      * @tc.steps: step1. Create when data is empty;
172      */
173     std::unique_ptr<Snapshot> ret = snapshot.Create(data);
174     EXPECT_EQ(nullptr, ret);
175     /**
176      * @tc.steps: step2. Create when data is not empty;
177      */
178     data.emplace_back(1);
179     EXPECT_EQ(nullptr, ret);
180     DTEST_LOG << "SnapshotTest testCreate001 end" << std::endl;
181 }
182 
183 /**
184  * @tc.name: testGetCreatedTime001
185  * @tc.desc: test GetCreatedTime
186  * @tc.type: FUNC
187  * @tc.require: I5Y2VH
188  */
189 HWTEST_F(SnapshotTest, testGetCreatedTime001, TestSize.Level3)
190 {
191     DTEST_LOG << "SnapshotTest testGetCreatedTime001 start" << std::endl;
192     Snapshot snapshot;
193     int64_t ret = snapshot.GetCreatedTime();
194     EXPECT_EQ(0, ret);
195     DTEST_LOG << "SnapshotTest testGetCreatedTime001 end" << std::endl;
196 }
197 
198 /**
199  * @tc.name: testGetLastAccessTime001
200  * @tc.desc: test GetLastAccessTime
201  * @tc.type: FUNC
202  * @tc.require: I5Y2VH
203  */
204 HWTEST_F(SnapshotTest, testGetLastAccessTime001, TestSize.Level3)
205 {
206     DTEST_LOG << "SnapshotTest testGetCreatedTime001 start" << std::endl;
207     Snapshot snapshot;
208     int64_t ret = snapshot.GetLastAccessTime();
209     EXPECT_EQ(0, ret);
210     DTEST_LOG << "SnapshotTest testGetLastAccessTime001 end" << std::endl;
211 }
212 
213 /**
214  * @tc.name: testUpdateLastAccessTime001
215  * @tc.desc: test UpdateLastAccessTime
216  * @tc.type: FUNC
217  * @tc.require: I5Y2VH
218  */
219 HWTEST_F(SnapshotTest, testUpdateLastAccessTime001, TestSize.Level3)
220 {
221     DTEST_LOG << "SnapshotTest testUpdateLastAccessTime001 start" << std::endl;
222     Snapshot snapshot;
223     snapshot.UpdateLastAccessTime(TEST_PARCEL_WRITE_VALUE);
224     EXPECT_EQ((int64_t)TEST_PARCEL_WRITE_VALUE, snapshot.lastAccessTime_);
225     DTEST_LOG << "SnapshotTest testUpdateLastAccessTime001 end" << std::endl;
226 }
227 } // DistributedSchedule
228 } // namespace OHOS