dictation_nudge_controller_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <string>
  5. #include "ash/accessibility/accessibility_controller_impl.h"
  6. #include "ash/accessibility/dictation_nudge.h"
  7. #include "ash/accessibility/dictation_nudge_controller.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/tray/system_nudge_label.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/run_loop.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #include "ui/compositor/layer.h"
  15. #include "ui/compositor/layer_animator.h"
  16. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  17. #include "ui/views/test/widget_test.h"
  18. #include "ui/views/widget/widget.h"
  19. #include "ui/views/widget/widget_observer.h"
  20. using ::testing::HasSubstr;
  21. namespace ash {
  22. // Tests for showing the Dictation Nudge from AccessibilityControllerImpl.
  23. class DictationNudgeControllerTest : public AshTestBase {
  24. public:
  25. DictationNudgeControllerTest() = default;
  26. DictationNudgeControllerTest(const DictationNudgeControllerTest&) = delete;
  27. DictationNudgeControllerTest& operator=(const DictationNudgeControllerTest&) =
  28. delete;
  29. ~DictationNudgeControllerTest() override = default;
  30. // AshTestBase:
  31. void SetUp() override {
  32. AshTestBase::SetUp();
  33. Shell::Get()->accessibility_controller()->dictation().SetEnabled(true);
  34. }
  35. void ShowDictationLanguageUpgradedNudge(std::string dictation_locale,
  36. std::string application_locale) {
  37. Shell::Get()
  38. ->accessibility_controller()
  39. ->ShowDictationLanguageUpgradedNudge(dictation_locale,
  40. application_locale);
  41. }
  42. DictationNudgeController* GetDictationNudgeController() {
  43. return Shell::Get()
  44. ->accessibility_controller()
  45. ->GetDictationNudgeControllerForTest();
  46. }
  47. std::unique_ptr<SystemNudgeLabel> GetDictationNudgeLabel(
  48. DictationNudge* nudge) {
  49. return nudge->CreateLabelView();
  50. }
  51. void WaitForWidgetDestruction(DictationNudgeController* controller,
  52. SystemNudge* nudge) {
  53. views::Widget* nudge_widget = nudge->widget();
  54. ASSERT_TRUE(nudge_widget);
  55. EXPECT_FALSE(nudge_widget->IsClosed());
  56. // Slow down the duration of the nudge.
  57. ui::ScopedAnimationDurationScaleMode test_duration_mode(
  58. ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
  59. // Pretend the hide nudge timer has elapsed.
  60. views::test::WidgetDestroyedWaiter widget_destroyed_waiter(nudge_widget);
  61. controller->FireHideNudgeTimerForTesting();
  62. EXPECT_TRUE(nudge_widget->GetLayer()->GetAnimator()->is_animating());
  63. widget_destroyed_waiter.Wait();
  64. }
  65. };
  66. TEST_F(DictationNudgeControllerTest, ShowsAndHidesNudge) {
  67. EXPECT_FALSE(GetDictationNudgeController());
  68. ShowDictationLanguageUpgradedNudge("en-US", "en-US");
  69. DictationNudgeController* controller = GetDictationNudgeController();
  70. ASSERT_TRUE(controller);
  71. SystemNudge* nudge = controller->GetSystemNudgeForTesting();
  72. ASSERT_TRUE(nudge);
  73. WaitForWidgetDestruction(controller, nudge);
  74. }
  75. TEST_F(DictationNudgeControllerTest, SetsLabelBasedOnApplicationLocale) {
  76. struct {
  77. std::string locale;
  78. std::string application_locale;
  79. std::string label;
  80. } kTestCases[] = {
  81. {"en-US", "en-US", "English"},
  82. {"es-ES", "en-US", "Spanish"},
  83. {"en-US", "es-ES", "inglés"},
  84. {"es-ES", "es-ES", "español"},
  85. };
  86. for (const auto& testcase : kTestCases) {
  87. ShowDictationLanguageUpgradedNudge(testcase.locale,
  88. testcase.application_locale);
  89. DictationNudgeController* controller = GetDictationNudgeController();
  90. ASSERT_TRUE(controller);
  91. DictationNudge* nudge =
  92. static_cast<DictationNudge*>(controller->GetSystemNudgeForTesting());
  93. ASSERT_TRUE(nudge);
  94. std::unique_ptr<SystemNudgeLabel> label = GetDictationNudgeLabel(nudge);
  95. std::string text = base::UTF16ToUTF8(label->GetText());
  96. EXPECT_THAT(text, HasSubstr(testcase.label));
  97. WaitForWidgetDestruction(controller, nudge);
  98. }
  99. }
  100. } // namespace ash