Home | History | Annotate | Download | only in first_run
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ash/first_run/first_run_helper.h"
      6 
      7 #include "ash/first_run/desktop_cleaner.h"
      8 #include "ash/shell.h"
      9 #include "ash/shell_window_ids.h"
     10 #include "ash/test/ash_test_base.h"
     11 #include "ui/events/event_handler.h"
     12 #include "ui/events/test/event_generator.h"
     13 #include "ui/views/widget/widget.h"
     14 #include "ui/views/window/dialog_delegate.h"
     15 
     16 namespace ash {
     17 namespace test {
     18 
     19 namespace {
     20 
     21 class TestModalDialogDelegate : public views::DialogDelegateView {
     22  public:
     23   TestModalDialogDelegate() {}
     24   virtual ~TestModalDialogDelegate() {}
     25 
     26   // Overridden from views::WidgetDelegate:
     27   virtual ui::ModalType GetModalType() const OVERRIDE {
     28     return ui::MODAL_TYPE_SYSTEM;
     29   }
     30 
     31  private:
     32   DISALLOW_COPY_AND_ASSIGN(TestModalDialogDelegate);
     33 };
     34 
     35 class CountingEventHandler : public ui::EventHandler {
     36  public:
     37   // Handler resets |*mouse_events_registered_| during construction and updates
     38   // it after each registered event.
     39   explicit CountingEventHandler(int* mouse_events_registered)
     40       : mouse_events_registered_(mouse_events_registered) {
     41     *mouse_events_registered = 0;
     42   }
     43 
     44   virtual ~CountingEventHandler() {}
     45 
     46  private:
     47   // ui::EventHandler overrides.
     48   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
     49     ++*mouse_events_registered_;
     50   }
     51 
     52   int* mouse_events_registered_;
     53 
     54   DISALLOW_COPY_AND_ASSIGN(CountingEventHandler);
     55 };
     56 
     57 }  // namespace
     58 
     59 class FirstRunHelperTest : public AshTestBase,
     60                            public FirstRunHelper::Observer {
     61  public:
     62   FirstRunHelperTest() : cancelled_times_(0) {}
     63 
     64   virtual ~FirstRunHelperTest() {}
     65 
     66   virtual void SetUp() OVERRIDE {
     67     AshTestBase::SetUp();
     68     CheckContainersAreVisible();
     69     helper_.reset(ash::Shell::GetInstance()->CreateFirstRunHelper());
     70     helper_->AddObserver(this);
     71     helper_->GetOverlayWidget()->Show();
     72   }
     73 
     74   virtual void TearDown() OVERRIDE {
     75     EXPECT_TRUE(helper_.get());
     76     helper_.reset();
     77     CheckContainersAreVisible();
     78     AshTestBase::TearDown();
     79   }
     80 
     81   void CheckContainersAreVisible() const {
     82     aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
     83     std::vector<int> containers_to_check =
     84         DesktopCleaner::GetContainersToHideForTest();
     85     for (size_t i = 0; i < containers_to_check.size(); ++i) {
     86       aura::Window* container =
     87           Shell::GetContainer(root_window, containers_to_check[i]);
     88       EXPECT_TRUE(container->IsVisible());
     89     }
     90   }
     91 
     92   void CheckContainersAreHidden() const {
     93     aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
     94     std::vector<int> containers_to_check =
     95         DesktopCleaner::GetContainersToHideForTest();
     96     for (size_t i = 0; i < containers_to_check.size(); ++i) {
     97       aura::Window* container =
     98           Shell::GetContainer(root_window, containers_to_check[i]);
     99       EXPECT_TRUE(!container->IsVisible());
    100     }
    101   }
    102 
    103   FirstRunHelper* helper() { return helper_.get(); }
    104 
    105   int cancelled_times() const { return cancelled_times_; }
    106 
    107  private:
    108   // FirstRunHelper::Observer overrides.
    109   virtual void OnCancelled() OVERRIDE {
    110     ++cancelled_times_;
    111   }
    112 
    113   scoped_ptr<FirstRunHelper> helper_;
    114   int cancelled_times_;
    115 
    116   DISALLOW_COPY_AND_ASSIGN(FirstRunHelperTest);
    117 };
    118 
    119 // This test creates helper, checks that containers are hidden and then
    120 // destructs helper.
    121 TEST_F(FirstRunHelperTest, ContainersAreHidden) {
    122   CheckContainersAreHidden();
    123 }
    124 
    125 // Tests that helper correctly handles Escape key press.
    126 TEST_F(FirstRunHelperTest, Cancel) {
    127   GetEventGenerator().PressKey(ui::VKEY_ESCAPE, 0);
    128   EXPECT_EQ(cancelled_times(), 1);
    129 }
    130 
    131 // Tests that modal window doesn't block events for overlay window.
    132 TEST_F(FirstRunHelperTest, ModalWindowDoesNotBlock) {
    133   views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
    134       new TestModalDialogDelegate(), CurrentContext(), NULL);
    135   modal_dialog->Show();
    136   // TODO(dzhioev): modal window should not steal focus from overlay window.
    137   aura::Window* overlay_window = helper()->GetOverlayWidget()->GetNativeView();
    138   overlay_window->Focus();
    139   EXPECT_TRUE(overlay_window->HasFocus());
    140   int mouse_events;
    141   CountingEventHandler handler(&mouse_events);
    142   overlay_window->AddPreTargetHandler(&handler);
    143   GetEventGenerator().PressLeftButton();
    144   GetEventGenerator().ReleaseLeftButton();
    145   EXPECT_EQ(mouse_events, 2);
    146   overlay_window->RemovePreTargetHandler(&handler);
    147 }
    148 
    149 }  // namespace test
    150 }  // namespace ash
    151