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

Side by Side Diff: services/ui/view_manager/tests/view_manager_test.cc

Issue 1949233002: Create a RegisterViewAssociate method in ViewManager (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Moved launching/registering of view associates to launcher Created 4 years, 7 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 2016 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 "mojo/public/cpp/application/application_impl.h"
6 #include "mojo/public/cpp/application/connect.h"
7 #include "mojo/services/ui/views/interfaces/view_manager.mojom.h"
8 #include "mojo/services/ui/views/interfaces/views.mojom.h"
9 #include "services/ui/view_manager/tests/mock_view_associate.h"
10 #include "services/ui/view_manager/tests/view_manager_test_base.h"
11
12 namespace view_manager {
13 namespace test {
14
15 class MockViewListener : public mojo::ui::ViewListener {
16 public:
17 MockViewListener() {}
18 ~MockViewListener() override {}
19
20 void OnPropertiesChanged(
21 uint32_t scene_version,
22 mojo::ui::ViewPropertiesPtr properties,
23 const OnPropertiesChangedCallback& callback) override {}
24 };
25
26 class MockViewAssociateWatcher : public mojo::ui::ViewAssociateWatcher {
27 public:
28 MockViewAssociateWatcher() {}
29 ~MockViewAssociateWatcher() override {}
30
31 void OnViewAssociateConnectionError(
32 const mojo::String& label,
33 const OnViewAssociateConnectionErrorCallback& callback) override {
34 on_view_association_connection_error_invokecount++;
35 on_view_association_connection_label = label;
36 callback.Run();
37 };
38
39 int on_view_association_connection_error_invokecount = 0;
40 std::string on_view_association_connection_label;
41 };
42
43 class ViewManagerTest : public ViewManagerTestBase {
44 public:
45 ViewManagerTest() {}
46 ~ViewManagerTest() override {}
47
48 void SetUp() override {
49 ViewManagerTestBase::SetUp();
50
51 // Connect to view manager
52 mojo::ConnectToService(application_impl()->shell(),
53 "mojo:view_manager_service",
54 mojo::GetProxy(&view_manager_));
55 }
56
57 protected:
58 mojo::ui::ViewManagerPtr view_manager_;
59
60 private:
61 DISALLOW_COPY_AND_ASSIGN(ViewManagerTest);
62 };
63
64 TEST_F(ViewManagerTest, CreateAViewManagerAndView) {
65 // Create and bind a mock view listener
66 mojo::ui::ViewListenerPtr view_listener;
67 MockViewListener mock_view_listener;
68 mojo::Binding<mojo::ui::ViewListener> view_listener_binding(
69 &mock_view_listener, mojo::GetProxy(&view_listener));
70
71 // Create a view
72 mojo::ui::ViewPtr view;
73 mojo::ui::ViewOwnerPtr client_view_owner;
74 view_manager_->CreateView(mojo::GetProxy(&view),
75 mojo::GetProxy(&client_view_owner),
76 view_listener.Pass(), "test_view");
77
78 // Call View::GetToken. Check that you get the callback.
79 int view_token_callback_invokecount = 0;
80 auto view_token_callback = [&view_token_callback_invokecount](
81 mojo::ui::ViewTokenPtr token) { view_token_callback_invokecount++; };
82
83 EXPECT_EQ(0, view_token_callback_invokecount);
84 view->GetToken(view_token_callback);
85
86 KICK_MESSAGE_LOOP_WHILE(view_token_callback_invokecount != 1);
87
88 EXPECT_EQ(1, view_token_callback_invokecount);
89 }
90
91 TEST_F(ViewManagerTest, ConnectAMockViewAssociate) {
92 // Create and bind a MockViewAssociate
93 mojo::InterfaceHandle<mojo::ui::ViewAssociate> associate;
94 MockViewAssociate mock_view_associate;
95 mojo::Binding<mojo::ui::ViewAssociate> view_associate_binding(
96 &mock_view_associate, mojo::GetProxy(&associate));
97
98 // Call ViewManager::RegisterViewAssociate. MockViewAssociate::Connect
99 // should be called back
100 EXPECT_EQ(0, mock_view_associate.connect_invokecount);
101 view_manager_->RegisterViewAssociate(associate.Pass(), "test_view_associate");
102
103 KICK_MESSAGE_LOOP_WHILE(mock_view_associate.connect_invokecount != 1);
104
105 EXPECT_EQ(1, mock_view_associate.connect_invokecount);
106 }
107
108 TEST_F(ViewManagerTest, DisconnectAMockViewAssociate) {
109 // Create a ViewAssociateWatcher which we will use at the end of the test
110 mojo::ui::ViewAssociateWatcherPtr view_associate_watcher;
111 MockViewAssociateWatcher mock_view_associate_watcher;
112 mojo::Binding<mojo::ui::ViewAssociateWatcher> view_associate_watcher_binding(
113 &mock_view_associate_watcher, mojo::GetProxy(&view_associate_watcher));
114
115 const std::string mock_view_associate_label = "test_view_associate_xyz";
116 {
117 // Create and bind a MockViewAssociate
118 mojo::InterfaceHandle<mojo::ui::ViewAssociate> associate;
119 MockViewAssociate mock_view_associate;
120 mojo::Binding<mojo::ui::ViewAssociate> view_associate_binding(
121 &mock_view_associate, mojo::GetProxy(&associate));
122
123 // Call ViewManager::RegisterViewAssociate. MockViewAssociate::Connect
124 // should be called back
125 EXPECT_EQ(0, mock_view_associate.connect_invokecount);
126 view_manager_->RegisterViewAssociate(associate.Pass(),
127 mock_view_associate_label);
128
129 KICK_MESSAGE_LOOP_WHILE(mock_view_associate.connect_invokecount != 1);
130
131 EXPECT_EQ(1, mock_view_associate.connect_invokecount);
132
133 EXPECT_EQ(0, mock_view_associate_watcher
134 .on_view_association_connection_error_invokecount);
135 view_manager_->RegisterViewAssociateWatcher(view_associate_watcher.Pass());
136 }
137
138 // mock_view_associate is out of scope, should be destroyed
139 // we expect to get a callback in mock_view_associate_watcher
140 KICK_MESSAGE_LOOP_WHILE(
141 mock_view_associate_watcher
142 .on_view_association_connection_error_invokecount != 1)
143
144 EXPECT_EQ(1, mock_view_associate_watcher
145 .on_view_association_connection_error_invokecount);
146 EXPECT_EQ(mock_view_associate_label,
147 mock_view_associate_watcher.on_view_association_connection_label);
148 }
149
150 } // namespace test
151 } // namespace view_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698