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 #include "mock_pixel_map.h"
17 
18 using ::testing::_;
19 using ::testing::Return;
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Effect {
24 constexpr int32_t WIDTH = 960;
25 constexpr int32_t HEIGHT = 1280;
26 constexpr PixelFormat PIXEL_FORMAT = PixelFormat::RGBA_8888;
27 constexpr int32_t PIXEL_BYTES = 4;
28 
MockPixelMap()29 MockPixelMap::MockPixelMap() : MockPixelMap(WIDTH, HEIGHT)
30 {
31 }
32 
MockPixelMap(int32_t width,int32_t height)33 MockPixelMap::MockPixelMap(int32_t width, int32_t height)
34 {
35     int32_t rowStride = width * PIXEL_BYTES;
36     int32_t byteCount = rowStride * height;
37     buffer = malloc(byteCount);
38     ON_CALL(*this, GetWidth()).WillByDefault(Return(width));
39     ON_CALL(*this, GetHeight()).WillByDefault(Return(height));
40     ON_CALL(*this, GetPixelFormat()).WillByDefault(Return(PIXEL_FORMAT));
41     ON_CALL(*this, GetRowBytes()).WillByDefault(Return(rowStride));
42     ON_CALL(*this, GetRowStride()).WillByDefault(Return(rowStride));
43     ON_CALL(*this, GetByteCount()).WillByDefault(Return(byteCount));
44     ON_CALL(*this, GetCapacity()).WillByDefault(Return(byteCount));
45     ON_CALL(*this, GetPixels()).WillByDefault(Return(static_cast<const uint8_t *>(buffer)));
46     ON_CALL(*this, GetPixelBytes()).WillByDefault(Return(PIXEL_BYTES));
47     ON_CALL(*this, GetImageInfo(_)).WillByDefault([&width, &height](ImageInfo &imageInfo) {
48         imageInfo.size.width = width;
49         imageInfo.size.height = height;
50         imageInfo.pixelFormat = PIXEL_FORMAT;
51     });
52     ON_CALL(*this, GetAllocatorType()).WillByDefault(Return(AllocatorType::HEAP_ALLOC));
53     ON_CALL(*this, SetPixelsAddr(_, _, _, _, _)).WillByDefault(
54         [this](void *addr, void *context, uint32_t size, AllocatorType type, CustomFreePixelMap func) {
55             if (this->buffer != nullptr) {
56                 free(this->buffer);
57             }
58             buffer = addr;
59         });
60 }
61 
~MockPixelMap()62 MockPixelMap::~MockPixelMap()
63 {
64     if (buffer != nullptr) {
65         free(buffer);
66         buffer = nullptr;
67     }
68 }
69 } // namespace Effect
70 } // namespace Media
71 } // namespace OHOS