| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/animation/ink_drop_factory.h" | |
| 6 | |
| 7 #include <memory> | 5 #include <memory> |
| 8 | 6 |
| 9 #include "base/macros.h" | 7 #include "base/macros.h" |
| 10 #include "base/test/test_mock_time_task_runner.h" | 8 #include "base/test/test_mock_time_task_runner.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/base/material_design/material_design_controller.h" | |
| 15 #include "ui/base/test/material_design_controller_test_api.h" | 12 #include "ui/base/test/material_design_controller_test_api.h" |
| 16 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 13 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
| 17 #include "ui/views/animation/ink_drop.h" | 14 #include "ui/views/animation/ink_drop.h" |
| 18 #include "ui/views/animation/ink_drop_host.h" | 15 #include "ui/views/animation/ink_drop_host.h" |
| 19 #include "ui/views/animation/ink_drop_impl.h" | 16 #include "ui/views/animation/ink_drop_impl.h" |
| 20 #include "ui/views/animation/ink_drop_state.h" | 17 #include "ui/views/animation/ink_drop_state.h" |
| 18 #include "ui/views/animation/ink_drop_stub.h" |
| 21 #include "ui/views/animation/test/test_ink_drop_host.h" | 19 #include "ui/views/animation/test/test_ink_drop_host.h" |
| 22 | 20 |
| 23 namespace views { | 21 namespace views { |
| 22 namespace test { |
| 24 | 23 |
| 25 class InkDropFactoryTest | 24 // Enumeration of all the different InkDrop types. |
| 26 : public testing::TestWithParam< | 25 enum InkDropType { INK_DROP_STUB, INK_DROP_IMPL }; |
| 27 testing::tuple<ui::MaterialDesignController::Mode>> { | 26 |
| 27 class InkDropTest : public testing::TestWithParam<testing::tuple<InkDropType>> { |
| 28 public: | 28 public: |
| 29 InkDropFactoryTest(); | 29 InkDropTest(); |
| 30 ~InkDropFactoryTest(); | 30 ~InkDropTest(); |
| 31 | 31 |
| 32 protected: | 32 protected: |
| 33 // A dummy InkDropHost required to create an InkDrop. | 33 // A dummy InkDropHost required to create an InkDrop. |
| 34 TestInkDropHost test_ink_drop_host_; | 34 TestInkDropHost test_ink_drop_host_; |
| 35 | 35 |
| 36 // The InkDrop returned by the InkDropFactory test target. | 36 // The InkDrop returned by the InkDropFactory test target. |
| 37 std::unique_ptr<InkDrop> ink_drop_; | 37 std::unique_ptr<InkDrop> ink_drop_; |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 // Extracts and returns the material design mode from the test parameters. | 40 // Extracts and returns the InkDropType from the test parameters. |
| 41 ui::MaterialDesignController::Mode GetMaterialMode() const; | 41 InkDropType GetInkDropType() const; |
| 42 | 42 |
| 43 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_; | 43 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_; |
| 44 | 44 |
| 45 // Required by base::Timer's. | 45 // Required by base::Timer's. |
| 46 std::unique_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; | 46 std::unique_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 47 | 47 |
| 48 std::unique_ptr<ui::test::MaterialDesignControllerTestAPI> | 48 DISALLOW_COPY_AND_ASSIGN(InkDropTest); |
| 49 material_design_state_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(InkDropFactoryTest); | |
| 52 }; | 49 }; |
| 53 | 50 |
| 54 InkDropFactoryTest::InkDropFactoryTest() : ink_drop_(nullptr) { | 51 InkDropTest::InkDropTest() : ink_drop_(nullptr) { |
| 55 // Any call by a previous test to MaterialDesignController::GetMode() will | |
| 56 // initialize and cache the mode. This ensures that these tests will run from | |
| 57 // a non-initialized state. | |
| 58 material_design_state_.reset( | |
| 59 new ui::test::MaterialDesignControllerTestAPI(GetMaterialMode())); | |
| 60 ink_drop_.reset( | |
| 61 InkDropFactory::CreateInkDrop(&test_ink_drop_host_).release()); | |
| 62 | |
| 63 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( | 52 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( |
| 64 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); | 53 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); |
| 65 | 54 |
| 66 switch (GetMaterialMode()) { | 55 switch (InkDropType()) { |
| 67 case ui::MaterialDesignController::NON_MATERIAL: | 56 case INK_DROP_STUB: |
| 57 ink_drop_.reset(new InkDropStub()); |
| 68 break; | 58 break; |
| 69 case ui::MaterialDesignController::MATERIAL_NORMAL: | 59 case INK_DROP_IMPL: |
| 70 case ui::MaterialDesignController::MATERIAL_HYBRID: | 60 ink_drop_.reset(new InkDropImpl(&test_ink_drop_host_)); |
| 71 // The Timer's used by the InkDropImpl class require a | 61 // The Timer's used by the InkDropImpl class require a |
| 72 // base::ThreadTaskRunnerHandle instance. | 62 // base::ThreadTaskRunnerHandle instance. |
| 73 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( | 63 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( |
| 74 new base::TestMockTimeTaskRunner); | 64 new base::TestMockTimeTaskRunner); |
| 75 thread_task_runner_handle_.reset( | 65 thread_task_runner_handle_.reset( |
| 76 new base::ThreadTaskRunnerHandle(task_runner)); | 66 new base::ThreadTaskRunnerHandle(task_runner)); |
| 77 break; | 67 break; |
| 78 } | 68 } |
| 79 } | 69 } |
| 80 | 70 |
| 81 InkDropFactoryTest::~InkDropFactoryTest() { | 71 InkDropTest::~InkDropTest() {} |
| 82 material_design_state_.reset(); | |
| 83 } | |
| 84 | 72 |
| 85 ui::MaterialDesignController::Mode InkDropFactoryTest::GetMaterialMode() const { | 73 InkDropType InkDropTest::GetInkDropType() const { |
| 86 return testing::get<0>(GetParam()); | 74 return testing::get<0>(GetParam()); |
| 87 } | 75 } |
| 88 | 76 |
| 89 // Note: First argument is optional and intentionally left blank. | 77 // Note: First argument is optional and intentionally left blank. |
| 90 // (it's a prefix for the generated test cases) | 78 // (it's a prefix for the generated test cases) |
| 91 INSTANTIATE_TEST_CASE_P( | 79 INSTANTIATE_TEST_CASE_P(, |
| 92 , | 80 InkDropTest, |
| 93 InkDropFactoryTest, | 81 testing::Values(INK_DROP_STUB, INK_DROP_IMPL)); |
| 94 testing::Values(ui::MaterialDesignController::NON_MATERIAL, | |
| 95 ui::MaterialDesignController::MATERIAL_NORMAL, | |
| 96 ui::MaterialDesignController::MATERIAL_HYBRID)); | |
| 97 | 82 |
| 98 TEST_P(InkDropFactoryTest, | 83 TEST_P(InkDropTest, |
| 99 VerifyInkDropLayersRemovedAfterDestructionWhenRippleIsActive) { | 84 VerifyInkDropLayersRemovedAfterDestructionWhenRippleIsActive) { |
| 100 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); | 85 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); |
| 101 ink_drop_.reset(); | 86 ink_drop_.reset(); |
| 102 EXPECT_EQ(0, test_ink_drop_host_.num_ink_drop_layers()); | 87 EXPECT_EQ(0, test_ink_drop_host_.num_ink_drop_layers()); |
| 103 } | 88 } |
| 104 | 89 |
| 105 TEST_P(InkDropFactoryTest, | 90 TEST_P(InkDropTest, |
| 106 VerifyInkDropLayersRemovedAfterDestructionWhenHoverIsActive) { | 91 VerifyInkDropLayersRemovedAfterDestructionWhenHoverIsActive) { |
| 107 test_ink_drop_host_.set_should_show_highlight(true); | 92 test_ink_drop_host_.set_should_show_highlight(true); |
| 108 ink_drop_->SetHovered(true); | 93 ink_drop_->SetHovered(true); |
| 109 ink_drop_.reset(); | 94 ink_drop_.reset(); |
| 110 EXPECT_EQ(0, test_ink_drop_host_.num_ink_drop_layers()); | 95 EXPECT_EQ(0, test_ink_drop_host_.num_ink_drop_layers()); |
| 111 } | 96 } |
| 112 | 97 |
| 113 TEST_P(InkDropFactoryTest, StateIsHiddenInitially) { | 98 TEST_P(InkDropTest, StateIsHiddenInitially) { |
| 114 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); | 99 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); |
| 115 } | 100 } |
| 116 | 101 |
| 117 TEST_P(InkDropFactoryTest, TypicalQuickAction) { | 102 TEST_P(InkDropTest, TypicalQuickAction) { |
| 118 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); | 103 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); |
| 119 ink_drop_->AnimateToState(InkDropState::ACTION_TRIGGERED); | 104 ink_drop_->AnimateToState(InkDropState::ACTION_TRIGGERED); |
| 120 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); | 105 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); |
| 121 } | 106 } |
| 122 | 107 |
| 123 TEST_P(InkDropFactoryTest, CancelQuickAction) { | 108 TEST_P(InkDropTest, CancelQuickAction) { |
| 124 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); | 109 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); |
| 125 ink_drop_->AnimateToState(InkDropState::HIDDEN); | 110 ink_drop_->AnimateToState(InkDropState::HIDDEN); |
| 126 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); | 111 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); |
| 127 } | 112 } |
| 128 | 113 |
| 129 TEST_P(InkDropFactoryTest, TypicalSlowAction) { | 114 TEST_P(InkDropTest, TypicalSlowAction) { |
| 130 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); | 115 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); |
| 131 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING); | 116 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING); |
| 132 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_TRIGGERED); | 117 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_TRIGGERED); |
| 133 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); | 118 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); |
| 134 } | 119 } |
| 135 | 120 |
| 136 TEST_P(InkDropFactoryTest, CancelSlowAction) { | 121 TEST_P(InkDropTest, CancelSlowAction) { |
| 137 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); | 122 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); |
| 138 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING); | 123 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING); |
| 139 ink_drop_->AnimateToState(InkDropState::HIDDEN); | 124 ink_drop_->AnimateToState(InkDropState::HIDDEN); |
| 140 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); | 125 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); |
| 141 } | 126 } |
| 142 | 127 |
| 143 TEST_P(InkDropFactoryTest, TypicalQuickActivated) { | 128 TEST_P(InkDropTest, TypicalQuickActivated) { |
| 144 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); | 129 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); |
| 145 ink_drop_->AnimateToState(InkDropState::ACTIVATED); | 130 ink_drop_->AnimateToState(InkDropState::ACTIVATED); |
| 146 ink_drop_->AnimateToState(InkDropState::DEACTIVATED); | 131 ink_drop_->AnimateToState(InkDropState::DEACTIVATED); |
| 147 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); | 132 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); |
| 148 } | 133 } |
| 149 | 134 |
| 150 TEST_P(InkDropFactoryTest, TypicalSlowActivated) { | 135 TEST_P(InkDropTest, TypicalSlowActivated) { |
| 151 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); | 136 ink_drop_->AnimateToState(InkDropState::ACTION_PENDING); |
| 152 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING); | 137 ink_drop_->AnimateToState(InkDropState::ALTERNATE_ACTION_PENDING); |
| 153 ink_drop_->AnimateToState(InkDropState::ACTIVATED); | 138 ink_drop_->AnimateToState(InkDropState::ACTIVATED); |
| 154 ink_drop_->AnimateToState(InkDropState::DEACTIVATED); | 139 ink_drop_->AnimateToState(InkDropState::DEACTIVATED); |
| 155 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); | 140 EXPECT_EQ(InkDropState::HIDDEN, ink_drop_->GetTargetInkDropState()); |
| 156 } | 141 } |
| 157 | 142 |
| 143 } // namespace test |
| 158 } // namespace views | 144 } // namespace views |
| OLD | NEW |