projecting_observer_chromeos_unittest.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 "ash/display/projecting_observer_chromeos.h" 6 7#include "base/memory/scoped_vector.h" 8#include "chromeos/dbus/fake_dbus_thread_manager.h" 9#include "chromeos/dbus/fake_power_manager_client.h" 10#include "testing/gtest/include/gtest/gtest.h" 11#include "ui/display/chromeos/test/test_display_snapshot.h" 12 13namespace ash { 14namespace { 15 16ui::TestDisplaySnapshot* CreateInternalSnapshot() { 17 ui::TestDisplaySnapshot* output = new ui::TestDisplaySnapshot(); 18 output->set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); 19 return output; 20} 21 22ui::TestDisplaySnapshot* CreateVGASnapshot() { 23 ui::TestDisplaySnapshot* output = new ui::TestDisplaySnapshot(); 24 output->set_type(ui::DISPLAY_CONNECTION_TYPE_VGA); 25 return output; 26} 27 28ui::DisplayConfigurator::DisplayStateList CreateOutputs( 29 const ScopedVector<ui::TestDisplaySnapshot>& displays) { 30 ui::DisplayConfigurator::DisplayStateList outputs; 31 for (size_t i = 0; i < displays.size(); ++i) { 32 ui::DisplayConfigurator::DisplayState state; 33 state.display = displays[i]; 34 outputs.push_back(state); 35 } 36 37 return outputs; 38} 39 40class ProjectingObserverTest : public testing::Test { 41 public: 42 ProjectingObserverTest() : observer_(new ProjectingObserver()) { 43 chromeos::FakeDBusThreadManager* dbus_manager = 44 new chromeos::FakeDBusThreadManager(); 45 fake_power_client_ = new chromeos::FakePowerManagerClient(); 46 47 dbus_manager->SetPowerManagerClient( 48 scoped_ptr<chromeos::PowerManagerClient>(fake_power_client_)); 49 50 // Takes ownership of |dbus_manager|. 51 chromeos::DBusThreadManager::InitializeForTesting(dbus_manager); 52 } 53 54 virtual ~ProjectingObserverTest() { 55 chromeos::DBusThreadManager::Shutdown(); 56 } 57 58 protected: 59 scoped_ptr<ProjectingObserver> observer_; 60 chromeos::FakePowerManagerClient* fake_power_client_; // Not owned. 61 62 DISALLOW_COPY_AND_ASSIGN(ProjectingObserverTest); 63}; 64 65} // namespace 66 67TEST_F(ProjectingObserverTest, CheckNoDisplay) { 68 ScopedVector<ui::TestDisplaySnapshot> displays; 69 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 70 observer_->OnDisplayModeChanged(outputs); 71 72 EXPECT_EQ(1, fake_power_client_->num_set_is_projecting_calls()); 73 EXPECT_FALSE(fake_power_client_->is_projecting()); 74} 75 76TEST_F(ProjectingObserverTest, CheckWithoutInternalDisplay) { 77 ScopedVector<ui::TestDisplaySnapshot> displays; 78 displays.push_back(CreateVGASnapshot()); 79 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 80 observer_->OnDisplayModeChanged(outputs); 81 82 EXPECT_EQ(1, fake_power_client_->num_set_is_projecting_calls()); 83 EXPECT_FALSE(fake_power_client_->is_projecting()); 84} 85 86TEST_F(ProjectingObserverTest, CheckWithInternalDisplay) { 87 ScopedVector<ui::TestDisplaySnapshot> displays; 88 displays.push_back(CreateInternalSnapshot()); 89 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 90 observer_->OnDisplayModeChanged(outputs); 91 92 EXPECT_EQ(1, fake_power_client_->num_set_is_projecting_calls()); 93 EXPECT_FALSE(fake_power_client_->is_projecting()); 94} 95 96TEST_F(ProjectingObserverTest, CheckWithTwoVGADisplays) { 97 ScopedVector<ui::TestDisplaySnapshot> displays; 98 displays.push_back(CreateVGASnapshot()); 99 displays.push_back(CreateVGASnapshot()); 100 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 101 observer_->OnDisplayModeChanged(outputs); 102 103 EXPECT_EQ(1, fake_power_client_->num_set_is_projecting_calls()); 104 // We need at least 1 internal display to set projecting to on. 105 EXPECT_FALSE(fake_power_client_->is_projecting()); 106} 107 108TEST_F(ProjectingObserverTest, CheckWithInternalAndVGADisplays) { 109 ScopedVector<ui::TestDisplaySnapshot> displays; 110 displays.push_back(CreateInternalSnapshot()); 111 displays.push_back(CreateVGASnapshot()); 112 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 113 observer_->OnDisplayModeChanged(outputs); 114 115 EXPECT_EQ(1, fake_power_client_->num_set_is_projecting_calls()); 116 EXPECT_TRUE(fake_power_client_->is_projecting()); 117} 118 119TEST_F(ProjectingObserverTest, CheckWithVGADisplayAndOneCastingSession) { 120 ScopedVector<ui::TestDisplaySnapshot> displays; 121 displays.push_back(CreateVGASnapshot()); 122 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 123 observer_->OnDisplayModeChanged(outputs); 124 125 observer_->OnCastingSessionStartedOrStopped(true); 126 127 EXPECT_EQ(2, fake_power_client_->num_set_is_projecting_calls()); 128 // Need at least one internal display to set projecting state to |true|. 129 EXPECT_FALSE(fake_power_client_->is_projecting()); 130} 131 132TEST_F(ProjectingObserverTest, CheckWithInternalDisplayAndOneCastingSession) { 133 ScopedVector<ui::TestDisplaySnapshot> displays; 134 displays.push_back(CreateInternalSnapshot()); 135 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 136 observer_->OnDisplayModeChanged(outputs); 137 138 observer_->OnCastingSessionStartedOrStopped(true); 139 140 EXPECT_EQ(2, fake_power_client_->num_set_is_projecting_calls()); 141 EXPECT_TRUE(fake_power_client_->is_projecting()); 142} 143 144TEST_F(ProjectingObserverTest, CheckProjectingAfterClosingACastingSession) { 145 ScopedVector<ui::TestDisplaySnapshot> displays; 146 displays.push_back(CreateInternalSnapshot()); 147 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 148 observer_->OnDisplayModeChanged(outputs); 149 150 observer_->OnCastingSessionStartedOrStopped(true); 151 observer_->OnCastingSessionStartedOrStopped(true); 152 153 EXPECT_EQ(3, fake_power_client_->num_set_is_projecting_calls()); 154 EXPECT_TRUE(fake_power_client_->is_projecting()); 155 156 observer_->OnCastingSessionStartedOrStopped(false); 157 158 EXPECT_EQ(4, fake_power_client_->num_set_is_projecting_calls()); 159 EXPECT_TRUE(fake_power_client_->is_projecting()); 160} 161 162TEST_F(ProjectingObserverTest, 163 CheckStopProjectingAfterClosingAllCastingSessions) { 164 ScopedVector<ui::TestDisplaySnapshot> displays; 165 displays.push_back(CreateInternalSnapshot()); 166 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 167 observer_->OnDisplayModeChanged(outputs); 168 169 observer_->OnCastingSessionStartedOrStopped(true); 170 observer_->OnCastingSessionStartedOrStopped(false); 171 172 EXPECT_EQ(3, fake_power_client_->num_set_is_projecting_calls()); 173 EXPECT_FALSE(fake_power_client_->is_projecting()); 174} 175 176TEST_F(ProjectingObserverTest, 177 CheckStopProjectingAfterDisconnectingSecondOutput) { 178 ScopedVector<ui::TestDisplaySnapshot> displays; 179 displays.push_back(CreateInternalSnapshot()); 180 displays.push_back(CreateVGASnapshot()); 181 ui::DisplayConfigurator::DisplayStateList outputs = CreateOutputs(displays); 182 observer_->OnDisplayModeChanged(outputs); 183 184 // Remove VGA output. 185 outputs.erase(outputs.begin() + 1); 186 observer_->OnDisplayModeChanged(outputs); 187 188 EXPECT_EQ(2, fake_power_client_->num_set_is_projecting_calls()); 189 EXPECT_FALSE(fake_power_client_->is_projecting()); 190} 191 192} // namespace ash 193