| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/monitor/multi_monitor_manager.h" | 5 #include "ash/monitor/multi_monitor_manager.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "ui/aura/env.h" | 13 #include "ui/aura/env.h" |
| 14 #include "ui/aura/monitor.h" | 14 #include "ui/aura/monitor.h" |
| 15 #include "ui/aura/root_window.h" | 15 #include "ui/aura/root_window.h" |
| 16 #include "ui/aura/window_observer.h" |
| 16 | 17 |
| 17 namespace ash { | 18 namespace ash { |
| 18 namespace test { | 19 namespace test { |
| 19 | 20 |
| 20 using std::vector; | 21 using std::vector; |
| 21 using std::string; | 22 using std::string; |
| 22 using aura::Monitor; | 23 using aura::Monitor; |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 vector<const aura::Monitor*> CreateMonitorsFromString( | 27 vector<const aura::Monitor*> CreateMonitorsFromString( |
| 27 const std::string specs) { | 28 const std::string specs) { |
| 28 vector<const aura::Monitor*> monitors; | 29 vector<const aura::Monitor*> monitors; |
| 29 vector<string> parts; | 30 vector<string> parts; |
| 30 base::SplitString(specs, ',', &parts); | 31 base::SplitString(specs, ',', &parts); |
| 31 for (vector<string>::const_iterator iter = parts.begin(); | 32 for (vector<string>::const_iterator iter = parts.begin(); |
| 32 iter != parts.end(); ++iter) { | 33 iter != parts.end(); ++iter) { |
| 33 monitors.push_back(aura::MonitorManager::CreateMonitorFromSpec(*iter)); | 34 monitors.push_back(aura::MonitorManager::CreateMonitorFromSpec(*iter)); |
| 34 } | 35 } |
| 35 return monitors; | 36 return monitors; |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 class MultiMonitorManagerTest : public test::AshTestBase, | 41 class MultiMonitorManagerTest : public test::AshTestBase, |
| 41 public aura::MonitorObserver { | 42 public aura::MonitorObserver, |
| 43 public aura::WindowObserver { |
| 42 public: | 44 public: |
| 43 MultiMonitorManagerTest() : removed_count_(0U) {} | 45 MultiMonitorManagerTest() |
| 46 : removed_count_(0U), |
| 47 root_window_destroyed_(false) { |
| 48 } |
| 44 virtual ~MultiMonitorManagerTest() {} | 49 virtual ~MultiMonitorManagerTest() {} |
| 45 | 50 |
| 46 virtual void SetUp() OVERRIDE { | 51 virtual void SetUp() OVERRIDE { |
| 47 AshTestBase::SetUp(); | 52 AshTestBase::SetUp(); |
| 48 monitor_manager()->AddObserver(this); | 53 monitor_manager()->AddObserver(this); |
| 54 Shell::GetRootWindow()->AddObserver(this); |
| 49 } | 55 } |
| 50 virtual void TearDown() OVERRIDE { | 56 virtual void TearDown() OVERRIDE { |
| 57 Shell::GetRootWindow()->RemoveObserver(this); |
| 51 monitor_manager()->RemoveObserver(this); | 58 monitor_manager()->RemoveObserver(this); |
| 52 AshTestBase::TearDown(); | 59 AshTestBase::TearDown(); |
| 53 } | 60 } |
| 54 | 61 |
| 55 aura::MonitorManager* monitor_manager() { | 62 aura::MonitorManager* monitor_manager() { |
| 56 return aura::Env::GetInstance()->monitor_manager(); | 63 return aura::Env::GetInstance()->monitor_manager(); |
| 57 } | 64 } |
| 58 const vector<const Monitor*>& changed() const { return changed_; } | 65 const vector<const Monitor*>& changed() const { return changed_; } |
| 59 const vector<const Monitor*>& added() const { return added_; } | 66 const vector<const Monitor*>& added() const { return added_; } |
| 60 | 67 |
| 61 string GetCountSummary() const { | 68 string GetCountSummary() const { |
| 62 return StringPrintf("%"PRIuS" %"PRIuS" %"PRIuS, | 69 return StringPrintf("%"PRIuS" %"PRIuS" %"PRIuS, |
| 63 changed_.size(), added_.size(), removed_count_); | 70 changed_.size(), added_.size(), removed_count_); |
| 64 } | 71 } |
| 65 | 72 |
| 66 void reset() { | 73 void reset() { |
| 67 changed_.clear(); | 74 changed_.clear(); |
| 68 added_.clear(); | 75 added_.clear(); |
| 69 removed_count_ = 0U; | 76 removed_count_ = 0U; |
| 77 root_window_destroyed_ = false; |
| 78 } |
| 79 |
| 80 bool root_window_destroyed() const { |
| 81 return root_window_destroyed_; |
| 70 } | 82 } |
| 71 | 83 |
| 72 // aura::MonitorObserver overrides: | 84 // aura::MonitorObserver overrides: |
| 73 virtual void OnMonitorBoundsChanged(const Monitor* monitor) OVERRIDE { | 85 virtual void OnMonitorBoundsChanged(const Monitor* monitor) OVERRIDE { |
| 74 changed_.push_back(monitor); | 86 changed_.push_back(monitor); |
| 75 } | 87 } |
| 76 virtual void OnMonitorAdded(Monitor* new_monitor) OVERRIDE { | 88 virtual void OnMonitorAdded(Monitor* new_monitor) OVERRIDE { |
| 77 added_.push_back(new_monitor); | 89 added_.push_back(new_monitor); |
| 78 } | 90 } |
| 79 virtual void OnMonitorRemoved(const Monitor* old_monitor) OVERRIDE { | 91 virtual void OnMonitorRemoved(const Monitor* old_monitor) OVERRIDE { |
| 80 ++removed_count_; | 92 ++removed_count_; |
| 81 } | 93 } |
| 82 | 94 |
| 95 // aura::WindowObserver overrides: |
| 96 virtual void OnWindowDestroying(aura::Window* window) { |
| 97 ASSERT_EQ(Shell::GetRootWindow(), window); |
| 98 root_window_destroyed_ = true; |
| 99 } |
| 100 |
| 83 void UpdateMonitor(const std::string str) { | 101 void UpdateMonitor(const std::string str) { |
| 84 vector<const aura::Monitor*> monitors = CreateMonitorsFromString(str); | 102 vector<const aura::Monitor*> monitors = CreateMonitorsFromString(str); |
| 85 monitor_manager()->OnNativeMonitorsChanged(monitors); | 103 monitor_manager()->OnNativeMonitorsChanged(monitors); |
| 86 STLDeleteContainerPointers(monitors.begin(), monitors.end()); | 104 STLDeleteContainerPointers(monitors.begin(), monitors.end()); |
| 87 } | 105 } |
| 88 | 106 |
| 89 private: | 107 private: |
| 90 vector<const Monitor*> changed_; | 108 vector<const Monitor*> changed_; |
| 91 vector<const Monitor*> added_; | 109 vector<const Monitor*> added_; |
| 92 size_t removed_count_; | 110 size_t removed_count_; |
| 111 bool root_window_destroyed_; |
| 93 | 112 |
| 94 DISALLOW_COPY_AND_ASSIGN(MultiMonitorManagerTest); | 113 DISALLOW_COPY_AND_ASSIGN(MultiMonitorManagerTest); |
| 95 }; | 114 }; |
| 96 | 115 |
| 97 TEST_F(MultiMonitorManagerTest, NativeMonitorTest) { | 116 TEST_F(MultiMonitorManagerTest, NativeMonitorTest) { |
| 98 aura::MonitorManager::set_use_fullscreen_host_window(true); | 117 aura::MonitorManager::set_use_fullscreen_host_window(true); |
| 99 | 118 |
| 100 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); | 119 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); |
| 101 | 120 |
| 102 // Update primary and add seconary. | 121 // Update primary and add seconary. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 128 EXPECT_EQ(monitor_manager()->GetMonitorAt(1), added()[0]); | 147 EXPECT_EQ(monitor_manager()->GetMonitorAt(1), added()[0]); |
| 129 EXPECT_EQ("1001,0 600x400", added()[0]->bounds().ToString()); | 148 EXPECT_EQ("1001,0 600x400", added()[0]->bounds().ToString()); |
| 130 reset(); | 149 reset(); |
| 131 | 150 |
| 132 // Secondary removed, primary changed. | 151 // Secondary removed, primary changed. |
| 133 UpdateMonitor("0+0-800x300"); | 152 UpdateMonitor("0+0-800x300"); |
| 134 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); | 153 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); |
| 135 EXPECT_EQ("1 0 1", GetCountSummary()); | 154 EXPECT_EQ("1 0 1", GetCountSummary()); |
| 136 EXPECT_EQ(monitor_manager()->GetMonitorAt(0), changed()[0]); | 155 EXPECT_EQ(monitor_manager()->GetMonitorAt(0), changed()[0]); |
| 137 EXPECT_EQ("0,0 800x300", changed()[0]->bounds().ToString()); | 156 EXPECT_EQ("0,0 800x300", changed()[0]->bounds().ToString()); |
| 157 reset(); |
| 158 |
| 159 // # of monitor can go to zero when screen is off. |
| 160 const vector<const Monitor*> empty; |
| 161 monitor_manager()->OnNativeMonitorsChanged(empty); |
| 162 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); |
| 163 EXPECT_EQ("0 0 0", GetCountSummary()); |
| 164 EXPECT_FALSE(root_window_destroyed()); |
| 165 // Monitor configuration stays the same |
| 166 EXPECT_EQ("0,0 800x300", |
| 167 monitor_manager()->GetMonitorAt(0)->bounds().ToString()); |
| 168 reset(); |
| 169 |
| 170 // Connect to monitor again |
| 171 UpdateMonitor("100+100-500x400"); |
| 172 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); |
| 173 EXPECT_EQ("1 0 0", GetCountSummary()); |
| 174 EXPECT_FALSE(root_window_destroyed()); |
| 175 EXPECT_EQ("100,100 500x400", changed()[0]->bounds().ToString()); |
| 176 reset(); |
| 177 |
| 178 // Go back to zero and wake up with multiple monitors. |
| 179 monitor_manager()->OnNativeMonitorsChanged(empty); |
| 180 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); |
| 181 EXPECT_FALSE(root_window_destroyed()); |
| 182 reset(); |
| 183 |
| 184 // Add secondary. |
| 185 UpdateMonitor("0+0-1000x600,1000+0-600x400"); |
| 186 EXPECT_EQ(2U, monitor_manager()->GetNumMonitors()); |
| 187 EXPECT_EQ("0,0 1000x600", |
| 188 monitor_manager()->GetMonitorAt(0)->bounds().ToString()); |
| 189 EXPECT_EQ("1000,0 600x400", |
| 190 monitor_manager()->GetMonitorAt(1)->bounds().ToString()); |
| 191 reset(); |
| 138 | 192 |
| 139 aura::MonitorManager::set_use_fullscreen_host_window(false); | 193 aura::MonitorManager::set_use_fullscreen_host_window(false); |
| 140 } | 194 } |
| 141 | 195 |
| 142 // Test in emulation mode (use_fullscreen_host_window=false) | 196 // Test in emulation mode (use_fullscreen_host_window=false) |
| 143 TEST_F(MultiMonitorManagerTest, EmulatorTest) { | 197 TEST_F(MultiMonitorManagerTest, EmulatorTest) { |
| 144 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); | 198 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); |
| 145 | 199 |
| 146 internal::MultiMonitorManager::AddRemoveMonitor(); | 200 internal::MultiMonitorManager::AddRemoveMonitor(); |
| 147 // Update primary and add seconary. | 201 // Update primary and add seconary. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 169 reset(); | 223 reset(); |
| 170 | 224 |
| 171 internal::MultiMonitorManager::CycleMonitor(); | 225 internal::MultiMonitorManager::CycleMonitor(); |
| 172 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); | 226 EXPECT_EQ(1U, monitor_manager()->GetNumMonitors()); |
| 173 EXPECT_EQ("0 0 0", GetCountSummary()); | 227 EXPECT_EQ("0 0 0", GetCountSummary()); |
| 174 reset(); | 228 reset(); |
| 175 } | 229 } |
| 176 | 230 |
| 177 } // namespace test | 231 } // namespace test |
| 178 } // namespace ash | 232 } // namespace ash |
| OLD | NEW |