• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: testCreate002
185  * @tc.desc: test Create
186  * @tc.type: FUNC
187  * @tc.require: I5Y2VH
188  */
189 HWTEST_F(SnapshotTest, testCreate002, TestSize.Level3)
190 {
191     DTEST_LOG << "SnapshotTest testCreate002 start" << std::endl;
192     Snapshot snapshot;
193     std::vector<uint8_t> data(sizeof(uint32_t), 0);
194     uint32_t msgSzie = 10;
195     memcpy(data.data(), &msgSzie, sizeof(uint32_t));
196 
197     std::unique_ptr<Snapshot> ret = snapshot.Create(data);
198     EXPECT_EQ(nullptr, ret);
199     data.emplace_back(1);
200     EXPECT_EQ(nullptr, ret);
201     DTEST_LOG << "SnapshotTest testCreate002 end" << std::endl;
202 }
203 
204 /**
205  * @tc.name: testCreate003
206  * @tc.desc: test Create
207  * @tc.type: FUNC
208  * @tc.require: I5Y2VH
209  */
210 HWTEST_F(SnapshotTest, testCreate003, TestSize.Level3)
211 {
212     DTEST_LOG << "SnapshotTest testCreate003 start" << std::endl;
213     Snapshot snapshot;
214     std::vector<uint8_t> data(12, 0); // totalSize = 12
215     uint32_t msgSize = 8; // msgSize + sizeof(uint32_t) = 8 + 4 = 12
216     memcpy(data.data(), &msgSize, sizeof(uint32_t));
217 
218     std::unique_ptr<Snapshot> ret = snapshot.Create(data);
219     EXPECT_EQ(nullptr, ret);
220     data.emplace_back(1);
221     EXPECT_EQ(nullptr, ret);
222     DTEST_LOG << "SnapshotTest testCreate003 end" << std::endl;
223 }
224 
225 /**
226  * @tc.name: testCreate004
227  * @tc.desc: test Create
228  * @tc.type: FUNC
229  * @tc.require: I5Y2VH
230  */
231 HWTEST_F(SnapshotTest, testCreate004, TestSize.Level3)
232 {
233     DTEST_LOG << "SnapshotTest testCreate004 start" << std::endl;
234     Snapshot snapshot;
235     std::vector<uint8_t> data(20, 0); // totalSize = 20
236     uint32_t msgSize = 10; // msgSize + sizeof(uint32_t) = 10 + 4 = 14 < 20
237     memcpy(data.data(), &msgSize, sizeof(uint32_t));
238 
239     std::unique_ptr<Snapshot> ret = snapshot.Create(data);
240     EXPECT_EQ(nullptr, ret);
241     data.emplace_back(1);
242     EXPECT_EQ(nullptr, ret);
243     DTEST_LOG << "SnapshotTest testCreate004 end" << std::endl;
244 }
245 
246 /**
247  * @tc.name: testCreate005
248  * @tc.desc: test Create
249  * @tc.type: FUNC
250  * @tc.require: I5Y2VH
251  */
252 HWTEST_F(SnapshotTest, testCreate005, TestSize.Level3)
253 {
254     DTEST_LOG << "SnapshotTest testCreate005 start" << std::endl;
255     Snapshot snapshot;
256     std::vector<uint8_t> data(12, 0); // totalSize = 12
257     uint32_t msgSize = 6; // msgSize + sizeof(uint32_t) = 6 + 4 = 10 < totalSize (12)
258     memcpy(data.data(), &msgSize, sizeof(uint32_t));
259 
260     std::unique_ptr<Snapshot> ret = snapshot.Create(data);
261     EXPECT_EQ(nullptr, ret);
262     data.emplace_back(1);
263     EXPECT_EQ(nullptr, ret);
264     DTEST_LOG << "SnapshotTest testCreate005 end" << std::endl;
265 }
266 
267 /**
268  * @tc.name: testGetCreatedTime001
269  * @tc.desc: test GetCreatedTime
270  * @tc.type: FUNC
271  * @tc.require: I5Y2VH
272  */
273 HWTEST_F(SnapshotTest, testGetCreatedTime001, TestSize.Level3)
274 {
275     DTEST_LOG << "SnapshotTest testGetCreatedTime001 start" << std::endl;
276     Snapshot snapshot;
277     int64_t ret = snapshot.GetCreatedTime();
278     EXPECT_EQ(0, ret);
279     DTEST_LOG << "SnapshotTest testGetCreatedTime001 end" << std::endl;
280 }
281 
282 /**
283  * @tc.name: testGetLastAccessTime001
284  * @tc.desc: test GetLastAccessTime
285  * @tc.type: FUNC
286  * @tc.require: I5Y2VH
287  */
288 HWTEST_F(SnapshotTest, testGetLastAccessTime001, TestSize.Level3)
289 {
290     DTEST_LOG << "SnapshotTest testGetCreatedTime001 start" << std::endl;
291     Snapshot snapshot;
292     int64_t ret = snapshot.GetLastAccessTime();
293     EXPECT_EQ(0, ret);
294     DTEST_LOG << "SnapshotTest testGetLastAccessTime001 end" << std::endl;
295 }
296 
297 /**
298  * @tc.name: testUpdateLastAccessTime001
299  * @tc.desc: test UpdateLastAccessTime
300  * @tc.type: FUNC
301  * @tc.require: I5Y2VH
302  */
303 HWTEST_F(SnapshotTest, testUpdateLastAccessTime001, TestSize.Level3)
304 {
305     DTEST_LOG << "SnapshotTest testUpdateLastAccessTime001 start" << std::endl;
306     Snapshot snapshot;
307     snapshot.UpdateLastAccessTime(TEST_PARCEL_WRITE_VALUE);
308     EXPECT_EQ((int64_t)TEST_PARCEL_WRITE_VALUE, snapshot.lastAccessTime_);
309     DTEST_LOG << "SnapshotTest testUpdateLastAccessTime001 end" << std::endl;
310 }
311 
312 /**
313  * @tc.name: testWritePixelMap001
314  * @tc.desc: test WritePixelMap
315  * @tc.type: FUNC
316  * @tc.require: I5Y2VH
317  */
318 HWTEST_F(SnapshotTest, testWritePixelMap001, TestSize.Level3)
319 {
320     DTEST_LOG << "SnapshotTest testWritePixelMap001 start" << std::endl;
321     Snapshot snapshot;
322     MessageParcel data;
323     auto ret = snapshot.WritePixelMap(data);
324     EXPECT_EQ(ret, false);
325     DTEST_LOG << "SnapshotTest testWritePixelMap001 end" << std::endl;
326 }
327 } // DistributedSchedule
328 } // namespace OHOS