Index: services/ui/view_manager/tests/view_manager_test.cc |
diff --git a/services/ui/view_manager/tests/view_manager_test.cc b/services/ui/view_manager/tests/view_manager_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9d20c6a604f26c011f9c1766bd72b2e2141551bf |
--- /dev/null |
+++ b/services/ui/view_manager/tests/view_manager_test.cc |
@@ -0,0 +1,151 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/connect.h" |
+#include "mojo/services/ui/views/interfaces/view_manager.mojom.h" |
+#include "mojo/services/ui/views/interfaces/views.mojom.h" |
+#include "services/ui/view_manager/tests/mock_view_associate.h" |
+#include "services/ui/view_manager/tests/view_manager_test_base.h" |
+ |
+namespace view_manager { |
+namespace test { |
+ |
+class MockViewListener : public mojo::ui::ViewListener { |
+ public: |
+ MockViewListener() {} |
+ ~MockViewListener() override {} |
+ |
+ void OnPropertiesChanged( |
+ uint32_t scene_version, |
+ mojo::ui::ViewPropertiesPtr properties, |
+ const OnPropertiesChangedCallback& callback) override {} |
+}; |
+ |
+class MockViewAssociateWatcher : public mojo::ui::ViewAssociateWatcher { |
+ public: |
+ MockViewAssociateWatcher() {} |
+ ~MockViewAssociateWatcher() override {} |
+ |
+ void OnViewAssociateConnectionError( |
+ const mojo::String& label, |
+ const OnViewAssociateConnectionErrorCallback& callback) override { |
+ on_view_association_connection_error_invokecount++; |
+ on_view_association_connection_label = label; |
+ callback.Run(); |
+ }; |
+ |
+ int on_view_association_connection_error_invokecount = 0; |
+ std::string on_view_association_connection_label; |
+}; |
+ |
+class ViewManagerTest : public ViewManagerTestBase { |
+ public: |
+ ViewManagerTest() {} |
+ ~ViewManagerTest() override {} |
+ |
+ void SetUp() override { |
+ ViewManagerTestBase::SetUp(); |
+ |
+ // Connect to view manager |
+ mojo::ConnectToService(application_impl()->shell(), |
+ "mojo:view_manager_service", |
+ mojo::GetProxy(&view_manager_)); |
+ } |
+ |
+ protected: |
+ mojo::ui::ViewManagerPtr view_manager_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(ViewManagerTest); |
+}; |
+ |
+TEST_F(ViewManagerTest, CreateAViewManagerAndView) { |
+ // Create and bind a mock view listener |
+ mojo::ui::ViewListenerPtr view_listener; |
+ MockViewListener mock_view_listener; |
+ mojo::Binding<mojo::ui::ViewListener> view_listener_binding( |
+ &mock_view_listener, mojo::GetProxy(&view_listener)); |
+ |
+ // Create a view |
+ mojo::ui::ViewPtr view; |
+ mojo::ui::ViewOwnerPtr client_view_owner; |
+ view_manager_->CreateView(mojo::GetProxy(&view), |
+ mojo::GetProxy(&client_view_owner), |
+ view_listener.Pass(), "test_view"); |
+ |
+ // Call View::GetToken. Check that you get the callback. |
+ int view_token_callback_invokecount = 0; |
+ auto view_token_callback = [&view_token_callback_invokecount]( |
+ mojo::ui::ViewTokenPtr token) { view_token_callback_invokecount++; }; |
+ |
+ EXPECT_EQ(0, view_token_callback_invokecount); |
+ view->GetToken(view_token_callback); |
+ |
+ KICK_MESSAGE_LOOP_WHILE(view_token_callback_invokecount != 1); |
+ |
+ EXPECT_EQ(1, view_token_callback_invokecount); |
+} |
+ |
+TEST_F(ViewManagerTest, ConnectAMockViewAssociate) { |
+ // Create and bind a MockViewAssociate |
+ mojo::InterfaceHandle<mojo::ui::ViewAssociate> associate; |
+ MockViewAssociate mock_view_associate; |
+ mojo::Binding<mojo::ui::ViewAssociate> view_associate_binding( |
+ &mock_view_associate, mojo::GetProxy(&associate)); |
+ |
+ // Call ViewManager::RegisterViewAssociate. MockViewAssociate::Connect |
+ // should be called back |
+ EXPECT_EQ(0, mock_view_associate.connect_invokecount); |
+ view_manager_->RegisterViewAssociate(associate.Pass(), "test_view_associate"); |
+ |
+ KICK_MESSAGE_LOOP_WHILE(mock_view_associate.connect_invokecount != 1); |
+ |
+ EXPECT_EQ(1, mock_view_associate.connect_invokecount); |
+} |
+ |
+TEST_F(ViewManagerTest, DisconnectAMockViewAssociate) { |
+ // Create a ViewAssociateWatcher which we will use at the end of the test |
+ mojo::ui::ViewAssociateWatcherPtr view_associate_watcher; |
+ MockViewAssociateWatcher mock_view_associate_watcher; |
+ mojo::Binding<mojo::ui::ViewAssociateWatcher> view_associate_watcher_binding( |
+ &mock_view_associate_watcher, mojo::GetProxy(&view_associate_watcher)); |
+ |
+ const std::string mock_view_associate_label = "test_view_associate_xyz"; |
+ { |
+ // Create and bind a MockViewAssociate |
+ mojo::InterfaceHandle<mojo::ui::ViewAssociate> associate; |
+ MockViewAssociate mock_view_associate; |
+ mojo::Binding<mojo::ui::ViewAssociate> view_associate_binding( |
+ &mock_view_associate, mojo::GetProxy(&associate)); |
+ |
+ // Call ViewManager::RegisterViewAssociate. MockViewAssociate::Connect |
+ // should be called back |
+ EXPECT_EQ(0, mock_view_associate.connect_invokecount); |
+ view_manager_->RegisterViewAssociate(associate.Pass(), |
+ mock_view_associate_label); |
+ |
+ KICK_MESSAGE_LOOP_WHILE(mock_view_associate.connect_invokecount != 1); |
+ |
+ EXPECT_EQ(1, mock_view_associate.connect_invokecount); |
+ |
+ EXPECT_EQ(0, mock_view_associate_watcher |
+ .on_view_association_connection_error_invokecount); |
+ view_manager_->RegisterViewAssociateWatcher(view_associate_watcher.Pass()); |
+ } |
+ |
+ // mock_view_associate is out of scope, should be destroyed |
+ // we expect to get a callback in mock_view_associate_watcher |
+ KICK_MESSAGE_LOOP_WHILE( |
+ mock_view_associate_watcher |
+ .on_view_association_connection_error_invokecount != 1) |
+ |
+ EXPECT_EQ(1, mock_view_associate_watcher |
+ .on_view_association_connection_error_invokecount); |
+ EXPECT_EQ(mock_view_associate_label, |
+ mock_view_associate_watcher.on_view_association_connection_label); |
+} |
+ |
+} // namespace test |
+} // namespace view_manager |