diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 6ac19bbaa3f7f4ff30f93f2686627a52663e473b..624f5b03ec36f1f01d637f54d88679baa8c8f6ee 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -72,7 +72,10 @@ ohos_unittest("external_file_access_test") { } module_out_path = "user_file_service/tests" - sources = [ "external_file_access_test.cpp" ] + sources = [ + "external_file_access_test.cpp", + "external_notify_test.cpp", + ] include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", diff --git a/test/unittest/external_notify_test.cpp b/test/unittest/external_notify_test.cpp index e2fa83e48ceefba8b5fba677b71a6c1d6d820933..ac546cd36a5023edba67cea9be1b17635363df2c 100644 --- a/test/unittest/external_notify_test.cpp +++ b/test/unittest/external_notify_test.cpp @@ -45,7 +45,8 @@ shared_ptr g_fah = nullptr; int g_notifyEvent = -1; int g_notifyFlag = -1; string g_notifyUri = ""; -const int SLEEP_TIME = 100 * 1000; +vector g_notifyUris; +const int SLEEP_TIME = 600 * 1000; const int UID_TRANSFORM_TMP = 20000000; const int UID_DEFAULT = 0; shared_ptr g_context = nullptr; @@ -130,7 +131,8 @@ void MyObserver::OnChange(NotifyMessage ¬ifyMessage) g_notifyEvent = static_cast(notifyMessage.notifyType_); std::string notifyUri = notifyMessage.uris_[0]; g_notifyUri = notifyUri; - GTEST_LOG_(INFO) << "enter notify uri =" + notifyUri + "type =" + std::to_string(g_notifyEvent); + GTEST_LOG_(INFO) << "enter notify uri =" + notifyUri + " type =" + std::to_string(g_notifyEvent) +" uri size" + + std::to_string(notifyMessage.uris_.size()); } class TestObserver : public ObserverCallbackStub { @@ -144,6 +146,7 @@ void TestObserver::OnChange(NotifyMessage ¬ifyMessage) { g_notifyFlag = static_cast(notifyMessage.notifyType_); std::string notifyUri = notifyMessage.uris_[0]; + g_notifyUris = notifyMessage.uris_; GTEST_LOG_(INFO) << "enter TestObserver uri =" + notifyUri + "type =" + std::to_string(g_notifyFlag); } @@ -262,8 +265,8 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0001, testing::ext 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 parentUri(info[1].uri); Uri newFileUri1(""); result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); @@ -308,7 +311,8 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0001, testing::ext 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."; } @@ -389,6 +393,7 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0002, testing::ext 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); @@ -565,6 +570,7 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0005, testing::ext EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); result = g_fah->Delete(uri_dir); EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + sleep(1); } } catch (...) { GTEST_LOG_(ERROR) << "external_file_access_notify_0005 occurs an exception."; @@ -1037,4 +1043,133 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0016, testing::ext } GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0016"; } + +/** + * @tc.number: user_file_service_external_file_access_notify_0017 + * @tc.name: external_file_access_notify_0017 + * @tc.desc: Test event changes exceeding 500ms trigger callback + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0017, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0017"; + 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(); + Uri parentUri(info[1].uri); + Uri newFileDir1(""); + result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri fileUri(""); + result = g_fah->CreateFile(newFileDir1, "uri_file", fileUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME /10); + EXPECT_NE(g_notifyEvent, ADD_EVENT); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, ADD_EVENT); + + result = g_fah->UnregisterNotify(newFileDir1, myObserver1); + 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_0017 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0017"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0018 + * @tc.name: external_file_access_notify_0018 + * @tc.desc: Test event changes More than 32 notifications callback + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0018, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0018"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) TestObserver(); + Uri parentUri(info[1].uri); + Uri newFileDir1(""); + result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri fileUri(""); + for (int i = 0; i < 64; i++) { + result = g_fah->CreateFile(newFileDir1, "uri_file" + to_string(i), fileUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + sleep(1); + EXPECT_LE(g_notifyUris.size(), 32); + g_notifyUris.clear(); + result = g_fah->UnregisterNotify(newFileDir1, myObserver1); + 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_0018 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0018"; +} + +/** + * @tc.number: user_file_service_external_file_access_notify_0019 + * @tc.name: external_file_access_notify_0019 + * @tc.desc: Test event changes More than one notifications callback + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0019, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0019"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) TestObserver(); + Uri parentUri(info[1].uri); + Uri newFileDir1(""); + result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri fileUri(""); + for (int i = 0; i < 10; i++) { + result = g_fah->CreateFile(newFileDir1, "uri_file" + to_string(i), fileUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + } + usleep(SLEEP_TIME); + EXPECT_GT(g_notifyUris.size(), 1); + g_notifyUris.clear(); + result = g_fah->UnregisterNotify(newFileDir1, myObserver1); + 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_0019 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0019"; +} } // namespace