123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // Copyright 2021 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include <string>
- #include "ash/accessibility/accessibility_controller_impl.h"
- #include "ash/accessibility/dictation_nudge.h"
- #include "ash/accessibility/dictation_nudge_controller.h"
- #include "ash/shell.h"
- #include "ash/system/tray/system_nudge_label.h"
- #include "ash/test/ash_test_base.h"
- #include "base/run_loop.h"
- #include "base/strings/utf_string_conversions.h"
- #include "testing/gmock/include/gmock/gmock.h"
- #include "ui/compositor/layer.h"
- #include "ui/compositor/layer_animator.h"
- #include "ui/compositor/scoped_animation_duration_scale_mode.h"
- #include "ui/views/test/widget_test.h"
- #include "ui/views/widget/widget.h"
- #include "ui/views/widget/widget_observer.h"
- using ::testing::HasSubstr;
- namespace ash {
- // Tests for showing the Dictation Nudge from AccessibilityControllerImpl.
- class DictationNudgeControllerTest : public AshTestBase {
- public:
- DictationNudgeControllerTest() = default;
- DictationNudgeControllerTest(const DictationNudgeControllerTest&) = delete;
- DictationNudgeControllerTest& operator=(const DictationNudgeControllerTest&) =
- delete;
- ~DictationNudgeControllerTest() override = default;
- // AshTestBase:
- void SetUp() override {
- AshTestBase::SetUp();
- Shell::Get()->accessibility_controller()->dictation().SetEnabled(true);
- }
- void ShowDictationLanguageUpgradedNudge(std::string dictation_locale,
- std::string application_locale) {
- Shell::Get()
- ->accessibility_controller()
- ->ShowDictationLanguageUpgradedNudge(dictation_locale,
- application_locale);
- }
- DictationNudgeController* GetDictationNudgeController() {
- return Shell::Get()
- ->accessibility_controller()
- ->GetDictationNudgeControllerForTest();
- }
- std::unique_ptr<SystemNudgeLabel> GetDictationNudgeLabel(
- DictationNudge* nudge) {
- return nudge->CreateLabelView();
- }
- void WaitForWidgetDestruction(DictationNudgeController* controller,
- SystemNudge* nudge) {
- views::Widget* nudge_widget = nudge->widget();
- ASSERT_TRUE(nudge_widget);
- EXPECT_FALSE(nudge_widget->IsClosed());
- // Slow down the duration of the nudge.
- ui::ScopedAnimationDurationScaleMode test_duration_mode(
- ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
- // Pretend the hide nudge timer has elapsed.
- views::test::WidgetDestroyedWaiter widget_destroyed_waiter(nudge_widget);
- controller->FireHideNudgeTimerForTesting();
- EXPECT_TRUE(nudge_widget->GetLayer()->GetAnimator()->is_animating());
- widget_destroyed_waiter.Wait();
- }
- };
- TEST_F(DictationNudgeControllerTest, ShowsAndHidesNudge) {
- EXPECT_FALSE(GetDictationNudgeController());
- ShowDictationLanguageUpgradedNudge("en-US", "en-US");
- DictationNudgeController* controller = GetDictationNudgeController();
- ASSERT_TRUE(controller);
- SystemNudge* nudge = controller->GetSystemNudgeForTesting();
- ASSERT_TRUE(nudge);
- WaitForWidgetDestruction(controller, nudge);
- }
- TEST_F(DictationNudgeControllerTest, SetsLabelBasedOnApplicationLocale) {
- struct {
- std::string locale;
- std::string application_locale;
- std::string label;
- } kTestCases[] = {
- {"en-US", "en-US", "English"},
- {"es-ES", "en-US", "Spanish"},
- {"en-US", "es-ES", "inglés"},
- {"es-ES", "es-ES", "español"},
- };
- for (const auto& testcase : kTestCases) {
- ShowDictationLanguageUpgradedNudge(testcase.locale,
- testcase.application_locale);
- DictationNudgeController* controller = GetDictationNudgeController();
- ASSERT_TRUE(controller);
- DictationNudge* nudge =
- static_cast<DictationNudge*>(controller->GetSystemNudgeForTesting());
- ASSERT_TRUE(nudge);
- std::unique_ptr<SystemNudgeLabel> label = GetDictationNudgeLabel(nudge);
- std::string text = base::UTF16ToUTF8(label->GetText());
- EXPECT_THAT(text, HasSubstr(testcase.label));
- WaitForWidgetDestruction(controller, nudge);
- }
- }
- } // namespace ash
|