1 /*
2  * Copyright (C) 2024 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 #ifndef MOCK_DATA_STREAM_H
17 #define MOCK_DATA_STREAM_H
18 
19 #include "source_stream.h"
20 
21 namespace OHOS {
22 namespace ImagePlugin {
23 static constexpr size_t NUMBER_ONE = 1;
24 static constexpr size_t NUMBER_TWO = 2;
25 class MockInputDataStream : public Media::SourceStream {
26 public:
27     MockInputDataStream() = default;
~MockInputDataStream()28     ~MockInputDataStream() {}
29 
UpdateData(const uint8_t * data,uint32_t size,bool isCompleted)30     uint32_t UpdateData(const uint8_t *data, uint32_t size, bool isCompleted) override
31     {
32         return Media::ERR_IMAGE_DATA_UNSUPPORT;
33     }
34 
Read(uint32_t desiredSize,DataStreamBuffer & outData)35     bool Read(uint32_t desiredSize, DataStreamBuffer &outData) override
36     {
37         if (streamSize == NUMBER_ONE) {
38             streamBuffer = std::make_shared<uint8_t>(streamSize);
39             outData.inputStreamBuffer = streamBuffer.get();
40         } else if (streamSize == NUMBER_TWO) {
41             outData.dataSize = streamSize;
42         }
43         return returnValue_;
44     }
45 
Read(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)46     bool Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override
47     {
48         return returnValue_;
49     }
50 
Peek(uint32_t desiredSize,DataStreamBuffer & outData)51     bool Peek(uint32_t desiredSize, DataStreamBuffer &outData) override
52     {
53         return returnValue_;
54     }
55 
Peek(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)56     bool Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override
57     {
58         return returnValue_;
59     }
60 
Tell()61     uint32_t Tell() override
62     {
63         return 0;
64     }
65 
Seek(uint32_t position)66     bool Seek(uint32_t position) override
67     {
68         return returnValue_;
69     }
70 
GetStreamType()71     uint32_t GetStreamType() override
72     {
73         return -1;
74     }
75 
GetDataPtr()76     uint8_t *GetDataPtr() override
77     {
78         return nullptr;
79     }
80 
IsStreamCompleted()81     bool IsStreamCompleted() override
82     {
83         return returnValue_;
84     }
85 
GetStreamSize()86     size_t GetStreamSize() override
87     {
88         return streamSize;
89     }
90 
SetReturn(bool returnValue)91     void SetReturn(bool returnValue)
92     {
93         returnValue_ = returnValue;
94     }
95 
SetStreamSize(size_t size)96     void SetStreamSize(size_t size)
97     {
98         streamSize = size;
99     }
100 
101 private:
102     bool returnValue_ = false;
103     size_t streamSize = 0;
104     std::shared_ptr<uint8_t> streamBuffer = nullptr;
105 };
106 
107 class MockOutputDataStream : public OutputDataStream {
108 public:
109     MockOutputDataStream() = default;
~MockOutputDataStream()110     ~MockOutputDataStream() {}
111 
Write(const uint8_t * buffer,uint32_t size)112     bool Write(const uint8_t *buffer, uint32_t size)
113     {
114         return bool_;
115     }
116 
117 private:
118     bool bool_;
119 };
120 } // namespace ImagePlugin
121 } // namespace OHOS
122 
123 #endif // MOCK_DATA_STREAM_H