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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_tabstrip.h" | 9 #include "chrome/browser/ui/browser_tabstrip.h" |
10 #include "chrome/test/base/in_process_browser_test.h" | 10 #include "chrome/test/base/in_process_browser_test.h" |
11 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
| 12 #include "content/browser/device_orientation/motion.h" |
12 #include "content/browser/device_orientation/orientation.h" | 13 #include "content/browser/device_orientation/orientation.h" |
13 #include "content/browser/device_orientation/provider.h" | 14 #include "content/browser/device_orientation/provider.h" |
14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
15 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
16 | 17 |
17 namespace device_orientation { | 18 namespace device_orientation { |
18 | 19 |
19 class MockProvider : public Provider { | 20 class MockProvider : public Provider { |
20 public: | 21 public: |
21 explicit MockProvider(const Orientation& orientation) | 22 explicit MockProvider(const Motion& motion, const Orientation& orientation) |
22 : orientation_(orientation), | 23 : motion_(motion), |
23 added_observer_(false), | 24 orientation_(orientation), |
24 removed_observer_(false) {} | 25 added_motion_observer_(false), |
| 26 added_orientation_observer_(false), |
| 27 removed_motion_observer_(false), |
| 28 removed_orientation_observer_(false) {} |
25 | 29 |
26 virtual void AddObserver(Observer* observer) { | 30 virtual void AddMotionObserver(MotionObserver* observer) { |
27 added_observer_ = true; | 31 added_motion_observer_ = true; |
| 32 observer->OnMotionUpdate(motion_); |
| 33 } |
| 34 virtual void AddOrientationObserver(OrientationObserver* observer) { |
| 35 added_orientation_observer_ = true; |
28 observer->OnOrientationUpdate(orientation_); | 36 observer->OnOrientationUpdate(orientation_); |
29 } | 37 } |
30 virtual void RemoveObserver(Observer* observer) { | 38 virtual void RemoveMotionObserver(MotionObserver* observer) { |
31 removed_observer_ = true; | 39 removed_motion_observer_ = true; |
| 40 } |
| 41 virtual void RemoveOrientationObserver(OrientationObserver* observer) { |
| 42 removed_orientation_observer_ = true; |
32 } | 43 } |
33 | 44 |
| 45 Motion motion_; |
34 Orientation orientation_; | 46 Orientation orientation_; |
35 bool added_observer_; | 47 bool added_motion_observer_; |
36 bool removed_observer_; | 48 bool added_orientation_observer_; |
| 49 bool removed_motion_observer_; |
| 50 bool removed_orientation_observer_; |
37 | 51 |
38 private: | 52 private: |
39 virtual ~MockProvider() {} | 53 virtual ~MockProvider() {} |
40 }; | 54 }; |
41 | 55 |
42 class DeviceOrientationBrowserTest : public InProcessBrowserTest { | 56 class DeviceOrientationBrowserTest : public InProcessBrowserTest { |
43 public: | 57 public: |
44 // From InProcessBrowserTest. | 58 // From InProcessBrowserTest. |
45 virtual void SetUpCommandLine(CommandLine* command_line) { | 59 virtual void SetUpCommandLine(CommandLine* command_line) { |
46 EXPECT_TRUE(!command_line->HasSwitch(switches::kDisableDeviceOrientation)); | 60 EXPECT_TRUE(!command_line->HasSwitch(switches::kDisableDeviceOrientation)); |
47 } | 61 } |
48 | 62 |
49 GURL testUrl(const FilePath::CharType* filename) { | 63 GURL testUrl(const FilePath::CharType* filename) { |
50 const FilePath kTestDir(FILE_PATH_LITERAL("device_orientation")); | 64 const FilePath kTestDir(FILE_PATH_LITERAL("device_orientation")); |
51 return ui_test_utils::GetTestUrl(kTestDir, FilePath(filename)); | 65 return ui_test_utils::GetTestUrl(kTestDir, FilePath(filename)); |
52 } | 66 } |
53 }; | 67 }; |
54 | 68 |
55 // crbug.com/113952 | 69 IN_PROC_BROWSER_TEST_F(DeviceOrientationBrowserTest, BasicMotionTest) { |
56 IN_PROC_BROWSER_TEST_F(DeviceOrientationBrowserTest, BasicTest) { | 70 const Orientation kTestOrientation; |
57 const Orientation kTestOrientation(true, 1, true, 2, true, 3, true, true); | 71 Motion test_motion; |
58 scoped_refptr<MockProvider> provider(new MockProvider(kTestOrientation)); | 72 test_motion.set_acceleration_x(1); |
| 73 test_motion.set_acceleration_y(2); |
| 74 test_motion.set_acceleration_z(3); |
| 75 test_motion.set_acceleration_including_gravity_x(4); |
| 76 test_motion.set_acceleration_including_gravity_y(5); |
| 77 test_motion.set_acceleration_including_gravity_z(6); |
| 78 test_motion.set_rotation_rate_alpha(7); |
| 79 test_motion.set_rotation_rate_beta(8); |
| 80 test_motion.set_rotation_rate_gamma(9); |
| 81 test_motion.set_interval(10); |
| 82 scoped_refptr<MockProvider> provider( |
| 83 new MockProvider(test_motion, kTestOrientation)); |
| 84 Provider::SetInstanceForTests(provider.get()); |
| 85 |
| 86 // The test page will register an event handler for motion events, |
| 87 // expects to get an event with test_motion motion, |
| 88 // then removes the event handler and navigates to #pass. |
| 89 GURL test_url = testUrl(FILE_PATH_LITERAL("device_motion_test.html")); |
| 90 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), |
| 91 test_url, |
| 92 2); |
| 93 |
| 94 // Check that the page got the event it expected and that the provider |
| 95 // saw requests for adding and removing an observer. |
| 96 EXPECT_EQ("pass", chrome::GetActiveWebContents(browser())->GetURL().ref()); |
| 97 EXPECT_TRUE(provider->added_motion_observer_); |
| 98 EXPECT_TRUE(provider->removed_motion_observer_); |
| 99 } |
| 100 |
| 101 IN_PROC_BROWSER_TEST_F(DeviceOrientationBrowserTest, BasicOrientationTest) { |
| 102 const Motion kMotion; |
| 103 const Orientation test_orientation(true, 1, true, 2, true, 3, true, true); |
| 104 scoped_refptr<MockProvider> provider( |
| 105 new MockProvider(kMotion, test_orientation)); |
59 Provider::SetInstanceForTests(provider.get()); | 106 Provider::SetInstanceForTests(provider.get()); |
60 | 107 |
61 // The test page will register an event handler for orientation events, | 108 // The test page will register an event handler for orientation events, |
62 // expects to get an event with kTestOrientation orientation, | 109 // expects to get an event with test_orientation orientation, |
63 // then removes the event handler and navigates to #pass. | 110 // then removes the event handler and navigates to #pass. |
64 GURL test_url = testUrl(FILE_PATH_LITERAL("device_orientation_test.html")); | 111 GURL test_url = testUrl(FILE_PATH_LITERAL("device_orientation_test.html")); |
65 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), | 112 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), |
66 test_url, | 113 test_url, |
67 2); | 114 2); |
68 | 115 |
69 // Check that the page got the event it expected and that the provider | 116 // Check that the page got the event it expected and that the provider |
70 // saw requests for adding and removing an observer. | 117 // saw requests for adding and removing an observer. |
71 EXPECT_EQ("pass", chrome::GetActiveWebContents(browser())->GetURL().ref()); | 118 EXPECT_EQ("pass", chrome::GetActiveWebContents(browser())->GetURL().ref()); |
72 EXPECT_TRUE(provider->added_observer_); | 119 EXPECT_TRUE(provider->added_orientation_observer_); |
73 EXPECT_TRUE(provider->removed_observer_); | 120 EXPECT_TRUE(provider->removed_orientation_observer_); |
74 } | 121 } |
75 | 122 |
76 } // namespace device_orientation | 123 } // namespace device_orientation |
OLD | NEW |