summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/first_run_bubble_unittest.cc
blob: 7cdf5f825e849424b15c448076868ec2b9f27042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2012 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/macros.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/views/first_run_bubble.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/events/event.h"
#include "ui/events/event_processor.h"
#include "ui/events/event_utils.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

// Provides functionality to observe the widget passed in the constructor for
// the widget closing event.
class WidgetClosingObserver : public views::WidgetObserver {
 public:
  explicit WidgetClosingObserver(views::Widget* widget)
      : widget_(widget),
        widget_destroyed_(false) {
    widget_->AddObserver(this);
  }

  ~WidgetClosingObserver() override {
    if (widget_)
      widget_->RemoveObserver(this);
  }

  void OnWidgetClosing(views::Widget* widget) override {
    DCHECK(widget == widget_);
    widget_->RemoveObserver(this);
    widget_destroyed_ = true;
    widget_ = nullptr;
  }

  bool widget_destroyed() const {
    return widget_destroyed_;
  }

 private:
  views::Widget* widget_;
  bool widget_destroyed_;

  DISALLOW_COPY_AND_ASSIGN(WidgetClosingObserver);
};

class FirstRunBubbleTest : public views::ViewsTestBase,
                           views::WidgetObserver {
 public:
  FirstRunBubbleTest();
  ~FirstRunBubbleTest() override;

  // Overrides from views::ViewsTestBase:
  void SetUp() override;
  void TearDown() override;

  void CreateAndCloseBubbleOnEventTest(ui::Event* event);

 protected:
  TestingProfile* profile() { return profile_.get(); }

 private:
  scoped_ptr<TestingProfile> profile_;

  DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleTest);
};

FirstRunBubbleTest::FirstRunBubbleTest() {}

FirstRunBubbleTest::~FirstRunBubbleTest() {}

void FirstRunBubbleTest::SetUp() {
  ViewsTestBase::SetUp();
  profile_.reset(new TestingProfile());
  TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
      profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor);
  TemplateURLService* turl_model =
      TemplateURLServiceFactory::GetForProfile(profile_.get());
  turl_model->Load();
}

void FirstRunBubbleTest::TearDown() {
  ViewsTestBase::TearDown();
  profile_.reset();
  TestingBrowserProcess::DeleteInstance();
}

void FirstRunBubbleTest::CreateAndCloseBubbleOnEventTest(ui::Event* event) {
  // Create the anchor and parent widgets.
  views::Widget::InitParams params =
      CreateParams(views::Widget::InitParams::TYPE_WINDOW);
  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  scoped_ptr<views::Widget> anchor_widget(new views::Widget);
  anchor_widget->Init(params);
  anchor_widget->SetBounds(gfx::Rect(10, 10, 500, 500));
  anchor_widget->Show();

  FirstRunBubble* delegate =
      FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView());
  EXPECT_TRUE(delegate != NULL);

  anchor_widget->GetFocusManager()->SetFocusedView(
      anchor_widget->GetContentsView());

  scoped_ptr<WidgetClosingObserver> widget_observer(
      new WidgetClosingObserver(delegate->GetWidget()));

  ui::EventDispatchDetails details =
      anchor_widget->GetNativeWindow()->GetHost()->event_processor()->
          OnEventFromSource(event);
  EXPECT_FALSE(details.dispatcher_destroyed);

  EXPECT_TRUE(widget_observer->widget_destroyed());
}

TEST_F(FirstRunBubbleTest, CreateAndClose) {
  // Create the anchor and parent widgets.
  views::Widget::InitParams params =
      CreateParams(views::Widget::InitParams::TYPE_WINDOW);
  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  scoped_ptr<views::Widget> anchor_widget(new views::Widget);
  anchor_widget->Init(params);
  anchor_widget->Show();

  FirstRunBubble* delegate =
      FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView());
  EXPECT_TRUE(delegate != NULL);
  delegate->GetWidget()->CloseNow();
}

// Tests that the first run bubble is closed when keyboard events are
// dispatched to the anchor widget.
TEST_F(FirstRunBubbleTest, CloseBubbleOnKeyEvent) {
  ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE);
  CreateAndCloseBubbleOnEventTest(&key_event);
}

TEST_F(FirstRunBubbleTest, CloseBubbleOnMouseDownEvent) {
  gfx::Point pt(110, 210);
  ui::MouseEvent mouse_down(
      ui::ET_MOUSE_PRESSED, pt, pt, ui::EventTimeForNow(),
      ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
  CreateAndCloseBubbleOnEventTest(&mouse_down);
}

TEST_F(FirstRunBubbleTest, CloseBubbleOnTouchDownEvent) {
  ui::TouchEvent touch_down(
      ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, ui::EventTimeForNow());
  CreateAndCloseBubbleOnEventTest(&touch_down);
}