autozoom_toast_controller_unittest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2022 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 "ash/system/camera/autozoom_toast_controller.h"
  5. #include "ash/system/unified/unified_system_tray.h"
  6. #include "ash/test/ash_test_base.h"
  7. namespace ash {
  8. class TestDelegate : public AutozoomToastController::Delegate {
  9. public:
  10. void AddAutozoomObserver(AutozoomObserver* observer) override {
  11. ASSERT_EQ(autozoom_observer, nullptr);
  12. autozoom_observer = observer;
  13. }
  14. void RemoveAutozoomObserver(AutozoomObserver* observer) override {
  15. ASSERT_EQ(autozoom_observer, observer);
  16. autozoom_observer = nullptr;
  17. }
  18. bool IsAutozoomEnabled() override { return autozoom_enabled_; }
  19. bool IsAutozoomControlEnabled() override { return autozoom_control_enabled_; }
  20. void SetAutozoomEnabled(bool autozoom_enabled) {
  21. autozoom_enabled_ = autozoom_enabled;
  22. if (autozoom_observer != nullptr) {
  23. autozoom_observer->OnAutozoomStateChanged(
  24. autozoom_enabled_ ? cros::mojom::CameraAutoFramingState::ON_SINGLE
  25. : cros::mojom::CameraAutoFramingState::OFF);
  26. }
  27. }
  28. void SetAutozoomControlEnabled(bool autozoom_control_enabled) {
  29. autozoom_control_enabled_ = autozoom_control_enabled;
  30. if (autozoom_observer != nullptr) {
  31. autozoom_observer->OnAutozoomControlEnabledChanged(
  32. autozoom_control_enabled_);
  33. }
  34. }
  35. AutozoomObserver* autozoom_observer = nullptr;
  36. private:
  37. bool autozoom_enabled_ = false;
  38. bool autozoom_control_enabled_ = false;
  39. };
  40. class AutozoomToastControllerTest : public AshTestBase {
  41. public:
  42. AutozoomToastControllerTest() = default;
  43. ~AutozoomToastControllerTest() override = default;
  44. AutozoomToastControllerTest(const AutozoomToastControllerTest&) = delete;
  45. AutozoomToastControllerTest& operator=(const AutozoomToastControllerTest&) =
  46. delete;
  47. // AshTestBase:
  48. void SetUp() override {
  49. AshTestBase::SetUp();
  50. auto delegate = std::make_unique<TestDelegate>();
  51. delegate_ = delegate.get();
  52. controller_ = std::make_unique<AutozoomToastController>(
  53. GetPrimaryUnifiedSystemTray(), std::move(delegate));
  54. }
  55. void TearDown() override {
  56. controller_ = nullptr;
  57. delegate_ = nullptr;
  58. AshTestBase::TearDown();
  59. }
  60. views::Widget* bubble_widget() {
  61. return controller_->bubble_widget_for_test();
  62. }
  63. std::unique_ptr<AutozoomToastController> controller_;
  64. TestDelegate* delegate_;
  65. };
  66. TEST_F(AutozoomToastControllerTest, ShowToastWhenCameraActive) {
  67. EXPECT_EQ(bubble_widget(), nullptr);
  68. // No toast when enabling camera when autozoom is disabled.
  69. delegate_->SetAutozoomControlEnabled(true);
  70. EXPECT_EQ(bubble_widget(), nullptr);
  71. // No toast when enabling autozoom when camera is already active.
  72. delegate_->SetAutozoomEnabled(true);
  73. EXPECT_EQ(bubble_widget(), nullptr);
  74. // Toast is shown when autozoom is enabled when camera become active.
  75. delegate_->SetAutozoomControlEnabled(false);
  76. delegate_->SetAutozoomControlEnabled(true);
  77. ASSERT_NE(bubble_widget(), nullptr);
  78. EXPECT_TRUE(bubble_widget()->IsVisible());
  79. }
  80. } // namespace ash