From 46b4803e80d3ea4834e39f6c008283ecbfa3b6b6 Mon Sep 17 00:00:00 2001 From: caochuan Date: Fri, 20 Oct 2023 15:25:27 +0800 Subject: [PATCH] aggregated test Signed-off-by: caochuan --- test/unittest/BUILD.gn | 5 +- test/unittest/external_notify_test.cpp | 138 ++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 3 deletions(-) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 9f70161b..996390c5 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -60,7 +60,10 @@ ohos_unittest("medialibrary_file_access_test") { 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 5618df0f..c85a0441 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; @@ -129,7 +130,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 { @@ -143,6 +145,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); } @@ -388,6 +391,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); @@ -564,6 +568,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."; @@ -1036,4 +1041,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 -- Gitee