OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/search_engines/template_url_service_factory.h" | 5 #include "chrome/browser/search_engines/template_url_service_factory.h" |
6 #include "chrome/browser/ui/views/first_run_bubble.h" | 6 #include "chrome/browser/ui/views/first_run_bubble.h" |
7 #include "chrome/test/base/testing_browser_process.h" | 7 #include "chrome/test/base/testing_browser_process.h" |
8 #include "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.h" |
9 #include "components/search_engines/template_url.h" | 9 #include "components/search_engines/template_url.h" |
10 #include "components/search_engines/template_url_service.h" | 10 #include "components/search_engines/template_url_service.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/events/event.h" |
| 15 #include "ui/events/event_processor.h" |
12 #include "ui/views/test/views_test_base.h" | 16 #include "ui/views/test/views_test_base.h" |
13 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
14 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
15 | 19 |
16 class FirstRunBubbleTest : public views::ViewsTestBase { | 20 class FirstRunBubbleTest : public views::ViewsTestBase, |
| 21 views::WidgetObserver { |
17 public: | 22 public: |
18 FirstRunBubbleTest(); | 23 FirstRunBubbleTest(); |
19 ~FirstRunBubbleTest() override; | 24 ~FirstRunBubbleTest() override; |
20 | 25 |
21 // Overrides from views::ViewsTestBase: | 26 // Overrides from views::ViewsTestBase: |
22 void SetUp() override; | 27 void SetUp() override; |
23 void TearDown() override; | 28 void TearDown() override; |
24 | 29 |
25 protected: | 30 protected: |
26 TestingProfile* profile() { return profile_.get(); } | 31 TestingProfile* profile() { return profile_.get(); } |
27 | 32 |
28 private: | 33 private: |
29 scoped_ptr<TestingProfile> profile_; | 34 scoped_ptr<TestingProfile> profile_; |
30 | 35 |
31 DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleTest); | 36 DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleTest); |
32 }; | 37 }; |
33 | 38 |
34 FirstRunBubbleTest::FirstRunBubbleTest() {} | 39 FirstRunBubbleTest::FirstRunBubbleTest() {} |
| 40 |
35 FirstRunBubbleTest::~FirstRunBubbleTest() {} | 41 FirstRunBubbleTest::~FirstRunBubbleTest() {} |
36 | 42 |
37 void FirstRunBubbleTest::SetUp() { | 43 void FirstRunBubbleTest::SetUp() { |
38 ViewsTestBase::SetUp(); | 44 ViewsTestBase::SetUp(); |
39 profile_.reset(new TestingProfile()); | 45 profile_.reset(new TestingProfile()); |
40 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 46 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
41 profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor); | 47 profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor); |
42 TemplateURLService* turl_model = | 48 TemplateURLService* turl_model = |
43 TemplateURLServiceFactory::GetForProfile(profile_.get()); | 49 TemplateURLServiceFactory::GetForProfile(profile_.get()); |
44 turl_model->Load(); | 50 turl_model->Load(); |
45 } | 51 } |
46 | 52 |
47 void FirstRunBubbleTest::TearDown() { | 53 void FirstRunBubbleTest::TearDown() { |
48 ViewsTestBase::TearDown(); | 54 ViewsTestBase::TearDown(); |
49 profile_.reset(); | 55 profile_.reset(); |
50 TestingBrowserProcess::DeleteInstance(); | 56 TestingBrowserProcess::DeleteInstance(); |
51 } | 57 } |
52 | 58 |
| 59 // Provides functionality to observe the widget passed in the constructor for |
| 60 // the widget closing event. |
| 61 class WidgetClosingObserver : public views::WidgetObserver { |
| 62 public: |
| 63 WidgetClosingObserver(views::Widget* widget) |
| 64 : widget_(widget), |
| 65 widget_destroyed_(false) { |
| 66 widget_->AddObserver(this); |
| 67 } |
| 68 |
| 69 ~WidgetClosingObserver() override { |
| 70 if (widget_) |
| 71 widget_->RemoveObserver(this); |
| 72 } |
| 73 |
| 74 void OnWidgetClosing(views::Widget* widget) override { |
| 75 DCHECK(widget == widget_); |
| 76 widget_->RemoveObserver(this); |
| 77 widget_destroyed_ = true; |
| 78 widget_ = nullptr; |
| 79 } |
| 80 |
| 81 bool widget_destroyed() const { |
| 82 return widget_destroyed_; |
| 83 } |
| 84 |
| 85 private: |
| 86 views::Widget* widget_; |
| 87 bool widget_destroyed_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(WidgetClosingObserver); |
| 90 }; |
| 91 |
53 TEST_F(FirstRunBubbleTest, CreateAndClose) { | 92 TEST_F(FirstRunBubbleTest, CreateAndClose) { |
54 // Create the anchor and parent widgets. | 93 // Create the anchor and parent widgets. |
55 views::Widget::InitParams params = | 94 views::Widget::InitParams params = |
56 CreateParams(views::Widget::InitParams::TYPE_WINDOW); | 95 CreateParams(views::Widget::InitParams::TYPE_WINDOW); |
57 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 96 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
58 scoped_ptr<views::Widget> anchor_widget(new views::Widget); | 97 scoped_ptr<views::Widget> anchor_widget(new views::Widget); |
59 anchor_widget->Init(params); | 98 anchor_widget->Init(params); |
60 anchor_widget->Show(); | 99 anchor_widget->Show(); |
61 | 100 |
62 FirstRunBubble* delegate = | 101 FirstRunBubble* delegate = |
63 FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView()); | 102 FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView()); |
64 EXPECT_TRUE(delegate != NULL); | 103 EXPECT_TRUE(delegate != NULL); |
65 delegate->GetWidget()->CloseNow(); | 104 delegate->GetWidget()->CloseNow(); |
66 } | 105 } |
| 106 |
| 107 // Tests that the first run bubble is closed when keyboard events are |
| 108 // dispatched to the anchor widget. |
| 109 TEST_F(FirstRunBubbleTest, CloseBubbleOnKeyEvent) { |
| 110 // Create the anchor and parent widgets. |
| 111 views::Widget::InitParams params = |
| 112 CreateParams(views::Widget::InitParams::TYPE_WINDOW); |
| 113 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 114 scoped_ptr<views::Widget> anchor_widget(new views::Widget); |
| 115 anchor_widget->Init(params); |
| 116 anchor_widget->Show(); |
| 117 |
| 118 FirstRunBubble* delegate = |
| 119 FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView()); |
| 120 EXPECT_TRUE(delegate != NULL); |
| 121 |
| 122 anchor_widget->GetFocusManager()->SetFocusedView( |
| 123 anchor_widget->GetContentsView()); |
| 124 |
| 125 scoped_ptr<WidgetClosingObserver> widget_observer( |
| 126 new WidgetClosingObserver(delegate->GetWidget())); |
| 127 |
| 128 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); |
| 129 |
| 130 ui::EventDispatchDetails details = |
| 131 anchor_widget->GetNativeWindow()->GetHost()->event_processor()-> |
| 132 OnEventFromSource(&key_event); |
| 133 EXPECT_FALSE(details.dispatcher_destroyed); |
| 134 |
| 135 EXPECT_TRUE(widget_observer->widget_destroyed()); |
| 136 } |
OLD | NEW |