Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/message_loop/message_loop.h" | |
|
msw
2017/04/11 01:19:52
nit: probably not needed (transitive include shoul
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 11 #include "base/strings/string_piece.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "base/test/scoped_task_scheduler.h" | |
| 14 #include "base/time/time.h" | |
|
msw
2017/04/11 01:19:52
nit: remove this if you can use event generator
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 15 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" | |
| 16 #include "chrome/browser/ui/content_settings/content_setting_bubble_model_delega te.h" | |
| 17 #include "chrome/browser/ui/content_settings/content_setting_image_model.h" | |
|
msw
2017/04/11 01:19:52
nit: needed?
Charlie Harrison
2017/04/11 12:20:45
Nope, removed.
| |
| 18 #include "chrome/browser/ui/test/test_confirm_bubble_model.h" | |
|
msw
2017/04/11 01:19:52
nit: needed?
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 19 #include "chrome/test/base/testing_profile.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 #include "content/public/test/test_browser_thread.h" | |
| 22 #include "content/public/test/test_web_contents_factory.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 #include "ui/views/bubble/bubble_dialog_delegate.h" | |
| 25 #include "ui/views/test/views_test_base.h" | |
| 26 #include "ui/views/view.h" | |
| 27 #include "ui/views/widget/widget.h" | |
| 28 #include "ui/views/window/dialog_client_view.h" | |
| 29 | |
| 30 using views::Widget; | |
|
msw
2017/04/11 01:19:52
nit: remove this, add one more explicit views:: na
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 31 | |
| 32 class TestContentSettingBubbleModel : public ContentSettingBubbleModel { | |
|
msw
2017/04/11 01:19:52
nit: add a comment, describe the behavior of apply
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 33 public: | |
| 34 TestContentSettingBubbleModel(ContentSettingBubbleModel::Delegate* delegate, | |
| 35 content::WebContents* web_contents, | |
| 36 Profile* profile, | |
| 37 base::string16 done_button_text) | |
| 38 : ContentSettingBubbleModel(delegate, web_contents, profile), | |
|
msw
2017/04/11 01:19:52
Can you pass null a web_contents and profile?
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 39 done_button_text_(done_button_text) { | |
| 40 SetTitle(); | |
| 41 SetManageText(); | |
| 42 } | |
| 43 | |
| 44 ~TestContentSettingBubbleModel() override {} | |
| 45 | |
| 46 void SetTitle() override { set_title(base::UTF8ToUTF16("Title")); } | |
| 47 void SetManageText() override { | |
| 48 set_manage_text(base::UTF8ToUTF16("test")); | |
| 49 set_show_manage_text_as_checkbox(true); | |
| 50 } | |
| 51 | |
| 52 void OnManageCheckboxChecked(bool is_checked) override { | |
| 53 set_done_button_text(done_button_text_); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 base::string16 done_button_text_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(TestContentSettingBubbleModel); | |
| 60 }; | |
| 61 | |
| 62 class ContentSettingBubbleContentsTest | |
| 63 : public views::ViewsTestBase, | |
| 64 public ContentSettingBubbleModelDelegate { | |
|
msw
2017/04/11 01:19:52
nit: make a separate test delegate class instead
Charlie Harrison
2017/04/11 12:20:46
Done. Now the test class is empty!
| |
| 65 public: | |
| 66 ContentSettingBubbleContentsTest() | |
| 67 : ui_thread_(content::BrowserThread::UI, message_loop()), | |
|
msw
2017/04/11 01:19:52
q: Is this and |scoped_task_scheduler_| actually n
Charlie Harrison
2017/04/11 12:20:45
It's needed for the web contents factory, but it's
| |
| 68 scoped_task_scheduler_(message_loop()) {} | |
| 69 | |
| 70 void SetUp() override { | |
| 71 views::ViewsTestBase::SetUp(); | |
| 72 web_contents_ = web_contents_factory_.CreateWebContents(&profile_); | |
| 73 } | |
| 74 | |
| 75 content::WebContents* web_contents() { return web_contents_; } | |
| 76 | |
| 77 Profile* profile() { return &profile_; } | |
| 78 | |
| 79 // ContentSettingBubbleModelDelegate: | |
| 80 void ShowCollectedCookiesDialog(content::WebContents* web_contents) override { | |
| 81 } | |
| 82 void ShowContentSettingsPage(ContentSettingsType type) override {} | |
| 83 void ShowMediaSettingsPage() override {} | |
| 84 void ShowLearnMorePage(ContentSettingsType type) override {} | |
| 85 | |
| 86 private: | |
| 87 content::TestBrowserThread ui_thread_; | |
| 88 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
| 89 | |
| 90 TestingProfile profile_; | |
|
msw
2017/04/11 01:19:52
optional nit: if it's simpler, move the profile an
Charlie Harrison
2017/04/11 12:20:45
Not needed, I was able to remove all the web conte
| |
| 91 content::TestWebContentsFactory web_contents_factory_; | |
| 92 | |
| 93 content::WebContents* web_contents_ = nullptr; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(ContentSettingBubbleContentsTest); | |
| 96 }; | |
| 97 | |
| 98 // TODO(csharrison): Consider moving some functionality to the harness if more | |
|
msw
2017/04/11 01:19:52
nit: remove this todo, it's understood.
Charlie Harrison
2017/04/11 12:20:45
Done.
| |
| 99 // tests need to be written. | |
| 100 // | |
| 101 // Tests that clicking the checkbox successfully changes the text within the | |
| 102 // done button, and that the size is properly updated. | |
| 103 TEST_F(ContentSettingBubbleContentsTest, CheckboxClicked_ResizesDoneButton) { | |
| 104 // Set up the root widget. | |
|
msw
2017/04/11 01:19:53
nit: "Set up the root widget, needed to show the b
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 105 views::Widget* widget = views::Widget::CreateWindowWithContextAndBounds( | |
| 106 nullptr, GetContext(), gfx::Rect(350, 0, 100, 100)); | |
| 107 views::View* view = new views::View(); | |
|
msw
2017/04/11 01:19:52
nit: pass widget->GetContentsView() to the ctor in
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 108 widget->GetContentsView()->AddChildView(view); | |
| 109 view->SetBounds(0, 0, 100, 100); | |
| 110 widget->Show(); | |
| 111 | |
| 112 base::string16 done_button_text = base::UTF8ToUTF16("01234567890123456789"); | |
| 113 | |
| 114 TestContentSettingBubbleModel* bubble_model = | |
| 115 new TestContentSettingBubbleModel(this, web_contents(), profile(), | |
| 116 done_button_text); | |
| 117 ContentSettingBubbleContents* bubble_view = new ContentSettingBubbleContents( | |
| 118 bubble_model, web_contents(), view, views::BubbleBorder::TOP_RIGHT); | |
| 119 views::Widget* bubble_widget = | |
| 120 views::BubbleDialogDelegateView::CreateBubble(bubble_view); | |
| 121 bubble_widget->Show(); | |
|
msw
2017/04/11 01:19:52
optional nit: you can inline the widget like this:
Charlie Harrison
2017/04/11 12:20:45
Done.
| |
| 122 | |
| 123 views::LabelButton* button = bubble_view->GetDialogClientView()->ok_button(); | |
| 124 EXPECT_NE(done_button_text, button->GetText()); | |
| 125 gfx::Size before_size = button->GetPreferredSize(); | |
|
msw
2017/04/11 01:19:52
nit: use the actual width instead of the preferred
Charlie Harrison
2017/04/11 12:20:45
Done.
| |
| 126 | |
| 127 // Check the checkbox. This entails finding it in the view hierarchy via some | |
|
msw
2017/04/11 01:19:52
You can optionally add a views::Checkbox* ContentS
Charlie Harrison
2017/04/11 12:20:45
I think I prefer this way. Adding getters for any
| |
| 128 // iteration. | |
| 129 auto children = bubble_view->GetChildrenInZOrder(); | |
| 130 for (auto* child : children) { | |
| 131 if (base::StringPiece(child->GetClassName()) == "Checkbox") { | |
|
msw
2017/04/11 01:19:52
q: is stringpiece actually needed?
Charlie Harrison
2017/04/11 12:20:46
IMO it is nicer style than using strncmp or someth
| |
| 132 views::Checkbox* b = static_cast<views::Checkbox*>(child); | |
| 133 ui::MouseEvent e(ui::ET_MOUSE_RELEASED, gfx::Point(0, 0), gfx::Point(), | |
|
msw
2017/04/11 01:19:52
nit: use an EventGenerator instead
Charlie Harrison
2017/04/11 12:20:46
Done.
| |
| 134 base::TimeTicks::Now(), ui::EF_LEFT_MOUSE_BUTTON, | |
| 135 ui::EF_LEFT_MOUSE_BUTTON); | |
| 136 EXPECT_FALSE(b->checked()); | |
| 137 b->OnMouseReleased(e); | |
| 138 EXPECT_TRUE(b->checked()); | |
| 139 break; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 // Text should change, and the preferred width should change to match it. The | |
|
msw
2017/04/11 01:19:53
optional nit: just say "The done button's text and
Charlie Harrison
2017/04/11 12:20:45
Done.
| |
| 144 // actual width should change to match the preferred width. | |
| 145 EXPECT_EQ(done_button_text, button->GetText()); | |
| 146 gfx::Size after_size = button->GetPreferredSize(); | |
| 147 EXPECT_GT(after_size.width(), before_size.width()); | |
| 148 EXPECT_EQ(after_size.width(), button->width()); | |
| 149 | |
| 150 widget->Close(); | |
| 151 } | |
| OLD | NEW |