| OLD | NEW |
| (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 <stdint.h> |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "components/mus/common/types.h" |
| 13 #include "components/mus/common/util.h" |
| 14 #include "components/mus/public/interfaces/window_tree.mojom.h" |
| 15 #include "components/mus/surfaces/surfaces_state.h" |
| 16 #include "components/mus/ws/connection_manager.h" |
| 17 #include "components/mus/ws/connection_manager_delegate.h" |
| 18 #include "components/mus/ws/display_binding.h" |
| 19 #include "components/mus/ws/display_manager.h" |
| 20 #include "components/mus/ws/ids.h" |
| 21 #include "components/mus/ws/platform_display.h" |
| 22 #include "components/mus/ws/platform_display_factory.h" |
| 23 #include "components/mus/ws/server_window.h" |
| 24 #include "components/mus/ws/test_utils.h" |
| 25 #include "components/mus/ws/window_manager_state.h" |
| 26 #include "components/mus/ws/window_tree.h" |
| 27 #include "components/mus/ws/window_tree_binding.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "ui/gfx/geometry/rect.h" |
| 30 |
| 31 namespace mus { |
| 32 namespace ws { |
| 33 namespace test { |
| 34 namespace { |
| 35 |
| 36 class TestWindowManagerFactory : public mojom::WindowManagerFactory { |
| 37 public: |
| 38 TestWindowManagerFactory() {} |
| 39 ~TestWindowManagerFactory() override {} |
| 40 |
| 41 // mojom::WindowManagerFactory: |
| 42 void CreateWindowManager( |
| 43 mus::mojom::DisplayPtr display, |
| 44 mus::mojom::WindowTreeClientRequest client) override {} |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(TestWindowManagerFactory); |
| 48 }; |
| 49 |
| 50 class TestDisplayManagerObserver : public mojom::DisplayManagerObserver { |
| 51 public: |
| 52 TestDisplayManagerObserver() {} |
| 53 ~TestDisplayManagerObserver() override {} |
| 54 |
| 55 std::string GetAndClearObserverCalls() { |
| 56 std::string result; |
| 57 std::swap(observer_calls_, result); |
| 58 return result; |
| 59 } |
| 60 |
| 61 private: |
| 62 void AddCall(const std::string& call) { |
| 63 if (!observer_calls_.empty()) |
| 64 observer_calls_ += "\n"; |
| 65 observer_calls_ += call; |
| 66 } |
| 67 |
| 68 std::string DisplayIdsToString( |
| 69 const mojo::Array<mojom::DisplayPtr>& displays) { |
| 70 std::string display_ids; |
| 71 for (const auto& display : displays) { |
| 72 if (!display_ids.empty()) |
| 73 display_ids += " "; |
| 74 display_ids += base::Int64ToString(display->id); |
| 75 } |
| 76 return display_ids; |
| 77 } |
| 78 |
| 79 // mojom::DisplayManagerObserver: |
| 80 void OnDisplays(mojo::Array<mojom::DisplayPtr> displays) override { |
| 81 AddCall("OnDisplays " + DisplayIdsToString(displays)); |
| 82 } |
| 83 void OnDisplaysChanged(mojo::Array<mojom::DisplayPtr> displays) override { |
| 84 AddCall("OnDisplaysChanged " + DisplayIdsToString(displays)); |
| 85 } |
| 86 void OnDisplayRemoved(int64_t id) override { |
| 87 AddCall("OnDisplayRemoved " + base::Int64ToString(id)); |
| 88 } |
| 89 |
| 90 std::string observer_calls_; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(TestDisplayManagerObserver); |
| 93 }; |
| 94 |
| 95 mojom::FrameDecorationValuesPtr CreateDefaultFrameDecorationValues() { |
| 96 mojom::FrameDecorationValuesPtr frame_decoration_values = |
| 97 mojom::FrameDecorationValues::New(); |
| 98 frame_decoration_values->normal_client_area_insets = mojo::Insets::New(); |
| 99 frame_decoration_values->maximized_client_area_insets = mojo::Insets::New(); |
| 100 return frame_decoration_values; |
| 101 } |
| 102 |
| 103 } // namespace |
| 104 |
| 105 // ----------------------------------------------------------------------------- |
| 106 |
| 107 class UserDisplayManagerTest : public testing::Test { |
| 108 public: |
| 109 UserDisplayManagerTest() |
| 110 : cursor_id_(0), platform_display_factory_(&cursor_id_) {} |
| 111 ~UserDisplayManagerTest() override {} |
| 112 |
| 113 protected: |
| 114 // testing::Test: |
| 115 void SetUp() override { |
| 116 PlatformDisplay::set_factory_for_testing(&platform_display_factory_); |
| 117 connection_manager_.reset(new ConnectionManager( |
| 118 &connection_manager_delegate_, scoped_refptr<SurfacesState>())); |
| 119 connection_manager_delegate_.set_connection_manager( |
| 120 connection_manager_.get()); |
| 121 } |
| 122 |
| 123 protected: |
| 124 int32_t cursor_id_; |
| 125 TestPlatformDisplayFactory platform_display_factory_; |
| 126 TestConnectionManagerDelegate connection_manager_delegate_; |
| 127 scoped_ptr<ConnectionManager> connection_manager_; |
| 128 base::MessageLoop message_loop_; |
| 129 TestWindowManagerFactory test_window_manager_factory_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(UserDisplayManagerTest); |
| 132 }; |
| 133 |
| 134 TEST_F(UserDisplayManagerTest, OnlyNotifyWhenFrameDecorationsSet) { |
| 135 connection_manager_delegate_.set_num_displays_to_create(1); |
| 136 |
| 137 const UserId kUserId1 = "2"; |
| 138 TestDisplayManagerObserver display_manager_observer1; |
| 139 DisplayManager* display_manager = connection_manager_->display_manager(); |
| 140 WindowManagerFactoryRegistryTestApi( |
| 141 connection_manager_->window_manager_factory_registry()) |
| 142 .AddService(kUserId1, &test_window_manager_factory_); |
| 143 UserDisplayManager* user_display_manager1 = |
| 144 display_manager->GetUserDisplayManager(kUserId1); |
| 145 ASSERT_TRUE(user_display_manager1); |
| 146 UserDisplayManagerTestApi(user_display_manager1) |
| 147 .SetTestObserver(&display_manager_observer1); |
| 148 // Observer should not have been notified yet. |
| 149 EXPECT_EQ(std::string(), |
| 150 display_manager_observer1.GetAndClearObserverCalls()); |
| 151 |
| 152 // Set the frame decoration values, which should trigger sending immediately. |
| 153 ASSERT_EQ(1u, display_manager->displays().size()); |
| 154 Display* display1 = *display_manager->displays().begin(); |
| 155 display1->GetWindowManagerStateForUser(kUserId1)->SetFrameDecorationValues( |
| 156 CreateDefaultFrameDecorationValues()); |
| 157 EXPECT_EQ("OnDisplays 1", |
| 158 display_manager_observer1.GetAndClearObserverCalls()); |
| 159 |
| 160 UserDisplayManagerTestApi(user_display_manager1).SetTestObserver(nullptr); |
| 161 } |
| 162 |
| 163 TEST_F(UserDisplayManagerTest, AddObserverAfterFrameDecorationsSet) { |
| 164 connection_manager_delegate_.set_num_displays_to_create(1); |
| 165 |
| 166 const UserId kUserId1 = "2"; |
| 167 TestDisplayManagerObserver display_manager_observer1; |
| 168 DisplayManager* display_manager = connection_manager_->display_manager(); |
| 169 WindowManagerFactoryRegistryTestApi( |
| 170 connection_manager_->window_manager_factory_registry()) |
| 171 .AddService(kUserId1, &test_window_manager_factory_); |
| 172 UserDisplayManager* user_display_manager1 = |
| 173 display_manager->GetUserDisplayManager(kUserId1); |
| 174 ASSERT_TRUE(user_display_manager1); |
| 175 ASSERT_EQ(1u, display_manager->displays().size()); |
| 176 Display* display1 = *display_manager->displays().begin(); |
| 177 display1->GetWindowManagerStateForUser(kUserId1)->SetFrameDecorationValues( |
| 178 CreateDefaultFrameDecorationValues()); |
| 179 |
| 180 UserDisplayManagerTestApi(user_display_manager1) |
| 181 .SetTestObserver(&display_manager_observer1); |
| 182 EXPECT_EQ("OnDisplays 1", |
| 183 display_manager_observer1.GetAndClearObserverCalls()); |
| 184 |
| 185 UserDisplayManagerTestApi(user_display_manager1).SetTestObserver(nullptr); |
| 186 } |
| 187 |
| 188 TEST_F(UserDisplayManagerTest, AddRemoveDisplay) { |
| 189 connection_manager_delegate_.set_num_displays_to_create(1); |
| 190 |
| 191 const UserId kUserId1 = "2"; |
| 192 TestDisplayManagerObserver display_manager_observer1; |
| 193 DisplayManager* display_manager = connection_manager_->display_manager(); |
| 194 WindowManagerFactoryRegistryTestApi( |
| 195 connection_manager_->window_manager_factory_registry()) |
| 196 .AddService(kUserId1, &test_window_manager_factory_); |
| 197 UserDisplayManager* user_display_manager1 = |
| 198 display_manager->GetUserDisplayManager(kUserId1); |
| 199 ASSERT_TRUE(user_display_manager1); |
| 200 ASSERT_EQ(1u, display_manager->displays().size()); |
| 201 Display* display1 = *display_manager->displays().begin(); |
| 202 display1->GetWindowManagerStateForUser(kUserId1)->SetFrameDecorationValues( |
| 203 CreateDefaultFrameDecorationValues()); |
| 204 UserDisplayManagerTestApi(user_display_manager1) |
| 205 .SetTestObserver(&display_manager_observer1); |
| 206 EXPECT_EQ("OnDisplays 1", |
| 207 display_manager_observer1.GetAndClearObserverCalls()); |
| 208 |
| 209 // Add another display. |
| 210 Display* display2 = |
| 211 new Display(connection_manager_.get(), nullptr, scoped_refptr<GpuState>(), |
| 212 scoped_refptr<mus::SurfacesState>()); |
| 213 display2->Init(nullptr); |
| 214 |
| 215 // Observer should not have been notified yet as frame decorations not set. |
| 216 EXPECT_EQ("", display_manager_observer1.GetAndClearObserverCalls()); |
| 217 display2->GetWindowManagerStateForUser(kUserId1)->SetFrameDecorationValues( |
| 218 CreateDefaultFrameDecorationValues()); |
| 219 EXPECT_EQ("OnDisplaysChanged 2", |
| 220 display_manager_observer1.GetAndClearObserverCalls()); |
| 221 |
| 222 // Remove the display and verify observer is notified. |
| 223 display_manager->DestroyDisplay(display2); |
| 224 display2 = nullptr; |
| 225 EXPECT_EQ("OnDisplayRemoved 2", |
| 226 display_manager_observer1.GetAndClearObserverCalls()); |
| 227 |
| 228 UserDisplayManagerTestApi(user_display_manager1).SetTestObserver(nullptr); |
| 229 } |
| 230 |
| 231 } // namespace test |
| 232 } // namespace ws |
| 233 } // namespace mus |
| OLD | NEW |