Chromium Code Reviews| 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 // Quit the run loop. | |
|
Ken Rockot(use gerrit already)
2017/05/11 15:38:16
nit: Unnecessary comment, and I think too specific
leonhsl(Using Gerrit)
2017/05/12 02:02:59
Done.
| |
| 67 vibrate_done_.Run(); | |
| 68 } | |
| 69 void Cancel(const CancelCallback& callback) override { callback.Run(); } | |
| 70 | |
| 71 int64_t vibrate_milliseconds_ = -1; | |
| 72 base::Closure vibrate_done_; | |
| 73 mojo::Binding<device::mojom::VibrationManager> binding_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(VibrationTest); | |
| 76 }; | |
| 77 | |
| 78 IN_PROC_BROWSER_TEST_F(VibrationTest, Vibrate) { | |
| 79 ASSERT_EQ(-1, vibrate_milliseconds()); | |
| 80 | |
| 81 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); | |
| 82 base::RunLoop run_loop; | |
| 83 ASSERT_TRUE(TriggerVibrate(1234, run_loop.QuitClosure())); | |
| 84 run_loop.Run(); | |
| 85 | |
| 86 ASSERT_EQ(1234, vibrate_milliseconds()); | |
| 87 } | |
| 88 | |
| 89 } // namespace | |
| 90 | |
| 91 } // namespace content | |
| OLD | NEW |