Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/views/toolbar/reload_button.h" | |
| 6 | |
| 5 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 6 #include "chrome/browser/ui/views/toolbar/reload_button.h" | |
| 7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 8 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/events/event_utils.h" | 11 #include "ui/events/event_utils.h" |
| 12 #include "ui/views/test/test_views_delegate.h" | |
| 11 | 13 |
| 12 class ReloadButtonTest : public ChromeRenderViewHostTestHarness { | 14 class ReloadButtonTest : public ChromeRenderViewHostTestHarness { |
| 13 public: | 15 public: |
| 14 ReloadButtonTest(); | 16 ReloadButtonTest(); |
| 15 | 17 |
| 16 void CheckState(bool enabled, | 18 void CheckState(bool enabled, |
| 17 ReloadButton::Mode intended_mode, | 19 ReloadButton::Mode intended_mode, |
| 18 ReloadButton::Mode visible_mode, | 20 ReloadButton::Mode visible_mode, |
| 19 bool double_click_timer_running, | 21 bool double_click_timer_running, |
| 20 bool stop_to_reload_timer_running); | 22 bool stop_to_reload_timer_running); |
| 21 | 23 |
| 22 // These accessors eliminate the need to declare each testcase as a friend. | 24 // These accessors eliminate the need to declare each testcase as a friend. |
| 23 void set_mouse_hovered(bool hovered) { | 25 void set_mouse_hovered(bool hovered) { |
| 24 reload_.testing_mouse_hovered_ = hovered; | 26 reload_.testing_mouse_hovered_ = hovered; |
| 25 } | 27 } |
| 26 int reload_count() { return reload_.testing_reload_count_; } | 28 int reload_count() { return reload_.testing_reload_count_; } |
| 27 | 29 |
| 28 protected: | 30 protected: |
| 31 ReloadButton& reload() { return reload_; } | |
|
Peter Kasting
2017/03/03 00:05:55
Nit: I suggest returning a pointer rather than a n
| |
| 32 | |
| 33 private: | |
| 34 views::TestViewsDelegate views_delegate_; | |
| 29 ReloadButton reload_; | 35 ReloadButton reload_; |
| 36 | |
| 37 DISALLOW_COPY_AND_ASSIGN(ReloadButtonTest); | |
| 30 }; | 38 }; |
| 31 | 39 |
| 32 ReloadButtonTest::ReloadButtonTest() : reload_(profile(), nullptr) { | 40 ReloadButtonTest::ReloadButtonTest() : reload_(profile(), nullptr) { |
| 33 // Set the timer delays to 0 so that timers will fire as soon as we tell the | 41 // Set the timer delays to 0 so that timers will fire as soon as we tell the |
| 34 // message loop to run pending tasks. | 42 // message loop to run pending tasks. |
| 35 reload_.double_click_timer_delay_ = base::TimeDelta(); | 43 reload_.double_click_timer_delay_ = base::TimeDelta(); |
| 36 reload_.stop_to_reload_timer_delay_ = base::TimeDelta(); | 44 reload_.stop_to_reload_timer_delay_ = base::TimeDelta(); |
| 37 } | 45 } |
| 38 | 46 |
| 39 void ReloadButtonTest::CheckState(bool enabled, | 47 void ReloadButtonTest::CheckState(bool enabled, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 52 | 60 |
| 53 TEST_F(ReloadButtonTest, Basic) { | 61 TEST_F(ReloadButtonTest, Basic) { |
| 54 // The stop/reload button starts in the "enabled reload" state with no timers | 62 // The stop/reload button starts in the "enabled reload" state with no timers |
| 55 // running. | 63 // running. |
| 56 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, | 64 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, |
| 57 false); | 65 false); |
| 58 | 66 |
| 59 // Press the button. This should start the double-click timer. | 67 // Press the button. This should start the double-click timer. |
| 60 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 68 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 61 ui::EventTimeForNow(), 0, 0); | 69 ui::EventTimeForNow(), 0, 0); |
| 62 reload_.ButtonPressed(&reload_, e); | 70 reload().ButtonPressed(&reload(), e); |
| 63 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true, | 71 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true, |
| 64 false); | 72 false); |
| 65 | 73 |
| 66 // Now change the mode (as if the browser had started loading the page). This | 74 // Now change the mode (as if the browser had started loading the page). This |
| 67 // should cancel the double-click timer since the button is not hovered. | 75 // should cancel the double-click timer since the button is not hovered. |
| 68 reload_.ChangeMode(ReloadButton::MODE_STOP, false); | 76 reload().ChangeMode(ReloadButton::MODE_STOP, false); |
| 69 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false, | 77 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false, |
| 70 false); | 78 false); |
| 71 | 79 |
| 72 // Press the button again. This should change back to reload. | 80 // Press the button again. This should change back to reload. |
| 73 reload_.ButtonPressed(&reload_, e); | 81 reload().ButtonPressed(&reload(), e); |
| 74 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, | 82 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, |
| 75 false); | 83 false); |
| 76 } | 84 } |
| 77 | 85 |
| 78 TEST_F(ReloadButtonTest, DoubleClickTimer) { | 86 TEST_F(ReloadButtonTest, DoubleClickTimer) { |
| 79 // Start by pressing the button. | 87 // Start by pressing the button. |
| 80 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 88 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 81 ui::EventTimeForNow(), 0, 0); | 89 ui::EventTimeForNow(), 0, 0); |
| 82 reload_.ButtonPressed(&reload_, e); | 90 reload().ButtonPressed(&reload(), e); |
| 83 | 91 |
| 84 // Try to press the button again. This should do nothing because the timer is | 92 // Try to press the button again. This should do nothing because the timer is |
| 85 // running. | 93 // running. |
| 86 int original_reload_count = reload_count(); | 94 int original_reload_count = reload_count(); |
| 87 reload_.ButtonPressed(&reload_, e); | 95 reload().ButtonPressed(&reload(), e); |
| 88 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true, | 96 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true, |
| 89 false); | 97 false); |
| 90 EXPECT_EQ(original_reload_count, reload_count()); | 98 EXPECT_EQ(original_reload_count, reload_count()); |
| 91 | 99 |
| 92 // Hover the button, and change mode. The visible mode should not change, | 100 // Hover the button, and change mode. The visible mode should not change, |
| 93 // again because the timer is running. | 101 // again because the timer is running. |
| 94 set_mouse_hovered(true); | 102 set_mouse_hovered(true); |
| 95 reload_.ChangeMode(ReloadButton::MODE_STOP, false); | 103 reload().ChangeMode(ReloadButton::MODE_STOP, false); |
| 96 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_RELOAD, true, | 104 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_RELOAD, true, |
| 97 false); | 105 false); |
| 98 | 106 |
| 99 // Now fire the timer. This should complete the mode change. | 107 // Now fire the timer. This should complete the mode change. |
| 100 base::RunLoop().RunUntilIdle(); | 108 base::RunLoop().RunUntilIdle(); |
| 101 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false, | 109 CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false, |
| 102 false); | 110 false); |
| 103 } | 111 } |
| 104 | 112 |
| 105 TEST_F(ReloadButtonTest, DisableOnHover) { | 113 TEST_F(ReloadButtonTest, DisableOnHover) { |
| 106 // Change to stop and hover. | 114 // Change to stop and hover. |
| 107 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 115 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 108 ui::EventTimeForNow(), 0, 0); | 116 ui::EventTimeForNow(), 0, 0); |
| 109 reload_.ButtonPressed(&reload_, e); | 117 reload().ButtonPressed(&reload(), e); |
| 110 reload_.ChangeMode(ReloadButton::MODE_STOP, false); | 118 reload().ChangeMode(ReloadButton::MODE_STOP, false); |
| 111 set_mouse_hovered(true); | 119 set_mouse_hovered(true); |
| 112 | 120 |
| 113 // Now change back to reload. This should result in a disabled stop button | 121 // Now change back to reload. This should result in a disabled stop button |
| 114 // due to the hover. | 122 // due to the hover. |
| 115 reload_.ChangeMode(ReloadButton::MODE_RELOAD, false); | 123 reload().ChangeMode(ReloadButton::MODE_RELOAD, false); |
| 116 CheckState(false, ReloadButton::MODE_RELOAD, ReloadButton::MODE_STOP, false, | 124 CheckState(false, ReloadButton::MODE_RELOAD, ReloadButton::MODE_STOP, false, |
| 117 true); | 125 true); |
| 118 | 126 |
| 119 // Un-hover the button, which should allow it to reset. | 127 // Un-hover the button, which should allow it to reset. |
| 120 set_mouse_hovered(false); | 128 set_mouse_hovered(false); |
| 121 ui::MouseEvent e2(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), | 129 ui::MouseEvent e2(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), |
| 122 ui::EventTimeForNow(), 0, 0); | 130 ui::EventTimeForNow(), 0, 0); |
| 123 reload_.OnMouseExited(e2); | 131 reload().OnMouseExited(e2); |
| 124 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, | 132 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, |
| 125 false); | 133 false); |
| 126 } | 134 } |
| 127 | 135 |
| 128 TEST_F(ReloadButtonTest, ResetOnClick) { | 136 TEST_F(ReloadButtonTest, ResetOnClick) { |
| 129 // Change to stop and hover. | 137 // Change to stop and hover. |
| 130 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 138 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 131 ui::EventTimeForNow(), 0, 0); | 139 ui::EventTimeForNow(), 0, 0); |
| 132 reload_.ButtonPressed(&reload_, e); | 140 reload().ButtonPressed(&reload(), e); |
| 133 reload_.ChangeMode(ReloadButton::MODE_STOP, false); | 141 reload().ChangeMode(ReloadButton::MODE_STOP, false); |
| 134 set_mouse_hovered(true); | 142 set_mouse_hovered(true); |
| 135 | 143 |
| 136 // Press the button. This should change back to reload despite the hover, | 144 // Press the button. This should change back to reload despite the hover, |
| 137 // because it's a direct user action. | 145 // because it's a direct user action. |
| 138 reload_.ButtonPressed(&reload_, e); | 146 reload().ButtonPressed(&reload(), e); |
| 139 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, | 147 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, |
| 140 false); | 148 false); |
| 141 } | 149 } |
| 142 | 150 |
| 143 TEST_F(ReloadButtonTest, ResetOnTimer) { | 151 TEST_F(ReloadButtonTest, ResetOnTimer) { |
| 144 // Change to stop, hover, and change back to reload. | 152 // Change to stop, hover, and change back to reload. |
| 145 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 153 ui::MouseEvent e(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 146 ui::EventTimeForNow(), 0, 0); | 154 ui::EventTimeForNow(), 0, 0); |
| 147 reload_.ButtonPressed(&reload_, e); | 155 reload().ButtonPressed(&reload(), e); |
| 148 reload_.ChangeMode(ReloadButton::MODE_STOP, false); | 156 reload().ChangeMode(ReloadButton::MODE_STOP, false); |
| 149 set_mouse_hovered(true); | 157 set_mouse_hovered(true); |
| 150 reload_.ChangeMode(ReloadButton::MODE_RELOAD, false); | 158 reload().ChangeMode(ReloadButton::MODE_RELOAD, false); |
| 151 | 159 |
| 152 // Now fire the stop-to-reload timer. This should reset the button. | 160 // Now fire the stop-to-reload timer. This should reset the button. |
| 153 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 154 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, | 162 CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false, |
| 155 false); | 163 false); |
| 156 } | 164 } |
| OLD | NEW |