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

Side by Side Diff: mojo/services/window_manager/window_manager_api_unittest.cc

Issue 1049993002: Get mojo_shell building inside chromium checkout. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix presubmit Created 5 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
OLDNEW
(Empty)
1 // Copyright 2014 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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/run_loop.h"
9 #include "mojo/public/cpp/application/application_delegate.h"
10 #include "mojo/public/cpp/application/application_impl.h"
11 #include "mojo/public/cpp/application/service_provider_impl.h"
12 #include "mojo/public/interfaces/application/service_provider.mojom.h"
13 #include "mojo/shell/application_manager/application_manager.h"
14 #include "mojo/shell/shell_test_helper.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/mojo_services/src/view_manager/public/cpp/types.h"
17 #include "third_party/mojo_services/src/view_manager/public/cpp/view.h"
18 #include "third_party/mojo_services/src/view_manager/public/cpp/view_manager.h"
19 #include "third_party/mojo_services/src/view_manager/public/cpp/view_manager_cli ent_factory.h"
20 #include "third_party/mojo_services/src/view_manager/public/cpp/view_manager_del egate.h"
21 #include "third_party/mojo_services/src/view_manager/public/interfaces/view_mana ger.mojom.h"
22 #include "third_party/mojo_services/src/window_manager/public/interfaces/window_ manager.mojom.h"
23
24 using mojo::ApplicationImpl;
25 using mojo::Id;
26 using mojo::View;
27
28 namespace window_manager {
29 namespace {
30
31 const char kTestServiceURL[] = "mojo:test_url";
32
33 void EmptyResultCallback(bool result) {}
34
35 class TestWindowManagerObserver : public mojo::WindowManagerObserver {
36 public:
37 using NodeIdCallback = base::Callback<void(Id)>;
38
39 explicit TestWindowManagerObserver(
40 mojo::InterfaceRequest<mojo::WindowManagerObserver> observer_request)
41 : binding_(this, observer_request.Pass()) {}
42 ~TestWindowManagerObserver() override {}
43
44 void set_focus_changed_callback(const NodeIdCallback& callback) {
45 focus_changed_callback_ = callback;
46 }
47 void set_active_window_changed_callback(const NodeIdCallback& callback) {
48 active_window_changed_callback_ = callback;
49 }
50
51 private:
52 // Overridden from mojo::WindowManagerObserver:
53 void OnCaptureChanged(Id new_capture_node_id) override {}
54 void OnFocusChanged(Id focused_node_id) override {
55 if (!focus_changed_callback_.is_null())
56 focus_changed_callback_.Run(focused_node_id);
57 }
58 void OnActiveWindowChanged(Id active_window) override {
59 if (!active_window_changed_callback_.is_null())
60 active_window_changed_callback_.Run(active_window);
61 }
62
63 NodeIdCallback focus_changed_callback_;
64 NodeIdCallback active_window_changed_callback_;
65 mojo::Binding<WindowManagerObserver> binding_;
66
67 DISALLOW_COPY_AND_ASSIGN(TestWindowManagerObserver);
68 };
69
70 class TestApplicationLoader : public mojo::shell::ApplicationLoader,
71 public mojo::ApplicationDelegate,
72 public mojo::ViewManagerDelegate {
73 public:
74 typedef base::Callback<void(View*)> RootAddedCallback;
75
76 explicit TestApplicationLoader(const RootAddedCallback& root_added_callback)
77 : root_added_callback_(root_added_callback) {}
78 ~TestApplicationLoader() override {}
79
80 private:
81 // Overridden from mojo::shell::ApplicationLoader:
82 void Load(
83 const GURL& url,
84 mojo::InterfaceRequest<mojo::Application> application_request) override {
85 ASSERT_TRUE(application_request.is_pending());
86 scoped_ptr<ApplicationImpl> app(
87 new ApplicationImpl(this, application_request.Pass()));
88 apps_.push_back(app.release());
89 }
90
91 // Overridden from mojo::ApplicationDelegate:
92 void Initialize(ApplicationImpl* app) override {
93 view_manager_client_factory_.reset(
94 new mojo::ViewManagerClientFactory(app->shell(), this));
95 }
96
97 bool ConfigureIncomingConnection(
98 mojo::ApplicationConnection* connection) override {
99 connection->AddService(view_manager_client_factory_.get());
100 return true;
101 }
102
103 // Overridden from mojo::ViewManagerDelegate:
104 void OnEmbed(View* root,
105 mojo::InterfaceRequest<mojo::ServiceProvider> services,
106 mojo::ServiceProviderPtr exposed_services) override {
107 root_added_callback_.Run(root);
108 }
109 void OnViewManagerDisconnected(mojo::ViewManager* view_manager) override {}
110
111 RootAddedCallback root_added_callback_;
112
113 ScopedVector<ApplicationImpl> apps_;
114 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_;
115
116 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader);
117 };
118
119 } // namespace
120
121 class WindowManagerApiTest : public testing::Test {
122 public:
123 WindowManagerApiTest() {}
124 ~WindowManagerApiTest() override {}
125
126 protected:
127 Id WaitForEmbed() {
128 Id id;
129 base::RunLoop run_loop;
130 root_added_callback_ = base::Bind(&WindowManagerApiTest::OnEmbed,
131 base::Unretained(this), &id, &run_loop);
132 run_loop.Run();
133 return id;
134 }
135
136 Id WaitForFocusChange() {
137 Id new_focused;
138 base::RunLoop run_loop;
139 window_manager_observer()->set_focus_changed_callback(
140 base::Bind(&WindowManagerApiTest::OnFocusChanged,
141 base::Unretained(this), &new_focused, &run_loop));
142 run_loop.Run();
143 return new_focused;
144 }
145
146 Id WaitForActiveWindowChange() {
147 Id new_active;
148 base::RunLoop run_loop;
149 window_manager_observer()->set_active_window_changed_callback(
150 base::Bind(&WindowManagerApiTest::OnActiveWindowChanged,
151 base::Unretained(this), &new_active, &run_loop));
152 run_loop.Run();
153 return new_active;
154 }
155
156 Id OpenWindow() {
157 return OpenWindowWithURL(kTestServiceURL);
158 }
159
160 Id OpenWindowWithURL(const std::string& url) {
161 base::RunLoop run_loop;
162 window_manager_->Embed(url, nullptr, nullptr);
163 run_loop.Run();
164 return WaitForEmbed();
165 }
166
167 TestWindowManagerObserver* window_manager_observer() {
168 return window_manager_observer_.get();
169 }
170
171 mojo::WindowManagerPtr window_manager_;
172
173 private:
174 // Overridden from testing::Test:
175 void SetUp() override {
176 test_helper_.reset(new mojo::shell::ShellTestHelper);
177 test_helper_->Init();
178 test_helper_->AddURLMapping(GURL("mojo:window_manager"),
179 GURL("mojo:core_window_manager"));
180 test_helper_->SetLoaderForURL(
181 scoped_ptr<mojo::shell::ApplicationLoader>(
182 new TestApplicationLoader(base::Bind(
183 &WindowManagerApiTest::OnRootAdded, base::Unretained(this)))),
184 GURL(kTestServiceURL));
185 ConnectToWindowManager2();
186 }
187 void TearDown() override {}
188
189 void ConnectToWindowManager2() {
190 test_helper_->application_manager()->ConnectToService(
191 GURL("mojo:window_manager"), &window_manager_);
192 base::RunLoop connect_loop;
193 mojo::WindowManagerObserverPtr observer;
194 window_manager_observer_.reset(
195 new TestWindowManagerObserver(GetProxy(&observer)));
196
197 window_manager_->GetFocusedAndActiveViews(
198 observer.Pass(),
199 base::Bind(&WindowManagerApiTest::GotFocusedAndActiveViews,
200 base::Unretained(this)));
201 connect_loop.Run();
202
203 // The RunLoop above ensures the connection to the window manager completes.
204 // Without this the ApplicationManager would load the window manager twice.
205 test_helper_->application_manager()->ConnectToService(
206 GURL("mojo:core_window_manager"), &window_manager_);
207 }
208
209 void GotFocusedAndActiveViews(uint32_t, uint32_t, uint32_t) {}
210
211 void OnRootAdded(View* root) {
212 if (!root_added_callback_.is_null())
213 root_added_callback_.Run(root);
214 }
215
216 void OnEmbed(Id* root_id,
217 base::RunLoop* loop,
218 View* root) {
219 *root_id = root->id();
220 loop->Quit();
221 }
222
223 void OnFocusChanged(Id* new_focused,
224 base::RunLoop* run_loop,
225 Id focused_node_id) {
226 *new_focused = focused_node_id;
227 run_loop->Quit();
228 }
229
230 void OnActiveWindowChanged(Id* new_active,
231 base::RunLoop* run_loop,
232 Id active_node_id) {
233 *new_active = active_node_id;
234 run_loop->Quit();
235 }
236
237 scoped_ptr<mojo::shell::ShellTestHelper> test_helper_;
238 scoped_ptr<TestWindowManagerObserver> window_manager_observer_;
239 TestApplicationLoader::RootAddedCallback root_added_callback_;
240
241 DISALLOW_COPY_AND_ASSIGN(WindowManagerApiTest);
242 };
243
244 // TODO(sky): resolve this. Temporarily disabled as ApplicationManager ends up
245 // loading windowmanager twice because of the mapping of window_manager to
246 // core_window_manager.
247 TEST_F(WindowManagerApiTest, DISABLED_FocusAndActivateWindow) {
248 Id first_window = OpenWindow();
249 window_manager_->FocusWindow(first_window, base::Bind(&EmptyResultCallback));
250 Id id = WaitForFocusChange();
251 EXPECT_EQ(id, first_window);
252
253 Id second_window = OpenWindow();
254 window_manager_->ActivateWindow(second_window,
255 base::Bind(&EmptyResultCallback));
256 id = WaitForActiveWindowChange();
257 EXPECT_EQ(id, second_window);
258 }
259
260 } // namespace window_manager
OLDNEW
« no previous file with comments | « mojo/services/window_manager/view_targeter_unittest.cc ('k') | mojo/services/window_manager/window_manager_app.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698