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/display/display_manager.h" | 5 #include "ash/display/display_manager.h" |
6 | 6 |
| 7 #include "ash/accelerometer/accelerometer_controller.h" |
7 #include "ash/display/display_controller.h" | 8 #include "ash/display/display_controller.h" |
8 #include "ash/display/display_layout_store.h" | 9 #include "ash/display/display_layout_store.h" |
9 #include "ash/screen_util.h" | 10 #include "ash/screen_util.h" |
10 #include "ash/shell.h" | 11 #include "ash/shell.h" |
11 #include "ash/test/ash_test_base.h" | 12 #include "ash/test/ash_test_base.h" |
12 #include "ash/test/display_manager_test_api.h" | 13 #include "ash/test/display_manager_test_api.h" |
13 #include "ash/test/mirror_window_test_api.h" | 14 #include "ash/test/mirror_window_test_api.h" |
14 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "ui/aura/env.h" | 18 #include "ui/aura/env.h" |
18 #include "ui/aura/test/event_generator.h" | 19 #include "ui/aura/test/event_generator.h" |
19 #include "ui/aura/window_observer.h" | 20 #include "ui/aura/window_observer.h" |
20 #include "ui/aura/window_tree_host.h" | 21 #include "ui/aura/window_tree_host.h" |
21 #include "ui/gfx/display_observer.h" | 22 #include "ui/gfx/display_observer.h" |
22 #include "ui/gfx/display.h" | 23 #include "ui/gfx/display.h" |
23 #include "ui/gfx/screen.h" | 24 #include "ui/gfx/screen.h" |
24 #include "ui/gfx/screen_type_delegate.h" | 25 #include "ui/gfx/screen_type_delegate.h" |
| 26 #include "ui/gfx/vector3d_f.h" |
25 | 27 |
26 namespace ash { | 28 namespace ash { |
27 namespace internal { | 29 namespace internal { |
28 | 30 |
29 using std::vector; | 31 using std::vector; |
30 using std::string; | 32 using std::string; |
31 | 33 |
32 using base::StringPrintf; | 34 using base::StringPrintf; |
33 | 35 |
34 namespace { | 36 namespace { |
35 | 37 |
36 std::string ToDisplayName(int64 id) { | 38 std::string ToDisplayName(int64 id) { |
37 return "x-" + base::Int64ToString(id); | 39 return "x-" + base::Int64ToString(id); |
38 } | 40 } |
39 | 41 |
| 42 const float DegreesToRadians(float degrees) { |
| 43 return degrees * 3.1415926f / 180.0f; |
| 44 } |
| 45 |
40 } // namespace | 46 } // namespace |
41 | 47 |
42 class DisplayManagerTest : public test::AshTestBase, | 48 class DisplayManagerTest : public test::AshTestBase, |
43 public gfx::DisplayObserver, | 49 public gfx::DisplayObserver, |
44 public aura::WindowObserver { | 50 public aura::WindowObserver { |
45 public: | 51 public: |
46 DisplayManagerTest() | 52 DisplayManagerTest() |
47 : removed_count_(0U), | 53 : removed_count_(0U), |
48 root_window_destroyed_(false) { | 54 root_window_destroyed_(false) { |
49 } | 55 } |
50 virtual ~DisplayManagerTest() {} | 56 virtual ~DisplayManagerTest() {} |
51 | 57 |
52 virtual void SetUp() OVERRIDE { | 58 virtual void SetUp() OVERRIDE { |
53 AshTestBase::SetUp(); | 59 AshTestBase::SetUp(); |
54 Shell::GetScreen()->AddObserver(this); | 60 Shell::GetScreen()->AddObserver(this); |
55 Shell::GetPrimaryRootWindow()->AddObserver(this); | 61 Shell::GetPrimaryRootWindow()->AddObserver(this); |
| 62 |
| 63 // Set the primary display to be the internal display for the accelerometer |
| 64 // tests. |
| 65 gfx::Display::SetInternalDisplayId(display_manager()->GetDisplayAt(0).id()); |
| 66 |
| 67 // Remove the accelerometer observer so that tests can deliver test values. |
| 68 Shell::GetInstance()->accelerometer_controller()->RemoveObserver( |
| 69 display_manager()); |
56 } | 70 } |
57 virtual void TearDown() OVERRIDE { | 71 virtual void TearDown() OVERRIDE { |
| 72 Shell::GetInstance()->accelerometer_controller()->AddObserver( |
| 73 display_manager()); |
58 Shell::GetPrimaryRootWindow()->RemoveObserver(this); | 74 Shell::GetPrimaryRootWindow()->RemoveObserver(this); |
59 Shell::GetScreen()->RemoveObserver(this); | 75 Shell::GetScreen()->RemoveObserver(this); |
60 AshTestBase::TearDown(); | 76 AshTestBase::TearDown(); |
61 } | 77 } |
62 | 78 |
63 DisplayManager* display_manager() { | 79 DisplayManager* display_manager() { |
64 return Shell::GetInstance()->display_manager(); | 80 return Shell::GetInstance()->display_manager(); |
65 } | 81 } |
| 82 const DisplayManager* display_manager() const { |
| 83 return Shell::GetInstance()->display_manager(); |
| 84 } |
66 const vector<gfx::Display>& changed() const { return changed_; } | 85 const vector<gfx::Display>& changed() const { return changed_; } |
67 const vector<gfx::Display>& added() const { return added_; } | 86 const vector<gfx::Display>& added() const { return added_; } |
68 | 87 |
69 string GetCountSummary() const { | 88 string GetCountSummary() const { |
70 return StringPrintf("%" PRIuS " %" PRIuS " %" PRIuS, | 89 return StringPrintf("%" PRIuS " %" PRIuS " %" PRIuS, |
71 changed_.size(), added_.size(), removed_count_); | 90 changed_.size(), added_.size(), removed_count_); |
72 } | 91 } |
73 | 92 |
74 void reset() { | 93 void reset() { |
75 changed_.clear(); | 94 changed_.clear(); |
76 added_.clear(); | 95 added_.clear(); |
77 removed_count_ = 0U; | 96 removed_count_ = 0U; |
78 root_window_destroyed_ = false; | 97 root_window_destroyed_ = false; |
79 } | 98 } |
80 | 99 |
| 100 void TriggerAccelerometerUpdate(const gfx::Vector3dF& base, |
| 101 const gfx::Vector3dF& lid) { |
| 102 display_manager()->OnAccelerometerUpdated(base, lid); |
| 103 } |
| 104 |
| 105 gfx::Display::Rotation GetInternalDisplayRotation() const { |
| 106 return display_manager()->GetDisplayInfo( |
| 107 gfx::Display::InternalDisplayId()).rotation(); |
| 108 } |
| 109 |
81 bool root_window_destroyed() const { | 110 bool root_window_destroyed() const { |
82 return root_window_destroyed_; | 111 return root_window_destroyed_; |
83 } | 112 } |
84 | 113 |
85 const DisplayInfo& GetDisplayInfo(const gfx::Display& display) { | 114 const DisplayInfo& GetDisplayInfo(const gfx::Display& display) { |
86 return display_manager()->GetDisplayInfo(display.id()); | 115 return display_manager()->GetDisplayInfo(display.id()); |
87 } | 116 } |
88 | 117 |
89 const DisplayInfo& GetDisplayInfoAt(int index) { | 118 const DisplayInfo& GetDisplayInfoAt(int index) { |
90 return GetDisplayInfo(display_manager()->GetDisplayAt(index)); | 119 return GetDisplayInfo(display_manager()->GetDisplayAt(index)); |
(...skipping 1099 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1190 EXPECT_EQ("300x400", host1->GetBounds().size().ToString()); | 1219 EXPECT_EQ("300x400", host1->GetBounds().size().ToString()); |
1191 | 1220 |
1192 UpdateDisplay("100+200-100x200,300+500-200x300"); | 1221 UpdateDisplay("100+200-100x200,300+500-200x300"); |
1193 ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays()); | 1222 ASSERT_EQ(2, Shell::GetScreen()->GetNumDisplays()); |
1194 EXPECT_EQ("100,200", host0->GetBounds().origin().ToString()); | 1223 EXPECT_EQ("100,200", host0->GetBounds().origin().ToString()); |
1195 EXPECT_EQ("100x200", host0->GetBounds().size().ToString()); | 1224 EXPECT_EQ("100x200", host0->GetBounds().size().ToString()); |
1196 EXPECT_EQ("300,500", host1->GetBounds().origin().ToString()); | 1225 EXPECT_EQ("300,500", host1->GetBounds().origin().ToString()); |
1197 EXPECT_EQ("200x300", host1->GetBounds().size().ToString()); | 1226 EXPECT_EQ("200x300", host1->GetBounds().size().ToString()); |
1198 } | 1227 } |
1199 | 1228 |
| 1229 // Tests that accelerometer readings in each of the screen angles will trigger |
| 1230 // a rotation of the internal display. |
| 1231 TEST_F(DisplayManagerTest, AccelerometerRotation) { |
| 1232 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 1.0f, 0.0f), |
| 1233 gfx::Vector3dF(0.0f, 1.0f, 0.0f)); |
| 1234 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation()); |
| 1235 TriggerAccelerometerUpdate(gfx::Vector3dF(1.0f, 0.0f, 0.0f), |
| 1236 gfx::Vector3dF(1.0f, 0.0f, 0.0f)); |
| 1237 EXPECT_EQ(gfx::Display::ROTATE_180, GetInternalDisplayRotation()); |
| 1238 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -1.0f, 0.0f), |
| 1239 gfx::Vector3dF(0.0f, -1.0f, 0.0f)); |
| 1240 EXPECT_EQ(gfx::Display::ROTATE_270, GetInternalDisplayRotation()); |
| 1241 TriggerAccelerometerUpdate(gfx::Vector3dF(-1.0f, 0.0f, 0.0f), |
| 1242 gfx::Vector3dF(-1.0f, 0.0f, 0.0f)); |
| 1243 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1244 } |
| 1245 |
| 1246 // Tests that low angles are ignored by the accelerometer (i.e. when the device |
| 1247 // is almost laying flat). |
| 1248 TEST_F(DisplayManagerTest, AccelerometerIgnoresLowAngles) { |
| 1249 TriggerAccelerometerUpdate(gfx::Vector3dF(-1.0f, 0.0f, -1.0f), |
| 1250 gfx::Vector3dF(-1.0f, 0.0f, -1.0f)); |
| 1251 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1252 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.2f, -1.0f), |
| 1253 gfx::Vector3dF(0.0f, 0.2f, -1.0f)); |
| 1254 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1255 TriggerAccelerometerUpdate(gfx::Vector3dF(0.2f, 0.0f, -1.0f), |
| 1256 gfx::Vector3dF(0.2f, 0.0f, -1.0f)); |
| 1257 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1258 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -0.2f, -1.0f), |
| 1259 gfx::Vector3dF(0.0f, -0.2f, -1.0f)); |
| 1260 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1261 TriggerAccelerometerUpdate(gfx::Vector3dF(-0.2f, 0.0f, -1.0f), |
| 1262 gfx::Vector3dF(-0.2f, 0.0f, -1.0f)); |
| 1263 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1264 } |
| 1265 |
| 1266 // Tests that the display will stick to the current orientation beyond the |
| 1267 // halfway point, preventing frequent updates back and forth. |
| 1268 TEST_F(DisplayManagerTest, AccelerometerSticky) { |
| 1269 gfx::Vector3dF gravity(-1.0f, 0.0f, 0.0f); |
| 1270 TriggerAccelerometerUpdate(gravity, gravity); |
| 1271 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1272 |
| 1273 // Turn past half-way point to next direction and rotation should remain |
| 1274 // the same. |
| 1275 float degrees = 50.0; |
| 1276 gravity.set_x(-cos(DegreesToRadians(degrees))); |
| 1277 gravity.set_y(sin(DegreesToRadians(degrees))); |
| 1278 TriggerAccelerometerUpdate(gravity, gravity); |
| 1279 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); |
| 1280 |
| 1281 // Turn more and the screen should rotate. |
| 1282 degrees = 70.0; |
| 1283 gravity.set_x(-cos(DegreesToRadians(degrees))); |
| 1284 gravity.set_y(sin(DegreesToRadians(degrees))); |
| 1285 TriggerAccelerometerUpdate(gravity, gravity); |
| 1286 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation()); |
| 1287 |
| 1288 // Turn back just beyond the half-way point and the new rotation should |
| 1289 // still be in effect. |
| 1290 degrees = 40.0; |
| 1291 gravity.set_x(-cos(DegreesToRadians(degrees))); |
| 1292 gravity.set_y(sin(DegreesToRadians(degrees))); |
| 1293 TriggerAccelerometerUpdate(gravity, gravity); |
| 1294 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation()); |
| 1295 } |
1200 | 1296 |
1201 class ScreenShutdownTest : public test::AshTestBase { | 1297 class ScreenShutdownTest : public test::AshTestBase { |
1202 public: | 1298 public: |
1203 ScreenShutdownTest() { | 1299 ScreenShutdownTest() { |
1204 } | 1300 } |
1205 virtual ~ScreenShutdownTest() {} | 1301 virtual ~ScreenShutdownTest() {} |
1206 | 1302 |
1207 virtual void TearDown() OVERRIDE { | 1303 virtual void TearDown() OVERRIDE { |
1208 gfx::Screen* orig_screen = | 1304 gfx::Screen* orig_screen = |
1209 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE); | 1305 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE); |
(...skipping 15 matching lines...) Expand all Loading... |
1225 }; | 1321 }; |
1226 | 1322 |
1227 TEST_F(ScreenShutdownTest, ScreenAfterShutdown) { | 1323 TEST_F(ScreenShutdownTest, ScreenAfterShutdown) { |
1228 if (!SupportsMultipleDisplays()) | 1324 if (!SupportsMultipleDisplays()) |
1229 return; | 1325 return; |
1230 UpdateDisplay("500x300,800x400"); | 1326 UpdateDisplay("500x300,800x400"); |
1231 } | 1327 } |
1232 | 1328 |
1233 } // namespace internal | 1329 } // namespace internal |
1234 } // namespace ash | 1330 } // namespace ash |
OLD | NEW |