| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 <utility> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/test/browser_test_utils.h" |
| 14 #include "content/public/test/content_browser_test.h" |
| 15 #include "content/public/test/content_browser_test_utils.h" |
| 16 #include "content/shell/browser/shell.h" |
| 17 #include "mojo/public/cpp/bindings/binding.h" |
| 18 #include "services/device/public/interfaces/constants.mojom.h" |
| 19 #include "services/device/public/interfaces/vibration_manager.mojom.h" |
| 20 #include "services/service_manager/public/cpp/service_context.h" |
| 21 |
| 22 namespace content { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class VibrationTest : public ContentBrowserTest, |
| 27 public device::mojom::VibrationManager { |
| 28 public: |
| 29 VibrationTest() : binding_(this){}; |
| 30 |
| 31 void SetUpOnMainThread() override { |
| 32 // Because Device Service also runs in this process(browser process), here |
| 33 // we can directly set our binder to intercept interface requests against |
| 34 // it. |
| 35 service_manager::ServiceContext::SetGlobalBinderForTesting( |
| 36 device::mojom::kServiceName, device::mojom::VibrationManager::Name_, |
| 37 base::Bind(&VibrationTest::BindVibrationManager, |
| 38 base::Unretained(this))); |
| 39 } |
| 40 |
| 41 void BindVibrationManager(const service_manager::BindSourceInfo& source_info, |
| 42 const std::string& interface_name, |
| 43 mojo::ScopedMessagePipeHandle handle) { |
| 44 binding_.Bind(std::move(handle)); |
| 45 } |
| 46 |
| 47 protected: |
| 48 bool TriggerVibrate(int duration, base::Closure vibrate_done) { |
| 49 vibrate_done_ = vibrate_done; |
| 50 |
| 51 bool result; |
| 52 RenderFrameHost* frame = shell()->web_contents()->GetMainFrame(); |
| 53 std::string script = "domAutomationController.send(navigator.vibrate(" + |
| 54 base::IntToString(duration) + "))"; |
| 55 EXPECT_TRUE(ExecuteScriptAndExtractBool(frame, script, &result)); |
| 56 return result; |
| 57 } |
| 58 |
| 59 int64_t vibrate_milliseconds() { return vibrate_milliseconds_; } |
| 60 |
| 61 private: |
| 62 // device::mojom::VibrationManager: |
| 63 void Vibrate(int64_t milliseconds, const VibrateCallback& callback) override { |
| 64 vibrate_milliseconds_ = milliseconds; |
| 65 callback.Run(); |
| 66 vibrate_done_.Run(); |
| 67 } |
| 68 void Cancel(const CancelCallback& callback) override { callback.Run(); } |
| 69 |
| 70 int64_t vibrate_milliseconds_ = -1; |
| 71 base::Closure vibrate_done_; |
| 72 mojo::Binding<device::mojom::VibrationManager> binding_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(VibrationTest); |
| 75 }; |
| 76 |
| 77 IN_PROC_BROWSER_TEST_F(VibrationTest, Vibrate) { |
| 78 ASSERT_EQ(-1, vibrate_milliseconds()); |
| 79 |
| 80 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); |
| 81 base::RunLoop run_loop; |
| 82 ASSERT_TRUE(TriggerVibrate(1234, run_loop.QuitClosure())); |
| 83 run_loop.Run(); |
| 84 |
| 85 ASSERT_EQ(1234, vibrate_milliseconds()); |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 } // namespace content |
| OLD | NEW |