• 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 "mediascanner_unit_test.h"
17 #include "hilog/log.h"
18 
19 using OHOS::HiviewDFX::HiLog;
20 using OHOS::HiviewDFX::HiLogLabel;
21 
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Media {
29 namespace {
30     constexpr HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "ApplicationMediaScannerGtest"};
31     shared_ptr<IMediaScannerClient> g_msInstance = nullptr;
32 
33     int32_t g_callbackStatus(0);
34     int32_t g_filescanstatus(0);
35     bool g_isCallbackReceived(false);
36 
37     std::string g_callbackName("");
38     std::mutex g_mutex;
39     std::condition_variable g_condVar;
40     string g_prefixPath = "/storage/media/local/files";
41     const mode_t CHOWN_RW_UG = 0660;
42 } // namespace
43 
ApplicationCallback(const std::string & testCaseName)44 ApplicationCallback::ApplicationCallback(const std::string &testCaseName) : testCaseName_(testCaseName) {}
45 
OnScanFinished(const int32_t status,const std::string & uri,const std::string & path)46 void ApplicationCallback::OnScanFinished(const int32_t status, const std::string &uri, const std::string &path)
47 {
48     HiLog::Info(LABEL, "OnScanFinished invoked");
49     g_callbackStatus = status;
50     g_callbackName = testCaseName_;
51     g_isCallbackReceived = true;
52     g_condVar.notify_all();
53 }
54 
WaitForCallback()55 void MediaScannerUnitTest::WaitForCallback()
56 {
57     HiLog::Info(LABEL, "WaitForCallback invoked");
58     std::unique_lock<std::mutex> lock(g_mutex);
59     g_condVar.wait_until(lock, std::chrono::system_clock::now() + std::chrono::minutes(1),
60         []() { return g_isCallbackReceived == true; });
61 }
62 
SetUpTestCase(void)63 void MediaScannerUnitTest::SetUpTestCase(void)
64 {
65     HiLog::Info(LABEL, "SetUpTestCase invoked");
66     g_msInstance = MediaScannerHelperFactory::CreateScannerHelper();
67     if (g_msInstance == nullptr) {
68         HiLog::Error(LABEL, "Scanner instance not available");
69     }
70 }
71 
TearDownTestCase(void)72 void MediaScannerUnitTest::TearDownTestCase(void)
73 {
74     HiLog::Info(LABEL, "TearDownTestCase invoked");
75     g_msInstance = nullptr;
76 
77     if (remove("/storage/media/100/local/files/gtest_Audio1.aac") != 0
78         || remove("/storage/media/100/local/files/gtest_Image1.jpg") != 0
79         || remove("/storage/media/100/local/files/gtest_Video1.mp4") != 0
80         || remove("/storage/media/100/local/files/gtest_Text1.txt") != 0
81         || remove("/storage/media/100/local/files/.HiddenFile") != 0) {
82         HiLog::Error(LABEL, "Test files deletion failed");
83     }
84 }
85 
86 // SetUp:Execute before each test case
SetUp()87 void MediaScannerUnitTest::SetUp() {}
88 
TearDown(void)89 void MediaScannerUnitTest::TearDown(void) {}
90 
CreateFile(const string & filePath)91 bool CreateFile(const string &filePath)
92 {
93     HiLog::Info(LABEL, "Creating new file: %{private}s", filePath.c_str());
94     bool errCode = false;
95 
96     if (filePath.empty()) {
97         return errCode;
98     }
99 
100     ofstream file(filePath);
101     if (!file) {
102         MEDIA_ERR_LOG("Output file path could not be created");
103         return errCode;
104     }
105 
106     if (chmod(filePath.c_str(), CHOWN_RW_UG) == 0) {
107         errCode = true;
108     }
109 
110     file.close();
111 
112     return errCode;
113 }
114 
115 /*
116  * Feature: MediaScanner
117  * Function: Scan a directory with media files
118  * SubFunction: NA
119  * FunctionPoints: NA
120  * EnvConditions: NA
121  * CaseDescription:
122  */
123 HWTEST_F(MediaScannerUnitTest,  mediascanner_ScanDir_test_001, TestSize.Level0)
124 {
125     string path = g_prefixPath;
126     HiLog::Info(LABEL, "ScanDir test case for: %{private}s", path.c_str());
127 
128     EXPECT_EQ((g_msInstance != nullptr), true);
129     int result;
130     std::string testcaseName("mediascanner_ScanDir_test_001");
131     g_isCallbackReceived = false;
132     g_callbackStatus = -1;
133     g_callbackName = "";
134     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
135     result = g_msInstance->ScanDir(path, appCallback);
136     EXPECT_EQ(result, g_filescanstatus);
137 
138     if (result == 0) {
139         // Wait here for callback. If not callback for 2 mintues, will skip this step
140         WaitForCallback();
141         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
142         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
143     }
144 }
145 
146 /*
147  * Feature : MediaScannerUnitTest
148  * Function : Scan an image file
149  * SubFunction : NA
150  * FunctionPoints : NA
151  * EnvContions : NA
152  * CaseDescription : NA
153  */
154 HWTEST_F(MediaScannerUnitTest, mediascanner_ScanImage_Test_001, TestSize.Level0)
155 {
156     string path = g_prefixPath + "/gtest_Image1.jpg";
157     HiLog::Info(LABEL, "ScanFile test case for: %{private}s", path.c_str());
158 
159     bool createRes = CreateFile("/storage/media/100/local/files/gtest_Image1.jpg");
160     EXPECT_EQ(createRes, true);
161 
162     // scan the file
163     int result;
164     std::string testcaseName("mediascanner_ScanImage_Test_001");
165     g_isCallbackReceived = false;
166     g_callbackStatus = -1;
167     g_callbackName = "";
168     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
169 
170     EXPECT_EQ((g_msInstance != nullptr), true);
171     result = g_msInstance->ScanFile(path, appCallback);
172     EXPECT_EQ(result, g_filescanstatus);
173     if (result == 0) {
174         // Wait here for callback. If not callback for 2 mintues, will skip this step
175         WaitForCallback();
176         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
177         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
178     }
179 }
180 
181 /*
182  * Feature : MediaScannerUnitTest
183  * Function : Scan a video file
184  * SubFunction : NA
185  * FunctionPoints : NA
186  * EnvContions : NA
187  * CaseDescription : NA
188  */
189 HWTEST_F(MediaScannerUnitTest, mediascanner_ScanVideo_Test_001, TestSize.Level0)
190 {
191     string path = g_prefixPath + "/gtest_Video1.mp4";
192     HiLog::Info(LABEL, "ScanFile test case for: %{private}s", path.c_str());
193 
194     bool createRes = CreateFile("/storage/media/100/local/files/gtest_Video1.mp4");
195     EXPECT_EQ(createRes, true);
196 
197     int result;
198     std::string testcaseName("mediascanner_ScanVideo_Test_001");
199     g_isCallbackReceived = false;
200     g_callbackStatus = -1;
201     g_callbackName = "";
202     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
203 
204     EXPECT_EQ((g_msInstance != nullptr), true);
205     result = g_msInstance->ScanFile(path, appCallback);
206     EXPECT_EQ(result, g_filescanstatus);
207     if (result == 0) {
208         // Wait here for callback. If not callback for 2 mintues, will skip this step
209         WaitForCallback();
210         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
211         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
212     }
213 }
214 
215 /*
216  * Feature : MediaScannerUnitTest
217  * Function : Scan an audio file
218  * SubFunction : NA
219  * FunctionPoints : NA
220  * EnvContions : NA
221  * CaseDescription : NA
222  */
223 HWTEST_F(MediaScannerUnitTest, mediascanner_ScanAudio_Test_001, TestSize.Level0)
224 {
225     string path = g_prefixPath + "/gtest_Audio1.aac";
226     HiLog::Info(LABEL, "ScanFile test case for: %{private}s", path.c_str());
227 
228     bool createRes = CreateFile("/storage/media/100/local/files/gtest_Audio1.aac");
229     EXPECT_EQ(createRes, true);
230 
231     int result;
232     std::string testcaseName("mediascanner_ScanAudio_Test_001");
233     g_isCallbackReceived = false;
234     g_callbackStatus = -1;
235     g_callbackName = "";
236     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
237 
238     EXPECT_EQ((g_msInstance != nullptr), true);
239     result = g_msInstance->ScanFile(path, appCallback);
240     EXPECT_EQ(result, g_filescanstatus);
241     if (result == 0) {
242         // Wait here for callback. If not callback for 2 mintues, will skip this step
243         WaitForCallback();
244         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
245         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
246     }
247 }
248 
249 /*
250  * Feature : MediaScannerUnitTest
251  * Function : Scan a normal text file
252  * SubFunction : NA
253  * FunctionPoints : NA
254  * EnvContions : NA
255  * CaseDescription : NA
256  */
257 HWTEST_F(MediaScannerUnitTest, mediascanner_ScanTextFile_Test_001, TestSize.Level0)
258 {
259     string path = g_prefixPath + "/gtest_Text1.txt";
260     HiLog::Info(LABEL, "ScanFile test case for: %{private}s", path.c_str());
261 
262     bool createRes = CreateFile("/storage/media/100/local/files/gtest_Text1.txt");
263     EXPECT_EQ(createRes, true);
264 
265     int result;
266     std::string testcaseName("mediascanner_ScanTextFile_Test_001");
267     g_isCallbackReceived = false;
268     g_callbackStatus = -1;
269     g_callbackName = "";
270     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
271 
272     EXPECT_EQ((g_msInstance != nullptr), true);
273     result = g_msInstance->ScanFile(path, appCallback);
274     EXPECT_EQ(result, g_filescanstatus);
275     if (result == 0) {
276         // Wait here for callback. If not callback for 2 mintues, will skip this step
277         WaitForCallback();
278         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
279         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
280     }
281 }
282 
283 /*
284  * Feature : MediaScannerUnitTest
285  * Function : Scan a hidden file
286  * SubFunction : NA
287  * FunctionPoints : NA
288  * EnvContions : NA
289  * CaseDescription : NA
290  */
291 HWTEST_F(MediaScannerUnitTest, mediascanner_ScanHiddenFile_Test_001, TestSize.Level0)
292 {
293     string path = g_prefixPath + "/.HiddenFile";
294     HiLog::Info(LABEL, "ScanFile test case for: %{private}s", path.c_str());
295 
296     bool createRes = CreateFile("/storage/media/100/local/files/.HiddenFile");
297     EXPECT_EQ(createRes, true);
298 
299     int result;
300     std::string testcaseName("mediascanner_ScanHiddenFile_Test_001");
301     g_isCallbackReceived = false;
302     g_callbackStatus = -1;
303     g_callbackName = "";
304     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
305 
306     EXPECT_EQ((g_msInstance != nullptr), true);
307     result = g_msInstance->ScanFile(path, appCallback);
308     EXPECT_EQ(result, g_filescanstatus);
309     if (result == 0) {
310         // Wait here for callback. If not callback for 2 mintues, will skip this step
311         WaitForCallback();
312         EXPECT_EQ(g_callbackStatus, ERR_NOT_ACCESSIBLE);
313         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
314     }
315 }
316 
317 /*
318  * Feature: MediaScanner
319  * Function: Scan a directory with image, video, audio and other type of files
320  * SubFunction: NA
321  * FunctionPoints: NA
322  * EnvConditions: NA
323  * CaseDescription:
324  */
325 HWTEST_F(MediaScannerUnitTest,  mediascanner_ScanDir_test_002, TestSize.Level0)
326 {
327     string path = g_prefixPath;
328     HiLog::Info(LABEL, "ScanDir test case for: %{private}s", path.c_str());
329 
330     int result;
331     std::string testcaseName("mediascanner_ScanDir_test_002");
332     g_isCallbackReceived = false;
333     g_callbackStatus = -1;
334     g_callbackName = "";
335     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
336 
337     EXPECT_EQ((g_msInstance != nullptr), true);
338     result = g_msInstance->ScanDir(path, appCallback);
339     EXPECT_EQ(result, g_filescanstatus);
340 
341     if (result == 0) {
342         // Wait here for callback. If not callback for 2 mintues, will skip this step
343         WaitForCallback();
344         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
345         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
346     }
347 }
348 
349 /*
350  * Feature: MediaScanner
351  * Function: Scan a directory with path provided as relative, must convert to canonical form
352  * SubFunction: NA
353  * FunctionPoints: NA
354  * EnvConditions: NA
355  * CaseDescription:
356  */
357 HWTEST_F(MediaScannerUnitTest,  mediascanner_ScanDir_CononicalPathtest_001, TestSize.Level0)
358 {
359     string path = g_prefixPath + "/../files";
360     HiLog::Info(LABEL, "ScanDir test case for: %{private}s", path.c_str());
361 
362     int result;
363     std::string testcaseName("mediascanner_ScanDir_CononicalPathtest_001");
364     g_isCallbackReceived = false;
365     g_callbackStatus = -1;
366     g_callbackName = "";
367     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
368 
369     EXPECT_EQ((g_msInstance != nullptr), true);
370     result = g_msInstance->ScanDir(path, appCallback);
371     EXPECT_EQ(result, g_filescanstatus);
372 
373     if (result == 0) {
374         // Wait here for callback. If not callback for 2 mintues, will skip this step
375         WaitForCallback();
376         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
377         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
378     }
379 }
380 
381 /*
382  * Feature: MediaScanner
383  * Function: Scan an image file with path provided as relative, must convert to canonical form
384  * SubFunction: NA
385  * FunctionPoints: NA
386  * EnvConditions: NA
387  * CaseDescription:
388  */
389 HWTEST_F(MediaScannerUnitTest,  mediascanner_ScanFile_CononicalPathtest_001, TestSize.Level0)
390 {
391     string path = g_prefixPath + "/../files/gtest_Image1.jpg";
392     HiLog::Info(LABEL, "ScanFile test case for: %{private}s", path.c_str());
393 
394     int result;
395     std::string testcaseName("mediascanner_ScanFile_CononicalPathtest_001");
396     g_isCallbackReceived = false;
397     g_callbackStatus = -1;
398     g_callbackName = "";
399     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
400 
401     EXPECT_EQ((g_msInstance != nullptr), true);
402     result = g_msInstance->ScanFile(path, appCallback);
403     EXPECT_EQ(result, g_filescanstatus);
404 
405     if (result == 0) {
406         // Wait here for callback. If not callback for 2 mintues, will skip this step
407         WaitForCallback();
408         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
409         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
410     }
411 }
412 
413 /*
414  * Feature: MediaScanner
415  * Function: Scan a text file with path provided as relative, must convert to canonical form
416  * SubFunction: NA
417  * FunctionPoints: NA
418  * EnvConditions: NA
419  * CaseDescription:
420  */
421 HWTEST_F(MediaScannerUnitTest,  mediascanner_ScanFile_CononicalPathtest_002, TestSize.Level0)
422 {
423     string path = g_prefixPath + "/../files/gtest_Text1.txt";
424     HiLog::Info(LABEL, "ScanFile test case for: %{private}s", path.c_str());
425 
426     int result;
427     std::string testcaseName("mediascanner_ScanFile_CononicalPathtest_002");
428     g_isCallbackReceived = false;
429     g_callbackStatus = -1;
430     g_callbackName = "";
431     auto appCallback = make_shared<ApplicationCallback>(testcaseName);
432 
433     EXPECT_EQ((g_msInstance != nullptr), true);
434     result = g_msInstance->ScanFile(path, appCallback);
435     EXPECT_EQ(result, g_filescanstatus);
436 
437     if (result == 0) {
438         // Wait here for callback. If not callback for 2 mintues, will skip this step
439         WaitForCallback();
440         EXPECT_EQ(g_callbackStatus, g_filescanstatus);
441         EXPECT_STREQ(g_callbackName.c_str(), testcaseName.c_str());
442     }
443 }
444 } // namespace Media
445 } // namespace OHOS
446