diff --git a/services/cloudfiledaemon/include/ipc/cloud_daemon_stub.h b/services/cloudfiledaemon/include/ipc/cloud_daemon_stub.h index 0d7044e31a1ffd97ee018f8a9d73a688fabb85bf..a09df582c76cebe4d62c846f7fc4fd4b32201741 100644 --- a/services/cloudfiledaemon/include/ipc/cloud_daemon_stub.h +++ b/services/cloudfiledaemon/include/ipc/cloud_daemon_stub.h @@ -38,6 +38,11 @@ private: int32_t HandleStartFuseInner(MessageParcel &data, MessageParcel &reply); }; + +class ICloudDaemonBroker : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.filemanagement.distributedfile.clouddaemon") +}; } // namespace CloudFile } // namespace FileManagement } // namespace OHOS diff --git a/services/cloudfiledaemon/src/utils/setting_data_helper.cpp b/services/cloudfiledaemon/src/utils/setting_data_helper.cpp index 40f1173911b5cf1790d0866b84a67fe1bc7cbf3a..e846cfcfe8cb5a32e1c0d9191121f86cd94d85e0 100644 --- a/services/cloudfiledaemon/src/utils/setting_data_helper.cpp +++ b/services/cloudfiledaemon/src/utils/setting_data_helper.cpp @@ -14,6 +14,7 @@ */ #include "setting_data_helper.h" +#include "cloud_daemon_stub.h" #include "data_sync_const.h" #include "datashare_helper.h" #include "settings_data_manager.h" @@ -25,7 +26,9 @@ namespace CloudFile { using namespace std; using namespace OHOS::FileManagement::CloudSync; namespace { -static const string SETTING_DATA_QUERY_URI = "datashareproxy://"; +static const string SETTINGS_DATA_COMMON_URI = + "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true"; +static const string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; static const string SYNC_SWITCH_KEY = "photos_sync_options"; } // namespace @@ -47,16 +50,34 @@ bool SettingDataHelper::IsDataShareReady() return true; } // try get DataShareHelper - DataShare::CreateOptions options; - options.enabled_ = true; - auto dataShareHelper = DataShare::DataShareHelper::Creator(SETTING_DATA_QUERY_URI, options); - if (dataShareHelper == nullptr) { - LOGE("get data share helper fail, will retry again after recv ready event"); + auto remote = sptr(new (std::nothrow) IRemoteStub()); + if (remote == nullptr) { + LOGE("remote is nullptr"); return false; } - bool ret = dataShareHelper->Release(); - LOGI("release data share helper, ret=%{public}d", ret); - isDataShareReady_ = true; + auto remoteObj = remote->AsObject(); + if (remoteObj == nullptr) { + LOGE("remoteObj is nullptr"); + return false; + } + + pair> ret = + DataShare::DataShareHelper::Create(remoteObj, SETTINGS_DATA_COMMON_URI, SETTINGS_DATA_EXT_URI); + LOGI("create datashare helper, ret=%{public}d", ret.first); + if (ret.first == DataShare::E_DATA_SHARE_NOT_READY) { + LOGE("create datashare helper faild"); + isDataShareReady_ = false; + return false; + } + if (ret.first == DataShare::E_OK) { + auto helper = ret.second; + if (helper != nullptr) { + bool releaseRet = helper->Release(); + LOGI("release datashare helper, ret=%{public}d", releaseRet); + } + isDataShareReady_ = true; + return true; + } return true; } diff --git a/test/unittests/cloud_disk/BUILD.gn b/test/unittests/cloud_disk/BUILD.gn index edfce2a26c100d6d1cd25d0ac0f1f6b6bbbacf7f..ebdd6f8a79e398b93c0ea61e4a8740e303b770d5 100644 --- a/test/unittests/cloud_disk/BUILD.gn +++ b/test/unittests/cloud_disk/BUILD.gn @@ -22,11 +22,13 @@ ohos_unittest("account_status_listener_test") { "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/include", "${distributedfile_path}/utils/log/include", "${distributedfile_path}/utils/ioctl/include", + "${innerkits_native_path}/cloud_daemon_kit_inner", "${innerkits_native_path}/cloud_file_kit_inner", "${innerkits_native_path}/cloud_file_kit_inner/big_data_statistics", "${services_path}/cloudfiledaemon/include/", "${services_path}/cloudfiledaemon/include/cloud_disk/", "${services_path}/cloudfiledaemon/include/fuse_manager/", + "${services_path}/cloudfiledaemon/include/ipc/", "${services_path}/cloudfiledaemon/include/utils/", ] diff --git a/test/unittests/services_daemon/BUILD.gn b/test/unittests/services_daemon/BUILD.gn index 56cd110061b7a793e10cf908c7460cff83d77580..b6a5ac2453f677fd752e51ecd7952b3338b3b4ec 100644 --- a/test/unittests/services_daemon/BUILD.gn +++ b/test/unittests/services_daemon/BUILD.gn @@ -41,7 +41,9 @@ ohos_unittest("fuse_manager_test") { "${distributedfile_path}/services/cloudfiledaemon/include", "${distributedfile_path}/services/cloudfiledaemon/include/fuse_manager", "${distributedfile_path}/utils/ioctl/include", + "${innerkits_native_path}/cloud_daemon_kit_inner", "${services_path}/cloudfiledaemon/include/cloud_disk/", + "${services_path}/cloudfiledaemon/include/ipc/", "${services_path}/cloudfiledaemon/include/utils/", "${services_path}/cloudfiledaemon/src/fuse_manager", "${distributedfile_path}/adapter/cloud_adapter_example/include", diff --git a/test/unittests/services_daemon/mock/data_helper_mock.cpp b/test/unittests/services_daemon/mock/data_helper_mock.cpp index 8c2bfc79c75f56ef8c643425e3b2b86dc5cb4440..afa18b73ccc3452018e23b8d75d6b59e93056687 100644 --- a/test/unittests/services_daemon/mock/data_helper_mock.cpp +++ b/test/unittests/services_daemon/mock/data_helper_mock.cpp @@ -16,13 +16,12 @@ namespace OHOS::DataShare { using namespace OHOS::FileManagement::CloudFile; -std::shared_ptr DataShareHelper::Creator(const std::string &strUri, - const CreateOptions &options, - const std::string &bundleName, - const int waitTime, - bool isSystem) +std::pair> DataShareHelper::Create(const sptr &token, + const std::string &strUri, + const std::string &extUri, + const int waitTime) { - return IDataHelper::ins->Creator(); + return IDataHelper::ins->Create(); } } // namespace OHOS::DataShare diff --git a/test/unittests/services_daemon/mock/data_helper_mock.h b/test/unittests/services_daemon/mock/data_helper_mock.h index 87afdd7df869f7a2c0db04c7bacd2bcaec641c4b..1d40f93473b88ac12e67ae6beceaca4b45061609 100644 --- a/test/unittests/services_daemon/mock/data_helper_mock.h +++ b/test/unittests/services_daemon/mock/data_helper_mock.h @@ -97,7 +97,7 @@ public: class IDataHelper { public: virtual ~IDataHelper() = default; - virtual std::shared_ptr Creator() = 0; + virtual std::pair> Create() = 0; virtual SwitchStatus GetSwitchStatus() = 0; virtual int32_t QuerySwitchStatus() = 0; @@ -107,7 +107,7 @@ public: class DataHelperMock : public IDataHelper { public: - MOCK_METHOD0(Creator, std::shared_ptr()); + MOCK_METHOD0(Create, std::pair>()); MOCK_METHOD0(GetSwitchStatus, SwitchStatus()); MOCK_METHOD0(QuerySwitchStatus, int32_t()); }; diff --git a/test/unittests/services_daemon/setting_data_helper_test.cpp b/test/unittests/services_daemon/setting_data_helper_test.cpp index 6c85941bae6b55ca6eaf97296081d7099275fbae..7628aeb53623e2df1d4514ea05c731e219be681a 100644 --- a/test/unittests/services_daemon/setting_data_helper_test.cpp +++ b/test/unittests/services_daemon/setting_data_helper_test.cpp @@ -126,7 +126,7 @@ HWTEST_F(SettingDataHelperTest, IsDataShareReadyTest002, TestSize.Level1) { GTEST_LOG_(INFO) << "IsDataShareReadyTest002 start"; try { - EXPECT_CALL(*mock_, Creator()).WillOnce(Return(nullptr)); + EXPECT_CALL(*mock_, Create()).WillOnce(Return(make_pair(DataShare::E_DATA_SHARE_NOT_READY, nullptr))); SettingDataHelper::GetInstance().isDataShareReady_ = false; bool ret = SettingDataHelper::GetInstance().IsDataShareReady(); EXPECT_EQ(ret, false); @@ -146,9 +146,7 @@ HWTEST_F(SettingDataHelperTest, IsDataShareReadyTest003, TestSize.Level1) { GTEST_LOG_(INFO) << "IsDataShareReadyTest003 start"; try { - shared_ptr helper = std::make_shared();; - EXPECT_CALL(*helper, Release()).WillOnce(Return(true)); - EXPECT_CALL(*mock_, Creator()).WillOnce(Return(helper)); + EXPECT_CALL(*mock_, Create()).WillOnce(Return(make_pair(DataShare::E_OK, nullptr))); SettingDataHelper::GetInstance().isDataShareReady_ = false; bool ret = SettingDataHelper::GetInstance().IsDataShareReady(); EXPECT_EQ(ret, true); @@ -159,6 +157,48 @@ HWTEST_F(SettingDataHelperTest, IsDataShareReadyTest003, TestSize.Level1) GTEST_LOG_(INFO) << "IsDataShareReadyTest003 end"; } +/** + * @tc.name: IsDataShareReadyTest004 + * @tc.desc: Verify the IsDataShareReady function + * @tc.type: FUNC + */ +HWTEST_F(SettingDataHelperTest, IsDataShareReadyTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IsDataShareReadyTest004 start"; + try { + shared_ptr helper = std::make_shared();; + EXPECT_CALL(*helper, Release()).WillOnce(Return(true)); + EXPECT_CALL(*mock_, Create()).WillOnce(Return(make_pair(DataShare::E_OK, helper))); + SettingDataHelper::GetInstance().isDataShareReady_ = false; + bool ret = SettingDataHelper::GetInstance().IsDataShareReady(); + EXPECT_EQ(ret, true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IsDataShareReadyTest004 failed"; + } + GTEST_LOG_(INFO) << "IsDataShareReadyTest004 end"; +} + +/** + * @tc.name: IsDataShareReadyTest005 + * @tc.desc: Verify the IsDataShareReady function + * @tc.type: FUNC + */ +HWTEST_F(SettingDataHelperTest, IsDataShareReadyTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IsDataShareReadyTest005 start"; + try { + EXPECT_CALL(*mock_, Create()).WillOnce(Return(make_pair(DataShare::E_ERROR, nullptr))); + SettingDataHelper::GetInstance().isDataShareReady_ = false; + bool ret = SettingDataHelper::GetInstance().IsDataShareReady(); + EXPECT_EQ(ret, true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IsDataShareReadyTest005 failed"; + } + GTEST_LOG_(INFO) << "IsDataShareReadyTest005 end"; +} + /** * @tc.name: GetSwitchStatus001 * @tc.desc: Verify the GetSwitchStatus function @@ -208,7 +248,7 @@ HWTEST_F(SettingDataHelperTest, GetSwitchStatus003, TestSize.Level1) try { EXPECT_CALL(*mock_, GetSwitchStatus()).WillOnce(Return(SwitchStatus::NONE)); string bundle = SettingDataHelper::GetInstance().GetActiveBundle(); - EXPECT_EQ(bundle, ""); + EXPECT_EQ(bundle, GALLERY_BUNDLE_NAME); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GetSwitchStatus003 failed"; @@ -247,7 +287,7 @@ HWTEST_F(SettingDataHelperTest, InitActiveBundle002, TestSize.Level1) SettingDataHelper::GetInstance().isBundleInited_ = false; SettingDataHelper::GetInstance().isDataShareReady_ = false; - EXPECT_CALL(*mock_, Creator()).WillOnce(Return(nullptr)); + EXPECT_CALL(*mock_, Create()).WillOnce(Return(make_pair(DataShare::E_DATA_SHARE_NOT_READY, nullptr))); bool ret = SettingDataHelper::GetInstance().InitActiveBundle(); EXPECT_EQ(ret, false); @@ -272,8 +312,7 @@ HWTEST_F(SettingDataHelperTest, InitActiveBundle003, TestSize.Level1) shared_ptr helper = std::make_shared(); EXPECT_CALL(*helper, Release()).WillOnce(Return(true)); - EXPECT_CALL(*mock_, Creator()).WillOnce(Return(helper)); - EXPECT_CALL(*mock_, QuerySwitchStatus()).WillOnce(Return(E_OK)); + EXPECT_CALL(*mock_, Create()).WillOnce(Return(make_pair(DataShare::E_OK, helper))); EXPECT_CALL(*mock_, GetSwitchStatus()).WillOnce(Return(CLOUD_SPACE)); bool ret = SettingDataHelper::GetInstance().InitActiveBundle(); @@ -294,7 +333,6 @@ HWTEST_F(SettingDataHelperTest, UpdateActiveBundle001, TestSize.Level1) { GTEST_LOG_(INFO) << "UpdateActiveBundle001 start"; try { - EXPECT_CALL(*mock_, QuerySwitchStatus()).WillOnce(Return(E_OK)); EXPECT_CALL(*mock_, GetSwitchStatus()).WillOnce(Return(CLOUD_SPACE)); SettingDataHelper::GetInstance().UpdateActiveBundle();