| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 <stdint.h> | |
| 6 #include <utility> | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "build/build_config.h" | |
| 13 #include "content/public/browser/render_frame_host.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "content/public/test/browser_test_utils.h" | |
| 16 #include "content/public/test/content_browser_test.h" | |
| 17 #include "content/public/test/content_browser_test_utils.h" | |
| 18 #include "content/public/test/test_utils.h" | |
| 19 #include "content/shell/browser/shell.h" | |
| 20 #include "device/vibration/vibration_manager.mojom.h" | |
| 21 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 22 #include "services/service_manager/public/cpp/interface_registry.h" | |
| 23 | |
| 24 // These tests run against a dummy implementation of the VibrationManager | |
| 25 // service. That is, they verify that the service implementation is correctly | |
| 26 // exposed to the renderer, whatever the implementation is. | |
| 27 | |
| 28 namespace content { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 // Global, record milliseconds when FakeVibrationManager::Vibrate got called. | |
| 33 int64_t g_vibrate_milliseconds = -1; | |
| 34 | |
| 35 // Global, record whether FakeVibrationManager::Cancel got called. | |
| 36 bool g_cancelled = false; | |
| 37 | |
| 38 // Global, wait for end of execution for FakeVibrationManager::Vibrate. | |
| 39 scoped_refptr<MessageLoopRunner> g_wait_vibrate_runner; | |
| 40 | |
| 41 // Global, wait for end of execution for FakeVibrationManager::Cancel. | |
| 42 scoped_refptr<MessageLoopRunner> g_wait_cancel_runner; | |
| 43 | |
| 44 void ResetGlobalValues() { | |
| 45 g_vibrate_milliseconds = -1; | |
| 46 g_cancelled = false; | |
| 47 | |
| 48 g_wait_vibrate_runner = new MessageLoopRunner(); | |
| 49 g_wait_cancel_runner = new MessageLoopRunner(); | |
| 50 } | |
| 51 | |
| 52 class FakeVibrationManager : public device::mojom::VibrationManager { | |
| 53 public: | |
| 54 FakeVibrationManager() {} | |
| 55 ~FakeVibrationManager() override {} | |
| 56 | |
| 57 static void Create(device::mojom::VibrationManagerRequest request) { | |
| 58 mojo::MakeStrongBinding(base::MakeUnique<FakeVibrationManager>(), | |
| 59 std::move(request)); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 void Vibrate(int64_t milliseconds, const VibrateCallback& callback) override { | |
| 64 g_vibrate_milliseconds = milliseconds; | |
| 65 callback.Run(); | |
| 66 g_wait_vibrate_runner->Quit(); | |
| 67 } | |
| 68 | |
| 69 void Cancel(const CancelCallback& callback) override { | |
| 70 g_cancelled = true; | |
| 71 callback.Run(); | |
| 72 g_wait_cancel_runner->Quit(); | |
| 73 } | |
| 74 }; | |
| 75 | |
| 76 class VibrationTest : public ContentBrowserTest { | |
| 77 public: | |
| 78 VibrationTest() {} | |
| 79 | |
| 80 void SetUpOnMainThread() override { | |
| 81 ResetGlobalValues(); | |
| 82 GetMainFrame()->GetInterfaceRegistry()->AddInterface( | |
| 83 base::Bind(&FakeVibrationManager::Create)); | |
| 84 } | |
| 85 | |
| 86 bool Vibrate(int duration) { return Vibrate(duration, GetMainFrame()); } | |
| 87 | |
| 88 bool Vibrate(int duration, RenderFrameHost* frame) { | |
| 89 bool result; | |
| 90 std::string script = "domAutomationController.send(navigator.vibrate(" + | |
| 91 base::IntToString(duration) + "))"; | |
| 92 EXPECT_TRUE(ExecuteScriptAndExtractBool(frame, script, &result)); | |
| 93 return result; | |
| 94 } | |
| 95 | |
| 96 bool Cancel() { | |
| 97 bool result; | |
| 98 std::string script = "domAutomationController.send(navigator.vibrate(0))"; | |
| 99 EXPECT_TRUE(ExecuteScriptAndExtractBool(GetMainFrame(), script, &result)); | |
| 100 return result; | |
| 101 } | |
| 102 | |
| 103 bool DestroyIframe() { | |
| 104 std::string script = | |
| 105 "document.getElementById('test_iframe').parentNode.innerHTML = ''"; | |
| 106 return ExecuteScript(GetMainFrame(), script); | |
| 107 } | |
| 108 | |
| 109 RenderFrameHost* GetMainFrame() { | |
| 110 return shell()->web_contents()->GetMainFrame(); | |
| 111 } | |
| 112 | |
| 113 private: | |
| 114 DISALLOW_COPY_AND_ASSIGN(VibrationTest); | |
| 115 }; | |
| 116 | |
| 117 IN_PROC_BROWSER_TEST_F(VibrationTest, Vibrate) { | |
| 118 ASSERT_EQ(-1, g_vibrate_milliseconds); | |
| 119 | |
| 120 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); | |
| 121 ASSERT_TRUE(Vibrate(1234)); | |
| 122 g_wait_vibrate_runner->Run(); | |
| 123 | |
| 124 ASSERT_EQ(1234, g_vibrate_milliseconds); | |
| 125 } | |
| 126 | |
| 127 IN_PROC_BROWSER_TEST_F(VibrationTest, Cancel) { | |
| 128 ASSERT_FALSE(g_cancelled); | |
| 129 | |
| 130 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); | |
| 131 ASSERT_TRUE(Vibrate(1234)); | |
| 132 g_wait_vibrate_runner->Run(); | |
| 133 ASSERT_TRUE(Cancel()); | |
| 134 g_wait_cancel_runner->Run(); | |
| 135 | |
| 136 ASSERT_TRUE(g_cancelled); | |
| 137 } | |
| 138 | |
| 139 IN_PROC_BROWSER_TEST_F(VibrationTest, | |
| 140 CancelVibrationFromMainFrameWhenMainFrameIsReloaded) { | |
| 141 ASSERT_FALSE(g_cancelled); | |
| 142 | |
| 143 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); | |
| 144 ASSERT_TRUE(Vibrate(1234)); | |
| 145 g_wait_vibrate_runner->Run(); | |
| 146 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); | |
| 147 g_wait_cancel_runner->Run(); | |
| 148 | |
| 149 ASSERT_TRUE(g_cancelled); | |
| 150 } | |
| 151 | |
| 152 IN_PROC_BROWSER_TEST_F(VibrationTest, | |
| 153 CancelVibrationFromSubFrameWhenSubFrameIsReloaded) { | |
| 154 ASSERT_FALSE(g_cancelled); | |
| 155 | |
| 156 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "page_with_iframe.html"))); | |
| 157 RenderFrameHost* iframe = ChildFrameAt(GetMainFrame(), 0); | |
| 158 iframe->GetInterfaceRegistry()->AddInterface( | |
| 159 base::Bind(&FakeVibrationManager::Create)); | |
| 160 ASSERT_TRUE(Vibrate(1234, iframe)); | |
| 161 g_wait_vibrate_runner->Run(); | |
| 162 ASSERT_TRUE(NavigateIframeToURL(shell()->web_contents(), "test_iframe", | |
| 163 GetTestUrl(".", "title1.html"))); | |
| 164 g_wait_cancel_runner->Run(); | |
| 165 | |
| 166 ASSERT_TRUE(g_cancelled); | |
| 167 } | |
| 168 | |
| 169 IN_PROC_BROWSER_TEST_F(VibrationTest, | |
| 170 CancelVibrationFromSubFrameWhenMainFrameIsReloaded) { | |
| 171 ASSERT_FALSE(g_cancelled); | |
| 172 | |
| 173 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "page_with_iframe.html"))); | |
| 174 RenderFrameHost* iframe = ChildFrameAt(GetMainFrame(), 0); | |
| 175 iframe->GetInterfaceRegistry()->AddInterface( | |
| 176 base::Bind(&FakeVibrationManager::Create)); | |
| 177 ASSERT_TRUE(Vibrate(1234, iframe)); | |
| 178 g_wait_vibrate_runner->Run(); | |
| 179 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "page_with_iframe.html"))); | |
| 180 g_wait_cancel_runner->Run(); | |
| 181 | |
| 182 ASSERT_TRUE(g_cancelled); | |
| 183 } | |
| 184 | |
| 185 IN_PROC_BROWSER_TEST_F(VibrationTest, | |
| 186 CancelVibrationFromSubFrameWhenSubFrameIsDestroyed) { | |
| 187 ASSERT_FALSE(g_cancelled); | |
| 188 | |
| 189 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "page_with_iframe.html"))); | |
| 190 RenderFrameHost* iframe = ChildFrameAt(GetMainFrame(), 0); | |
| 191 iframe->GetInterfaceRegistry()->AddInterface( | |
| 192 base::Bind(&FakeVibrationManager::Create)); | |
| 193 ASSERT_TRUE(Vibrate(1234, iframe)); | |
| 194 g_wait_vibrate_runner->Run(); | |
| 195 ASSERT_TRUE(DestroyIframe()); | |
| 196 g_wait_cancel_runner->Run(); | |
| 197 | |
| 198 ASSERT_TRUE(g_cancelled); | |
| 199 } | |
| 200 | |
| 201 } // namespace | |
| 202 | |
| 203 } // namespace content | |
| OLD | NEW |