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 3 // found in the LICENSE file. 4 5 #include "base/command_line.h" 6 #include "base/files/file_path.h" 7 #include "base/memory/ref_counted.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "content/browser/device_orientation/device_data.h" 10 #include "content/browser/device_orientation/orientation.h" 11 #include "content/browser/device_orientation/provider.h" 12 #include "content/public/browser/web_contents.h" 13 #include "content/public/common/content_switches.h" 14 #include "content/shell/shell.h" 15 #include "content/test/content_browser_test.h" 16 #include "content/test/content_browser_test_utils.h" 17 18 namespace content { 19 20 class MockProvider : public Provider { 21 public: 22 MockProvider(const DeviceData* device_data, DeviceData::Type type) 23 : device_data_(device_data), 24 device_data_type_(type), 25 added_observer_(false), 26 removed_observer_(false) { 27 } 28 29 virtual void AddObserver(Observer* observer) OVERRIDE { 30 added_observer_ = true; 31 observer->OnDeviceDataUpdate(device_data_.get(), device_data_type_); 32 } 33 virtual void RemoveObserver(Observer* observer) OVERRIDE { 34 removed_observer_ = true; 35 } 36 37 scoped_refptr<const DeviceData> device_data_; 38 DeviceData::Type device_data_type_; 39 bool added_observer_; 40 bool removed_observer_; 41 42 private: 43 virtual ~MockProvider() {} 44 }; 45 46 class DeviceOrientationBrowserTest : public ContentBrowserTest { 47 public: 48 // From ContentBrowserTest. 49 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 50 EXPECT_TRUE(!command_line->HasSwitch(switches::kDisableDeviceOrientation)); 51 } 52 }; 53 54 // crbug.com/113952 55 IN_PROC_BROWSER_TEST_F(DeviceOrientationBrowserTest, BasicTest) { 56 scoped_refptr<Orientation> test_orientation(new Orientation()); 57 test_orientation->set_alpha(1); 58 test_orientation->set_beta(2); 59 test_orientation->set_gamma(3); 60 test_orientation->set_absolute(true); 61 scoped_refptr<MockProvider> provider( 62 new MockProvider(test_orientation.get(), DeviceData::kTypeOrientation)); 63 Provider::SetInstanceForTests(provider.get()); 64 65 // The test page will register an event handler for orientation events, 66 // expects to get an event with kTestOrientation orientation, 67 // then removes the event handler and navigates to #pass. 68 GURL test_url = GetTestUrl( 69 "device_orientation", "device_orientation_test.html"); 70 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 71 72 // Check that the page got the event it expected and that the provider 73 // saw requests for adding and removing an observer. 74 EXPECT_EQ("pass", shell()->web_contents()->GetURL().ref()); 75 EXPECT_TRUE(provider->added_observer_); 76 EXPECT_TRUE(provider->removed_observer_); 77 } 78 79 } // namespace content 80