ink_drop_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2015 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 "base/test/test_mock_time_task_runner.h"
  6. #include "base/threading/thread_task_runner_handle.h"
  7. #include "base/timer/timer.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  10. #include "ui/views/animation/ink_drop.h"
  11. #include "ui/views/animation/ink_drop_host_view.h"
  12. #include "ui/views/animation/ink_drop_impl.h"
  13. #include "ui/views/animation/ink_drop_state.h"
  14. #include "ui/views/animation/ink_drop_stub.h"
  15. #include "ui/views/animation/test/test_ink_drop_host.h"
  16. namespace views {
  17. namespace test {
  18. // Enumeration of all the different InkDrop types.
  19. enum InkDropType { INK_DROP_STUB, INK_DROP_IMPL };
  20. class InkDropTest : public testing::TestWithParam<testing::tuple<InkDropType>> {
  21. public:
  22. InkDropTest();
  23. InkDropTest(const InkDropTest&) = delete;
  24. InkDropTest& operator=(const InkDropTest&) = delete;
  25. ~InkDropTest() override;
  26. protected:
  27. // A dummy InkDropHost required to create an InkDrop.
  28. TestInkDropHost test_ink_drop_host_;
  29. // The InkDrop returned by the InkDropFactory test target.
  30. std::unique_ptr<InkDrop> ink_drop_;
  31. private:
  32. // Extracts and returns the InkDropType from the test parameters.
  33. InkDropType GetInkDropType() const;
  34. std::unique_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
  35. // Required by base::Timer's.
  36. std::unique_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
  37. };
  38. InkDropTest::InkDropTest() : ink_drop_(nullptr) {
  39. zero_duration_mode_ = std::make_unique<ui::ScopedAnimationDurationScaleMode>(
  40. ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
  41. switch (GetInkDropType()) {
  42. case INK_DROP_STUB:
  43. ink_drop_ = std::make_unique<InkDropStub>();
  44. break;
  45. case INK_DROP_IMPL:
  46. ink_drop_ = std::make_unique<InkDropImpl>(
  47. InkDrop::Get(&test_ink_drop_host_), gfx::Size(),
  48. InkDropImpl::AutoHighlightMode::NONE);
  49. // The Timer's used by the InkDropImpl class require a
  50. // base::ThreadTaskRunnerHandle instance.
  51. scoped_refptr<base::TestMockTimeTaskRunner> task_runner(
  52. new base::TestMockTimeTaskRunner);
  53. thread_task_runner_handle_ =
  54. std::make_unique<base::ThreadTaskRunnerHandle>(task_runner);
  55. break;
  56. }
  57. }
  58. InkDropTest::~InkDropTest() = default;
  59. InkDropType InkDropTest::GetInkDropType() const {
  60. return testing::get<0>(GetParam());
  61. }
  62. // Note: First argument is optional and intentionally left blank.
  63. // (it's a prefix for the generated test cases)
  64. INSTANTIATE_TEST_SUITE_P(All,
  65. InkDropTest,
  66. testing::Values(INK_DROP_STUB, INK_DROP_IMPL));
  67. TEST_P(InkDropTest,
  68. VerifyInkDropLayersRemovedAfterDestructionWhenRippleIsActive) {
  69. ink_drop_->AnimateToState(InkDropState::ACTION_PENDING);
  70. ink_drop_.reset();
  71. EXPECT_EQ(0, test_ink_drop_host_.num_ink_drop_layers());
  72. }
  73. TEST_P(InkDropTest, StateIsHiddenInitially) {
  74. EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState());
  75. }
  76. TEST_P(InkDropTest, TypicalQuickAction) {
  77. ink_drop_->AnimateToState(InkDropState::ACTION_PENDING);
  78. ink_drop_->AnimateToState(InkDropState::ACTION_TRIGGERED);
  79. EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState());
  80. }
  81. TEST_P(InkDropTest, CancelQuickAction) {
  82. ink_drop_->AnimateToState(InkDropState::ACTION_PENDING);
  83. ink_drop_->AnimateToState(InkDropState::HIDDEN);
  84. EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState());
  85. }
  86. TEST_P(InkDropTest, TypicalSlowAction) {
  87. ink_drop_->AnimateToState(InkDropState::ACTION_PENDING);
  88. ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING);
  89. ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_TRIGGERED);
  90. EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState());
  91. }
  92. TEST_P(InkDropTest, CancelSlowAction) {
  93. ink_drop_->AnimateToState(InkDropState::ACTION_PENDING);
  94. ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING);
  95. ink_drop_->AnimateToState(InkDropState::HIDDEN);
  96. EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState());
  97. }
  98. TEST_P(InkDropTest, TypicalQuickActivated) {
  99. ink_drop_->AnimateToState(InkDropState::ACTION_PENDING);
  100. ink_drop_->AnimateToState(InkDropState::ACTIVATED);
  101. ink_drop_->AnimateToState(InkDropState::DEACTIVATED);
  102. EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState());
  103. }
  104. TEST_P(InkDropTest, TypicalSlowActivated) {
  105. ink_drop_->AnimateToState(InkDropState::ACTION_PENDING);
  106. ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING);
  107. ink_drop_->AnimateToState(InkDropState::ACTIVATED);
  108. ink_drop_->AnimateToState(InkDropState::DEACTIVATED);
  109. EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState());
  110. }
  111. } // namespace test
  112. } // namespace views