diff --git a/BUILD.gn b/BUILD.gn index 9e318da81c3daae2f0c777d3bfb7259f91619ded..f423d80e3bd3d99ef7271d93ecd9625d61827b76 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -28,6 +28,7 @@ group("accesstoken_build_module_test") { "//base/security/access_token/interfaces/innerkits/token_setproc/test:unittest", "//base/security/access_token/services/accesstokenmanager/test:unittest", "//base/security/access_token/services/common/database/test:unittest", + "//base/security/access_token/services/privacymanager/test:unittest", ] } if (token_sync_enable == true) { diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 4f5321129ba37fa0653758e427019d5145762c17..5061892c0a08fc4365d783c52cd833e559a1c16a 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -67,6 +67,7 @@ if (is_standard_system && ability_base_enable == true) { "src/record/permission_record_repository.cpp", "src/record/permission_used_record_cache.cpp", "src/sensitive/application_status_change_callback.cpp", + "src/sensitive/mic_global_switch_change_callback.cpp", "src/sensitive/sensitive_resource_manager.cpp", "src/service/privacy_manager_service.cpp", "src/service/privacy_manager_stub.cpp", diff --git a/services/privacymanager/include/sensitive/mic_global_switch_change_callback.h b/services/privacymanager/include/sensitive/mic_global_switch_change_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..bc34e01b003e690a8c227f5b4b3fc1c2776feffe --- /dev/null +++ b/services/privacymanager/include/sensitive/mic_global_switch_change_callback.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GLOBAL_SWITCH_CHANGE_CALLBACK_H +#define GLOBAL_SWITCH_CHANGE_CALLBACK_H + +#include "audio_routing_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +typedef void (*OnMicGlobalSwitchChangeCallback)(bool switchStatus); +class MicGlobalSwitchChangeCallback : public AudioStandard::AudioManagerMicStateChangeCallback { +public: + MicGlobalSwitchChangeCallback() = default; + virtual ~MicGlobalSwitchChangeCallback() {} + void OnMicStateUpdated(const AudioStandard::MicStateChangeEvent &micStateChangeEvent) override; + + void SetCallback(OnMicGlobalSwitchChangeCallback callback); + OnMicGlobalSwitchChangeCallback GetCallback() const; + +private: + OnMicGlobalSwitchChangeCallback callback_ = nullptr; +}; + +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // GLOBAL_SWITCH_CHANGE_CALLBACK_H \ No newline at end of file diff --git a/services/privacymanager/include/sensitive/sensitive_resource_manager.h b/services/privacymanager/include/sensitive/sensitive_resource_manager.h index 6dc2c8d5a7b181034f118da850c3fbe5c745b8f8..8afd08fce75c49af313e4b1c015d749dbb1912f7 100644 --- a/services/privacymanager/include/sensitive/sensitive_resource_manager.h +++ b/services/privacymanager/include/sensitive/sensitive_resource_manager.h @@ -21,6 +21,7 @@ #include #include "app_mgr_proxy.h" #include "application_status_change_callback.h" +#include "mic_global_switch_change_callback.h" #include "safe_map.h" namespace OHOS { @@ -49,11 +50,18 @@ public: bool GetAppStatus(const std::string& pkgName, int32_t& status); bool GetGlobalSwitch(const ResourceType type); void SetGlobalSwitch(const ResourceType type, bool switchStatus); + void SetFlowWindowStatus(bool isshow); + bool IsFlowWindowShow(); + + void ShowDialog(const ResourceType& type); // register and unregister app status change callback bool RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback); bool UnRegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback); - + + // mic global switch + bool RegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback); + bool UnRegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback); private: bool InitProxy(); OHOS::sptr GetAppManagerProxy(); @@ -62,6 +70,10 @@ private: std::mutex appStatusMutex_; std::vector> appStateCallbacks_; SafeMap switchStatusMap_; + std::mutex floatwindowStatusMutex_; + bool floatwindowStatus_ = false; + std::mutex MicGlobalSwitchMutex_; + std::vector> MicGlobalSwitchCallbacks_; std::mutex mutex_; sptr appMgrProxy_; }; diff --git a/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp b/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4496a56066c6e1f9a0780d16a55b34fd578f70af --- /dev/null +++ b/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mic_global_switch_change_callback.h" +#include "accesstoken_log.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "GlobalSwitchChangeCallback" +}; +} + +void MicGlobalSwitchChangeCallback::OnMicStateUpdated(const AudioStandard::MicStateChangeEvent &micStateChangeEvent) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(MicGlobalStatus=%{public}d)", micStateChangeEvent.mute); + + if (callback_ != nullptr) { + callback_(micStateChangeEvent.mute); + } +} + +void MicGlobalSwitchChangeCallback::SetCallback(OnMicGlobalSwitchChangeCallback callback) +{ + callback_ = callback; +} + +OnMicGlobalSwitchChangeCallback MicGlobalSwitchChangeCallback::GetCallback() const +{ + return callback_; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp b/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp index 46aea1b19e89b383d530198392cec4ab563da245..ba6c3e2c257e9149f4606d1274f1a8ee4fa01998 100644 --- a/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp +++ b/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp @@ -33,6 +33,13 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static const size_t MAX_CALLBACK_SIZE = 200; } +using namespace OHOS::Rosen; +using namespace OHOS::AudioStandard; + +static const std::string PERMISSION_MANAGER_BUNDLE_NAME = "com.ohos.permissionmanager"; +static const std::string PERMISSION_MANAGER_DIALOG_ABILITY = "com.ohos.permissionmanager.GlobalExtAbility"; +static const std::string RESOURCE_KEY = "ohos.sensitive.resource"; + SensitiveResourceManager& SensitiveResourceManager::GetInstance() { static SensitiveResourceManager instance; @@ -130,6 +137,46 @@ void SensitiveResourceManager::SetGlobalSwitch(const ResourceType type, bool swi switchStatusMap_[type] = switchStatus; } +void SensitiveResourceManager::SetFlowWindowStatus(bool isshow) +{ + std::lock_guard lock(floatwindowStatusMutex_); + floatwindowStatus_ = isshow; +} + +bool SensitiveResourceManager::IsFlowWindowShow() +{ + std::lock_guard lock(floatwindowStatusMutex_); + return floatwindowStatus_; +} + +void SensitiveResourceManager::ShowDialog(const ResourceType& type) +{ + AAFwk::Want want; + want.SetElementName(PERMISSION_MANAGER_BUNDLE_NAME, PERMISSION_MANAGER_DIALOG_ABILITY); + std::string typeStr = ""; + switch (type) { + case ResourceType::CAMERA: + typeStr = "camera"; + break; + case ResourceType::MICROPHONE: + typeStr = "microphone"; + break; + default: + break; + } + if (typeStr == "") { + ACCESSTOKEN_LOG_ERROR(LABEL, "type is invalid, type:%{public}d", type); + return; + } + want.SetParam(RESOURCE_KEY, typeStr); + ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, nullptr); + if (err != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Fail to StartAbility, err:%{public}d", err); + return; + } + return; +} + bool SensitiveResourceManager::RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback) { if (callback == nullptr) { @@ -208,6 +255,59 @@ bool SensitiveResourceManager::UnRegisterAppStatusChangeCallback(uint32_t tokenI return true; } + +bool SensitiveResourceManager::RegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback) +{ + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback could not be null."); + return false; + } + + std::lock_guard lock(MicGlobalSwitchMutex_); + auto iter = std::find_if(MicGlobalSwitchCallbacks_.begin(), MicGlobalSwitchCallbacks_.end(), + [callback](const std::shared_ptr& rec) { + return callback == rec->GetCallback(); + }); + if (iter != MicGlobalSwitchCallbacks_.end()) { + ACCESSTOKEN_LOG_WARN(LABEL, "callback is already registered"); + return true; + } + + std::shared_ptr listener(new MicGlobalSwitchChangeCallback()); + if (listener == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "new fail."); + return false; + } + listener->SetCallback(callback); + MicGlobalSwitchCallbacks_.emplace_back(listener); + + AudioStandard::AudioRoutingManager::GetInstance()->SetMicStateChangeCallback(listener); + ACCESSTOKEN_LOG_INFO(LABEL, "register Miceophone callback(%{public}p).", callback); + + return true; +} + +bool SensitiveResourceManager::UnRegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback) +{ + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback could not be null."); + return false; + } + + std::lock_guard lock(MicGlobalSwitchMutex_); + auto iter = std::find_if(MicGlobalSwitchCallbacks_.begin(), MicGlobalSwitchCallbacks_.end(), + [callback](const std::shared_ptr& rec) { + return callback == rec->GetCallback(); + }); + if (iter == MicGlobalSwitchCallbacks_.end()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback is not found."); + return false; + } + + MicGlobalSwitchCallbacks_.erase(iter); + ACCESSTOKEN_LOG_INFO(LABEL, "unregister callback(%{public}p).", callback); + return true; +} } // namespace AccessToken } // namespace Security } // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..5c4877cbddf7b8bdbded63d6de25f2c8260c0cc4 --- /dev/null +++ b/services/privacymanager/test/BUILD.gn @@ -0,0 +1,86 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/security/access_token/access_token.gni") +import("//build/test.gni") + +ohos_unittest("libsensitive_resource_manager_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "//base/security/access_token/frameworks/privacy/include", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/privacy/include", + "//base/security/access_token/services/common/database/include", + "//base/global/resource_management/interfaces/inner_api/include", + "//base/security/access_token/services/privacymanager/include/sensitive", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/token_setproc/include", + "//foundation/ability/ability_runtime/interfaces/kits/native/ability/ability_runtime", + "//foundation/arkui/napi", + "//foundation/multimedia/audio_framework/interfaces/inner_api/native/audiomanager/include", + "//foundation/window/window_manager/wm/include", + "//third_party/googletest/include", + "//commonlibrary/c_utils/base/include", + ] + + sources = [ + "//base/security/access_token/services/privacymanager/src/sensitive/application_status_change_callback.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp", + "unittest/sensitive_resource_manager_test.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + if (dlp_permission_enable == true) { + cflags_cc += [ "-DSUPPORT_SANDBOX_APP" ] + } + + configs = [ "//base/security/access_token/config:coverage_flags" ] + + deps = [ + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", + "//base/security/access_token/services/privacymanager:privacymanager", + "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", + "//foundation/ability/ability_runtime/services/abilitymgr:abilityms_target", + "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", + "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_runtime:ability_context_native", + "ability_runtime:ability_manager", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "multimedia_audio_framework:audio_client", + "samgr:samgr_proxy", + "window_manager:libwm", + ] +} + +group("unittest") { + testonly = true + deps = [ ":libsensitive_resource_manager_test" ] +} diff --git a/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a20bde10a02f5f24a7c09e58f309079c049ac17 --- /dev/null +++ b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp @@ -0,0 +1,275 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "ability_context_impl.h" +#include "accesstoken_kit.h" +#include "audio_system_manager.h" +#include "sensitive_resource_manager.h" +#include "token_setproc.h" +#include "window.h" +#include "window_scene.h" +#include "wm_common.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Rosen; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::AudioStandard; + +class SensitiveResourceManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void SensitiveResourceManagerTest::SetUpTestCase() +{ +} + +void SensitiveResourceManagerTest::TearDownTestCase() +{ +} + +void SensitiveResourceManagerTest::SetUp() +{ + SensitiveResourceManager::GetInstance().Init(); +} + +void SensitiveResourceManagerTest::TearDown() +{ +} + +static bool mic_isMute = true; +static void OnChangeMicGlobalSwitch(bool isMute) +{ + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch mic_isMute_before_set: " << mic_isMute; + mic_isMute = isMute; + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch mic_isMute_after_set: " << mic_isMute; +} + +static void ResetEnv() +{ + mic_isMute = true; +} + +/** + * @tc.name: GetGlobalSwitchTest_001 + * @tc.desc: Verify the GetGlobalSwitch with vaild ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, GetGlobalSwitchTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::MICROPHONE, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::MICROPHONE, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); +} + +/** + * @tc.name: GetGlobalSwitchTest_002 + * @tc.desc: Verify the GetGlobalSwitch abnormal branch ResourceType is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, GetGlobalSwitchTest_002, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::INVALID)); +} + +/** + * @tc.name: SetGlobalSwitchTest_001 + * @tc.desc: Verify the SetGlobalSwitch with vaild ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, SetGlobalSwitchTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::CAMERA, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::CAMERA)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::CAMERA, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::CAMERA)); +} + +/** + * @tc.name: SetGlobalSwitchTest_002 + * @tc.desc: Verify the SetGlobalSwitch abnormal branch ResourceType is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, SetGlobalSwitchTest_002, TestSize.Level1) +{ + bool isMicrophoneMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::INVALID, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(isMicrophoneMute, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::INVALID, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(isMicrophoneMute, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); +} + +/** + * @tc.name: FlowWindowStatusTest_001 + * @tc.desc: Verify the SetFlowWindowStatus IsFlowWindowShow. + * @tc.type: FUNC + * @tc.require: issueI5RWX5 + */ +HWTEST_F(SensitiveResourceManagerTest, FlowWindowStatusTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().SetFlowWindowStatus(true); + + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().IsFlowWindowShow()); + + SensitiveResourceManager::GetInstance().SetFlowWindowStatus(false); + + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().IsFlowWindowShow()); +} + +/** + * @tc.name: RegisterMicGlobalSwitchChangeCallbackTest_001 + * @tc.desc: call RegisterMicGlobalSwitchChangeCallback once. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterMicGlobalSwitchChangeCallbackTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); + + bool isMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, mic_isMute); + + ResetEnv(); + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, mic_isMute); + + ResetEnv(); + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(isMute); + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); +} + + +/** + * @tc.name: RegisterMicGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the RegisterMicGlobalSwitchChangeCallback abnormal branch callback is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterMicGlobalSwitchChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(nullptr)); + + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); +} + +/** + * @tc.name: UnRegisterMicGlobalSwitchChangeCallbackTest_001 + * @tc.desc: Verify the UnRegisterMicGlobalSwitchChangeCallback with vaild callback. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterMicGlobalSwitchChangeCallbackTest_001, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); +} + +/** + * @tc.name: UnRegisterMicGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the UnRegisterMicGlobalSwitchChangeCallback abnormal branch callback is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterMicGlobalSwitchChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(nullptr)); + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); +} + +/** + * @tc.name: ShowDialogTest + * @tc.desc: call ShowDialog once. + * @tc.type: FUNC + * @tc.require: issueI5RWXF issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, ShowDialogTest, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); + + bool isMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch isMute: " << isMute; + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(true); + usleep(5000000); // 5000000us = 5s + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::MICROPHONE); + usleep(5000000); // 5000000us = 5s + ASSERT_EQ(false, mic_isMute); + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(true); + usleep(5000000); // 5000000us = 5s + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::MICROPHONE); + usleep(5000000); // 5000000us = 5s + ASSERT_EQ(true, mic_isMute); + + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch isMute: " << isMute; + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(isMute); + + ResetEnv(); + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); +} + +/** + * @tc.name: ShowDialogTest_001 + * @tc.desc: call ShowDialog once with valid ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXF issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, ShowDialogTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::MICROPHONE); + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::CAMERA); +} + + +/** + * @tc.name: ShowDialogTest_002 + * @tc.desc: call ShowDialog once with invalid ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXF issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, ShowDialogTest_002, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::INVALID); +} \ No newline at end of file