| OLD | NEW |
| (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 <X11/extensions/Xrandr.h> | |
| 6 | |
| 7 #undef Bool | |
| 8 #undef None | |
| 9 | |
| 10 #include "chromeos/display/native_display_delegate_x11.h" | |
| 11 #include "chromeos/display/native_display_event_dispatcher_x11.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 OutputConfigurator::OutputSnapshot CreateOutput( | |
| 19 RROutput output, RRCrtc crtc, RRMode mode) { | |
| 20 OutputConfigurator::OutputSnapshot snapshot; | |
| 21 snapshot.output = output; | |
| 22 snapshot.crtc = crtc; | |
| 23 snapshot.current_mode = mode; | |
| 24 | |
| 25 return snapshot; | |
| 26 } | |
| 27 | |
| 28 class TestHelperDelegate : public NativeDisplayDelegateX11::HelperDelegate { | |
| 29 public: | |
| 30 TestHelperDelegate(); | |
| 31 virtual ~TestHelperDelegate(); | |
| 32 | |
| 33 int num_calls_update_xrandr_config() const { | |
| 34 return num_calls_update_xrandr_config_; | |
| 35 } | |
| 36 | |
| 37 int num_calls_notify_observers() const { | |
| 38 return num_calls_notify_observers_; | |
| 39 } | |
| 40 | |
| 41 void set_cached_outputs( | |
| 42 const std::vector<OutputConfigurator::OutputSnapshot>& outputs) { | |
| 43 cached_outputs_ = outputs; | |
| 44 } | |
| 45 | |
| 46 // NativeDisplayDelegateX11::HelperDelegate overrides: | |
| 47 virtual void UpdateXRandRConfiguration( | |
| 48 const base::NativeEvent& event) OVERRIDE; | |
| 49 virtual const std::vector<OutputConfigurator::OutputSnapshot>& | |
| 50 GetCachedOutputs() const OVERRIDE; | |
| 51 virtual void NotifyDisplayObservers() OVERRIDE; | |
| 52 | |
| 53 private: | |
| 54 int num_calls_update_xrandr_config_; | |
| 55 int num_calls_notify_observers_; | |
| 56 | |
| 57 std::vector<OutputConfigurator::OutputSnapshot> cached_outputs_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(TestHelperDelegate); | |
| 60 }; | |
| 61 | |
| 62 TestHelperDelegate::TestHelperDelegate() | |
| 63 : num_calls_update_xrandr_config_(0), | |
| 64 num_calls_notify_observers_(0) {} | |
| 65 | |
| 66 TestHelperDelegate::~TestHelperDelegate() {} | |
| 67 | |
| 68 void TestHelperDelegate::UpdateXRandRConfiguration( | |
| 69 const base::NativeEvent& event) { | |
| 70 ++num_calls_update_xrandr_config_; | |
| 71 } | |
| 72 | |
| 73 const std::vector<OutputConfigurator::OutputSnapshot>& | |
| 74 TestHelperDelegate::GetCachedOutputs() const { | |
| 75 return cached_outputs_; | |
| 76 } | |
| 77 | |
| 78 void TestHelperDelegate::NotifyDisplayObservers() { | |
| 79 ++num_calls_notify_observers_; | |
| 80 } | |
| 81 | |
| 82 //////////////////////////////////////////////////////////////////////////////// | |
| 83 // NativeDisplayEventDispatcherX11Test | |
| 84 | |
| 85 class NativeDisplayEventDispatcherX11Test : public testing::Test { | |
| 86 public: | |
| 87 NativeDisplayEventDispatcherX11Test(); | |
| 88 virtual ~NativeDisplayEventDispatcherX11Test(); | |
| 89 | |
| 90 protected: | |
| 91 void DispatchScreenChangeEvent(); | |
| 92 void DispatchOutputChangeEvent( | |
| 93 RROutput output, RRCrtc crtc, RRMode mode, bool connected); | |
| 94 | |
| 95 int xrandr_event_base_; | |
| 96 scoped_ptr<TestHelperDelegate> helper_delegate_; | |
| 97 scoped_ptr<NativeDisplayEventDispatcherX11> dispatcher_; | |
| 98 | |
| 99 private: | |
| 100 DISALLOW_COPY_AND_ASSIGN(NativeDisplayEventDispatcherX11Test); | |
| 101 }; | |
| 102 | |
| 103 NativeDisplayEventDispatcherX11Test::NativeDisplayEventDispatcherX11Test() | |
| 104 : xrandr_event_base_(10), | |
| 105 helper_delegate_(new TestHelperDelegate()), | |
| 106 dispatcher_(new NativeDisplayEventDispatcherX11(helper_delegate_.get(), | |
| 107 xrandr_event_base_)) { | |
| 108 } | |
| 109 | |
| 110 NativeDisplayEventDispatcherX11Test::~NativeDisplayEventDispatcherX11Test() {} | |
| 111 | |
| 112 void NativeDisplayEventDispatcherX11Test::DispatchScreenChangeEvent() { | |
| 113 XRRScreenChangeNotifyEvent event = {0}; | |
| 114 event.type = xrandr_event_base_ + RRScreenChangeNotify; | |
| 115 | |
| 116 dispatcher_->Dispatch(reinterpret_cast<const base::NativeEvent>(&event)); | |
| 117 } | |
| 118 | |
| 119 void NativeDisplayEventDispatcherX11Test::DispatchOutputChangeEvent( | |
| 120 RROutput output, RRCrtc crtc, RRMode mode, bool connected) { | |
| 121 XRROutputChangeNotifyEvent event = {0}; | |
| 122 event.type = xrandr_event_base_ + RRNotify; | |
| 123 event.subtype = RRNotify_OutputChange; | |
| 124 event.output = output; | |
| 125 event.crtc = crtc; | |
| 126 event.mode = mode; | |
| 127 event.connection = connected ? RR_Connected : RR_Disconnected; | |
| 128 | |
| 129 dispatcher_->Dispatch(reinterpret_cast<const base::NativeEvent>(&event)); | |
| 130 } | |
| 131 | |
| 132 } // namespace | |
| 133 | |
| 134 TEST_F(NativeDisplayEventDispatcherX11Test, OnScreenChangedEvent) { | |
| 135 DispatchScreenChangeEvent(); | |
| 136 EXPECT_EQ(1, helper_delegate_->num_calls_update_xrandr_config()); | |
| 137 EXPECT_EQ(0, helper_delegate_->num_calls_notify_observers()); | |
| 138 } | |
| 139 | |
| 140 TEST_F(NativeDisplayEventDispatcherX11Test, CheckNotificationOnFirstEvent) { | |
| 141 DispatchOutputChangeEvent(1, 10, 20, true); | |
| 142 EXPECT_EQ(0, helper_delegate_->num_calls_update_xrandr_config()); | |
| 143 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 144 } | |
| 145 | |
| 146 TEST_F(NativeDisplayEventDispatcherX11Test, CheckNotificationAfterSecondEvent) { | |
| 147 DispatchOutputChangeEvent(1, 10, 20, true); | |
| 148 | |
| 149 // Simulate addition of the first output to the cached output list. | |
| 150 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 151 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 152 helper_delegate_->set_cached_outputs(outputs); | |
| 153 | |
| 154 DispatchOutputChangeEvent(2, 11, 20, true); | |
| 155 EXPECT_EQ(2, helper_delegate_->num_calls_notify_observers()); | |
| 156 } | |
| 157 | |
| 158 TEST_F(NativeDisplayEventDispatcherX11Test, AvoidNotificationOnDuplicateEvent) { | |
| 159 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 160 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 161 helper_delegate_->set_cached_outputs(outputs); | |
| 162 | |
| 163 DispatchOutputChangeEvent(1, 10, 20, true); | |
| 164 EXPECT_EQ(0, helper_delegate_->num_calls_notify_observers()); | |
| 165 } | |
| 166 | |
| 167 TEST_F(NativeDisplayEventDispatcherX11Test, CheckNotificationOnDisconnect) { | |
| 168 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 169 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 170 helper_delegate_->set_cached_outputs(outputs); | |
| 171 | |
| 172 DispatchOutputChangeEvent(1, 10, 20, false); | |
| 173 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 174 } | |
| 175 | |
| 176 TEST_F(NativeDisplayEventDispatcherX11Test, CheckNotificationOnModeChange) { | |
| 177 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 178 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 179 helper_delegate_->set_cached_outputs(outputs); | |
| 180 | |
| 181 DispatchOutputChangeEvent(1, 10, 21, true); | |
| 182 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 183 } | |
| 184 | |
| 185 TEST_F(NativeDisplayEventDispatcherX11Test, CheckNotificationOnSecondOutput) { | |
| 186 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 187 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 188 helper_delegate_->set_cached_outputs(outputs); | |
| 189 | |
| 190 DispatchOutputChangeEvent(2, 11, 20, true); | |
| 191 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 192 } | |
| 193 | |
| 194 TEST_F(NativeDisplayEventDispatcherX11Test, CheckNotificationOnDifferentCrtc) { | |
| 195 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 196 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 197 helper_delegate_->set_cached_outputs(outputs); | |
| 198 | |
| 199 DispatchOutputChangeEvent(1, 11, 20, true); | |
| 200 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 201 } | |
| 202 | |
| 203 TEST_F(NativeDisplayEventDispatcherX11Test, | |
| 204 CheckNotificationOnSecondOutputDisconnect) { | |
| 205 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 206 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 207 outputs.push_back(CreateOutput(2, 11, 20)); | |
| 208 helper_delegate_->set_cached_outputs(outputs); | |
| 209 | |
| 210 DispatchOutputChangeEvent(2, 11, 20, false); | |
| 211 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 212 } | |
| 213 | |
| 214 TEST_F(NativeDisplayEventDispatcherX11Test, | |
| 215 AvoidDuplicateNotificationOnSecondOutputDisconnect) { | |
| 216 std::vector<OutputConfigurator::OutputSnapshot> outputs; | |
| 217 outputs.push_back(CreateOutput(1, 10, 20)); | |
| 218 outputs.push_back(CreateOutput(2, 11, 20)); | |
| 219 helper_delegate_->set_cached_outputs(outputs); | |
| 220 | |
| 221 DispatchOutputChangeEvent(2, 11, 20, false); | |
| 222 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 223 | |
| 224 // Simulate removal of second output from cached output list. | |
| 225 outputs.erase(outputs.begin() + 1); | |
| 226 helper_delegate_->set_cached_outputs(outputs); | |
| 227 | |
| 228 DispatchOutputChangeEvent(2, 11, 20, false); | |
| 229 EXPECT_EQ(1, helper_delegate_->num_calls_notify_observers()); | |
| 230 } | |
| 231 | |
| 232 } // namespace chromeos | |
| OLD | NEW |