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

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

Issue 2808223002: Add test for ContentSettingBubbleContents
Patch Set: set correct layout provider 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/browser/ui/views/harmony/chrome_layout_provider.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/events/test/event_generator.h"
19 #include "ui/gfx/native_widget_types.h"
20 #include "ui/views/bubble/bubble_dialog_delegate.h"
21 #include "ui/views/test/test_views_delegate.h"
22 #include "ui/views/test/views_test_base.h"
23 #include "ui/views/view.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/views/window/dialog_client_view.h"
26
27 // Bubble model with a checkbox to manage behavior. When the checkbox is toggled
28 // the done button's text is changed.
29 class TestContentSettingBubbleModel : public ContentSettingBubbleModel {
30 public:
31 TestContentSettingBubbleModel(ContentSettingBubbleModel::Delegate* delegate,
32 base::string16 done_button_text)
33 : ContentSettingBubbleModel(delegate, nullptr, nullptr),
34 done_button_text_(done_button_text) {
35 SetTitle();
36 SetManageText();
37 }
38
39 ~TestContentSettingBubbleModel() override {}
40
41 void SetTitle() override { set_title(base::UTF8ToUTF16("Title")); }
42 void SetManageText() override {
43 set_manage_text(base::UTF8ToUTF16("test"));
44 set_show_manage_text_as_checkbox(true);
45 }
46
47 void OnManageCheckboxChecked(bool is_checked) override {
48 set_done_button_text(done_button_text_);
49 }
50
51 private:
52 base::string16 done_button_text_;
53
54 DISALLOW_COPY_AND_ASSIGN(TestContentSettingBubbleModel);
55 };
56
57 class TestContentSettingBubbleModelDelegate
58 : public ContentSettingBubbleModelDelegate {
59 public:
60 TestContentSettingBubbleModelDelegate() {}
61
62 private:
63 // ContentSettingBubbleModelDelegate:
64 void ShowCollectedCookiesDialog(content::WebContents* web_contents) override {
65 }
66 void ShowContentSettingsPage(ContentSettingsType type) override {}
67 void ShowMediaSettingsPage() override {}
68 void ShowLearnMorePage(ContentSettingsType type) override {}
69
70 DISALLOW_COPY_AND_ASSIGN(TestContentSettingBubbleModelDelegate);
71 };
72
73 class ContentSettingBubbleContentsTest : public views::ViewsTestBase {
74 public:
75 ContentSettingBubbleContentsTest() {}
76 ~ContentSettingBubbleContentsTest() override {}
77
78 void SetUp() override {
79 views::ViewsTestBase::SetUp();
80 test_views_delegate()->set_layout_provider(
81 ChromeLayoutProvider::CreateLayoutProvider());
82 }
83
84 private:
85 DISALLOW_COPY_AND_ASSIGN(ContentSettingBubbleContentsTest);
86 };
87
88 // Tests that clicking the checkbox successfully changes the text within the
89 // done button, and that the size is properly updated.
90 TEST_F(ContentSettingBubbleContentsTest, CheckboxClicked_ResizesDoneButton) {
91 // Set up the root widget, needed to show the bubble.
92 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds(
93 nullptr, GetContext(), gfx::Rect(350, 0, 100, 100));
94 widget->Show();
95
96 base::string16 done_button_text = base::UTF8ToUTF16("01234567890123456789");
97
98 auto bubble_model_delegate =
99 base::MakeUnique<TestContentSettingBubbleModelDelegate>();
100 TestContentSettingBubbleModel* bubble_model =
101 new TestContentSettingBubbleModel(bubble_model_delegate.get(),
102 done_button_text);
103 ContentSettingBubbleContents* bubble_view = new ContentSettingBubbleContents(
104 bubble_model, nullptr, widget->GetContentsView(),
105 views::BubbleBorder::TOP_RIGHT);
106 views::BubbleDialogDelegateView::CreateBubble(bubble_view)->Show();
107
108 views::LabelButton* button = bubble_view->GetDialogClientView()->ok_button();
109 EXPECT_NE(done_button_text, button->GetText());
110 int before_width = button->width();
111
112 // Check the checkbox. This entails finding it in the view hierarchy via some
113 // iteration.
114 auto children = bubble_view->GetChildrenInZOrder();
115 for (auto* child : children) {
116 if (base::StringPiece(child->GetClassName()) == "Checkbox") {
117 views::Checkbox* checkbox = static_cast<views::Checkbox*>(child);
118 EXPECT_FALSE(checkbox->checked());
119
120 ui::test::EventGenerator event_generator(
121 bubble_view->GetWidget()->GetNativeWindow());
122 event_generator.MoveMouseToInHost(
123 checkbox->GetBoundsInScreen().CenterPoint());
124 event_generator.ClickLeftButton();
125
126 EXPECT_TRUE(checkbox->checked());
127 break;
128 }
129 }
130
131 // The done button's text and size should change.
132 EXPECT_EQ(done_button_text, button->GetText());
133 EXPECT_GT(button->width(), before_width);
134
135 widget->Close();
136 }
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