Home | History | Annotate | Download | only in views
      1 // Copyright (c) 2011 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 
      5 #include "base/message_loop.h"
      6 #include "chrome/browser/ui/views/reload_button.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 class ReloadButtonTest : public testing::Test {
     10  public:
     11   ReloadButtonTest();
     12 
     13   void CheckState(bool enabled,
     14                   ReloadButton::Mode intended_mode,
     15                   ReloadButton::Mode visible_mode,
     16                   bool double_click_timer_running,
     17                   bool stop_to_reload_timer_running);
     18 
     19   // These accessors eliminate the need to declare each testcase as a friend.
     20   void set_mouse_hovered(bool hovered) {
     21     reload_.testing_mouse_hovered_ = hovered;
     22   }
     23   int reload_count() { return reload_.testing_reload_count_; }
     24 
     25  protected:
     26   // We need a message loop for the timers to post events.
     27   MessageLoop loop_;
     28 
     29   ReloadButton reload_;
     30 };
     31 
     32 ReloadButtonTest::ReloadButtonTest() : reload_(NULL, NULL) {
     33   // Set the timer delays to 0 so that timers will fire as soon as we tell the
     34   // message loop to run pending tasks.
     35   reload_.double_click_timer_delay_ = base::TimeDelta();
     36   reload_.stop_to_reload_timer_delay_ = base::TimeDelta();
     37 }
     38 
     39 void ReloadButtonTest::CheckState(bool enabled,
     40                                   ReloadButton::Mode intended_mode,
     41                                   ReloadButton::Mode visible_mode,
     42                                   bool double_click_timer_running,
     43                                   bool stop_to_reload_timer_running) {
     44   EXPECT_EQ(enabled, reload_.IsEnabled());
     45   EXPECT_EQ(intended_mode, reload_.intended_mode_);
     46   EXPECT_EQ(visible_mode, reload_.visible_mode_);
     47   EXPECT_EQ(double_click_timer_running,
     48             reload_.double_click_timer_.IsRunning());
     49   EXPECT_EQ(stop_to_reload_timer_running,
     50             reload_.stop_to_reload_timer_.IsRunning());
     51 }
     52 
     53 TEST_F(ReloadButtonTest, Basic) {
     54   // The stop/reload button starts in the "enabled reload" state with no timers
     55   // running.
     56   CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
     57              false);
     58 
     59   // Press the button.  This should start the double-click timer.
     60   views::MouseEvent e(ui::ET_MOUSE_PRESSED, 0, 0, 0);
     61   reload_.ButtonPressed(&reload_, e);
     62   CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true,
     63              false);
     64 
     65   // Now change the mode (as if the browser had started loading the page).  This
     66   // should cancel the double-click timer since the button is not hovered.
     67   reload_.ChangeMode(ReloadButton::MODE_STOP, false);
     68   CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false,
     69              false);
     70 
     71   // Press the button again.  This should change back to reload.
     72   reload_.ButtonPressed(&reload_, e);
     73   CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
     74              false);
     75 }
     76 
     77 TEST_F(ReloadButtonTest, DoubleClickTimer) {
     78   // Start by pressing the button.
     79   views::MouseEvent e(ui::ET_MOUSE_PRESSED, 0, 0, 0);
     80   reload_.ButtonPressed(&reload_, e);
     81 
     82   // Try to press the button again.  This should do nothing because the timer is
     83   // running.
     84   int original_reload_count = reload_count();
     85   reload_.ButtonPressed(&reload_, e);
     86   CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, true,
     87              false);
     88   EXPECT_EQ(original_reload_count, reload_count());
     89 
     90   // Hover the button, and change mode.  The visible mode should not change,
     91   // again because the timer is running.
     92   set_mouse_hovered(true);
     93   reload_.ChangeMode(ReloadButton::MODE_STOP, false);
     94   CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_RELOAD, true,
     95              false);
     96 
     97   // Now fire the timer.  This should complete the mode change.
     98   loop_.RunAllPending();
     99   CheckState(true, ReloadButton::MODE_STOP, ReloadButton::MODE_STOP, false,
    100              false);
    101 }
    102 
    103 TEST_F(ReloadButtonTest, DisableOnHover) {
    104   // Change to stop and hover.
    105   views::MouseEvent e(ui::ET_MOUSE_PRESSED, 0, 0, 0);
    106   reload_.ButtonPressed(&reload_, e);
    107   reload_.ChangeMode(ReloadButton::MODE_STOP, false);
    108   set_mouse_hovered(true);
    109 
    110   // Now change back to reload.  This should result in a disabled stop button
    111   // due to the hover.
    112   reload_.ChangeMode(ReloadButton::MODE_RELOAD, false);
    113   CheckState(false, ReloadButton::MODE_RELOAD, ReloadButton::MODE_STOP, false,
    114              true);
    115 
    116   // Un-hover the button, which should allow it to reset.
    117   set_mouse_hovered(false);
    118   views::MouseEvent e2(ui::ET_MOUSE_MOVED, 0, 0, 0);
    119   reload_.OnMouseExited(e2);
    120   CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
    121              false);
    122 }
    123 
    124 TEST_F(ReloadButtonTest, ResetOnClick) {
    125   // Change to stop and hover.
    126   views::MouseEvent e(ui::ET_MOUSE_PRESSED, 0, 0, 0);
    127   reload_.ButtonPressed(&reload_, e);
    128   reload_.ChangeMode(ReloadButton::MODE_STOP, false);
    129   set_mouse_hovered(true);
    130 
    131   // Press the button.  This should change back to reload despite the hover,
    132   // because it's a direct user action.
    133   reload_.ButtonPressed(&reload_, e);
    134   CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
    135              false);
    136 }
    137 
    138 TEST_F(ReloadButtonTest, ResetOnTimer) {
    139   // Change to stop, hover, and change back to reload.
    140   views::MouseEvent e(ui::ET_MOUSE_PRESSED, 0, 0, 0);
    141   reload_.ButtonPressed(&reload_, e);
    142   reload_.ChangeMode(ReloadButton::MODE_STOP, false);
    143   set_mouse_hovered(true);
    144   reload_.ChangeMode(ReloadButton::MODE_RELOAD, false);
    145 
    146   // Now fire the stop-to-reload timer.  This should reset the button.
    147   loop_.RunAllPending();
    148   CheckState(true, ReloadButton::MODE_RELOAD, ReloadButton::MODE_RELOAD, false,
    149              false);
    150 }
    151