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 <ctime>
17 #include <gtest/gtest.h>
18
19 #include "directory_ex.h"
20 #include "hilog_wrapper.h"
21 #include "image_packer.h"
22 #include "pixel_map.h"
23 #include "wallpaper_manager.h"
24 #include "wallpaper_manager_kits.h"
25
26 constexpr int LOCKSCREEN = 1;
27 constexpr int HUNDRED = 100;
28 using namespace testing::ext;
29 using namespace testing;
30 using namespace OHOS::Media;
31 using namespace OHOS::HiviewDFX;
32 using namespace OHOS::MiscServices;
33
34 namespace OHOS {
35 namespace WallpaperMgrService {
36 constexpr const char *URI = "/data/test/theme/wallpaper/wallpaper_test.JPG";
37
38 class WallpaperPermissionTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 static void CreateTempImage();
45 static std::shared_ptr<PixelMap> CreateTempPixelMap();
46 };
47 const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
48 "\"SCHEMA_MODE\":\"STRICT\","
49 "\"SCHEMA_SKIPSIZE\":0,"
50 "\"SCHEMA_DEFINE\":{"
51 "\"age\":\"INTEGER, NOT NULL\""
52 "},"
53 "\"SCHEMA_INDEXES\":[\"$.age\"]}";
54
SetUpTestCase(void)55 void WallpaperPermissionTest::SetUpTestCase(void)
56 {
57 CreateTempImage();
58 HILOG_INFO("SetUpPermissionTestCase");
59 }
60
TearDownTestCase(void)61 void WallpaperPermissionTest::TearDownTestCase(void)
62 {
63 HILOG_INFO("PermissionTestTearDownTestCase");
64 }
65
SetUp(void)66 void WallpaperPermissionTest::SetUp(void)
67 {
68 }
69
TearDown(void)70 void WallpaperPermissionTest::TearDown(void)
71 {
72 }
73
CreateTempImage()74 void WallpaperPermissionTest::CreateTempImage()
75 {
76 std::shared_ptr<PixelMap> pixelMap = CreateTempPixelMap();
77 ImagePacker imagePacker;
78 PackOption option;
79 option.format = "image/jpeg";
80 option.quality = HUNDRED;
81 option.numberHint = 1;
82 std::set<std::string> formats;
83 imagePacker.GetSupportedFormats(formats);
84 imagePacker.StartPacking(URI, option);
85 HILOG_INFO("AddImage start");
86 imagePacker.AddImage(*pixelMap);
87 int64_t packedSize = 0;
88 HILOG_INFO("FinalizePacking start");
89 imagePacker.FinalizePacking(packedSize);
90 if (packedSize == 0) {
91 HILOG_INFO("FinalizePacking error");
92 }
93 }
94
CreateTempPixelMap()95 std::shared_ptr<PixelMap> WallpaperPermissionTest::CreateTempPixelMap()
96 {
97 uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
98 InitializationOptions opts = { { 5, 7 }, OHOS::Media::PixelFormat::ARGB_8888 };
99 std::unique_ptr<PixelMap> uniquePixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
100 std::shared_ptr<PixelMap> pixelMap = std::move(uniquePixelMap);
101 return pixelMap;
102 }
103
104 /********************* ResetWallpaper *********************/
105 /**
106 * @tc.name: ResetPermission001
107 * @tc.desc: Reset wallpaper throw permission error.
108 * @tc.type: FUNC
109 * @tc.require: issueI60MT1
110 */
111 HWTEST_F(WallpaperPermissionTest, ResetPermission001, TestSize.Level1)
112 {
113 HILOG_INFO("ResetPermission001 begin");
114 ApiInfo apiInfo{ false, false };
115 ErrorCode wallpaperErrorCode =
116 OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
117 EXPECT_EQ(wallpaperErrorCode, E_NO_PERMISSION) << "throw permission error successfully";
118 }
119 /********************* ResetWallpaper *********************/
120
121 /********************* GetFile *********************/
122 /**
123 * @tc.name: GetFilePermission001
124 * @tc.desc: GetFile with wallpaperType[1] throw permission error.
125 * @tc.type: FUNC
126 * @tc.require: issueI60MT1
127 */
128 HWTEST_F(WallpaperPermissionTest, GetFilePermission001, TestSize.Level0)
129 {
130 HILOG_INFO("GetFilePermission001 begin");
131 int32_t wallpaperFd = 0;
132 ErrorCode wallpaperErrorCode =
133 OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().GetFile(LOCKSCREEN, wallpaperFd);
134 EXPECT_EQ(wallpaperErrorCode, E_NO_PERMISSION) << "throw permission error successfully";
135 }
136 /********************* GetFile *********************/
137
138 /********************* GetPixelMap *********************/
139 /**
140 * @tc.name: GetPixelMapPermission001
141 * @tc.desc: GetPixelMap with wallpaperType[1] throw permission error.
142 * @tc.type: FUNC
143 * @tc.require: issueI60MT1
144 */
145 HWTEST_F(WallpaperPermissionTest, GetPixelMapPermission001, TestSize.Level0)
146 {
147 HILOG_INFO("GetPixelMapPermission001 begin");
148 ApiInfo apiInfo{ false, false };
149 std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
150 ErrorCode wallpaperErrorCode =
151 OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().GetPixelMap(LOCKSCREEN, apiInfo, pixelMap);
152 EXPECT_EQ(wallpaperErrorCode, E_NO_PERMISSION) << "throw permission error successfully";
153 }
154 /********************* GetPixelMap *********************/
155
156 /********************* SetWallpaperByMap *********************/
157 /**
158 * @tc.name: SetWallpaperByMapPermission001
159 * @tc.desc: SetWallpaperByMap with wallpaperType[1] throw permission error.
160 * @tc.type: FUNC
161 * @tc.require: issueI60MT1
162 */
163 HWTEST_F(WallpaperPermissionTest, SetWallpaperByMapPermission001, TestSize.Level0)
164 {
165 HILOG_INFO("SetWallpaperByMapPermission001 begin");
166 std::shared_ptr<PixelMap> pixelMap = WallpaperPermissionTest::CreateTempPixelMap();
167 ApiInfo apiInfo{ false, false };
168 ErrorCode wallpaperErrorCode =
169 OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(pixelMap, 2, apiInfo);
170 EXPECT_EQ(wallpaperErrorCode, E_NO_PERMISSION) << "throw permission error successfully";
171 }
172 /********************* SetWallpaperByMap *********************/
173
174 /********************* SetWallpaperByUri *********************/
175 /**
176 * @tc.name: SetWallpaperByUriPermission001
177 * @tc.desc: SetWallpaperByUri with wallpaperType[1] throw permission error.
178 * @tc.type: FUNC
179 * @tc.require: issueI60MT1
180 */
181 HWTEST_F(WallpaperPermissionTest, SetWallpaperByUriPermission001, TestSize.Level0)
182 {
183 HILOG_INFO("SetWallpaperByUriPermission001 begin");
184 ApiInfo apiInfo{ false, false };
185 ErrorCode wallpaperErrorCode =
186 OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(URI, LOCKSCREEN, apiInfo);
187 EXPECT_EQ(wallpaperErrorCode, E_NO_PERMISSION) << "throw permission error successfully";
188 HILOG_INFO("SetWallpaperByUriPermission001 end");
189 }
190 /********************* SetWallpaperByUri *********************/
191 } // namespace WallpaperMgrService
192 } // namespace OHOS