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