summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-26 20:42:37 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-26 20:42:37 +0000
commit88ebe22bc07a208734ac3c07e006f1c0c6acf110 (patch)
treef938251c3dd5518c97ac291d04ccdf7f83dca88c /chrome/browser
parent291104b09f6ab9f81708175376fd3c954a695753 (diff)
downloadchromium_src-88ebe22bc07a208734ac3c07e006f1c0c6acf110.zip
chromium_src-88ebe22bc07a208734ac3c07e006f1c0c6acf110.tar.gz
chromium_src-88ebe22bc07a208734ac3c07e006f1c0c6acf110.tar.bz2
Improve reload button unittesting. Instead of doing very low-level tests of individual function outputs, test higher-level sequences of actions.
This also adds a views unittest alongside the existing GTK one. BUG=none TEST=none Review URL: http://codereview.chromium.org/4114001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63941 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/gtk/reload_button_gtk.cc54
-rw-r--r--chrome/browser/gtk/reload_button_gtk.h51
-rw-r--r--chrome/browser/gtk/reload_button_gtk_unittest.cc194
-rw-r--r--chrome/browser/views/reload_button.cc23
-rw-r--r--chrome/browser/views/reload_button.h22
-rw-r--r--chrome/browser/views/reload_button_unittest.cc120
6 files changed, 300 insertions, 164 deletions
diff --git a/chrome/browser/gtk/reload_button_gtk.cc b/chrome/browser/gtk/reload_button_gtk.cc
index e5e8857..4dd278b 100644
--- a/chrome/browser/gtk/reload_button_gtk.cc
+++ b/chrome/browser/gtk/reload_button_gtk.cc
@@ -25,15 +25,15 @@ ReloadButtonGtk::ReloadButtonGtk(LocationBarViewGtk* location_bar,
Browser* browser)
: location_bar_(location_bar),
browser_(browser),
- button_delay_(0),
- pretend_timer_is_running_for_unittest_(false),
intended_mode_(MODE_RELOAD),
visible_mode_(MODE_RELOAD),
theme_provider_(browser ?
GtkThemeProvider::GetFrom(browser->profile()) : NULL),
reload_(theme_provider_, IDR_RELOAD, IDR_RELOAD_P, IDR_RELOAD_H, 0),
stop_(theme_provider_, IDR_STOP, IDR_STOP_P, IDR_STOP_H, IDR_STOP_D),
- widget_(gtk_chrome_button_new()) {
+ widget_(gtk_chrome_button_new()),
+ testing_mouse_hovered_(false),
+ testing_reload_count_(0) {
gtk_widget_set_size_request(widget(), reload_.Width(), reload_.Height());
gtk_widget_set_app_paintable(widget(), TRUE);
@@ -57,6 +57,13 @@ ReloadButtonGtk::ReloadButtonGtk(LocationBarViewGtk* location_bar,
NotificationType::BROWSER_THEME_CHANGED,
Source<GtkThemeProvider>(theme_provider_));
}
+
+ // Set the default timer delay to the system double-click time.
+ int timer_delay_ms;
+ GtkSettings* settings = gtk_settings_get_default();
+ g_object_get(G_OBJECT(settings), "gtk-double-click-time", &timer_delay_ms,
+ NULL);
+ timer_delay_ = base::TimeDelta::FromMilliseconds(timer_delay_ms);
}
ReloadButtonGtk::~ReloadButtonGtk() {
@@ -69,9 +76,9 @@ void ReloadButtonGtk::ChangeMode(Mode mode, bool force) {
// If the change is forced, or the user isn't hovering the icon, or it's safe
// to change it to the other image type, make the change immediately;
// otherwise we'll let it happen later.
- if (force || GTK_WIDGET_STATE(widget()) == GTK_STATE_NORMAL ||
- ((mode == MODE_STOP) ?
- !timer_running() : (visible_mode_ != MODE_STOP))) {
+ if (force || ((GTK_WIDGET_STATE(widget()) == GTK_STATE_NORMAL) &&
+ !testing_mouse_hovered_) || ((mode == MODE_STOP) ?
+ !timer_.IsRunning() : (visible_mode_ != MODE_STOP))) {
timer_.Stop();
visible_mode_ = mode;
@@ -104,7 +111,7 @@ void ReloadButtonGtk::ChangeMode(Mode mode, bool force) {
void ReloadButtonGtk::Observe(NotificationType type,
const NotificationSource& source,
- const NotificationDetails& details) {
+ const NotificationDetails& /* details */) {
DCHECK(NotificationType::BROWSER_THEME_CHANGED == type);
GtkThemeProvider* provider = static_cast<GtkThemeProvider*>(
@@ -118,10 +125,10 @@ void ReloadButtonGtk::OnButtonTimer() {
ChangeMode(intended_mode_, false);
}
-void ReloadButtonGtk::OnClicked(GtkWidget* sender) {
+void ReloadButtonGtk::OnClicked(GtkWidget* /* sender */) {
if (visible_mode_ == MODE_STOP) {
- // The stop button is disabled because the user hovered over the button
- // until the stop action is no longer selectable.
+ // Do nothing if Stop was disabled due to an attempt to change back to
+ // RELOAD mode while hovered.
if (stop_.paint_override() == GTK_STATE_INSENSITIVE)
return;
@@ -131,7 +138,7 @@ void ReloadButtonGtk::OnClicked(GtkWidget* sender) {
// The user has clicked, so we can feel free to update the button,
// even if the mouse is still hovering.
ChangeMode(MODE_RELOAD, true);
- } else if (!timer_running()) {
+ } else if (!timer_.IsRunning()) {
// Shift-clicking or Ctrl-clicking the reload button means we should ignore
// any cached content.
int command;
@@ -148,31 +155,24 @@ void ReloadButtonGtk::OnClicked(GtkWidget* sender) {
WindowOpenDisposition disposition =
event_utils::DispositionFromEventFlags(modifier_state_uint);
- if (disposition == CURRENT_TAB) {
+ if ((disposition == CURRENT_TAB) && location_bar_) {
// Forcibly reset the location bar, since otherwise it won't discard any
// ongoing user edits, since it doesn't realize this is a user-initiated
// action.
location_bar_->Revert();
}
- // Figure out the system double-click time.
- if (button_delay_ == 0) {
- GtkSettings* settings = gtk_settings_get_default();
- g_object_get(G_OBJECT(settings), "gtk-double-click-time", &button_delay_,
- NULL);
- }
-
// Start a timer - while this timer is running, the reload button cannot be
// changed to a stop button. We do not set |intended_mode_| to MODE_STOP
// here as the browser will do that when it actually starts loading (which
// may happen synchronously, thus the need to do this before telling the
// browser to execute the reload command).
timer_.Stop();
- timer_.Start(base::TimeDelta::FromMilliseconds(button_delay_), this,
- &ReloadButtonGtk::OnButtonTimer);
+ timer_.Start(timer_delay_, this, &ReloadButtonGtk::OnButtonTimer);
if (browser_)
browser_->ExecuteCommandWithDisposition(command, disposition);
+ ++testing_reload_count_;
}
}
@@ -184,16 +184,16 @@ gboolean ReloadButtonGtk::OnExpose(GtkWidget* widget,
widget, e, hover_controller_.GetCurrentValue());
}
-gboolean ReloadButtonGtk::OnLeaveNotify(GtkWidget* widget,
- GdkEventCrossing* event) {
+gboolean ReloadButtonGtk::OnLeaveNotify(GtkWidget* /* widget */,
+ GdkEventCrossing* /* event */) {
ChangeMode(intended_mode_, true);
return FALSE;
}
-gboolean ReloadButtonGtk::OnQueryTooltip(GtkWidget* sender,
- gint x,
- gint y,
- gboolean keyboard_mode,
+gboolean ReloadButtonGtk::OnQueryTooltip(GtkWidget* /* sender */,
+ gint /* x */,
+ gint /* y */,
+ gboolean /* keyboard_mode */,
GtkTooltip* tooltip) {
// |location_bar_| can be NULL in tests.
if (!location_bar_)
diff --git a/chrome/browser/gtk/reload_button_gtk.h b/chrome/browser/gtk/reload_button_gtk.h
index 2a152a4..b8818dc 100644
--- a/chrome/browser/gtk/reload_button_gtk.h
+++ b/chrome/browser/gtk/reload_button_gtk.h
@@ -37,48 +37,48 @@ class ReloadButtonGtk : public NotificationObserver {
// Provide NotificationObserver implementation.
virtual void Observe(NotificationType type,
const NotificationSource& source,
- const NotificationDetails& details);
+ const NotificationDetails& /* details */);
private:
- friend class ReloadButtonGtkPeer;
+ friend class ReloadButtonGtkTest;
CHROMEGTK_CALLBACK_0(ReloadButtonGtk, void, OnClicked);
CHROMEGTK_CALLBACK_1(ReloadButtonGtk, gboolean, OnExpose, GdkEventExpose*);
- CHROMEGTK_CALLBACK_1(ReloadButtonGtk, gboolean, OnLeaveNotify,
+ CHROMEGTK_CALLBACK_1(ReloadButtonGtk,
+ gboolean,
+ OnLeaveNotify,
GdkEventCrossing*);
- CHROMEGTK_CALLBACK_4(ReloadButtonGtk, gboolean, OnQueryTooltip, gint, gint,
- gboolean, GtkTooltip*);
-
- void SetToggled();
-
- bool timer_running() const {
- return timer_.IsRunning() || pretend_timer_is_running_for_unittest_;
- }
+ CHROMEGTK_CALLBACK_4(ReloadButtonGtk,
+ gboolean,
+ OnQueryTooltip,
+ gint,
+ gint,
+ gboolean,
+ GtkTooltip*);
void OnButtonTimer();
void UpdateThemeButtons();
- // Used to listen for theme change notifications.
- NotificationRegistrar registrar_;
+ base::OneShotTimer<ReloadButtonGtk> timer_;
+ // These may be NULL when testing.
LocationBarViewGtk* const location_bar_;
-
- // Keep a pointer to the Browser object to execute commands on it.
Browser* const browser_;
- // Delay time to wait before allowing a mode change. This is to prevent a
- // mode switch while the user is double clicking.
- int button_delay_;
- base::OneShotTimer<ReloadButtonGtk> timer_;
- bool pretend_timer_is_running_for_unittest_;
+ // The delay time for the double-click timer. This is a member so that tests
+ // can modify it.
+ base::TimeDelta timer_delay_;
- // The mode we should be in.
+ // The mode we should be in assuming no timers are running.
Mode intended_mode_;
- // The currently-visible mode - this may different from the intended mode.
+ // The currently-visible mode - this may differ from the intended mode.
Mode visible_mode_;
+ // Used to listen for theme change notifications.
+ NotificationRegistrar registrar_;
+
GtkThemeProvider* theme_provider_;
CustomDrawButtonBase reload_;
@@ -87,6 +87,13 @@ class ReloadButtonGtk : public NotificationObserver {
OwnedWidgetGtk widget_;
+ // TESTING ONLY
+ // True if we should pretend the button is hovered.
+ bool testing_mouse_hovered_;
+ // Increments when we would tell the browser to "reload", so
+ // test code can tell whether we did so (as there may be no |browser_|).
+ int testing_reload_count_;
+
DISALLOW_IMPLICIT_CONSTRUCTORS(ReloadButtonGtk);
};
diff --git a/chrome/browser/gtk/reload_button_gtk_unittest.cc b/chrome/browser/gtk/reload_button_gtk_unittest.cc
index 12384b6..04e01d0 100644
--- a/chrome/browser/gtk/reload_button_gtk_unittest.cc
+++ b/chrome/browser/gtk/reload_button_gtk_unittest.cc
@@ -2,137 +2,125 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/message_loop.h"
#include "chrome/browser/gtk/reload_button_gtk.h"
#include "testing/gtest/include/gtest/gtest.h"
-class ReloadButtonGtkPeer {
+class ReloadButtonGtkTest : public testing::Test {
public:
- explicit ReloadButtonGtkPeer(ReloadButtonGtk* reload) : reload_(reload) { }
+ ReloadButtonGtkTest();
- // const accessors for internal state
- ReloadButtonGtk::Mode intended_mode() const {
- return reload_->intended_mode_;
- }
- ReloadButtonGtk::Mode visible_mode() const { return reload_->visible_mode_; }
+ void CheckState(bool enabled,
+ ReloadButtonGtk::Mode intended_mode,
+ ReloadButtonGtk::Mode visible_mode,
+ bool timer_running);
- // mutators for internal state
- void SetState(GtkStateType state) {
- gtk_widget_set_state(reload_->widget(), state);
- }
- void set_intended_mode(ReloadButtonGtk::Mode mode) {
- reload_->intended_mode_ = mode;
- }
- void set_visible_mode(ReloadButtonGtk::Mode mode) {
- reload_->visible_mode_ = mode;
+ // These accessors eliminate the need to declare each testcase as a friend.
+ void set_mouse_hovered(bool hovered) {
+ reload_.testing_mouse_hovered_ = hovered;
}
- void set_timer_running() {
- reload_->pretend_timer_is_running_for_unittest_ = true;
- }
- void set_timer_stopped() {
- reload_->pretend_timer_is_running_for_unittest_ = false;
- }
-
- // forwarders to private methods
- gboolean OnLeave() {
- return reload_->OnLeaveNotify(reload_->widget(), NULL);
- }
-
- void OnClicked() {
- reload_->OnClicked(reload_->widget());
- }
-
- private:
- ReloadButtonGtk* const reload_;
-};
-
-namespace {
+ int reload_count() { return reload_.testing_reload_count_; }
+ void fake_mouse_leave() { reload_.OnLeaveNotify(reload_.widget(), NULL); }
-class ReloadButtonGtkTest : public testing::Test {
protected:
- ReloadButtonGtkTest() : reload_(NULL, NULL), peer_(&reload_) { }
+ // We need a message loop for the timers to post events.
+ MessageLoop loop_;
- protected:
ReloadButtonGtk reload_;
- ReloadButtonGtkPeer peer_;
};
-TEST_F(ReloadButtonGtkTest, ChangeModeReload) {
- reload_.ChangeMode(ReloadButtonGtk::MODE_RELOAD, true);
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.intended_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.visible_mode());
+ReloadButtonGtkTest::ReloadButtonGtkTest() : reload_(NULL, NULL) {
+ // Set the timer delay to 0 so that timers will fire as soon as we tell the
+ // message loop to run pending tasks.
+ reload_.timer_delay_ = base::TimeDelta();
}
-TEST_F(ReloadButtonGtkTest, ChangeModeStop) {
- reload_.ChangeMode(ReloadButtonGtk::MODE_STOP, true);
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.intended_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.visible_mode());
+void ReloadButtonGtkTest::CheckState(bool enabled,
+ ReloadButtonGtk::Mode intended_mode,
+ ReloadButtonGtk::Mode visible_mode,
+ bool timer_running) {
+ EXPECT_NE(enabled, reload_.stop_.paint_override() == GTK_STATE_INSENSITIVE);
+ EXPECT_EQ(intended_mode, reload_.intended_mode_);
+ EXPECT_EQ(visible_mode, reload_.visible_mode_);
+ EXPECT_EQ(timer_running, reload_.timer_.IsRunning());
}
-TEST_F(ReloadButtonGtkTest, ScheduleChangeModeNormalReload) {
- peer_.set_visible_mode(ReloadButtonGtk::MODE_STOP);
- peer_.SetState(GTK_STATE_NORMAL);
- reload_.ChangeMode(ReloadButtonGtk::MODE_RELOAD, false);
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.intended_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.visible_mode());
-}
+TEST_F(ReloadButtonGtkTest, Basic) {
+ // The stop/reload button starts in the "enabled reload" state with no timer
+ // running.
+ CheckState(true, ReloadButtonGtk::MODE_RELOAD, ReloadButtonGtk::MODE_RELOAD,
+ false);
-TEST_F(ReloadButtonGtkTest, ScheduleChangeModeHotReload) {
- peer_.set_visible_mode(ReloadButtonGtk::MODE_STOP);
- peer_.SetState(GTK_STATE_PRELIGHT);
- reload_.ChangeMode(ReloadButtonGtk::MODE_RELOAD, false);
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.intended_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.visible_mode());
-}
+ // Press the button. This should start the double-click timer.
+ gtk_button_clicked(GTK_BUTTON(reload_.widget()));
+ CheckState(true, ReloadButtonGtk::MODE_RELOAD, ReloadButtonGtk::MODE_RELOAD,
+ true);
-TEST_F(ReloadButtonGtkTest, ScheduleChangeModeNormalStop) {
- peer_.set_visible_mode(ReloadButtonGtk::MODE_RELOAD);
- peer_.SetState(GTK_STATE_NORMAL);
+ // Now change the mode (as if the browser had started loading the page). This
+ // should cancel the timer since the button is not hovered.
reload_.ChangeMode(ReloadButtonGtk::MODE_STOP, false);
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.intended_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.visible_mode());
+ CheckState(true, ReloadButtonGtk::MODE_STOP, ReloadButtonGtk::MODE_STOP,
+ false);
+
+ // Press the button again. This should change back to reload.
+ gtk_button_clicked(GTK_BUTTON(reload_.widget()));
+ CheckState(true, ReloadButtonGtk::MODE_RELOAD, ReloadButtonGtk::MODE_RELOAD,
+ false);
}
-TEST_F(ReloadButtonGtkTest, ScheduleChangeModeHotStop) {
- peer_.set_visible_mode(ReloadButtonGtk::MODE_RELOAD);
- peer_.SetState(GTK_STATE_PRELIGHT);
+TEST_F(ReloadButtonGtkTest, DoubleClickTimer) {
+ // Start by pressing the button.
+ gtk_button_clicked(GTK_BUTTON(reload_.widget()));
+
+ // Try to press the button again. This should do nothing because the timer is
+ // running.
+ int original_reload_count = reload_count();
+ gtk_button_clicked(GTK_BUTTON(reload_.widget()));
+ CheckState(true, ReloadButtonGtk::MODE_RELOAD, ReloadButtonGtk::MODE_RELOAD,
+ true);
+ EXPECT_EQ(original_reload_count, reload_count());
+
+ // Hover the button, and change mode. The visible mode should not change,
+ // again because the timer is running.
+ set_mouse_hovered(true);
reload_.ChangeMode(ReloadButtonGtk::MODE_STOP, false);
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.intended_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.visible_mode());
+ CheckState(true, ReloadButtonGtk::MODE_STOP, ReloadButtonGtk::MODE_RELOAD,
+ true);
+
+ // Now fire the timer. This should complete the mode change.
+ loop_.RunAllPending();
+ CheckState(true, ReloadButtonGtk::MODE_STOP, ReloadButtonGtk::MODE_STOP,
+ false);
}
-TEST_F(ReloadButtonGtkTest, ScheduleChangeModeTimerHotStop) {
- peer_.set_visible_mode(ReloadButtonGtk::MODE_RELOAD);
- peer_.SetState(GTK_STATE_PRELIGHT);
- peer_.set_timer_running();
+TEST_F(ReloadButtonGtkTest, DisableOnHover) {
+ // Start by pressing the button and proceeding with the mode change.
+ gtk_button_clicked(GTK_BUTTON(reload_.widget()));
reload_.ChangeMode(ReloadButtonGtk::MODE_STOP, false);
- peer_.set_timer_stopped();
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.intended_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.visible_mode());
-}
-TEST_F(ReloadButtonGtkTest, OnLeaveIntendedStop) {
- peer_.SetState(GTK_STATE_PRELIGHT);
- peer_.set_visible_mode(ReloadButtonGtk::MODE_RELOAD);
- peer_.set_intended_mode(ReloadButtonGtk::MODE_STOP);
- peer_.OnLeave();
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.visible_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_STOP, peer_.intended_mode());
+ // Now hover the button and change back. This should result in a disabled
+ // stop button.
+ set_mouse_hovered(true);
+ reload_.ChangeMode(ReloadButtonGtk::MODE_RELOAD, false);
+ CheckState(false, ReloadButtonGtk::MODE_RELOAD, ReloadButtonGtk::MODE_STOP,
+ false);
+
+ // Un-hover the button, which should allow it to reset.
+ set_mouse_hovered(false);
+ fake_mouse_leave();
+ CheckState(true, ReloadButtonGtk::MODE_RELOAD, ReloadButtonGtk::MODE_RELOAD,
+ false);
}
-TEST_F(ReloadButtonGtkTest, OnLeaveIntendedReload) {
- peer_.SetState(GTK_STATE_PRELIGHT);
- peer_.set_visible_mode(ReloadButtonGtk::MODE_STOP);
- peer_.set_intended_mode(ReloadButtonGtk::MODE_RELOAD);
- peer_.OnLeave();
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.visible_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.intended_mode());
-}
+TEST_F(ReloadButtonGtkTest, ResetOnClick) {
+ // Start by pressing the button and proceeding with the mode change.
+ gtk_button_clicked(GTK_BUTTON(reload_.widget()));
+ reload_.ChangeMode(ReloadButtonGtk::MODE_STOP, false);
-TEST_F(ReloadButtonGtkTest, OnClickedStop) {
- peer_.set_visible_mode(ReloadButtonGtk::MODE_STOP);
- peer_.OnClicked();
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.visible_mode());
- EXPECT_EQ(ReloadButtonGtk::MODE_RELOAD, peer_.intended_mode());
+ // Hover the button and click it. This should change back to reload despite
+ // the hover, because it's a direct user action.
+ set_mouse_hovered(true);
+ gtk_button_clicked(GTK_BUTTON(reload_.widget()));
+ CheckState(true, ReloadButtonGtk::MODE_RELOAD, ReloadButtonGtk::MODE_RELOAD,
+ false);
}
-
-} // namespace
diff --git a/chrome/browser/views/reload_button.cc b/chrome/browser/views/reload_button.cc
index 71dc5b1..40b7e61 100644
--- a/chrome/browser/views/reload_button.cc
+++ b/chrome/browser/views/reload_button.cc
@@ -19,8 +19,10 @@ ReloadButton::ReloadButton(LocationBarView* location_bar, Browser* browser)
location_bar_(location_bar),
browser_(browser),
intended_mode_(MODE_RELOAD),
- visible_mode_(MODE_RELOAD) {
- DCHECK(location_bar_);
+ visible_mode_(MODE_RELOAD),
+ timer_delay_(base::TimeDelta::FromMilliseconds(GetDoubleClickTimeMS())),
+ testing_mouse_hovered_(false),
+ testing_reload_count_(0) {
}
ReloadButton::~ReloadButton() {
@@ -32,7 +34,8 @@ void ReloadButton::ChangeMode(Mode mode, bool force) {
// If the change is forced, or the user isn't hovering the icon, or it's safe
// to change it to the other image type, make the change immediately;
// otherwise we'll let it happen later.
- if (force || !IsMouseHovered() || ((mode == MODE_STOP) ?
+ if (force || (!IsMouseHovered() && !testing_mouse_hovered_) ||
+ ((mode == MODE_STOP) ?
!timer_.IsRunning() : (visible_mode_ != MODE_STOP))) {
timer_.Stop();
SetToggled(mode == MODE_STOP);
@@ -50,10 +53,11 @@ void ReloadButton::ChangeMode(Mode mode, bool force) {
////////////////////////////////////////////////////////////////////////////////
// ReloadButton, views::ButtonListener implementation:
-void ReloadButton::ButtonPressed(views::Button* button,
+void ReloadButton::ButtonPressed(views::Button* /* button */,
const views::Event& event) {
if (visible_mode_ == MODE_STOP) {
- browser_->Stop();
+ if (browser_)
+ browser_->Stop();
// The user has clicked, so we can feel free to update the button,
// even if the mouse is still hovering.
ChangeMode(MODE_RELOAD, true);
@@ -74,7 +78,7 @@ void ReloadButton::ButtonPressed(views::Button* button,
WindowOpenDisposition disposition =
event_utils::DispositionFromEventFlags(flags);
- if (disposition == CURRENT_TAB) {
+ if ((disposition == CURRENT_TAB) && location_bar_) {
// Forcibly reset the location bar, since otherwise it won't discard any
// ongoing user edits, since it doesn't realize this is a user-initiated
// action.
@@ -87,10 +91,11 @@ void ReloadButton::ButtonPressed(views::Button* button,
// may happen synchronously, thus the need to do this before telling the
// browser to execute the reload command).
timer_.Stop();
- timer_.Start(base::TimeDelta::FromMilliseconds(GetDoubleClickTimeMS()),
- this, &ReloadButton::OnButtonTimer);
+ timer_.Start(timer_delay_, this, &ReloadButton::OnButtonTimer);
- browser_->ExecuteCommandWithDisposition(command, disposition);
+ if (browser_)
+ browser_->ExecuteCommandWithDisposition(command, disposition);
+ ++testing_reload_count_;
}
}
diff --git a/chrome/browser/views/reload_button.h b/chrome/browser/views/reload_button.h
index 6a8c29c..6d95c84 100644
--- a/chrome/browser/views/reload_button.h
+++ b/chrome/browser/views/reload_button.h
@@ -7,6 +7,7 @@
#pragma once
#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
#include "base/timer.h"
#include "views/controls/button/image_button.h"
@@ -37,26 +38,41 @@ class ReloadButton : public views::ToggleImageButton,
void ChangeMode(Mode mode, bool force);
// Overridden from views::ButtonListener:
- virtual void ButtonPressed(views::Button* button, const views::Event& event);
+ virtual void ButtonPressed(views::Button* /* button */,
+ const views::Event& event);
// Overridden from views::View:
virtual void OnMouseExited(const views::MouseEvent& e);
virtual bool GetTooltipText(const gfx::Point& p, std::wstring* tooltip);
private:
+ friend class ReloadButtonTest;
+
void OnButtonTimer();
base::OneShotTimer<ReloadButton> timer_;
+ // These may be NULL when testing.
LocationBarView* location_bar_;
Browser* browser_;
- // The mode we should be in
+ // The mode we should be in assuming no timers are running.
Mode intended_mode_;
- // The currently-visible mode - this may different from the intended mode
+ // The currently-visible mode - this may differ from the intended mode.
Mode visible_mode_;
+ // The delay time for the double-click timer. This is a member so that tests
+ // can modify it.
+ base::TimeDelta timer_delay_;
+
+ // TESTING ONLY
+ // True if we should pretend the button is hovered.
+ bool testing_mouse_hovered_;
+ // Increments when we would tell the browser to "reload", so
+ // test code can tell whether we did so (as there may be no |browser_|).
+ int testing_reload_count_;
+
DISALLOW_IMPLICIT_CONSTRUCTORS(ReloadButton);
};
diff --git a/chrome/browser/views/reload_button_unittest.cc b/chrome/browser/views/reload_button_unittest.cc
new file mode 100644
index 0000000..304106c
--- /dev/null
+++ b/chrome/browser/views/reload_button_unittest.cc
@@ -0,0 +1,120 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/message_loop.h"
+#include "chrome/browser/views/reload_button.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+class ReloadButtonTest : public testing::Test {
+ public:
+ ReloadButtonTest();
+
+ void CheckState(bool enabled,
+ ReloadButton::Mode intended_mode,
+ ReloadButton::Mode visible_mode,
+ bool timer_running);
+
+ // These accessors eliminate the need to declare each testcase as a friend.
+ void set_mouse_hovered(bool hovered) {
+ reload_.testing_mouse_hovered_ = hovered;
+ }
+ int reload_count() { return reload_.testing_reload_count_; }
+
+ protected:
+ // We need a message loop for the timers to post events.
+ MessageLoop loop_;
+
+ ReloadButton reload_;
+};
+
+ReloadButtonTest::ReloadButtonTest() : reload_(NULL, NULL) {
+ // Set the timer delay to 0 so that timers will fire as soon as we tell the
+ // message loop to run pending tasks.
+ reload_.timer_delay_ = base::TimeDelta();
+}
+
+void ReloadButtonTest::CheckState(bool enabled,
+ ReloadButton::Mode intended_mode,
+ ReloadButton::Mode visible_mode,
+ bool timer_running) {
+ EXPECT_EQ(enabled, reload_.IsEnabled());
+ EXPECT_EQ(intended_mode, reload_.intended_mode_);
+ EXPECT_EQ(visible_mode, reload_.visible_mode_);
+ EXPECT_EQ(timer_running, reload_.timer_.IsRunning());
+}
+
+TEST_F(ReloadButtonTest, Basic) {
+ // The stop/reload button starts in the "enabled reload" state with no timer
+ // running.
+ CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false);
+
+ // Press the button. This should start the double-click timer.
+ views::MouseEvent e(views::Event::ET_MOUSE_PRESSED, 0, 0, 0);
+ reload_.ButtonPressed(&reload_, e);
+ CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true);
+
+ // Now change the mode (as if the browser had started loading the page). This
+ // should cancel the timer since the button is not hovered.
+ reload_.ChangeMode(ReloadButton::MODE_STOP, false);
+ CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false);
+
+ // Press the button again. This should change back to reload.
+ reload_.ButtonPressed(&reload_, e);
+ CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false);
+}
+
+TEST_F(ReloadButtonTest, DoubleClickTimer) {
+ // Start by pressing the button.
+ views::MouseEvent e(views::Event::ET_MOUSE_PRESSED, 0, 0, 0);
+ reload_.ButtonPressed(&reload_, e);
+
+ // Try to press the button again. This should do nothing because the timer is
+ // running.
+ int original_reload_count = reload_count();
+ reload_.ButtonPressed(&reload_, e);
+ CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true);
+ EXPECT_EQ(original_reload_count, reload_count());
+
+ // Hover the button, and change mode. The visible mode should not change,
+ // again because the timer is running.
+ set_mouse_hovered(true);
+ reload_.ChangeMode(ReloadButton::MODE_STOP, false);
+ CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_RELOAD, true);
+
+ // Now fire the timer. This should complete the mode change.
+ loop_.RunAllPending();
+ CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false);
+}
+
+TEST_F(ReloadButtonTest, DisableOnHover) {
+ // Start by pressing the button and proceeding with the mode change.
+ views::MouseEvent e(views::Event::ET_MOUSE_PRESSED, 0, 0, 0);
+ reload_.ButtonPressed(&reload_, e);
+ reload_.ChangeMode(ReloadButton::MODE_STOP, false);
+
+ // Now hover the button and change back. This should result in a disabled
+ // stop button.
+ set_mouse_hovered(true);
+ reload_.ChangeMode(ReloadButton::MODE_RELOAD, false);
+ CheckState(false, ReloadButton::MODE_RELOAD, ReloadButton::MODE_STOP, false);
+
+ // Un-hover the button, which should allow it to reset.
+ set_mouse_hovered(false);
+ views::MouseEvent e2(views::Event::ET_MOUSE_MOVED, 0, 0, 0);
+ reload_.OnMouseExited(e2);
+ CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false);
+}
+
+TEST_F(ReloadButtonTest, ResetOnClick) {
+ // Start by pressing the button and proceeding with the mode change.
+ views::MouseEvent e(views::Event::ET_MOUSE_PRESSED, 0, 0, 0);
+ reload_.ButtonPressed(&reload_, e);
+ reload_.ChangeMode(ReloadButton::MODE_STOP, false);
+
+ // Hover the button and click it. This should change back to reload despite
+ // the hover, because it's a direct user action.
+ set_mouse_hovered(true);
+ reload_.ButtonPressed(&reload_, e);
+ CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false);
+}