Commit a2e4c1b8 authored by Robert Liao's avatar Robert Liao Committed by Commit Bot

Drop set_owned_by_client from NotificationViewMDTest Tests

The widget assumes ownership of the notification view in
NotificationViewMDTest.

Effort was also made to remove as many direct pointers to the widget
as possible to demonstrate the linkage.

BUG=1044687

Change-Id: I010d2cf742c043b5e257f22abb6a4688e3707877
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2203019
Commit-Queue: Tim Song <tengs@chromium.org>
Auto-Submit: Robert Liao <robliao@chromium.org>
Reviewed-by: default avatarTim Song <tengs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#769043}
parent 50c01f26
...@@ -117,13 +117,7 @@ class NotificationViewMDTest : public views::InkDropObserver, ...@@ -117,13 +117,7 @@ class NotificationViewMDTest : public views::InkDropObserver,
// Overridden from views::ViewObserver: // Overridden from views::ViewObserver:
void OnViewPreferredSizeChanged(views::View* observed_view) override; void OnViewPreferredSizeChanged(views::View* observed_view) override;
NotificationViewMD* notification_view() const { NotificationViewMD* notification_view() const { return notification_view_; }
return notification_view_.get();
}
views::Widget* widget() const {
DCHECK_EQ(widget_, notification_view()->GetWidget());
return widget_;
}
// Overridden from message_center::MessageCenterObserver: // Overridden from message_center::MessageCenterObserver:
void OnNotificationRemoved(const std::string& notification_id, void OnNotificationRemoved(const std::string& notification_id,
...@@ -168,8 +162,7 @@ class NotificationViewMDTest : public views::InkDropObserver, ...@@ -168,8 +162,7 @@ class NotificationViewMDTest : public views::InkDropObserver,
bool delete_on_notification_removed_ = false; bool delete_on_notification_removed_ = false;
std::set<std::string> removed_ids_; std::set<std::string> removed_ids_;
scoped_refptr<NotificationTestDelegate> delegate_; scoped_refptr<NotificationTestDelegate> delegate_;
std::unique_ptr<NotificationViewMD> notification_view_; NotificationViewMD* notification_view_ = nullptr;
views::Widget* widget_;
private: private:
DISALLOW_COPY_AND_ASSIGN(NotificationViewMDTest); DISALLOW_COPY_AND_ASSIGN(NotificationViewMDTest);
...@@ -215,9 +208,9 @@ void NotificationViewMDTest::TearDown() { ...@@ -215,9 +208,9 @@ void NotificationViewMDTest::TearDown() {
delete_on_notification_removed_); delete_on_notification_removed_);
if (notification_view_) { if (notification_view_) {
notification_view_->SetInkDropMode(MessageView::InkDropMode::OFF); notification_view_->SetInkDropMode(MessageView::InkDropMode::OFF);
static_cast<views::View*>(notification_view_.get())->RemoveObserver(this); static_cast<views::View*>(notification_view_)->RemoveObserver(this);
widget()->Close(); notification_view_->GetWidget()->Close();
notification_view_.reset(); notification_view_ = nullptr;
} }
MessageCenter::Shutdown(); MessageCenter::Shutdown();
views::ViewsTestBase::TearDown(); views::ViewsTestBase::TearDown();
...@@ -227,19 +220,20 @@ void NotificationViewMDTest::OnViewPreferredSizeChanged( ...@@ -227,19 +220,20 @@ void NotificationViewMDTest::OnViewPreferredSizeChanged(
views::View* observed_view) { views::View* observed_view) {
EXPECT_EQ(observed_view, notification_view()); EXPECT_EQ(observed_view, notification_view());
if (delete_on_preferred_size_changed_) { if (delete_on_preferred_size_changed_) {
widget()->CloseNow(); notification_view_->GetWidget()->CloseNow();
notification_view_.reset(); notification_view_ = nullptr;
return; return;
} }
widget()->SetSize(notification_view()->GetPreferredSize()); notification_view_->GetWidget()->SetSize(
notification_view()->GetPreferredSize());
} }
void NotificationViewMDTest::OnNotificationRemoved( void NotificationViewMDTest::OnNotificationRemoved(
const std::string& notification_id, const std::string& notification_id,
bool by_user) { bool by_user) {
if (delete_on_notification_removed_) { if (delete_on_notification_removed_) {
widget()->CloseNow(); notification_view_->GetWidget()->CloseNow();
notification_view_.reset(); notification_view_ = nullptr;
return; return;
} }
} }
...@@ -309,19 +303,20 @@ void NotificationViewMDTest::UpdateNotificationViews( ...@@ -309,19 +303,20 @@ void NotificationViewMDTest::UpdateNotificationViews(
// MessageViewFactory::Create. // MessageViewFactory::Create.
// TODO(tetsui): Confirm that NotificationViewMD options are same as one // TODO(tetsui): Confirm that NotificationViewMD options are same as one
// created by the method. // created by the method.
notification_view_ = std::make_unique<NotificationViewMD>(notification); auto notification_view = std::make_unique<NotificationViewMD>(notification);
static_cast<views::View*>(notification_view_.get())->AddObserver(this); static_cast<views::View*>(notification_view.get())->AddObserver(this);
notification_view_->set_owned_by_client();
views::Widget::InitParams init_params( views::Widget::InitParams init_params(
CreateParams(views::Widget::InitParams::TYPE_POPUP)); CreateParams(views::Widget::InitParams::TYPE_POPUP));
widget_ = new views::Widget(); // The native widget owns |widget| and |widget| owns |notification_view_|.
widget_->Init(std::move(init_params)); auto* widget = new views::Widget();
widget_->SetContentsView(notification_view_.get()); widget->Init(std::move(init_params));
widget_->SetSize(notification_view_->GetPreferredSize()); notification_view_ = notification_view.get();
widget_->Show(); widget->SetContentsView(notification_view.release());
widget_->widget_delegate()->SetCanActivate(true); widget->SetSize(notification_view_->GetPreferredSize());
widget_->Activate(); widget->Show();
widget->widget_delegate()->SetCanActivate(true);
widget->Activate();
} else { } else {
notification_view_->UpdateWithNotification(notification); notification_view_->UpdateWithNotification(notification);
} }
...@@ -472,7 +467,7 @@ TEST_F(NotificationViewMDTest, TestIconSizing) { ...@@ -472,7 +467,7 @@ TEST_F(NotificationViewMDTest, TestIconSizing) {
TEST_F(NotificationViewMDTest, UpdateButtonsStateTest) { TEST_F(NotificationViewMDTest, UpdateButtonsStateTest) {
std::unique_ptr<Notification> notification = CreateSimpleNotification(); std::unique_ptr<Notification> notification = CreateSimpleNotification();
notification_view()->CreateOrUpdateViews(*notification); notification_view()->CreateOrUpdateViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
// When collapsed, new buttons are not shown. // When collapsed, new buttons are not shown.
EXPECT_FALSE(notification_view()->expanded_); EXPECT_FALSE(notification_view()->expanded_);
...@@ -526,7 +521,7 @@ TEST_F(NotificationViewMDTest, UpdateButtonCountTest) { ...@@ -526,7 +521,7 @@ TEST_F(NotificationViewMDTest, UpdateButtonCountTest) {
std::unique_ptr<Notification> notification = CreateSimpleNotification(); std::unique_ptr<Notification> notification = CreateSimpleNotification();
notification->set_buttons(CreateButtons(2)); notification->set_buttons(CreateButtons(2));
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
// Action buttons are hidden by collapsed state. // Action buttons are hidden by collapsed state.
if (!notification_view()->expanded_) if (!notification_view()->expanded_)
...@@ -576,9 +571,10 @@ TEST_F(NotificationViewMDTest, TestActionButtonClick) { ...@@ -576,9 +571,10 @@ TEST_F(NotificationViewMDTest, TestActionButtonClick) {
notification->set_buttons(CreateButtons(2)); notification->set_buttons(CreateButtons(2));
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Action buttons are hidden by collapsed state. // Action buttons are hidden by collapsed state.
if (!notification_view()->expanded_) if (!notification_view()->expanded_)
...@@ -604,9 +600,10 @@ TEST_F(NotificationViewMDTest, TestInlineReply) { ...@@ -604,9 +600,10 @@ TEST_F(NotificationViewMDTest, TestInlineReply) {
buttons[1].placeholder = base::string16(); buttons[1].placeholder = base::string16();
notification->set_buttons(buttons); notification->set_buttons(buttons);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Action buttons are hidden by collapsed state. // Action buttons are hidden by collapsed state.
if (!notification_view()->expanded_) if (!notification_view()->expanded_)
...@@ -691,9 +688,10 @@ TEST_F(NotificationViewMDTest, TestInlineReplyRemovedByUpdate) { ...@@ -691,9 +688,10 @@ TEST_F(NotificationViewMDTest, TestInlineReplyRemovedByUpdate) {
buttons[1].placeholder = base::string16(); buttons[1].placeholder = base::string16();
notification->set_buttons(buttons); notification->set_buttons(buttons);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Action buttons are hidden by collapsed state. // Action buttons are hidden by collapsed state.
if (!notification_view()->expanded_) if (!notification_view()->expanded_)
...@@ -740,13 +738,14 @@ TEST_F(NotificationViewMDTest, TestInlineReplyActivateWithKeyPress) { ...@@ -740,13 +738,14 @@ TEST_F(NotificationViewMDTest, TestInlineReplyActivateWithKeyPress) {
buttons[1].placeholder = base::string16(); buttons[1].placeholder = base::string16();
notification->set_buttons(buttons); notification->set_buttons(buttons);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
// Action buttons are hidden by collapsed state. // Action buttons are hidden by collapsed state.
if (!notification_view()->expanded_) if (!notification_view()->expanded_)
notification_view()->ToggleExpanded(); notification_view()->ToggleExpanded();
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Press and release space key to open inline reply text field. // Press and release space key to open inline reply text field.
// Note: VKEY_RETURN should work too, but triggers a click on MacOS. // Note: VKEY_RETURN should work too, but triggers a click on MacOS.
...@@ -968,7 +967,8 @@ TEST_F(NotificationViewMDTest, ExpandLongMessage) { ...@@ -968,7 +967,8 @@ TEST_F(NotificationViewMDTest, ExpandLongMessage) {
gfx::Point done_cursor_location(1, 1); gfx::Point done_cursor_location(1, 1);
views::View::ConvertPointToScreen(notification_view()->header_row_, views::View::ConvertPointToScreen(notification_view()->header_row_,
&done_cursor_location); &done_cursor_location);
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
generator.MoveMouseTo(done_cursor_location); generator.MoveMouseTo(done_cursor_location);
generator.ClickLeftButton(); generator.ClickLeftButton();
...@@ -982,7 +982,7 @@ TEST_F(NotificationViewMDTest, TestAccentColor) { ...@@ -982,7 +982,7 @@ TEST_F(NotificationViewMDTest, TestAccentColor) {
std::unique_ptr<Notification> notification = CreateSimpleNotification(); std::unique_ptr<Notification> notification = CreateSimpleNotification();
notification->set_buttons(CreateButtons(2)); notification->set_buttons(CreateButtons(2));
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
// Action buttons are hidden by collapsed state. // Action buttons are hidden by collapsed state.
if (!notification_view()->expanded_) if (!notification_view()->expanded_)
...@@ -1097,7 +1097,8 @@ TEST_F(NotificationViewMDTest, UpdateInSettings) { ...@@ -1097,7 +1097,8 @@ TEST_F(NotificationViewMDTest, UpdateInSettings) {
notification->set_type(NOTIFICATION_TYPE_SIMPLE); notification->set_type(NOTIFICATION_TYPE_SIMPLE);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Inline settings will be shown by clicking settings button. // Inline settings will be shown by clicking settings button.
EXPECT_FALSE(notification_view()->settings_row_->GetVisible()); EXPECT_FALSE(notification_view()->settings_row_->GetVisible());
...@@ -1124,7 +1125,8 @@ TEST_F(NotificationViewMDTest, InlineSettings) { ...@@ -1124,7 +1125,8 @@ TEST_F(NotificationViewMDTest, InlineSettings) {
notification->set_type(NOTIFICATION_TYPE_SIMPLE); notification->set_type(NOTIFICATION_TYPE_SIMPLE);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Inline settings will be shown by clicking settings button. // Inline settings will be shown by clicking settings button.
EXPECT_FALSE(notification_view()->settings_row_->GetVisible()); EXPECT_FALSE(notification_view()->settings_row_->GetVisible());
...@@ -1183,7 +1185,8 @@ TEST_F(NotificationViewMDTest, InlineSettingsInkDropAnimation) { ...@@ -1183,7 +1185,8 @@ TEST_F(NotificationViewMDTest, InlineSettingsInkDropAnimation) {
notification->set_type(NOTIFICATION_TYPE_SIMPLE); notification->set_type(NOTIFICATION_TYPE_SIMPLE);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Inline settings will be shown by clicking settings button. // Inline settings will be shown by clicking settings button.
EXPECT_FALSE(notification_view()->settings_row_->GetVisible()); EXPECT_FALSE(notification_view()->settings_row_->GetVisible());
...@@ -1198,9 +1201,9 @@ TEST_F(NotificationViewMDTest, InlineSettingsInkDropAnimation) { ...@@ -1198,9 +1201,9 @@ TEST_F(NotificationViewMDTest, InlineSettingsInkDropAnimation) {
notification_view()->GetInkDrop()->AddObserver(this); notification_view()->GetInkDrop()->AddObserver(this);
// Resize the widget by 1px to simulate the expand animation. // Resize the widget by 1px to simulate the expand animation.
gfx::Rect size = widget()->GetWindowBoundsInScreen(); gfx::Rect size = notification_view()->GetWidget()->GetWindowBoundsInScreen();
size.Inset(0, 0, 0, 1); size.Inset(0, 0, 0, 1);
widget()->SetBounds(size); notification_view()->GetWidget()->SetBounds(size);
notification_view()->GetInkDrop()->RemoveObserver(this); notification_view()->GetInkDrop()->RemoveObserver(this);
...@@ -1213,7 +1216,7 @@ TEST_F(NotificationViewMDTest, TestClick) { ...@@ -1213,7 +1216,7 @@ TEST_F(NotificationViewMDTest, TestClick) {
delegate_->set_expecting_click(true); delegate_->set_expecting_click(true);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
ui::test::EventGenerator generator( ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget())); GetRootWindow(notification_view()->GetWidget()));
...@@ -1237,9 +1240,10 @@ TEST_F(NotificationViewMDTest, TestClickExpanded) { ...@@ -1237,9 +1240,10 @@ TEST_F(NotificationViewMDTest, TestClickExpanded) {
delegate_->set_expecting_click(true); delegate_->set_expecting_click(true);
UpdateNotificationViews(*notification); UpdateNotificationViews(*notification);
widget()->Show(); notification_view()->GetWidget()->Show();
ui::test::EventGenerator generator(GetRootWindow(widget())); ui::test::EventGenerator generator(
GetRootWindow(notification_view()->GetWidget()));
// Expand the notification if it's collapsed. // Expand the notification if it's collapsed.
if (!notification_view()->expanded_) if (!notification_view()->expanded_)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment