From 5bf6d7fccad5a72b1a2a02a2e1d40d93eebfded5 Mon Sep 17 00:00:00 2001 From: caochuan Date: Wed, 21 Jun 2023 14:58:17 +0800 Subject: [PATCH] feat: add notify test Signed-off-by: caochuan --- test/unittest/BUILD.gn | 2 + test/unittest/external_file_access_test.cpp | 1060 +++++++++++++++++++ 2 files changed, 1062 insertions(+) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index abea929a..b7fc43d6 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -66,6 +66,7 @@ ohos_unittest("external_file_access_test") { "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", "//third_party/googletest/googlemock/include/gmock", "${user_file_service_path}/interfaces/inner_api/file_access/include", + "${user_file_service_path}/services/native/file_access_service/include", ] configs = [ "//build/config/compiler:exceptions" ] @@ -75,6 +76,7 @@ ohos_unittest("external_file_access_test") { "${ability_runtime_path}/frameworks/native/appkit:app_context", "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + "${user_file_service_path}/services:file_access_service", "//third_party/googletest:gmock_main", ] diff --git a/test/unittest/external_file_access_test.cpp b/test/unittest/external_file_access_test.cpp index 43b1ef94..cf48fd78 100644 --- a/test/unittest/external_file_access_test.cpp +++ b/test/unittest/external_file_access_test.cpp @@ -23,8 +23,11 @@ #include "accesstoken_kit.h" #include "context_impl.h" #include "file_access_framework_errno.h" +#include "file_access_observer_common.h" #include "iservice_registry.h" +#include "iobserver_callback.h" #include "nativetoken_kit.h" +#include "observer_callback_stub.h" #include "token_setproc.h" #define private public @@ -43,6 +46,12 @@ shared_ptr g_fah = nullptr; int g_num = 0; const int UID_TRANSFORM_TMP = 20000000; const int UID_DEFAULT = 0; +const int SLEEP_TIME = 100 * 1000; +const int ADD_EVENT = 0; +const int DELETE_EVENT = 1; +const int UPDATE_EVENT = 2; +int g_notifyFlag = -1; +int g_notifyParentFlag = -1; shared_ptr g_context = nullptr; void SetNativeToken() @@ -112,6 +121,34 @@ public: void TearDown(){}; }; +class MyObserver : public ObserverCallbackStub { +public: + MyObserver() {}; + virtual ~MyObserver() {}; + void OnChange(NotifyMessage ¬ifyMessage) override; +}; + +void MyObserver::OnChange (NotifyMessage ¬ifyMessage) +{ + g_notifyFlag = static_cast(notifyMessage.notifyType_); + std::string notifyUri = notifyMessage.uris_[0]; + GTEST_LOG_(INFO) << "enter notify uri =" + notifyUri + "type =" + std::to_string(g_notifyFlag); +} + +class TestParentObserver : public ObserverCallbackStub { +public: + TestParentObserver() {}; + virtual ~TestParentObserver() {}; + void OnChange(NotifyMessage ¬ifyMessage) override; +}; + +void TestParentObserver::OnChange (NotifyMessage ¬ifyMessage) +{ + g_notifyParentFlag = static_cast(notifyMessage.notifyType_); + std::string notifyUri = notifyMessage.uris_[0]; + GTEST_LOG_(INFO) << "enter ParentObserver notify uri =" + notifyUri + "type =" + std::to_string(g_notifyParentFlag); +} + /** * @tc.number: user_file_service_external_file_access_OpenFile_0000 * @tc.name: external_file_access_OpenFile_0000 @@ -3901,4 +3938,1027 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Query_0007, testing::ext: } GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_Query_0007"; } + +/** + * @tc.number: user_file_service_external_file_access_Query_0007 + * @tc.name: external_file_access_Query_0007 + * @tc.desc: Test function of Query directory for SUCCESS, the folder and file name is chinese. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I70SX9 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_Query_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_Query_0007"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri parentUri(info[0].uri); + GTEST_LOG_(INFO) << parentUri.ToString(); + Uri newDirUriTest1(""); + Uri newDirUriTest2(""); + std::string displayName = "查询目录1"; + std::string relativePath = "/data/storage/el1/bundle/storage_daemon/"; + result = g_fah->Mkdir(parentUri, displayName, newDirUriTest1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + EXPECT_EQ(newDirUriTest1.ToString().find(displayName), std::string::npos); + result = g_fah->Mkdir(newDirUriTest1, "查询目录2", newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + EXPECT_EQ(newDirUriTest2.ToString().find("查询目录2"), std::string::npos); + Uri newFileUri1(""); + Uri newFileUri2(""); + std::string fileName = "查询文件.txt"; + result = g_fah->CreateFile(newDirUriTest1, fileName, newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + EXPECT_EQ(newFileUri1.ToString().find(fileName), std::string::npos); + result = g_fah->CreateFile(newDirUriTest2, fileName, newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + EXPECT_EQ(newFileUri2.ToString().find(fileName), std::string::npos); + int fd = -1; + std::string buff = "query test"; + result = g_fah->OpenFile(newFileUri1, WRITE_READ, fd); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + ssize_t fileSize = write(fd, buff.c_str(), buff.size()); + close(fd); + EXPECT_EQ(fileSize, buff.size()); + result = g_fah->OpenFile(newFileUri2, WRITE_READ, fd); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + 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); + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_Query_0007 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_Query_0007"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0000 + * @tc.name: external_file_access_notify_0000 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0000"; + try { + 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(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].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); + + result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, 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_notifyFlag, ADD_EVENT); + Uri renameDirUri1(""); + result = g_fah->Rename(newDirUriTest1, "testDir", renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + result = g_fah->Delete(newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + + Uri renameUri1(""); + result = g_fah->Rename(newFileUri1, "renameUri1", renameUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + result = g_fah->Delete(newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + + sleep(1); + result = g_fah->UnregisterNotify(newDirUriTest1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newDirUriTest2, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri2, myObserver4); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0000"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0001 + * @tc.name: external_file_access_notify_0001 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0001"; + try { + 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(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].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); + + Uri renameFileUri1(""); + result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + result = g_fah->Delete(newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + result = g_fah->Delete(newFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + + sleep(1); + result = g_fah->UnregisterNotify(newFileUri1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri2, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri3, myObserver3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0001 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0001"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0002 + * @tc.name: external_file_access_notify_0002 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0002"; + try { + vector info; + 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(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].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); + + 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); + + 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); + Uri renameDirUri1(""); + result = g_fah->Rename(newDirUriTest1, "renameDir", renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + result = g_fah->Delete(newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + Uri renameFileUri1(""); + result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + result = g_fah->Delete(newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + 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); + + result = g_fah->Delete(renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0002 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0002"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0003 + * @tc.name: external_file_access_notify_0003 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0003"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) MyObserver(); + sptr myObserver2 = new (std::nothrow) MyObserver(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + Uri newDirUriTest1(""); + result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri1(""); + result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newDirUriTest1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newDirUriTest1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0003 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0003"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0004 + * @tc.name: external_file_access_notify_0004 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0004"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) MyObserver(); + sptr myObserver2 = new (std::nothrow) MyObserver(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].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 newDirUriTest3(""); + result = g_fah->Mkdir(parentUri, "uri_dir3", newDirUriTest3); + 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); + Uri newFileUri3(""); + result = g_fah->CreateFile(parentUri, "uri_file3", newFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri moveFromUri(""); + result = g_fah->CreateFile(newDirUriTest1, "uri_dir1_file1", moveFromUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest3, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri3, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(moveFromUri, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + + Uri moveToUri(""); + result = g_fah->Move(moveFromUri, newDirUriTest2, moveToUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + Uri renameDirUri1(""); + result = g_fah->Rename(newDirUriTest1, "renameDir1", renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + Uri renameDirUri2(""); + result = g_fah->Rename(newDirUriTest2, "renameDir2", renameDirUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + Uri renameDirUri3(""); + result = g_fah->Rename(newDirUriTest3, "renameDir3", renameDirUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + + Uri renameFileUri1(""); + result = g_fah->Rename(newFileUri1, "renamefile1", renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + Uri renameFileUri2(""); + result = g_fah->Rename(newFileUri2, "renamefile2", renameFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + Uri renameFileUri3(""); + result = g_fah->Rename(newFileUri3, "renamefile3", renameFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + + sleep(2); + result = g_fah->UnregisterNotify(newDirUriTest1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newDirUriTest2, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newDirUriTest3, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri2, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri3, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + result = g_fah->Delete(renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameDirUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameDirUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0004 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0004"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0005 + * @tc.name: external_file_access_notify_0005 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0005"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) MyObserver(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].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_notifyFlag, ADD_EVENT); + 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); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + result = g_fah->Delete(uri_dirSub1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + + sleep(2); + result = g_fah->UnregisterNotify(uri_dir, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(uri_dir); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0005 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0005"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0006 + * @tc.name: external_file_access_notify_0006 + * @tc.desc: Duplicate UnregisterNotify failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0006"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) MyObserver(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + Uri newFileUri1(""); + result = g_fah->CreateFile(parentUri, "uri_file", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + sleep(1); + result = g_fah->UnregisterNotify(newFileUri1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver1); + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0006 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0006"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0007 + * @tc.name: external_file_access_notify_0007 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify failed when callback is Invalid. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0007"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + sptr myObserver1 = nullptr; + bool notifyForDescendants = true; + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + Uri newFileUri1(""); + result = g_fah->CreateFile(parentUri, "uri_file007", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1); + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver1); + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0007 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0007"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0008 + * @tc.name: external_file_access_notify_0008 + * @tc.desc: Test function of UnregisterNotify failed when uri is Invalid. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0008"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + sptr myObserver1 = new (std::nothrow) MyObserver(); + Uri newFileUri1("*/&%"); + bool notifyForDescendants = true; + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1); + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver1); + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0008 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0008"; +} + +/** + * @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 + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0009"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) MyObserver(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + Uri newFileUri1(""); + result = g_fah->CreateFile(parentUri, "测试文件", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newFileUri1); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + sleep(1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0009 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0009"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0010 + * @tc.name: external_file_access_notify_0010 + * @tc.desc: Test function of UnregisterNotify interface for failed when the Uri and callback do not match. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0010"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) MyObserver(); + sptr myObserver2 = new (std::nothrow) MyObserver(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].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); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + result = g_fah->UnregisterNotify(newFileUri1, myObserver2); + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri2, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0010 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0010"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0011 + * @tc.name: external_file_access_notify_0011 + * @tc.desc: Test function of RegisterNotify interface for SUCCESS when set the notifyForDescendants to true and + * @tc.desc: then set to false + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_0011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_0011"; + try { + 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) TestParentObserver(); + sptr myObserver2 = new (std::nothrow) MyObserver(); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].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); + EXPECT_EQ(g_notifyFlag, UPDATE_EVENT); + EXPECT_EQ(g_notifyParentFlag, UPDATE_EVENT); + + result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants2, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + sleep(2); + result = g_fah->Delete(renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + EXPECT_NE(g_notifyParentFlag, DELETE_EVENT); + + sleep(1); + result = g_fah->UnregisterNotify(newFileDir1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newFileDir1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0011 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_0011"; +} + +static void RegisterDirNotify(Uri parentUri, std::string dirName, IFileAccessObserver *observer) +{ + Uri newDirUriTest(""); + bool notifyForDescendants = true; + int result = g_fah->Mkdir(parentUri, dirName, newDirUriTest); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + sptr myObserver(observer); + result = g_fah->RegisterNotify(newDirUriTest, notifyForDescendants, myObserver); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(newDirUriTest); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyFlag, DELETE_EVENT); + sleep(2); + result = g_fah->UnregisterNotify(newDirUriTest, myObserver); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0012 + * @tc.name: external_file_access_notify_0012 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_00012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_00012"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + Uri newFileUri1(""); + GTEST_LOG_(INFO) << parentUri.ToString(); + sptr myObserver1 = new (std::nothrow) MyObserver(); + for (int i = 0; i < INIT_THREADS_NUMBER; i++) { + std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest", myObserver1.GetRefPtr()); + execthread1.join(); + } + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_00012 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_00012"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0013 + * @tc.name: external_file_access_notify_0013 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_00013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_00013"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + GTEST_LOG_(INFO) << parentUri.ToString(); + sptr myObserver1 = new (std::nothrow) MyObserver(); + std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest1", myObserver1.GetRefPtr()); + execthread1.join(); + std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest2", myObserver1.GetRefPtr()); + execthread2.join(); + std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest3", myObserver1.GetRefPtr()); + execthread3.join(); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_00013 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_00013"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0014 + * @tc.name: external_file_access_notify_0014 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_00014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_00014"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + GTEST_LOG_(INFO) << parentUri.ToString(); + sptr myObserver1 = new (std::nothrow) MyObserver(); + sptr myObserver2 = new (std::nothrow) MyObserver(); + sptr myObserver3 = new (std::nothrow) MyObserver(); + + std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest", myObserver1.GetRefPtr()); + execthread1.join(); + sleep(1); + std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest", myObserver2.GetRefPtr()); + execthread2.join(); + sleep(1); + std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest", myObserver3.GetRefPtr()); + execthread3.join(); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_00014 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_00014"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0015 + * @tc.name: external_file_access_notify_0015 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_notify_00015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_notify_00015"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + for (size_t i = 0; i < info.size(); i++) { + Uri parentUri(info[i].uri); + GTEST_LOG_(INFO) << parentUri.ToString(); + sptr myObserver1 = new (std::nothrow) MyObserver(); + sptr myObserver2 = new (std::nothrow) MyObserver(); + sptr myObserver3 = new (std::nothrow) MyObserver(); + + std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest1", myObserver1.GetRefPtr()); + execthread1.join(); + std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest2", myObserver2.GetRefPtr()); + execthread2.join(); + std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest3", myObserver3.GetRefPtr()); + execthread3.join(); + } + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_00015 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_notify_00015"; +} } // namespace \ No newline at end of file -- Gitee