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

Side by Side Diff: chrome/browser/ui/views/toolbar/toolbar_action_view_unittest.cc

Issue 962393002: Add TestWebContentsFactory to content/public/test (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Scott's Created 5 years, 9 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 | content/content_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/run_loop.h" 5 #include "base/run_loop.h"
6 #include "base/strings/string16.h" 6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/sessions/session_tab_helper.h" 8 #include "chrome/browser/sessions/session_tab_helper.h"
9 #include "chrome/browser/ui/toolbar/test_toolbar_action_view_controller.h" 9 #include "chrome/browser/ui/toolbar/test_toolbar_action_view_controller.h"
10 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" 10 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
11 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h" 11 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
12 #include "chrome/test/base/testing_profile.h" 12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/test/test_browser_thread.h" 13 #include "content/public/test/test_browser_thread.h"
14 #include "content/public/test/test_renderer_host.h" 14 #include "content/public/test/test_web_contents_factory.h"
15 #include "content/public/test/web_contents_tester.h"
16 #include "ui/accessibility/ax_view_state.h" 15 #include "ui/accessibility/ax_view_state.h"
17 #include "ui/events/test/event_generator.h" 16 #include "ui/events/test/event_generator.h"
18 #include "ui/views/test/views_test_base.h" 17 #include "ui/views/test/views_test_base.h"
19 18
20 #if defined(USE_AURA)
21 #include "ui/aura/env.h"
22 #endif
23
24 namespace { 19 namespace {
25 20
26 // A helper class to create test web contents (tabs) for unit tests, without
27 // inheriting from RenderViewTestHarness. Can create web contents, and will
28 // clean up after itself upon destruction. Owns all created web contents.
29 // A few notes:
30 // - Works well allocated on the stack, because it should be destroyed before
31 // associated browser context.
32 // - Doesn't play nice with web contents created any other way (because of
33 // the implementation of RenderViewHostTestEnabler). But if you are creating
34 // web contents already, what do you need this for? ;)
35 // TODO(devlin): Look around and see if this class is needed elsewhere; if so,
36 // move it there and expand the API a bit (methods to, e.g., delete/close a
37 // web contents, access existing web contents, etc).
38 class TestWebContentsFactory {
39 public:
40 // |init_aura| initializes the aura environment (and cleans it up at
41 // shutdown, which is necessary for web contents. Since this method should
42 // only be called once, this should only be true if no other part of the test
43 // has initialized the environment.
44 explicit TestWebContentsFactory(bool init_aura);
45 ~TestWebContentsFactory();
46
47 // Creates a new WebContents with the given |context|, and returns it.
48 content::WebContents* CreateWebContents(content::BrowserContext* context);
49 private:
50 // The test factory (and friends) for creating test web contents.
51 scoped_ptr<content::RenderViewHostTestEnabler> rvh_enabler_;
52 // The vector of web contents that this class created.
53 ScopedVector<content::WebContents> web_contents_;
54
55 // True if the factory initialized aura (and should thus tear it down).
56 bool init_aura_;
57
58 DISALLOW_COPY_AND_ASSIGN(TestWebContentsFactory);
59 };
60
61 TestWebContentsFactory::TestWebContentsFactory(bool init_aura)
62 : rvh_enabler_(new content::RenderViewHostTestEnabler()),
63 init_aura_(init_aura) {
64 #if defined(USE_AURA)
65 if (init_aura)
66 aura::Env::CreateInstance(true);
67 #endif
68 }
69
70 TestWebContentsFactory::~TestWebContentsFactory() {
71 web_contents_.clear();
72 // Let any posted tasks for web contents deletion run.
73 base::RunLoop().RunUntilIdle();
74 rvh_enabler_.reset();
75 // Let any posted tasks for RenderProcess/ViewHost deletion run.
76 base::RunLoop().RunUntilIdle();
77 #if defined(USE_AURA)
78 if (init_aura_)
79 aura::Env::DeleteInstance();
80 #endif
81 }
82
83 content::WebContents* TestWebContentsFactory::CreateWebContents(
84 content::BrowserContext* context) {
85 scoped_ptr<content::WebContents> web_contents(
86 content::WebContentsTester::CreateTestWebContents(context, nullptr));
87 DCHECK(web_contents);
88 web_contents_.push_back(web_contents.release());
89 return web_contents_.back();
90 }
91
92 // A test delegate for a toolbar action view. 21 // A test delegate for a toolbar action view.
93 class TestToolbarActionViewDelegate : public ToolbarActionView::Delegate { 22 class TestToolbarActionViewDelegate : public ToolbarActionView::Delegate {
94 public: 23 public:
95 TestToolbarActionViewDelegate() : shown_in_menu_(false), 24 TestToolbarActionViewDelegate() : shown_in_menu_(false),
96 overflow_reference_view_(nullptr), 25 overflow_reference_view_(nullptr),
97 web_contents_(nullptr) {} 26 web_contents_(nullptr) {}
98 ~TestToolbarActionViewDelegate() override {} 27 ~TestToolbarActionViewDelegate() override {}
99 28
100 // ToolbarActionView::Delegate: 29 // ToolbarActionView::Delegate:
101 content::WebContents* GetCurrentWebContents() override { 30 content::WebContents* GetCurrentWebContents() override {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 103
175 DISALLOW_COPY_AND_ASSIGN(ToolbarActionViewUnitTest); 104 DISALLOW_COPY_AND_ASSIGN(ToolbarActionViewUnitTest);
176 }; 105 };
177 106
178 // Test the basic ui of a ToolbarActionView and that it responds correctly to 107 // Test the basic ui of a ToolbarActionView and that it responds correctly to
179 // a controller's state. 108 // a controller's state.
180 TEST_F(ToolbarActionViewUnitTest, BasicToolbarActionViewTest) { 109 TEST_F(ToolbarActionViewUnitTest, BasicToolbarActionViewTest) {
181 TestingProfile profile; 110 TestingProfile profile;
182 111
183 // ViewsTestBase initializees the aura environment, so the factory shouldn't. 112 // ViewsTestBase initializees the aura environment, so the factory shouldn't.
184 TestWebContentsFactory web_contents_factory(false); 113 content::TestWebContentsFactory web_contents_factory;
185 114
186 TestToolbarActionViewController controller("fake controller"); 115 TestToolbarActionViewController controller("fake controller");
187 TestToolbarActionViewDelegate action_view_delegate; 116 TestToolbarActionViewDelegate action_view_delegate;
188 117
189 // Configure the test controller and delegate. 118 // Configure the test controller and delegate.
190 base::string16 name = base::ASCIIToUTF16("name"); 119 base::string16 name = base::ASCIIToUTF16("name");
191 controller.SetAccessibleName(name); 120 controller.SetAccessibleName(name);
192 base::string16 tooltip = base::ASCIIToUTF16("tooltip"); 121 base::string16 tooltip = base::ASCIIToUTF16("tooltip");
193 controller.SetTooltip(tooltip); 122 controller.SetTooltip(tooltip);
194 content::WebContents* web_contents = 123 content::WebContents* web_contents =
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 // If the view isn't visible, the overflow button should be pressed for 187 // If the view isn't visible, the overflow button should be pressed for
259 // popups. 188 // popups.
260 view.SetVisible(false); 189 view.SetVisible(false);
261 controller.ShowPopup(true); 190 controller.ShowPopup(true);
262 EXPECT_EQ(views::Button::STATE_NORMAL, view.state()); 191 EXPECT_EQ(views::Button::STATE_NORMAL, view.state());
263 EXPECT_EQ(views::Button::STATE_PRESSED, overflow_button.state()); 192 EXPECT_EQ(views::Button::STATE_PRESSED, overflow_button.state());
264 controller.HidePopup(); 193 controller.HidePopup();
265 EXPECT_EQ(views::Button::STATE_NORMAL, view.state()); 194 EXPECT_EQ(views::Button::STATE_NORMAL, view.state());
266 EXPECT_EQ(views::Button::STATE_NORMAL, overflow_button.state()); 195 EXPECT_EQ(views::Button::STATE_NORMAL, overflow_button.state());
267 } 196 }
OLDNEW
« no previous file with comments | « no previous file | content/content_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698