diff --git a/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp b/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp index c95ceeebb5825d85a0f800cde43d98e7955017fa..d903818935cd986baed61f06be661460e17050ad 100644 --- a/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp +++ b/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp @@ -85,6 +85,18 @@ napi_value NapiFileInfoExporter::ThrowError(napi_env env, int code) return nullptr; } +static int CallListFile(const FileInfoEntity* fileInfoEntity, FileIteratorEntity* fileIteratorEntity, + FileFilter& filter) +{ + fileIteratorEntity->fileAccessHelper = fileInfoEntity->fileAccessHelper; + fileIteratorEntity->fileInfo = fileInfoEntity->fileInfo; + fileIteratorEntity->offset = 0; + fileIteratorEntity->filter = std::move(filter); + fileIteratorEntity->flag = CALL_LISTFILE; + return fileInfoEntity->fileAccessHelper->ListFile(fileInfoEntity->fileInfo, fileIteratorEntity->offset, + fileIteratorEntity->filter, fileIteratorEntity->memInfo); +} + napi_value NapiFileInfoExporter::ListFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -126,13 +138,7 @@ napi_value NapiFileInfoExporter::ListFile(napi_env env, napi_callback_info info) return nullptr; } - fileIteratorEntity->fileAccessHelper = fileInfoEntity->fileAccessHelper; - fileIteratorEntity->fileInfo = fileInfoEntity->fileInfo; - fileIteratorEntity->offset = 0; - fileIteratorEntity->filter = std::move(filter); - fileIteratorEntity->flag = CALL_LISTFILE; - ret = fileInfoEntity->fileAccessHelper->ListFile(fileInfoEntity->fileInfo, fileIteratorEntity->offset, - fileIteratorEntity->filter, fileIteratorEntity->memInfo); + ret = CallListFile(fileInfoEntity, fileIteratorEntity, filter); if (ret != ERR_OK) { FileAccessFwk::SharedMemoryOperation::DestroySharedMemory(fileIteratorEntity->memInfo); return ThrowError(env, ret); diff --git a/frameworks/js/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp b/frameworks/js/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp index 897e65adb6f31d033b281372ac4d589de2efffdc..d458b1c0d215651d8892e6a7bb242b546d9e18af 100644 --- a/frameworks/js/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp +++ b/frameworks/js/napi/file_access_module/file_info/napi_file_iterator_exporter.cpp @@ -165,6 +165,29 @@ static bool FilterTrashAndRecentDir(const std::string &uri) return std::regex_match(uri, matchResult, TRASH_RECENT_DIR_REGEX) && matchResult.length() > 0; } +static int GetNextIterator(napi_value &objFileInfoExporter, FileIteratorEntity *fileIteratorEntity, + FileInfoEntity *fileInfoEntity, napi_env env, NVal& retNVal) +{ + int ret = E_GETRESULT; + bool isDone = false; + if (fileIteratorEntity->flag == CALL_LISTFILE) { + ret = MakeListFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); + } else if (fileIteratorEntity->flag == CALL_SCANFILE) { + ret = MakeScanFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); + } + while (!isDone && FilterTrashAndRecentDir(fileInfoEntity->fileInfo.uri)) { + fileInfoEntity = NClass::GetEntityOf(env, objFileInfoExporter); + retNVal = NVal::CreateObject(env); + HILOG_DEBUG("TRASH_DIR or RECENT_DIR: %{public}s", fileInfoEntity->fileInfo.uri.c_str()); + if (fileIteratorEntity->flag == CALL_LISTFILE) { + ret = MakeListFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); + } else if (fileIteratorEntity->flag == CALL_SCANFILE) { + ret = MakeScanFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); + } + } + return ret; +} + napi_value NapiFileIteratorExporter::Next(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -202,24 +225,7 @@ napi_value NapiFileIteratorExporter::Next(napi_env env, napi_callback_info info) } auto retNVal = NVal::CreateObject(env); - int ret = E_GETRESULT; - bool isDone = false; - if (fileIteratorEntity->flag == CALL_LISTFILE) { - ret = MakeListFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); - } else if (fileIteratorEntity->flag == CALL_SCANFILE) { - ret = MakeScanFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); - } - while (!isDone && FilterTrashAndRecentDir(fileInfoEntity->fileInfo.uri)) { - fileInfoEntity = NClass::GetEntityOf(env, objFileInfoExporter); - retNVal = NVal::CreateObject(env); - HILOG_DEBUG("TRASH_DIR or RECENT_DIR: %{public}s", fileInfoEntity->fileInfo.uri.c_str()); - if (fileIteratorEntity->flag == CALL_LISTFILE) { - ret = MakeListFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); - } else if (fileIteratorEntity->flag == CALL_SCANFILE) { - ret = MakeScanFileResult(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal, isDone); - } - } - + int ret = GetNextIterator(objFileInfoExporter, fileIteratorEntity, fileInfoEntity, env, retNVal); if (ret != ERR_OK) { NError(ret).ThrowErr(env); return nullptr; diff --git a/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp b/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp index 7c1f257b4254125956372624cc04d2339312d0af..4df5b5698d63248f28e4931cf1b567a73a5093df 100644 --- a/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp +++ b/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp @@ -82,6 +82,18 @@ napi_value NapiRootInfoExporter::Constructor(napi_env env, napi_callback_info in return funcArg.GetThisVar(); } +static int CallListFile(const RootInfoEntity* rootEntity, FileIteratorEntity* fileIteratorEntity, FileFilter& filter, + const FileInfo& fileInfo) +{ + fileIteratorEntity->fileAccessHelper = rootEntity->fileAccessHelper; + fileIteratorEntity->fileInfo = fileInfo; + fileIteratorEntity->offset = 0; + fileIteratorEntity->filter = std::move(filter); + fileIteratorEntity->flag = CALL_LISTFILE; + return rootEntity->fileAccessHelper->ListFile(fileInfo, fileIteratorEntity->offset, filter, + fileIteratorEntity->memInfo); +} + napi_value NapiRootInfoExporter::ListFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -121,13 +133,8 @@ napi_value NapiRootInfoExporter::ListFile(napi_env env, napi_callback_info info) NError(ret).ThrowErr(env); return nullptr; } - fileIteratorEntity->fileAccessHelper = rootEntity->fileAccessHelper; - fileIteratorEntity->fileInfo = fileInfo; - fileIteratorEntity->offset = 0; - fileIteratorEntity->filter = std::move(filter); - fileIteratorEntity->flag = CALL_LISTFILE; - ret = rootEntity->fileAccessHelper->ListFile(fileInfo, fileIteratorEntity->offset, filter, - fileIteratorEntity->memInfo); + + ret = CallListFile(rootEntity, fileIteratorEntity, filter, fileInfo); if (ret != ERR_OK) { FileAccessFwk::SharedMemoryOperation::DestroySharedMemory(fileIteratorEntity->memInfo); return NapiFileInfoExporter::ThrowError(env, ret); diff --git a/test/fuzztest/externalfileaccess_fuzzer/external_file_access_fuzzer.cpp b/test/fuzztest/externalfileaccess_fuzzer/external_file_access_fuzzer.cpp index 5ef6698ba9a7470bae6a4bd3fd78df249cd4009d..6dab686d59c122fab2141d0f3be3bd9f01ff8c83 100644 --- a/test/fuzztest/externalfileaccess_fuzzer/external_file_access_fuzzer.cpp +++ b/test/fuzztest/externalfileaccess_fuzzer/external_file_access_fuzzer.cpp @@ -281,14 +281,10 @@ bool MoveFuzzTest(const uint8_t* data, size_t size) Uri parentUri(info[i].uri); Uri newDirUriTest1(""); Uri newDirUriTest2(""); - result = helper->Mkdir(parentUri, "test1", newDirUriTest1); - if (result != OHOS::FileAccessFwk::ERR_OK) { - HILOG_ERROR("Mkdir failed. ret : %{public}d", result); - return false; - } - result = helper->Mkdir(parentUri, "test2", newDirUriTest2); - if (result != OHOS::FileAccessFwk::ERR_OK) { - HILOG_ERROR("Mkdir failed. ret : %{public}d", result); + int result1 = helper->Mkdir(parentUri, "test1", newDirUriTest1); + int result2 = helper->Mkdir(parentUri, "test2", newDirUriTest2); + if (result1 != OHOS::FileAccessFwk::ERR_OK || result2 != OHOS::FileAccessFwk::ERR_OK) { + HILOG_ERROR("Mkdir failed. ret : %{public}d, %{public}d", result1, result2); return false; } Uri testUri(""); @@ -303,14 +299,10 @@ bool MoveFuzzTest(const uint8_t* data, size_t size) HILOG_ERROR("Move failed. ret : %{public}d", result); return false; } - result = helper->Delete(newDirUriTest1); - if (result != OHOS::FileAccessFwk::ERR_OK) { - HILOG_ERROR("Delete failed. ret : %{public}d", result); - return false; - } - result = helper->Delete(newDirUriTest2); - if (result != OHOS::FileAccessFwk::ERR_OK) { - HILOG_ERROR("Delete failed. ret : %{public}d", result); + result1 = helper->Delete(newDirUriTest1); + result2 = helper->Delete(newDirUriTest2); + if (result1 != OHOS::FileAccessFwk::ERR_OK || result2 != OHOS::FileAccessFwk::ERR_OK) { + HILOG_ERROR("Delete failed. ret : %{public}d, %{public}d", result1, result2); return false; } } @@ -375,6 +367,10 @@ bool ListFileFuzzTest(const uint8_t* data, size_t size) SharedMemoryInfo memInfo; int result = SharedMemoryOperation::CreateSharedMemory("FileInfo List", DEFAULT_CAPACITY_200KB, memInfo); + if (result != OHOS::FileAccessFwk::ERR_OK) { + HILOG_ERROR("CreateSharedMemory failed. ret : %{public}d", result); + return false; + } FileFilter filter; result = helper->ListFile(fileInfo, offset, filter, memInfo); SharedMemoryOperation::DestroySharedMemory(memInfo); diff --git a/test/unittest/external_file_access_test.cpp b/test/unittest/external_file_access_test.cpp index 863b37d8b29d7c95d2c6dd26f732ade3db8389e8..3fb5f9df4b5aad7fe085304ae25ad19e9033399a 100644 --- a/test/unittest/external_file_access_test.cpp +++ b/test/unittest/external_file_access_test.cpp @@ -2403,6 +2403,26 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Copy_0005, testing::ext:: GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_Copy_0005"; } +static int ReadyToCopy06(Uri& parentUri, Uri& srcDir, const char* buff, int len) +{ + int result = g_fah->Mkdir(parentUri, "Copy_0006_src", srcDir); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri aFileUri(""); + result = g_fah->CreateFile(srcDir, "a.txt", aFileUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri bFileUri(""); + result = g_fah->CreateFile(srcDir, "b.txt", bFileUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + int fd = -1; + result = g_fah->OpenFile(bFileUri, WRITE_READ, fd); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + ssize_t srcFileSize = write(fd, buff, len); + EXPECT_EQ(srcFileSize, len); + close(fd); + return srcFileSize; +} + /** * @tc.number: user_file_service_external_file_access_Copy_0006 * @tc.name: external_file_access_Copy_0006 @@ -2419,25 +2439,13 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Copy_0006, testing::ext:: vector info; int result = g_fah->GetRoots(info); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + char buff[] = "Copy test content for b.txt"; for (size_t i = 0; i < info.size(); i++) { - Uri parentUri(info[i].uri); Uri srcDir(""); - result = g_fah->Mkdir(parentUri, "Copy_0006_src", srcDir); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri aFileUri(""); - result = g_fah->CreateFile(srcDir, "a.txt", aFileUri); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri bFileUri(""); - result = g_fah->CreateFile(srcDir, "b.txt", bFileUri); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - int fd = -1; - result = g_fah->OpenFile(bFileUri, WRITE_READ, fd); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - char buff[] = "Copy test content for b.txt"; - ssize_t srcFileSize = write(fd, buff, sizeof(buff)); - EXPECT_EQ(srcFileSize, sizeof(buff)); - close(fd); + Uri parentUri(info[i].uri); + int srcFileSize = ReadyToCopy06(parentUri, srcDir, buff, sizeof(buff)); + Uri bFileUri(""); Uri destDir(""); result = g_fah->Mkdir(parentUri, "Copy_0006_dest", destDir); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); @@ -2452,6 +2460,7 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Copy_0006, testing::ext:: EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); EXPECT_EQ(copyResult.size(), 0); + int fd = -1; result = g_fah->OpenFile(bFileUri, WRITE_READ, fd); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); ssize_t destFileSize = read(fd, buff, sizeof(buff)); @@ -2585,6 +2594,26 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Copy_0008, testing::ext:: GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_Copy_0008"; } +static int ReadyToCopy09(Uri& parentUri, Uri& srcDir, const char* buff, int len) +{ + int result = g_fah->Mkdir(parentUri, "Copy_0009_src", srcDir); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri aFileUri(""); + result = g_fah->CreateFile(srcDir, "a.txt", aFileUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri bFileUri(""); + result = g_fah->CreateFile(srcDir, "b.txt", bFileUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + int fd = -1; + result = g_fah->OpenFile(bFileUri, WRITE_READ, fd); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + ssize_t srcFileSize = write(fd, buff, len); + EXPECT_EQ(srcFileSize, len); + close(fd); + + return srcFileSize; +} + /** * @tc.number: user_file_service_external_file_access_Copy_0009 * @tc.name: external_file_access_Copy_0009 @@ -2601,28 +2630,16 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Copy_0009, testing::ext:: vector info; int result = g_fah->GetRoots(info); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + char buff[] = "Copy test content for b.txt"; for (size_t i = 0; i < info.size(); i++) { Uri parentUri(info[i].uri); Uri srcDir(""); - result = g_fah->Mkdir(parentUri, "Copy_0009_src", srcDir); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri aFileUri(""); - result = g_fah->CreateFile(srcDir, "a.txt", aFileUri); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri bFileUri(""); - result = g_fah->CreateFile(srcDir, "b.txt", bFileUri); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - int fd = -1; - result = g_fah->OpenFile(bFileUri, WRITE_READ, fd); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - char buff[] = "Copy test content for b.txt"; - ssize_t srcFileSize = write(fd, buff, sizeof(buff)); - EXPECT_EQ(srcFileSize, sizeof(buff)); - close(fd); + int srcFileSize = ReadyToCopy09(parentUri, srcDir, buff, sizeof(buff)); for (size_t j = i + 1; j < info.size(); j++) { Uri targetUri(info[j].uri); Uri destDir(""); + Uri bFileUri(""); result = g_fah->Mkdir(targetUri, "Copy_0009_dest", destDir); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); Uri destSrcDir(""); @@ -2636,6 +2653,7 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Copy_0009, testing::ext:: EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); EXPECT_EQ(copyResult.size(), 0); + int fd = -1; result = g_fah->OpenFile(bFileUri, WRITE_READ, fd); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); ssize_t destFileSize = read(fd, buff, sizeof(buff)); @@ -4782,6 +4800,32 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_GetProxyByUri_0001, testi GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_GetProxyByUri_0001"; } +static void CheckJson(Uri& newDirUriTest1, const std::string displayName, const std::string relativePath, int len) +{ + json testJson = { + {RELATIVE_PATH, " "}, + {DISPLAY_NAME, " "}, + {FILE_SIZE, " "}, + {DATE_MODIFIED, " "}, + {DATE_ADDED, " "}, + {HEIGHT, " "}, + {WIDTH, " "}, + {DURATION, " "} + }; + auto testJsonString = testJson.dump(); + int result = g_fah->Query(newDirUriTest1, testJsonString); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + auto jsonObject = json::parse(testJsonString); + EXPECT_EQ(jsonObject.at(DISPLAY_NAME), displayName); + EXPECT_EQ(jsonObject.at(FILE_SIZE), len + len); + EXPECT_EQ(jsonObject.at(RELATIVE_PATH), relativePath); + ASSERT_TRUE(jsonObject.at(DATE_MODIFIED) > 0); + ASSERT_TRUE(jsonObject.at(DATE_ADDED) > 0); + GTEST_LOG_(INFO) << " result" << testJsonString; + result = g_fah->Delete(newDirUriTest1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); +} + /** * @tc.number: user_file_service_external_file_access_Query_0000 * @tc.name: external_file_access_Query_0000 @@ -4827,28 +4871,7 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Query_0000, testing::ext: fileSize = write(fd, buff.c_str(), buff.size()); close(fd); EXPECT_EQ(fileSize, buff.size()); - json testJson = { - {RELATIVE_PATH, " "}, - {DISPLAY_NAME, " "}, - {FILE_SIZE, " "}, - {DATE_MODIFIED, " "}, - {DATE_ADDED, " "}, - {HEIGHT, " "}, - {WIDTH, " "}, - {DURATION, " "} - }; - auto testJsonString = testJson.dump(); - result = g_fah->Query(newDirUriTest1, testJsonString); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - auto jsonObject = json::parse(testJsonString); - EXPECT_EQ(jsonObject.at(DISPLAY_NAME), displayName); - EXPECT_EQ(jsonObject.at(FILE_SIZE), buff.size() * 2); - EXPECT_EQ(jsonObject.at(RELATIVE_PATH), relativePath); - ASSERT_TRUE(jsonObject.at(DATE_MODIFIED) > 0); - ASSERT_TRUE(jsonObject.at(DATE_ADDED) > 0); - GTEST_LOG_(INFO) << " result" << testJsonString; - result = g_fah->Delete(newDirUriTest1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + CheckJson(newDirUriTest1, displayName, relativePath, buff.size()); } catch (...) { GTEST_LOG_(ERROR) << "external_file_access_Query_0000 occurs an exception."; } @@ -5164,28 +5187,7 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Query_0007, testing::ext: fileSize = write(fd, buff.c_str(), buff.size()); close(fd); EXPECT_EQ(fileSize, buff.size()); - json testJson = { - {RELATIVE_PATH, " "}, - {DISPLAY_NAME, " "}, - {FILE_SIZE, " "}, - {DATE_MODIFIED, " "}, - {DATE_ADDED, " "}, - {HEIGHT, " "}, - {WIDTH, " "}, - {DURATION, " "} - }; - auto testJsonString = testJson.dump(); - result = g_fah->Query(newDirUriTest1, testJsonString); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - auto jsonObject = json::parse(testJsonString); - EXPECT_EQ(jsonObject.at(DISPLAY_NAME), displayName); - EXPECT_EQ(jsonObject.at(FILE_SIZE), buff.size() * 2); - EXPECT_EQ(jsonObject.at(RELATIVE_PATH), relativePath); - ASSERT_TRUE(jsonObject.at(DATE_MODIFIED) > 0); - ASSERT_TRUE(jsonObject.at(DATE_ADDED) > 0); - GTEST_LOG_(INFO) << " result" << testJsonString; - result = g_fah->Delete(newDirUriTest1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + CheckJson(newDirUriTest1, displayName, relativePath, buff.size()); } catch (...) { GTEST_LOG_(ERROR) << "external_file_access_Query_0007 occurs an exception."; } diff --git a/test/unittest/external_notify_test.cpp b/test/unittest/external_notify_test.cpp index dbe74f90fab6b388fb1704a8a1281717746c82cf..18fcca90dbf092766cf7d7e15c19166bcb1234d9 100644 --- a/test/unittest/external_notify_test.cpp +++ b/test/unittest/external_notify_test.cpp @@ -150,6 +150,74 @@ void TestObserver::OnChange(NotifyMessage ¬ifyMessage) GTEST_LOG_(INFO) << "enter TestObserver uri =" + notifyUri + "type =" + std::to_string(g_notifyFlag); } +static tuple ReadyRegisterNotify00(Uri& parentUri, + sptr& myObserver1, + sptr& myObserver2, + sptr& myObserver3, + sptr& myObserver4) +{ + bool notifyForDescendants = false; + Uri newDirUriTest1(""); + int result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newDirUriTest2(""); + result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri1(""); + result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + GTEST_LOG_(INFO) << newFileUri1.ToString(); + Uri newFileUri2(""); + result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + result = g_fah->RegisterNotify(newDirUriTest1, true, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver3); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver4); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + + return {newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2}; +} + +static tuple TriggerNotify00(Uri& newDirUriTest1, Uri& newDirUriTest2, Uri& newFileUri1, Uri& newFileUri2) +{ + Uri testUri(""); + int result = g_fah->CreateFile(newDirUriTest1, "testUri", testUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, ADD_EVENT); + EXPECT_EQ(g_notifyUri, testUri.ToString()); + Uri renameDirUri1(""); + result = g_fah->Rename(newDirUriTest1, "testDir", renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString()); + result = g_fah->Delete(newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, DELETE_EVENT); + EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString()); + + Uri renameUri1(""); + result = g_fah->Rename(newFileUri1, "renameUri1", renameUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newFileUri1.ToString()); + result = g_fah->Delete(newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, DELETE_EVENT); + EXPECT_EQ(g_notifyUri, newFileUri2.ToString()); + + return {renameDirUri1, renameUri1}; +} + /** * @tc.number: user_file_service_external_file_access_notify_0000 * @tc.name: external_file_access_notify_0000 @@ -166,64 +234,15 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0000, testing::ext vector info; int result = g_fah->GetRoots(info); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - bool notifyForDescendants = false; sptr myObserver1 = new (std::nothrow) MyObserver(); sptr myObserver2 = new (std::nothrow) MyObserver(); sptr myObserver3 = new (std::nothrow) MyObserver(); sptr myObserver4 = new (std::nothrow) MyObserver(); Uri parentUri(info[1].uri); - Uri newDirUriTest1(""); - result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newDirUriTest2(""); - result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newFileUri1(""); - result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - GTEST_LOG_(INFO) << newFileUri1.ToString(); - Uri newFileUri2(""); - result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - - result = g_fah->RegisterNotify(newDirUriTest1, true, myObserver1); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserver2); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver3); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver4); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - - Uri testUri(""); - result = g_fah->CreateFile(newDirUriTest1, "testUri", testUri); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, ADD_EVENT); - EXPECT_EQ(g_notifyUri, testUri.ToString()); - Uri renameDirUri1(""); - result = g_fah->Rename(newDirUriTest1, "testDir", renameDirUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, MOVED_SELF); - EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString()); - result = g_fah->Delete(newDirUriTest2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, DELETE_EVENT); - EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString()); + auto [newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2] = + ReadyRegisterNotify00(parentUri, myObserver1, myObserver2, myObserver3, myObserver4); - Uri renameUri1(""); - result = g_fah->Rename(newFileUri1, "renameUri1", renameUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, MOVED_SELF); - EXPECT_EQ(g_notifyUri, newFileUri1.ToString()); - result = g_fah->Delete(newFileUri2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, DELETE_EVENT); - EXPECT_EQ(g_notifyUri, newFileUri2.ToString()); + auto [renameDirUri1, renameUri1] = TriggerNotify00(newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2); sleep(1); result = g_fah->UnregisterNotify(newDirUriTest1, myObserver1); @@ -244,6 +263,30 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0000, testing::ext GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0000"; } +static tuple ReadyRegisterNotify01(Uri& parentUri, sptr myObserver1, + sptr myObserver2, sptr myObserver3) +{ + bool notifyForDescendants1 = true; + bool notifyForDescendants2 = false; + Uri newFileUri1(""); + int result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri2(""); + result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri3(""); + result = g_fah->CreateFile(parentUri, "uri_file3", newFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants1, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri3, notifyForDescendants2, myObserver3); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + return {newFileUri1, newFileUri2, newFileUri3}; +} + /** * @tc.number: user_file_service_external_file_access_notify_0001 * @tc.name: external_file_access_notify_0001 @@ -260,28 +303,13 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0001, testing::ext vector info; int result = g_fah->GetRoots(info); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - bool notifyForDescendants1 = true; - bool notifyForDescendants2 = false; + sptr myObserver1 = new (std::nothrow) MyObserver(); sptr myObserver2 = new (std::nothrow) MyObserver(); sptr myObserver3 = new (std::nothrow) MyObserver(); Uri parentUri(info[1].uri); - Uri newFileUri1(""); - result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newFileUri2(""); - result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newFileUri3(""); - result = g_fah->CreateFile(parentUri, "uri_file3", newFileUri3); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - - result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver1); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants1, myObserver2); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri3, notifyForDescendants2, myObserver3); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + auto [newFileUri1, newFileUri2, newFileUri3] = + ReadyRegisterNotify01(parentUri, myObserver1, myObserver2, myObserver3); Uri renameFileUri1(""); result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1); @@ -289,7 +317,7 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0001, testing::ext usleep(SLEEP_TIME); EXPECT_EQ(g_notifyEvent, MOVED_SELF); EXPECT_EQ(g_notifyUri, newFileUri1.ToString()); - + result = g_fah->Delete(newFileUri2); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); usleep(SLEEP_TIME); @@ -316,6 +344,55 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0001, testing::ext GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0001"; } +static tuple ReadyRegisterNotify02(Uri& parentUri) +{ + Uri newDirUriTest1(""); + int result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newDirUriTest2(""); + result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri1(""); + result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri2(""); + result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + return {newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2}; +} + +static tuple TriggerNotify02(Uri& newDirUriTest1, Uri& newDirUriTest2, Uri& newFileUri1, Uri& newFileUri2) +{ + const int tm = 2; + Uri testFile(""); + int result = g_fah->CreateFile(newDirUriTest1, "test_file", testFile); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri renameDirUri1(""); + result = g_fah->Rename(newDirUriTest1, "renameDir", renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString()); + result = g_fah->Delete(newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, DELETE_EVENT); + EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString()); + Uri renameFileUri1(""); + result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newFileUri1.ToString()); + result = g_fah->Delete(newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, DELETE_EVENT); + EXPECT_EQ(g_notifyUri, newFileUri2.ToString()); + sleep(tm); + return {renameDirUri1, renameFileUri1}; +} /** * @tc.number: user_file_service_external_file_access_notify_0002 * @tc.name: external_file_access_notify_0002 @@ -333,112 +410,39 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0002, testing::ext int result = g_fah->GetRoots(info); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); bool notifyForDescendants = true; - sptr myObserverDir11 = new (std::nothrow) MyObserver(); - sptr myObserverDir12 = new (std::nothrow) MyObserver(); - sptr myObserverDir13 = new (std::nothrow) MyObserver(); - sptr myObserverDir21 = new (std::nothrow) MyObserver(); - sptr myObserverDir22 = new (std::nothrow) MyObserver(); - sptr myObserverDir23 = new (std::nothrow) MyObserver(); - sptr myObserverFile11 = new (std::nothrow) MyObserver(); - sptr myObserverFile12 = new (std::nothrow) MyObserver(); - sptr myObserverFile13 = new (std::nothrow) MyObserver(); - sptr myObserverFile21 = new (std::nothrow) MyObserver(); - sptr myObserverFile22 = new (std::nothrow) MyObserver(); - sptr myObserverFile23 = new (std::nothrow) MyObserver(); + vector> observers; + const int len = 12; + const int group = 3; + for (int i = 0; i < len; i++) { + observers.emplace_back(new (std::nothrow) MyObserver()); + } Uri parentUri(info[1].uri); - Uri newDirUriTest1(""); - result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newDirUriTest2(""); - result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newFileUri1(""); - result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newFileUri2(""); - result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + auto [newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2] = ReadyRegisterNotify02(parentUri); - result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserverDir11); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserverDir12); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserverDir13); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - - result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserverDir21); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserverDir22); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserverDir23); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - - result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserverFile11); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserverFile12); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserverFile13); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + for (int i = 0; i < group; i++) { + result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, observers[i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, observers[3 + i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, observers[6 + i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, observers[9 + i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + } - result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserverFile21); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserverFile22); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserverFile23); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - - Uri testFile(""); - result = g_fah->CreateFile(newDirUriTest1, "test_file", testFile); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - Uri renameDirUri1(""); - result = g_fah->Rename(newDirUriTest1, "renameDir", renameDirUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, MOVED_SELF); - EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString()); - result = g_fah->Delete(newDirUriTest2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, DELETE_EVENT); - EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString()); - Uri renameFileUri1(""); - result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, MOVED_SELF); - EXPECT_EQ(g_notifyUri, newFileUri1.ToString()); - result = g_fah->Delete(newFileUri2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, DELETE_EVENT); - EXPECT_EQ(g_notifyUri, newFileUri2.ToString()); - sleep(2); - result = g_fah->UnregisterNotify(newDirUriTest1, myObserverDir11); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newDirUriTest1, myObserverDir12); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newDirUriTest1, myObserverDir13); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newDirUriTest2, myObserverDir21); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newDirUriTest2, myObserverDir22); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newDirUriTest2, myObserverDir23); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newFileUri1, myObserverFile11); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newFileUri1, myObserverFile12); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newFileUri1, myObserverFile13); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newFileUri2, myObserverFile21); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newFileUri2, myObserverFile22); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newFileUri2, myObserverFile23); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + auto [renameDirUri1, renameFileUri1] = + TriggerNotify02(newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2); + for (int i = 0; i < group; i++) { + result = g_fah->UnregisterNotify(newDirUriTest1, observers[i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newDirUriTest2, observers[3 + i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, observers[6 + i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri2, observers[9 + i]); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + } result = g_fah->Delete(renameDirUri1); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); result = g_fah->Delete(renameFileUri1); @@ -498,6 +502,34 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0003, testing::ext GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0003"; } +static tuple ReadyRegisterNotify05(Uri& parentUri, sptr& myObserver1) +{ + bool notifyForDescendants = true; + Uri uri_dir(""); + int result = g_fah->Mkdir(parentUri, "uri_dir123", uri_dir); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri uri_dirSub1(""); + result = g_fah->Mkdir(uri_dir, "uri_dirSub1", uri_dirSub1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(uri_dir, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(uri_dirSub1, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + Uri uri_dirSub2(""); + result = g_fah->Mkdir(uri_dir, "uri_dirSub2", uri_dirSub2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, ADD_EVENT); + EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString()); + result = g_fah->RegisterNotify(uri_dirSub2, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + Uri renameDirUri1(""); + result = g_fah->Rename(uri_dirSub2, "renameDir1", renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + return {uri_dir, uri_dirSub1, uri_dirSub2, renameDirUri1}; +} + /** * @tc.number: user_file_service_external_file_access_notify_0005 * @tc.name: external_file_access_notify_0005 @@ -514,31 +546,9 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0005, testing::ext vector info; int result = g_fah->GetRoots(info); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - bool notifyForDescendants = true; sptr myObserver1 = new (std::nothrow) MyObserver(); Uri parentUri(info[1].uri); - Uri uri_dir(""); - result = g_fah->Mkdir(parentUri, "uri_dir123", uri_dir); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri uri_dirSub1(""); - result = g_fah->Mkdir(uri_dir, "uri_dirSub1", uri_dirSub1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(uri_dir, notifyForDescendants, myObserver1); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(uri_dirSub1, notifyForDescendants, myObserver1); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - Uri uri_dirSub2(""); - result = g_fah->Mkdir(uri_dir, "uri_dirSub2", uri_dirSub2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); - EXPECT_EQ(g_notifyEvent, ADD_EVENT); - EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString()); - result = g_fah->RegisterNotify(uri_dirSub2, notifyForDescendants, myObserver1); - EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); - Uri renameDirUri1(""); - result = g_fah->Rename(uri_dirSub2, "renameDir1", renameDirUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME); + auto [uri_dir, uri_dirSub1, uri_dirSub2, renameDirUri1] = ReadyRegisterNotify05(parentUri, myObserver1); if (g_notifyEvent != MOVED_TO) { if (g_notifyEvent != MOVED_SELF) { EXPECT_EQ(g_notifyEvent, MOVED_FROM); @@ -675,24 +685,7 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0008, testing::ext } /** - * @tc.number: user_file_service_external_file_access_notify_0009 Uri parentUri(info[1].uri); - Uri newFileDir1(""); - result = g_fah->Mkdir(parentUri, "uri_dir0016", newFileDir1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver3); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - - result = g_fah->UnregisterNotify(newFileDir1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->UnregisterNotify(newFileDir1, myObserver1); - EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); - - result = g_fah->Delete(newFileDir1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + * @tc.number: user_file_service_external_file_access_notify_0009 * @tc.name: external_file_access_notify_0009 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS when the file is Chinese name. * @tc.size: MEDIUM @@ -776,6 +769,29 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0010, testing::ext GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0010"; } +static tuple ReadyRegisterNotify11(Uri& parentUri, Uri newFileDir1, sptr& myObserver1, + sptr& myObserver2) +{ + const int tm = SLEEP_TIME * 2; + bool notifyForDescendants1 = true; + int result = g_fah->Mkdir(parentUri, "uri_dir11", newFileDir1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri1(""); + result = g_fah->CreateFile(newFileDir1, "uri_file11", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + sleep(1); + result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri renameFileUri1(""); + result = g_fah->Rename(newFileUri1, "renamefile1", renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(tm); + + return {newFileUri1, renameFileUri1}; +} + /** * @tc.number: user_file_service_external_file_access_notify_0011 * @tc.name: external_file_access_notify_0011 @@ -793,26 +809,13 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0011, testing::ext vector info; int result = g_fah->GetRoots(info); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - bool notifyForDescendants1 = true; bool notifyForDescendants2 = false; sptr myObserver1 = new (std::nothrow) TestObserver(); sptr myObserver2 = new (std::nothrow) MyObserver(); Uri parentUri(info[1].uri); Uri newFileDir1(""); - result = g_fah->Mkdir(parentUri, "uri_dir11", newFileDir1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri newFileUri1(""); - result = g_fah->CreateFile(newFileDir1, "uri_file11", newFileUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - sleep(1); - result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants1, myObserver1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver2); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - Uri renameFileUri1(""); - result = g_fah->Rename(newFileUri1, "renamefile1", renameFileUri1); - EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); - usleep(SLEEP_TIME * 2); + auto [newFileUri1, renameFileUri1] = + ReadyRegisterNotify11(parentUri, newFileDir1, myObserver1, myObserver2); if (g_notifyEvent != MOVED_TO) { if (g_notifyEvent != MOVED_SELF) { EXPECT_EQ(g_notifyEvent, MOVED_FROM); @@ -1169,4 +1172,4 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0019, testing::ext } GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0019"; } -} // namespace +} // namespace \ No newline at end of file