| 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" | 12 #include "ui/aura/window.h" |
| 13 #include "ui/aura/window_tree_host.h" | 13 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
| 15 #include "ui/events/event_processor.h" | 15 #include "ui/events/event_processor.h" |
| 16 #include "ui/events/event_utils.h" |
| 16 #include "ui/views/test/views_test_base.h" | 17 #include "ui/views/test/views_test_base.h" |
| 17 #include "ui/views/view.h" | 18 #include "ui/views/view.h" |
| 18 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
| 19 | 20 |
| 21 // Provides functionality to observe the widget passed in the constructor for |
| 22 // the widget closing event. |
| 23 class WidgetClosingObserver : public views::WidgetObserver { |
| 24 public: |
| 25 explicit WidgetClosingObserver(views::Widget* widget) |
| 26 : widget_(widget), |
| 27 widget_destroyed_(false) { |
| 28 widget_->AddObserver(this); |
| 29 } |
| 30 |
| 31 ~WidgetClosingObserver() override { |
| 32 if (widget_) |
| 33 widget_->RemoveObserver(this); |
| 34 } |
| 35 |
| 36 void OnWidgetClosing(views::Widget* widget) override { |
| 37 DCHECK(widget == widget_); |
| 38 widget_->RemoveObserver(this); |
| 39 widget_destroyed_ = true; |
| 40 widget_ = nullptr; |
| 41 } |
| 42 |
| 43 bool widget_destroyed() const { |
| 44 return widget_destroyed_; |
| 45 } |
| 46 |
| 47 private: |
| 48 views::Widget* widget_; |
| 49 bool widget_destroyed_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(WidgetClosingObserver); |
| 52 }; |
| 53 |
| 20 class FirstRunBubbleTest : public views::ViewsTestBase, | 54 class FirstRunBubbleTest : public views::ViewsTestBase, |
| 21 views::WidgetObserver { | 55 views::WidgetObserver { |
| 22 public: | 56 public: |
| 23 FirstRunBubbleTest(); | 57 FirstRunBubbleTest(); |
| 24 ~FirstRunBubbleTest() override; | 58 ~FirstRunBubbleTest() override; |
| 25 | 59 |
| 26 // Overrides from views::ViewsTestBase: | 60 // Overrides from views::ViewsTestBase: |
| 27 void SetUp() override; | 61 void SetUp() override; |
| 28 void TearDown() override; | 62 void TearDown() override; |
| 29 | 63 |
| 64 void CreateAndCloseBubbleOnEventTest(ui::Event* event); |
| 65 |
| 30 protected: | 66 protected: |
| 31 TestingProfile* profile() { return profile_.get(); } | 67 TestingProfile* profile() { return profile_.get(); } |
| 32 | 68 |
| 33 private: | 69 private: |
| 34 scoped_ptr<TestingProfile> profile_; | 70 scoped_ptr<TestingProfile> profile_; |
| 35 | 71 |
| 36 DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleTest); | 72 DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleTest); |
| 37 }; | 73 }; |
| 38 | 74 |
| 39 FirstRunBubbleTest::FirstRunBubbleTest() {} | 75 FirstRunBubbleTest::FirstRunBubbleTest() {} |
| 40 | 76 |
| 41 FirstRunBubbleTest::~FirstRunBubbleTest() {} | 77 FirstRunBubbleTest::~FirstRunBubbleTest() {} |
| 42 | 78 |
| 43 void FirstRunBubbleTest::SetUp() { | 79 void FirstRunBubbleTest::SetUp() { |
| 44 ViewsTestBase::SetUp(); | 80 ViewsTestBase::SetUp(); |
| 45 profile_.reset(new TestingProfile()); | 81 profile_.reset(new TestingProfile()); |
| 46 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 82 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 47 profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor); | 83 profile_.get(), &TemplateURLServiceFactory::BuildInstanceFor); |
| 48 TemplateURLService* turl_model = | 84 TemplateURLService* turl_model = |
| 49 TemplateURLServiceFactory::GetForProfile(profile_.get()); | 85 TemplateURLServiceFactory::GetForProfile(profile_.get()); |
| 50 turl_model->Load(); | 86 turl_model->Load(); |
| 51 } | 87 } |
| 52 | 88 |
| 53 void FirstRunBubbleTest::TearDown() { | 89 void FirstRunBubbleTest::TearDown() { |
| 54 ViewsTestBase::TearDown(); | 90 ViewsTestBase::TearDown(); |
| 55 profile_.reset(); | 91 profile_.reset(); |
| 56 TestingBrowserProcess::DeleteInstance(); | 92 TestingBrowserProcess::DeleteInstance(); |
| 57 } | 93 } |
| 58 | 94 |
| 59 // Provides functionality to observe the widget passed in the constructor for | 95 void FirstRunBubbleTest::CreateAndCloseBubbleOnEventTest(ui::Event* event) { |
| 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 | |
| 92 TEST_F(FirstRunBubbleTest, CreateAndClose) { | |
| 93 // Create the anchor and parent widgets. | 96 // Create the anchor and parent widgets. |
| 94 views::Widget::InitParams params = | 97 views::Widget::InitParams params = |
| 95 CreateParams(views::Widget::InitParams::TYPE_WINDOW); | 98 CreateParams(views::Widget::InitParams::TYPE_WINDOW); |
| 96 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 99 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 97 scoped_ptr<views::Widget> anchor_widget(new views::Widget); | 100 scoped_ptr<views::Widget> anchor_widget(new views::Widget); |
| 98 anchor_widget->Init(params); | 101 anchor_widget->Init(params); |
| 102 anchor_widget->SetBounds(gfx::Rect(10, 10, 500, 500)); |
| 99 anchor_widget->Show(); | 103 anchor_widget->Show(); |
| 100 | 104 |
| 101 FirstRunBubble* delegate = | 105 FirstRunBubble* delegate = |
| 102 FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView()); | 106 FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView()); |
| 103 EXPECT_TRUE(delegate != NULL); | 107 EXPECT_TRUE(delegate != NULL); |
| 104 delegate->GetWidget()->CloseNow(); | 108 |
| 109 anchor_widget->GetFocusManager()->SetFocusedView( |
| 110 anchor_widget->GetContentsView()); |
| 111 |
| 112 scoped_ptr<WidgetClosingObserver> widget_observer( |
| 113 new WidgetClosingObserver(delegate->GetWidget())); |
| 114 |
| 115 ui::EventDispatchDetails details = |
| 116 anchor_widget->GetNativeWindow()->GetHost()->event_processor()-> |
| 117 OnEventFromSource(event); |
| 118 EXPECT_FALSE(details.dispatcher_destroyed); |
| 119 |
| 120 EXPECT_TRUE(widget_observer->widget_destroyed()); |
| 105 } | 121 } |
| 106 | 122 |
| 107 // Tests that the first run bubble is closed when keyboard events are | 123 TEST_F(FirstRunBubbleTest, CreateAndClose) { |
| 108 // dispatched to the anchor widget. | |
| 109 TEST_F(FirstRunBubbleTest, CloseBubbleOnKeyEvent) { | |
| 110 // Create the anchor and parent widgets. | 124 // Create the anchor and parent widgets. |
| 111 views::Widget::InitParams params = | 125 views::Widget::InitParams params = |
| 112 CreateParams(views::Widget::InitParams::TYPE_WINDOW); | 126 CreateParams(views::Widget::InitParams::TYPE_WINDOW); |
| 113 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 127 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 114 scoped_ptr<views::Widget> anchor_widget(new views::Widget); | 128 scoped_ptr<views::Widget> anchor_widget(new views::Widget); |
| 115 anchor_widget->Init(params); | 129 anchor_widget->Init(params); |
| 116 anchor_widget->Show(); | 130 anchor_widget->Show(); |
| 117 | 131 |
| 118 FirstRunBubble* delegate = | 132 FirstRunBubble* delegate = |
| 119 FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView()); | 133 FirstRunBubble::ShowBubble(NULL, anchor_widget->GetContentsView()); |
| 120 EXPECT_TRUE(delegate != NULL); | 134 EXPECT_TRUE(delegate != NULL); |
| 135 delegate->GetWidget()->CloseNow(); |
| 136 } |
| 121 | 137 |
| 122 anchor_widget->GetFocusManager()->SetFocusedView( | 138 // Tests that the first run bubble is closed when keyboard events are |
| 123 anchor_widget->GetContentsView()); | 139 // dispatched to the anchor widget. |
| 140 TEST_F(FirstRunBubbleTest, CloseBubbleOnKeyEvent) { |
| 141 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); |
| 142 CreateAndCloseBubbleOnEventTest(&key_event); |
| 143 } |
| 124 | 144 |
| 125 scoped_ptr<WidgetClosingObserver> widget_observer( | 145 TEST_F(FirstRunBubbleTest, CloseBubbleOnMouseDownEvent) { |
| 126 new WidgetClosingObserver(delegate->GetWidget())); | 146 gfx::Point pt(110, 210); |
| 147 ui::MouseEvent mouse_down( |
| 148 ui::ET_MOUSE_PRESSED, pt, pt, ui::EventTimeForNow(), |
| 149 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 150 CreateAndCloseBubbleOnEventTest(&mouse_down); |
| 151 } |
| 127 | 152 |
| 128 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); | 153 TEST_F(FirstRunBubbleTest, CloseBubbleOnTouchDownEvent) { |
| 154 ui::TouchEvent touch_down( |
| 155 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, ui::EventTimeForNow()); |
| 156 CreateAndCloseBubbleOnEventTest(&touch_down); |
| 157 } |
| 129 | 158 |
| 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 |