Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: chrome/browser/generic_sensor_browsertest.cc

Issue 2458453002: [sensors] Add Permission guard to the generic sensor apis.
Patch Set: rebase + blink reformat Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/macros.h" 6 #include "base/macros.h"
7 #include "base/memory/singleton.h" 7 #include "base/memory/singleton.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "base/threading/platform_thread.h" 9 #include "base/threading/platform_thread.h"
10 #include "build/build_config.h" 10 #include "chrome/browser/permissions/permissions_browsertest.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/content_switches.h" 12 #include "content/public/common/content_switches.h"
14 #include "content/public/test/content_browser_test.h"
15 #include "content/public/test/content_browser_test_utils.h"
16 #include "content/public/test/test_navigation_observer.h"
17 #include "content/public/test/test_utils.h"
18 #include "content/shell/browser/shell.h"
19 #include "content/shell/browser/shell_javascript_dialog_manager.h"
20 #include "device/generic_sensor/platform_sensor.h" 13 #include "device/generic_sensor/platform_sensor.h"
21 #include "device/generic_sensor/platform_sensor_provider.h" 14 #include "device/generic_sensor/platform_sensor_provider.h"
22 #include "device/generic_sensor/sensor_provider_impl.h" 15 #include "device/generic_sensor/sensor_provider_impl.h"
23 16
24 namespace content {
25
26 namespace { 17 namespace {
27 18
28 class FakeAmbientLightSensor : public device::PlatformSensor { 19 class FakeAmbientLightSensor : public device::PlatformSensor {
29 public: 20 public:
30 FakeAmbientLightSensor(device::mojom::SensorType type, 21 FakeAmbientLightSensor(device::mojom::SensorType type,
31 mojo::ScopedSharedBufferMapping mapping, 22 mojo::ScopedSharedBufferMapping mapping,
32 device::PlatformSensorProvider* provider) 23 device::PlatformSensorProvider* provider)
33 : PlatformSensor(type, std::move(mapping), provider) {} 24 : PlatformSensor(type, std::move(mapping), provider) {}
34 25
35 device::mojom::ReportingMode GetReportingMode() override { 26 device::mojom::ReportingMode GetReportingMode() override {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 callback.Run(std::move(sensor)); 73 callback.Run(std::move(sensor));
83 break; 74 break;
84 } 75 }
85 default: 76 default:
86 NOTIMPLEMENTED(); 77 NOTIMPLEMENTED();
87 callback.Run(nullptr); 78 callback.Run(nullptr);
88 } 79 }
89 } 80 }
90 }; 81 };
91 82
92 class GenericSensorBrowserTest : public ContentBrowserTest { 83 class GenericSensorBrowserTest : public PermissionsBrowserTest {
93 public: 84 public:
94 GenericSensorBrowserTest() 85 GenericSensorBrowserTest()
95 : io_loop_finished_event_( 86 : PermissionsBrowserTest(
87 "/generic_sensor/ambient_light_sensor_test.html"),
88 io_loop_finished_event_(
96 base::WaitableEvent::ResetPolicy::AUTOMATIC, 89 base::WaitableEvent::ResetPolicy::AUTOMATIC,
97 base::WaitableEvent::InitialState::NOT_SIGNALED) { 90 base::WaitableEvent::InitialState::NOT_SIGNALED) {
98 // TODO(darktears): remove when the GenericSensor feature goes stable. 91 // TODO(darktears): remove when the GenericSensor feature goes stable.
99 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); 92 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
100 cmd_line->AppendSwitchASCII(switches::kEnableFeatures, "GenericSensor"); 93 cmd_line->AppendSwitchASCII(switches::kEnableFeatures, "GenericSensor");
101 } 94 }
102 95
103 void SetUpOnMainThread() override { 96 void SetUpOnMainThread() override {
104 BrowserThread::PostTask( 97 PermissionsBrowserTest::SetUpOnMainThread();
105 BrowserThread::IO, FROM_HERE, 98 content::BrowserThread::PostTask(
99 content::BrowserThread::IO, FROM_HERE,
106 base::Bind(&GenericSensorBrowserTest::SetUpOnIOThread, 100 base::Bind(&GenericSensorBrowserTest::SetUpOnIOThread,
107 base::Unretained(this))); 101 base::Unretained(this)));
108 io_loop_finished_event_.Wait(); 102 io_loop_finished_event_.Wait();
109 } 103 }
110 104
105 void TriggerPrompt() override {
106 EXPECT_TRUE(RunScriptReturnBool("triggerSensorStart();"));
107 }
108
109 bool FeatureUsageSucceeds() override {
110 return GetWebContents()->GetLastCommittedURL().ref() == "pass";
111 }
112
111 void SetUpOnIOThread() { 113 void SetUpOnIOThread() {
112 device::PlatformSensorProvider::SetProviderForTesting( 114 device::PlatformSensorProvider::SetProviderForTesting(
113 FakeSensorProvider::GetInstance()); 115 FakeSensorProvider::GetInstance());
114 io_loop_finished_event_.Signal(); 116 io_loop_finished_event_.Signal();
115 } 117 }
116 118
117 void TearDown() override { 119 void TearDownOnMainThread() override {
118 device::PlatformSensorProvider::SetProviderForTesting(nullptr); 120 device::PlatformSensorProvider::SetProviderForTesting(nullptr);
121 PermissionsBrowserTest::TearDownOnMainThread();
119 } 122 }
120 123
121 public: 124 public:
122 base::WaitableEvent io_loop_finished_event_; 125 base::WaitableEvent io_loop_finished_event_;
123 }; 126 };
124 127
125 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, AmbientLightSensorTest) { 128 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, SensorSucceedsIfAllowed) {
126 // The test page will create an AmbientLightSensor object in Javascript, 129 CommonSucceedsIfAllowed();
127 // expects to get events with fake values then navigates to #pass. 130 }
128 GURL test_url = 131
129 GetTestUrl("generic_sensor", "ambient_light_sensor_test.html"); 132 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, SensorFailsIfBlocked) {
130 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 133 CommonFailsIfBlocked();
131 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 134 }
135
136 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, SensorFailsIfDismissed) {
137 CommonFailsIfDismissed();
138 }
139
140 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, SensorFailsBeforeRequesting) {
141 CommonFailsBeforeRequesting();
132 } 142 }
133 143
134 } // namespace 144 } // namespace
135
136 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698