Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1989)

Side by Side Diff: chrome/browser/ui/views/content_setting_bubble_contents_unittest.cc

Issue 2808223002: Add test for ContentSettingBubbleContents
Patch Set: GetNativeWindow(), avoid implicit type Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 "chrome/browser/ui/views/content_setting_bubble_contents.h"
6
7 #include <memory>
8
9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
14 #include "chrome/browser/ui/content_settings/content_setting_bubble_model_delega te.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/events/test/event_generator.h"
18 #include "ui/gfx/native_widget_types.h"
19 #include "ui/views/bubble/bubble_dialog_delegate.h"
20 #include "ui/views/test/views_test_base.h"
21 #include "ui/views/view.h"
22 #include "ui/views/widget/widget.h"
23 #include "ui/views/window/dialog_client_view.h"
24
25 // Bubble model with a checkbox to manage behavior. When the checkbox is toggled
26 // the done button's text is changed.
27 class TestContentSettingBubbleModel : public ContentSettingBubbleModel {
28 public:
29 TestContentSettingBubbleModel(ContentSettingBubbleModel::Delegate* delegate,
30 base::string16 done_button_text)
31 : ContentSettingBubbleModel(delegate, nullptr, nullptr),
32 done_button_text_(done_button_text) {
33 SetTitle();
34 SetManageText();
35 }
36
37 ~TestContentSettingBubbleModel() override {}
38
39 void SetTitle() override { set_title(base::UTF8ToUTF16("Title")); }
40 void SetManageText() override {
41 set_manage_text(base::UTF8ToUTF16("test"));
42 set_show_manage_text_as_checkbox(true);
43 }
44
45 void OnManageCheckboxChecked(bool is_checked) override {
46 set_done_button_text(done_button_text_);
47 }
48
49 private:
50 base::string16 done_button_text_;
51
52 DISALLOW_COPY_AND_ASSIGN(TestContentSettingBubbleModel);
53 };
54
55 class TestContentSettingBubbleModelDelegate
56 : public ContentSettingBubbleModelDelegate {
57 public:
58 TestContentSettingBubbleModelDelegate() {}
59
60 private:
61 // ContentSettingBubbleModelDelegate:
62 void ShowCollectedCookiesDialog(content::WebContents* web_contents) override {
63 }
64 void ShowContentSettingsPage(ContentSettingsType type) override {}
65 void ShowMediaSettingsPage() override {}
66 void ShowLearnMorePage(ContentSettingsType type) override {}
67
68 DISALLOW_COPY_AND_ASSIGN(TestContentSettingBubbleModelDelegate);
69 };
70
71 typedef views::ViewsTestBase ContentSettingBubbleContentsTest;
msw 2017/04/11 17:19:45 nit: using ContentSettingBubbleContentsTest = view
Charlie Harrison 2017/04/11 19:20:57 Done.
72
73 // Tests that clicking the checkbox successfully changes the text within the
74 // done button, and that the size is properly updated.
75 TEST_F(ContentSettingBubbleContentsTest, CheckboxClicked_ResizesDoneButton) {
76 // Set up the root widget, needed to show the bubble.
77 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
78 nullptr, GetContext(), gfx::Rect(350, 0, 100, 100));
79 widget->Show();
80
81 base::string16 done_button_text = base::UTF8ToUTF16("01234567890123456789");
82
83 auto bubble_model_delegate =
84 base::MakeUnique<TestContentSettingBubbleModelDelegate>();
85 TestContentSettingBubbleModel* bubble_model =
86 new TestContentSettingBubbleModel(bubble_model_delegate.get(),
87 done_button_text);
88 ContentSettingBubbleContents* bubble_view = new ContentSettingBubbleContents(
89 bubble_model, nullptr, widget->GetContentsView(),
90 views::BubbleBorder::TOP_RIGHT);
91 views::BubbleDialogDelegateView::CreateBubble(bubble_view)->Show();
92
93 views::LabelButton* button = bubble_view->GetDialogClientView()->ok_button();
94 EXPECT_NE(done_button_text, button->GetText());
95 int before_width = button->width();
96
97 // Check the checkbox. This entails finding it in the view hierarchy via some
98 // iteration.
99 auto children = bubble_view->GetChildrenInZOrder();
100 for (auto* child : children) {
101 if (base::StringPiece(child->GetClassName()) == "Checkbox") {
102 views::Checkbox* checkbox = static_cast<views::Checkbox*>(child);
103 EXPECT_FALSE(checkbox->checked());
104
105 ui::test::EventGenerator event_generator(widget->GetNativeWindow());
msw 2017/04/11 17:19:45 q: Hmm, should this be bubble_view->GetWidget()->G
Charlie Harrison 2017/04/11 19:20:57 I think it needs to be bubble_view->GetWidget()->G
msw 2017/04/11 19:27:35 Yup, typo on my part; just meant s/widget/bubble_v
106 event_generator.MoveMouseTo(checkbox->GetBoundsInScreen().CenterPoint());
107 event_generator.ClickLeftButton();
108
109 EXPECT_TRUE(checkbox->checked());
110 break;
111 }
112 }
113
114 // The done button's text and size should change.
115 EXPECT_EQ(done_button_text, button->GetText());
116 EXPECT_GT(button->width(), before_width);
117
118 widget->Close();
119 }
OLDNEW
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698