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
3 // found in the LICENSE file.
5 // These tests are disabled pending refactoring of mandoline:browser
7 #include "mandoline/ui/browser/browser.h"
9 #include "base/run_loop.h"
10 #include "components/view_manager/public/cpp/view.h"
11 #include "mojo/application/public/cpp/application_connection.h"
12 #include "mojo/application/public/cpp/application_delegate.h"
13 #include "mojo/application/public/cpp/application_impl.h"
14 #include "mojo/application/public/cpp/application_test_base.h"
18 class TestBrowser
: public Browser
{
20 TestBrowser(mojo::ApplicationImpl
* app
, BrowserDelegate
* delegate
)
21 : Browser(app
, delegate
, GURL()) {}
22 ~TestBrowser() override
{}
24 void WaitForOnEmbed() {
27 embed_run_loop_
.reset(new base::RunLoop
);
28 embed_run_loop_
->Run();
29 embed_run_loop_
.reset();
32 mojo::View
* root() { return root_
; }
35 // Overridden from Browser:
36 void OnEmbed(mojo::View
* root
) override
{
37 // Don't call the base class because we don't want to navigate.
41 embed_run_loop_
->Quit();
44 // If non-null we're waiting for OnEmbed() using this RunLoop.
45 scoped_ptr
<base::RunLoop
> embed_run_loop_
;
47 MOJO_DISALLOW_COPY_AND_ASSIGN(TestBrowser
);
50 class BrowserTest
: public mojo::test::ApplicationTestBase
,
51 public mojo::ApplicationDelegate
{
55 last_browser_closed_(nullptr) {}
57 // Creates a new Browser object.
58 TestBrowser
* CreateBrowser() {
61 TestBrowser
* browser
= new TestBrowser(app_
, this);
62 browsers_
.insert(browser
);
66 TestBrowser
* WaitForBrowserClosed() {
67 if (!last_browser_closed_
) {
68 browser_closed_run_loop_
.reset(new base::RunLoop
);
69 browser_closed_run_loop_
->Run();
70 browser_closed_run_loop_
.reset();
72 TestBrowser
* last_browser
= last_browser_closed_
;
73 last_browser_closed_
= nullptr;
77 // Overridden from ApplicationDelegate:
78 void Initialize(mojo::ApplicationImpl
* app
) override
{
82 // ApplicationTestBase:
83 ApplicationDelegate
* GetApplicationDelegate() override
{ return this; }
85 void BrowserClosed(Browser
* browser
) override
{
86 scoped_ptr
<Browser
> browser_owner(browser
);
87 TestBrowser
* test_browser
= static_cast<TestBrowser
*>(browser
);
88 DCHECK_GT(browsers_
.count(test_browser
), 0u);
89 browsers_
.erase(test_browser
);
90 last_browser_closed_
= test_browser
;
91 if (browser_closed_run_loop_
) {
92 browser_owner
.reset();
93 browser_closed_run_loop_
->Quit();
98 mojo::ApplicationImpl
* app_
;
99 std::set
<TestBrowser
*> browsers_
;
100 TestBrowser
* last_browser_closed_
;
101 scoped_ptr
<base::RunLoop
> browser_closed_run_loop_
;
103 MOJO_DISALLOW_COPY_AND_ASSIGN(BrowserTest
);
106 // This test verifies that closing a Browser closes the associated application
107 // connection with the view manager.
108 TEST_F(BrowserTest
, ClosingBrowserClosesAppConnection
) {
109 Browser
* browser
= CreateBrowser();
110 ASSERT_NE(nullptr, browser
);
111 mojo::ApplicationConnection
* view_manager_connection
=
112 browser
->view_manager_init_
.connection();
113 mojo::ApplicationConnection::TestApi
connection_test_api(
114 view_manager_connection
);
115 ASSERT_NE(nullptr, view_manager_connection
);
116 base::WeakPtr
<mojo::ApplicationConnection
> ptr
=
117 connection_test_api
.GetWeakPtr();
118 BrowserClosed(browser
);
122 // This test verifies that we can create two Browsers and each Browser has a
123 // different AppliationConnection and different root view.
124 TEST_F(BrowserTest
, TwoBrowsers
) {
125 TestBrowser
* browser1
= CreateBrowser();
126 mojo::ApplicationConnection
* browser1_connection
=
127 browser1
->view_manager_init_
.connection();
128 ASSERT_NE(nullptr, browser1
);
129 browser1
->WaitForOnEmbed();
131 TestBrowser
* browser2
= CreateBrowser();
132 mojo::ApplicationConnection
* browser2_connection
=
133 browser2
->view_manager_init_
.connection();
134 ASSERT_NE(nullptr, browser2
);
135 browser2
->WaitForOnEmbed();
137 // Verify that we have two different connections to the ViewManager.
138 ASSERT_NE(browser1_connection
, browser2_connection
);
140 // Verify that we have two different root nodes.
141 ASSERT_NE(browser1
->root(), browser2
->root());
143 // Deleting the view manager closes the connection.
144 delete browser1
->root()->connection();
145 EXPECT_EQ(browser1
, WaitForBrowserClosed());
147 delete browser2
->root()->connection();
148 EXPECT_EQ(browser2
, WaitForBrowserClosed());
151 } // namespace mandoline