media_notification_volume_slider_view_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "components/media_message_center/media_notification_volume_slider_view.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include "testing/gmock/include/gmock/gmock.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "ui/events/base_event_utils.h"
  9. #include "ui/views/test/views_test_base.h"
  10. namespace media_message_center {
  11. namespace {
  12. constexpr gfx::Size kVolumeSliderSize = {50, 20};
  13. constexpr float kScrollVolumeDelta = 0.1f;
  14. constexpr float kKeyVolumeDelta = 0.05f;
  15. } // namespace
  16. class MediaNotificationVolumeSliderViewTest : public views::ViewsTestBase {
  17. public:
  18. void SetUp() override {
  19. views::ViewsTestBase::SetUp();
  20. widget_ = CreateTestWidget();
  21. volume_slider_ = widget_->SetContentsView(
  22. std::make_unique<MediaNotificationVolumeSliderView>(base::BindRepeating(
  23. &MediaNotificationVolumeSliderViewTest::SetVolume,
  24. base::Unretained(this))));
  25. widget_->SetBounds(gfx::Rect(kVolumeSliderSize));
  26. widget_->Show();
  27. }
  28. void TearDown() override {
  29. widget_.reset();
  30. views::ViewsTestBase::TearDown();
  31. }
  32. void SimulateMousePressed(const gfx::Point& point) {
  33. volume_slider_->OnMousePressed(ui::MouseEvent(
  34. ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(), 0, 0));
  35. }
  36. void SimulateMouseWheelEvent(const gfx::Vector2d& vector) {
  37. volume_slider_->OnMouseWheel(ui::MouseWheelEvent(
  38. vector, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), 0, 0));
  39. }
  40. void SimulateKeyEvent(const ui::KeyboardCode key_code) {
  41. volume_slider_->OnKeyPressed(ui::KeyEvent(ui::ET_KEY_PRESSED, key_code, 0));
  42. }
  43. MediaNotificationVolumeSliderView* volume_slider() { return volume_slider_; }
  44. MOCK_METHOD1(SetVolume, void(float));
  45. private:
  46. std::unique_ptr<views::Widget> widget_;
  47. raw_ptr<MediaNotificationVolumeSliderView> volume_slider_;
  48. };
  49. TEST_F(MediaNotificationVolumeSliderViewTest, SetVolume) {
  50. EXPECT_CALL(*this, SetVolume(0.0f));
  51. SimulateMousePressed(gfx::Point(0, 0));
  52. EXPECT_CALL(*this, SetVolume(0.5f));
  53. SimulateMousePressed(gfx::Point(kVolumeSliderSize.width() / 2, 0));
  54. EXPECT_CALL(*this, SetVolume(1.0f));
  55. SimulateMousePressed(gfx::Point(kVolumeSliderSize.width(), 0));
  56. }
  57. TEST_F(MediaNotificationVolumeSliderViewTest, UpdateVolumeWithMouseWheel) {
  58. const float volume = 0.8f;
  59. volume_slider()->SetVolume(volume);
  60. EXPECT_CALL(*this, SetVolume(volume + kScrollVolumeDelta));
  61. SimulateMouseWheelEvent(gfx::Vector2d(0, 1));
  62. EXPECT_CALL(*this, SetVolume(volume - kScrollVolumeDelta));
  63. SimulateMouseWheelEvent(gfx::Vector2d(0, -1));
  64. // Test that volume set should be in between 0 and 1.
  65. volume_slider()->SetVolume(0.95f);
  66. EXPECT_CALL(*this, SetVolume(1.0f));
  67. SimulateMouseWheelEvent(gfx::Vector2d(0, 1));
  68. volume_slider()->SetVolume(0.05f);
  69. EXPECT_CALL(*this, SetVolume(0.0f));
  70. SimulateMouseWheelEvent(gfx::Vector2d(0, -1));
  71. }
  72. TEST_F(MediaNotificationVolumeSliderViewTest, UpdateVolumeWithKey) {
  73. const float volume = 0.5;
  74. volume_slider()->SetVolume(volume);
  75. EXPECT_CALL(*this, SetVolume(volume + kKeyVolumeDelta)).Times(2);
  76. SimulateKeyEvent(ui::KeyboardCode::VKEY_UP);
  77. SimulateKeyEvent(ui::KeyboardCode::VKEY_RIGHT);
  78. EXPECT_CALL(*this, SetVolume(volume - kKeyVolumeDelta)).Times(2);
  79. SimulateKeyEvent(ui::KeyboardCode::VKEY_DOWN);
  80. SimulateKeyEvent(ui::KeyboardCode::VKEY_LEFT);
  81. }
  82. } // namespace media_message_center