microphone_mute_notification_controller_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <memory>
  5. #include <string>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/public/cpp/microphone_mute_notification_delegate.h"
  8. #include "ash/system/microphone_mute/microphone_mute_notification_controller.h"
  9. #include "ash/test/ash_test_base.h"
  10. #include "base/check.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/test/scoped_feature_list.h"
  13. #include "chromeos/ash/components/dbus/audio/fake_cras_audio_client.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #include "ui/message_center/message_center.h"
  16. #include "ui/message_center/public/cpp/notification.h"
  17. #include "ui/message_center/public/cpp/notification_types.h"
  18. namespace ash {
  19. class FakeMicrophoneMuteNotificationDelegate
  20. : public MicrophoneMuteNotificationDelegate {
  21. public:
  22. absl::optional<std::u16string> GetAppAccessingMicrophone() override {
  23. return app_name_;
  24. }
  25. void SetAppAccessingMicrophone(
  26. const absl::optional<std::u16string> app_name) {
  27. app_name_ = app_name;
  28. }
  29. absl::optional<std::u16string> app_name_;
  30. };
  31. class MicrophoneMuteNotificationControllerTest : public AshTestBase {
  32. public:
  33. MicrophoneMuteNotificationControllerTest() {
  34. scoped_feature_list_.InitAndEnableFeature(features::kMicMuteNotifications);
  35. }
  36. ~MicrophoneMuteNotificationControllerTest() override = default;
  37. // AshTestBase:
  38. void SetUp() override {
  39. AshTestBase::SetUp();
  40. controller_ = std::make_unique<MicrophoneMuteNotificationController>();
  41. delegate_ = std::make_unique<FakeMicrophoneMuteNotificationDelegate>();
  42. }
  43. void TearDown() override {
  44. controller_.reset();
  45. delegate_.reset();
  46. SetMicrophoneMuteSwitchState(/*muted=*/false);
  47. AshTestBase::TearDown();
  48. }
  49. protected:
  50. message_center::Notification* GetNotification() {
  51. const message_center::NotificationList::Notifications& notifications =
  52. message_center::MessageCenter::Get()->GetVisibleNotifications();
  53. for (auto* notification : notifications) {
  54. if (notification->id() ==
  55. MicrophoneMuteNotificationController::kNotificationId) {
  56. return notification;
  57. }
  58. }
  59. return nullptr;
  60. }
  61. message_center::Notification* GetPopupNotification() {
  62. const message_center::NotificationList::PopupNotifications& notifications =
  63. message_center::MessageCenter::Get()->GetPopupNotifications();
  64. for (auto* notification : notifications) {
  65. if (notification->id() ==
  66. MicrophoneMuteNotificationController::kNotificationId) {
  67. return notification;
  68. }
  69. }
  70. return nullptr;
  71. }
  72. void MarkPopupAsShown() {
  73. message_center::MessageCenter::Get()->MarkSinglePopupAsShown(
  74. MicrophoneMuteNotificationController::kNotificationId, true);
  75. }
  76. void ClickOnNotificationButton() {
  77. message_center::MessageCenter::Get()->ClickOnNotificationButton(
  78. MicrophoneMuteNotificationController::kNotificationId,
  79. /*button_index=*/0);
  80. }
  81. void SetMicrophoneMuteSwitchState(bool muted) {
  82. ui::MicrophoneMuteSwitchMonitor::Get()->SetMicrophoneMuteSwitchValue(muted);
  83. }
  84. void MuteMicrophone() { CrasAudioHandler::Get()->SetInputMute(true); }
  85. void UnMuteMicrophone() { CrasAudioHandler::Get()->SetInputMute(false); }
  86. void SetNumberOfActiveInputStreams(int number_of_active_input_streams) {
  87. FakeCrasAudioClient::Get()->SetActiveInputStreamsWithPermission(
  88. {{"CRAS_CLIENT_TYPE_CHROME", number_of_active_input_streams}});
  89. }
  90. void LaunchApp(absl::optional<std::u16string> app_name) {
  91. delegate_->SetAppAccessingMicrophone(app_name);
  92. }
  93. private:
  94. base::test::ScopedFeatureList scoped_feature_list_;
  95. std::unique_ptr<MicrophoneMuteNotificationController> controller_;
  96. std::unique_ptr<FakeMicrophoneMuteNotificationDelegate> delegate_;
  97. };
  98. TEST_F(MicrophoneMuteNotificationControllerTest, SimpleMuteUnMute) {
  99. // No notification initially.
  100. EXPECT_FALSE(GetNotification());
  101. // Or when we mute.
  102. MuteMicrophone();
  103. EXPECT_FALSE(GetNotification());
  104. // Or when we unmute.
  105. UnMuteMicrophone();
  106. EXPECT_FALSE(GetNotification());
  107. }
  108. TEST_F(MicrophoneMuteNotificationControllerTest, LaunchAppNotUsingMicrophone) {
  109. // No notification initially.
  110. EXPECT_FALSE(GetNotification());
  111. // No notification when we unmute.
  112. UnMuteMicrophone();
  113. EXPECT_FALSE(GetNotification());
  114. // Launch an app that's not using the mic, should be no notification.
  115. LaunchApp(absl::nullopt);
  116. SetNumberOfActiveInputStreams(0);
  117. EXPECT_FALSE(GetNotification());
  118. // Mute the mic, still no notification because no app is using the mic.
  119. MuteMicrophone();
  120. EXPECT_FALSE(GetNotification());
  121. }
  122. TEST_F(MicrophoneMuteNotificationControllerTest, LaunchAppUsingMicrophone) {
  123. // No notification initially.
  124. EXPECT_FALSE(GetNotification());
  125. // No notification when we unmute.
  126. UnMuteMicrophone();
  127. EXPECT_FALSE(GetNotification());
  128. // Mute the mic, still no notification.
  129. MuteMicrophone();
  130. EXPECT_FALSE(GetNotification());
  131. // Launch an app that's using the mic. The microphone mute notification should
  132. // show as a popup
  133. LaunchApp(u"junior");
  134. SetNumberOfActiveInputStreams(1);
  135. EXPECT_TRUE(GetNotification());
  136. EXPECT_TRUE(GetPopupNotification());
  137. // Unmute again, notification goes down.
  138. UnMuteMicrophone();
  139. EXPECT_FALSE(GetNotification());
  140. }
  141. TEST_F(MicrophoneMuteNotificationControllerTest,
  142. SilentNotificationOnMuteWhileMicInUse) {
  143. // No notification initially.
  144. EXPECT_FALSE(GetNotification());
  145. // Launch an app that's using the mic, no notification because the microphone
  146. // is not muted.
  147. LaunchApp(u"junior");
  148. SetNumberOfActiveInputStreams(1);
  149. EXPECT_FALSE(GetNotification());
  150. // Mute the mic, a notification should be shown, but not as a popup.
  151. MuteMicrophone();
  152. EXPECT_TRUE(GetNotification());
  153. EXPECT_FALSE(GetPopupNotification());
  154. }
  155. TEST_F(MicrophoneMuteNotificationControllerTest,
  156. ShowPopupNotificationOnStreamAddition) {
  157. // Launch an app while microphone is muted.
  158. MuteMicrophone();
  159. LaunchApp(u"junior");
  160. SetNumberOfActiveInputStreams(1);
  161. ASSERT_TRUE(GetNotification());
  162. ASSERT_TRUE(GetPopupNotification());
  163. // Mark the notification as read.
  164. MarkPopupAsShown();
  165. ASSERT_FALSE(GetPopupNotification());
  166. // Add an app, and verify the notification popup gets shown.
  167. LaunchApp(u"rose");
  168. SetNumberOfActiveInputStreams(2);
  169. EXPECT_TRUE(GetNotification());
  170. EXPECT_TRUE(GetPopupNotification());
  171. }
  172. TEST_F(MicrophoneMuteNotificationControllerTest,
  173. RemovingStreamDoesNotShowPopup) {
  174. // Launch 2 apps while microphone is muted.
  175. MuteMicrophone();
  176. LaunchApp(u"junior");
  177. LaunchApp(u"rose");
  178. SetNumberOfActiveInputStreams(2);
  179. ASSERT_TRUE(GetNotification());
  180. ASSERT_TRUE(GetPopupNotification());
  181. // Mark the notification as read.
  182. MarkPopupAsShown();
  183. ASSERT_FALSE(GetPopupNotification());
  184. // Remove an active stream, and verify that the notification popup is not
  185. // reshown.
  186. SetNumberOfActiveInputStreams(1);
  187. EXPECT_TRUE(GetNotification());
  188. EXPECT_FALSE(GetPopupNotification());
  189. // The notification should be removed if all input streams are removed.
  190. LaunchApp(absl::nullopt);
  191. SetNumberOfActiveInputStreams(0);
  192. EXPECT_FALSE(GetNotification());
  193. }
  194. TEST_F(MicrophoneMuteNotificationControllerTest, MuteNotificationActionButton) {
  195. MuteMicrophone();
  196. LaunchApp(u"junior");
  197. SetNumberOfActiveInputStreams(1);
  198. // The mute notification should have an action button.
  199. message_center::Notification* notification = GetNotification();
  200. ASSERT_TRUE(notification);
  201. EXPECT_EQ(1u, notification->buttons().size());
  202. // Clicking the action button should unmute device.
  203. ClickOnNotificationButton();
  204. EXPECT_FALSE(chromeos::CrasAudioHandler::Get()->IsInputMuted());
  205. EXPECT_FALSE(GetNotification());
  206. }
  207. TEST_F(MicrophoneMuteNotificationControllerTest,
  208. NoNotificationActionButtonIfMutedByHwSwitch) {
  209. SetMicrophoneMuteSwitchState(/*muted=*/true);
  210. LaunchApp(u"junior");
  211. SetNumberOfActiveInputStreams(1);
  212. // The mute notification should not have an action button if device is muted
  213. // by a mute switch.
  214. message_center::Notification* notification = GetNotification();
  215. ASSERT_TRUE(notification);
  216. EXPECT_EQ(0u, notification->buttons().size());
  217. SetMicrophoneMuteSwitchState(/*muted=*/false);
  218. ASSERT_FALSE(chromeos::CrasAudioHandler::Get()->IsInputMuted());
  219. EXPECT_FALSE(GetNotification());
  220. }
  221. TEST_F(MicrophoneMuteNotificationControllerTest,
  222. TogglingMuteSwitchRemovesNotificationActionButton) {
  223. // Mute microphone, and activate an audio input stream.
  224. MuteMicrophone();
  225. LaunchApp(u"junior");
  226. SetNumberOfActiveInputStreams(1);
  227. // The mute notification should have an action button.
  228. message_center::Notification* notification = GetNotification();
  229. ASSERT_TRUE(notification);
  230. EXPECT_EQ(1u, notification->buttons().size());
  231. // Toggle microphone mute switch and verify the action button disappears.
  232. SetMicrophoneMuteSwitchState(/*muted=*/true);
  233. notification = GetNotification();
  234. ASSERT_TRUE(notification);
  235. EXPECT_EQ(0u, notification->buttons().size());
  236. SetMicrophoneMuteSwitchState(/*muted=*/false);
  237. ASSERT_FALSE(chromeos::CrasAudioHandler::Get()->IsInputMuted());
  238. EXPECT_FALSE(GetNotification());
  239. }
  240. TEST_F(MicrophoneMuteNotificationControllerTest,
  241. TogglingMuteSwitchDoesNotHideNotificationPopup) {
  242. // Mute microphone, and activate an audio input stream.
  243. MuteMicrophone();
  244. LaunchApp(u"junior");
  245. SetNumberOfActiveInputStreams(1);
  246. // Verify the notification popup is shown.
  247. ASSERT_TRUE(GetNotification());
  248. ASSERT_TRUE(GetPopupNotification());
  249. // Toggle microphone mute switch and verify that toggling mute switch alone
  250. // does not hide the notification popup.
  251. SetMicrophoneMuteSwitchState(/*muted=*/true);
  252. EXPECT_TRUE(GetNotification());
  253. EXPECT_TRUE(GetPopupNotification());
  254. SetMicrophoneMuteSwitchState(/*muted=*/false);
  255. ASSERT_FALSE(chromeos::CrasAudioHandler::Get()->IsInputMuted());
  256. EXPECT_FALSE(GetNotification());
  257. }
  258. TEST_F(MicrophoneMuteNotificationControllerTest,
  259. RemovingAllInputStreamsWhileHwSwitchToggled) {
  260. SetMicrophoneMuteSwitchState(/*muted=*/true);
  261. LaunchApp(u"junior");
  262. SetNumberOfActiveInputStreams(2);
  263. EXPECT_TRUE(GetNotification());
  264. EXPECT_TRUE(GetPopupNotification());
  265. SetNumberOfActiveInputStreams(0);
  266. EXPECT_FALSE(GetNotification());
  267. }
  268. TEST_F(MicrophoneMuteNotificationControllerTest,
  269. ToggleMicrophoneMuteSwitchWhileInputStreamActive) {
  270. // Launch an app using microphone, and toggle mute switch.
  271. LaunchApp(u"junior");
  272. SetNumberOfActiveInputStreams(1);
  273. SetMicrophoneMuteSwitchState(/*muted=*/true);
  274. // Notification should be shown, but not as a popup.
  275. EXPECT_TRUE(GetNotification());
  276. EXPECT_FALSE(GetPopupNotification());
  277. // Add another audio input stream, and verify the notification popup shows.
  278. LaunchApp(u"junior1");
  279. SetNumberOfActiveInputStreams(2);
  280. EXPECT_TRUE(GetNotification());
  281. EXPECT_TRUE(GetPopupNotification());
  282. // Mark notification as read, and then remove an audio input stream.
  283. MarkPopupAsShown();
  284. ASSERT_FALSE(GetPopupNotification());
  285. SetNumberOfActiveInputStreams(1);
  286. // Verify that notification popup is not reshown.
  287. EXPECT_TRUE(GetNotification());
  288. EXPECT_FALSE(GetPopupNotification());
  289. // Adding another stream shows a popup again.
  290. LaunchApp(u"rose");
  291. SetNumberOfActiveInputStreams(2);
  292. EXPECT_TRUE(GetNotification());
  293. EXPECT_TRUE(GetPopupNotification());
  294. }
  295. } // namespace ash