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