• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_NOTIFICATION_PIXEL_MAP_BUILDER_H
17 #define MOCK_NOTIFICATION_PIXEL_MAP_BUILDER_H
18 
19 #include "mock_fuzz_object.h"
20 
21 #include "pixel_map.h"
22 #include <fstream>
23 
24 namespace OHOS {
25 namespace Notification {
26 
27 constexpr uint32_t MAX_LENGTH_MODULO = 1024;
28 constexpr uint32_t PIXELFORMAT_MODULO = 8;
29 constexpr uint32_t ALPHATYPE_MODULO = 4;
30 constexpr uint32_t SCALEMODE_MODULO = 2;
31 
32 std::unique_ptr<Media::PixelMap> GetPixelMapFromOpts(FuzzedDataProvider *fdp,
33     Media::PixelFormat pixelFormat = Media::PixelFormat::UNKNOWN)
34 {
35     int32_t width = fdp->ConsumeIntegralInRange<int32_t>(0, MAX_LENGTH_MODULO);
36     int32_t height = fdp->ConsumeIntegralInRange<int32_t>(0, MAX_LENGTH_MODULO);
37     Media::InitializationOptions opts;
38     opts.size.width = width;
39     opts.size.height = height;
40     opts.srcPixelFormat = pixelFormat == Media::PixelFormat::UNKNOWN ?
41         static_cast<Media::PixelFormat>(fdp->ConsumeIntegralInRange<int32_t>(0, PIXELFORMAT_MODULO)) : pixelFormat;
42     opts.pixelFormat = pixelFormat == Media::PixelFormat::UNKNOWN ?
43         static_cast<Media::PixelFormat>(fdp->ConsumeIntegralInRange<int32_t>(0, PIXELFORMAT_MODULO)) : pixelFormat;
44     opts.alphaType = static_cast<Media::AlphaType>(fdp->ConsumeIntegralInRange<int32_t>(0, ALPHATYPE_MODULO));
45     opts.scaleMode = static_cast<Media::ScaleMode>(fdp->ConsumeIntegralInRange<int32_t>(0, SCALEMODE_MODULO));
46     opts.editable = fdp->ConsumeBool();
47     opts.useSourceIfMatch = fdp->ConsumeBool();
48     return Media::PixelMap::Create(opts);
49 }
50 
51 template <>
BuildSharedPtr(FuzzedDataProvider * fdp)52 std::shared_ptr<Media::PixelMap> ObjectBuilder<Media::PixelMap>::BuildSharedPtr(FuzzedDataProvider *fdp)
53 {
54     auto pixelMap = GetPixelMapFromOpts(fdp);
55     if (pixelMap == nullptr) {
56         return nullptr;
57     }
58     ANS_LOGE("Build mock veriables");
59     return std::shared_ptr<Media::PixelMap>(std::move(pixelMap.release()));
60 }
61 
62 }  // namespace Notification
63 }  // namespace OHOS
64 
65 #endif  // MOCK_NOTIFICATION_PIXEL_MAP_BUILDER_H
66