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

Side by Side Diff: mandoline/ui/browser/browser_apptest.cc

Issue 1254383016: ApplicationConnection lifetime management changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 4 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 | « mandoline/ui/browser/browser.cc ('k') | mandoline/ui/browser/desktop/desktop_ui.h » ('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 "mandoline/ui/browser/browser.h" 5 #include "mandoline/ui/browser/browser.h"
6 6
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "components/view_manager/public/cpp/view.h" 8 #include "components/view_manager/public/cpp/view.h"
9 #include "mandoline/ui/browser/browser_delegate.h" 9 #include "mandoline/ui/browser/browser_delegate.h"
10 #include "mojo/application/public/cpp/application_connection.h" 10 #include "mojo/application/public/cpp/application_connection.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 MOJO_DISALLOW_COPY_AND_ASSIGN(TestBrowser); 46 MOJO_DISALLOW_COPY_AND_ASSIGN(TestBrowser);
47 }; 47 };
48 48
49 class BrowserTest : public mojo::test::ApplicationTestBase, 49 class BrowserTest : public mojo::test::ApplicationTestBase,
50 public mojo::ApplicationDelegate, 50 public mojo::ApplicationDelegate,
51 public BrowserDelegate { 51 public BrowserDelegate {
52 public: 52 public:
53 BrowserTest() 53 BrowserTest()
54 : app_(nullptr), 54 : app_(nullptr),
55 last_closed_connection_(nullptr),
56 last_browser_closed_(nullptr) {} 55 last_browser_closed_(nullptr) {}
57 56
58 // Creates a new Browser object. 57 // Creates a new Browser object.
59 TestBrowser* CreateBrowser() { 58 TestBrowser* CreateBrowser() {
60 if (!app_) 59 if (!app_)
61 return nullptr; 60 return nullptr;
62 TestBrowser* browser = new TestBrowser(app_, this); 61 TestBrowser* browser = new TestBrowser(app_, this);
63 browsers_.insert(browser); 62 browsers_.insert(browser);
64 return browser; 63 return browser;
65 } 64 }
66 65
67 TestBrowser* WaitForBrowserClosed() { 66 TestBrowser* WaitForBrowserClosed() {
68 if (!last_browser_closed_) { 67 if (!last_browser_closed_) {
69 browser_closed_run_loop_.reset(new base::RunLoop); 68 browser_closed_run_loop_.reset(new base::RunLoop);
70 browser_closed_run_loop_->Run(); 69 browser_closed_run_loop_->Run();
71 browser_closed_run_loop_.reset(); 70 browser_closed_run_loop_.reset();
72 } 71 }
73 TestBrowser* last_browser = last_browser_closed_; 72 TestBrowser* last_browser = last_browser_closed_;
74 last_browser_closed_ = nullptr; 73 last_browser_closed_ = nullptr;
75 return last_browser; 74 return last_browser;
76 } 75 }
77 76
78 // Returns the last ApplicationConnection closed.
79 void* last_closed_connection() {
80 return last_closed_connection_;
81 }
82
83 // Overridden from ApplicationDelegate: 77 // Overridden from ApplicationDelegate:
84 void Initialize(mojo::ApplicationImpl* app) override { 78 void Initialize(mojo::ApplicationImpl* app) override {
85 app_ = app; 79 app_ = app;
86 } 80 }
87 81
88 void OnWillCloseConnection(mojo::ApplicationConnection* connection) override {
89 // WARNING: DO NOT FOLLOW THIS POINTER. IT WILL BE DESTROYED.
90 last_closed_connection_ = connection;
91 }
92
93 // ApplicationTestBase: 82 // ApplicationTestBase:
94 ApplicationDelegate* GetApplicationDelegate() override { return this; } 83 ApplicationDelegate* GetApplicationDelegate() override { return this; }
95 84
96 // Overridden from BrowserDelegate: 85 // Overridden from BrowserDelegate:
97 void BrowserClosed(Browser* browser) override { 86 void BrowserClosed(Browser* browser) override {
98 scoped_ptr<Browser> browser_owner(browser); 87 scoped_ptr<Browser> browser_owner(browser);
99 TestBrowser* test_browser = static_cast<TestBrowser*>(browser); 88 TestBrowser* test_browser = static_cast<TestBrowser*>(browser);
100 DCHECK_GT(browsers_.count(test_browser), 0u); 89 DCHECK_GT(browsers_.count(test_browser), 0u);
101 browsers_.erase(test_browser); 90 browsers_.erase(test_browser);
102 last_browser_closed_ = test_browser; 91 last_browser_closed_ = test_browser;
103 if (browser_closed_run_loop_) { 92 if (browser_closed_run_loop_) {
104 browser_owner.reset(); 93 browser_owner.reset();
105 browser_closed_run_loop_->Quit(); 94 browser_closed_run_loop_->Quit();
106 } 95 }
107 } 96 }
108 97
109 void InitUIIfNecessary(Browser* browser, mojo::View* root_view) override {} 98 void InitUIIfNecessary(Browser* browser, mojo::View* root_view) override {}
110 99
111 private: 100 private:
112 mojo::ApplicationImpl* app_; 101 mojo::ApplicationImpl* app_;
113 void* last_closed_connection_;
114 std::set<TestBrowser*> browsers_; 102 std::set<TestBrowser*> browsers_;
115 TestBrowser* last_browser_closed_; 103 TestBrowser* last_browser_closed_;
116 scoped_ptr<base::RunLoop> browser_closed_run_loop_; 104 scoped_ptr<base::RunLoop> browser_closed_run_loop_;
117 105
118 MOJO_DISALLOW_COPY_AND_ASSIGN(BrowserTest); 106 MOJO_DISALLOW_COPY_AND_ASSIGN(BrowserTest);
119 }; 107 };
120 108
121 // This test verifies that closing a Browser closes the associated application 109 // This test verifies that closing a Browser closes the associated application
122 // connection with the view manager. 110 // connection with the view manager.
123 TEST_F(BrowserTest, ClosingBrowserClosesAppConnection) { 111 TEST_F(BrowserTest, ClosingBrowserClosesAppConnection) {
124 Browser* browser = CreateBrowser(); 112 Browser* browser = CreateBrowser();
125 ASSERT_NE(nullptr, browser); 113 ASSERT_NE(nullptr, browser);
126 mojo::ApplicationConnection* view_manager_connection = 114 mojo::ApplicationConnection* view_manager_connection =
127 browser->view_manager_init_.connection(); 115 browser->view_manager_init_.connection();
116 mojo::ApplicationConnection::TestApi connection_test_api(
117 view_manager_connection);
128 ASSERT_NE(nullptr, view_manager_connection); 118 ASSERT_NE(nullptr, view_manager_connection);
119 base::WeakPtr<mojo::ApplicationConnection> ptr =
120 connection_test_api.GetWeakPtr();
129 BrowserClosed(browser); 121 BrowserClosed(browser);
130 EXPECT_EQ(last_closed_connection(), view_manager_connection); 122 EXPECT_FALSE(ptr);
131 } 123 }
132 124
133 // This test verifies that we can create two Browsers and each Browser has a 125 // This test verifies that we can create two Browsers and each Browser has a
134 // different AppliationConnection and different root view. 126 // different AppliationConnection and different root view.
135 TEST_F(BrowserTest, TwoBrowsers) { 127 TEST_F(BrowserTest, TwoBrowsers) {
136 TestBrowser* browser1 = CreateBrowser(); 128 TestBrowser* browser1 = CreateBrowser();
137 mojo::ApplicationConnection* browser1_connection = 129 mojo::ApplicationConnection* browser1_connection =
138 browser1->view_manager_init_.connection(); 130 browser1->view_manager_init_.connection();
139 ASSERT_NE(nullptr, browser1); 131 ASSERT_NE(nullptr, browser1);
140 browser1->WaitForOnEmbed(); 132 browser1->WaitForOnEmbed();
(...skipping 12 matching lines...) Expand all
153 145
154 // Deleting the view manager closes the connection. 146 // Deleting the view manager closes the connection.
155 delete browser1->root()->view_manager(); 147 delete browser1->root()->view_manager();
156 EXPECT_EQ(browser1, WaitForBrowserClosed()); 148 EXPECT_EQ(browser1, WaitForBrowserClosed());
157 149
158 delete browser2->root()->view_manager(); 150 delete browser2->root()->view_manager();
159 EXPECT_EQ(browser2, WaitForBrowserClosed()); 151 EXPECT_EQ(browser2, WaitForBrowserClosed());
160 } 152 }
161 153
162 } // namespace mandoline 154 } // namespace mandoline
OLDNEW
« no previous file with comments | « mandoline/ui/browser/browser.cc ('k') | mandoline/ui/browser/desktop/desktop_ui.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698