blob: 0c9aeeb0aa1fb8d9785f6ddbe26301122162d542 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2017 The Chromium Authors
leon.han31c48e202017-05-19 07:52:142// 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
Avi Drissmanadac21992023-01-11 23:46:397#include "base/functional/bind.h"
8#include "base/functional/callback_helpers.h"
leon.han31c48e202017-05-19 07:52:149#include "base/run_loop.h"
10#include "base/strings/string_number_conversions.h"
Ken Rockot05499cf2019-12-12 05:22:5411#include "content/browser/browser_interface_binders.h"
leon.han31c48e202017-05-19 07:52:1412#include "content/public/browser/render_frame_host.h"
13#include "content/public/browser/web_contents.h"
Seung Jae Limc16c5e12024-05-10 23:30:0014#include "content/public/browser/web_contents_observer.h"
Peter Kasting919ce652020-05-07 10:22:3615#include "content/public/test/browser_test.h"
leon.han31c48e202017-05-19 07:52:1416#include "content/public/test/browser_test_utils.h"
17#include "content/public/test/content_browser_test.h"
18#include "content/public/test/content_browser_test_utils.h"
19#include "content/shell/browser/shell.h"
Gyuyoung Kim1afa7032019-09-06 22:40:4620#include "mojo/public/cpp/bindings/receiver.h"
Seung Jae Limc16c5e12024-05-10 23:30:0021#include "mojo/public/cpp/bindings/remote.h"
Ke He31d0bb02018-02-24 07:16:2422#include "services/device/public/mojom/vibration_manager.mojom.h"
leon.han31c48e202017-05-19 07:52:1423
24namespace content {
25
26namespace {
27
Seung Jae Limc16c5e12024-05-10 23:30:0028class VibrationObserver : public WebContentsObserver {
29 public:
30 explicit VibrationObserver(WebContents* web_contents)
31 : WebContentsObserver(web_contents) {}
32
33 void VibrationRequested() override {
34 did_vibrate_ = true;
35 run_loop_.Quit();
36 }
37
38 void Wait() {
39 if (!did_vibrate_) {
40 run_loop_.Run();
41 }
42 }
43
44 bool DidVibrate() { return did_vibrate_; }
45
46 private:
47 bool did_vibrate_ = false;
48 base::RunLoop run_loop_;
49};
50
leon.han31c48e202017-05-19 07:52:1451class VibrationTest : public ContentBrowserTest,
52 public device::mojom::VibrationManager {
53 public:
Gyuyoung Kim1afa7032019-09-06 22:40:4654 VibrationTest() {
Ken Rockot05499cf2019-12-12 05:22:5455 OverrideVibrationManagerBinderForTesting(base::BindRepeating(
56 &VibrationTest::BindVibrationManager, base::Unretained(this)));
leon.han31c48e202017-05-19 07:52:1457 }
58
Peter Boström828b9022021-09-21 02:28:4359 VibrationTest(const VibrationTest&) = delete;
60 VibrationTest& operator=(const VibrationTest&) = delete;
61
Reilly Grant633165602018-07-11 00:48:0362 ~VibrationTest() override {
Ken Rockot05499cf2019-12-12 05:22:5463 OverrideVibrationManagerBinderForTesting(base::NullCallback());
Reilly Grant633165602018-07-11 00:48:0364 }
65
Gyuyoung Kim1afa7032019-09-06 22:40:4666 void BindVibrationManager(
Seung Jae Limc16c5e12024-05-10 23:30:0067 mojo::PendingReceiver<device::mojom::VibrationManager> receiver,
68 mojo::PendingRemote<device::mojom::VibrationManagerListener> listener) {
Gyuyoung Kim1afa7032019-09-06 22:40:4669 receiver_.Bind(std::move(receiver));
Seung Jae Limc16c5e12024-05-10 23:30:0070 listener_.Bind(std::move(listener));
leon.han31c48e202017-05-19 07:52:1471 }
72
73 protected:
Avi Drissmanc91bd8e2021-04-19 23:58:4474 void TriggerVibrate(int duration, base::OnceClosure vibrate_done) {
Tommy Nyquist4b749d02018-03-20 21:46:2975 vibrate_done_ = std::move(vibrate_done);
leon.han31c48e202017-05-19 07:52:1476
Dave Tapuska327c06c92022-06-13 20:31:5177 RenderFrameHost* frame = shell()->web_contents()->GetPrimaryMainFrame();
Avi Drissmanc91bd8e2021-04-19 23:58:4478 std::string script =
79 "navigator.vibrate(" + base::NumberToString(duration) + ")";
80 EXPECT_TRUE(ExecJs(frame, script));
leon.han31c48e202017-05-19 07:52:1481 }
82
83 int64_t vibrate_milliseconds() { return vibrate_milliseconds_; }
84
85 private:
86 // device::mojom::VibrationManager:
tzik4286ab42017-05-30 05:53:1887 void Vibrate(int64_t milliseconds, VibrateCallback callback) override {
leon.han31c48e202017-05-19 07:52:1488 vibrate_milliseconds_ = milliseconds;
tzik4286ab42017-05-30 05:53:1889 std::move(callback).Run();
danakjf416ce9d2019-12-11 20:45:4590 std::move(vibrate_done_).Run();
Seung Jae Limc16c5e12024-05-10 23:30:0091 listener_->OnVibrate();
leon.han31c48e202017-05-19 07:52:1492 }
tzik4286ab42017-05-30 05:53:1893 void Cancel(CancelCallback callback) override { std::move(callback).Run(); }
leon.han31c48e202017-05-19 07:52:1494
95 int64_t vibrate_milliseconds_ = -1;
danakjf416ce9d2019-12-11 20:45:4596 base::OnceClosure vibrate_done_;
Gyuyoung Kim1afa7032019-09-06 22:40:4697 mojo::Receiver<device::mojom::VibrationManager> receiver_{this};
Seung Jae Limc16c5e12024-05-10 23:30:0098 mojo::Remote<device::mojom::VibrationManagerListener> listener_;
leon.han31c48e202017-05-19 07:52:1499};
100
101IN_PROC_BROWSER_TEST_F(VibrationTest, Vibrate) {
102 ASSERT_EQ(-1, vibrate_milliseconds());
103
104 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html")));
105 base::RunLoop run_loop;
Avi Drissmanc91bd8e2021-04-19 23:58:44106 TriggerVibrate(1234, run_loop.QuitClosure());
leon.han31c48e202017-05-19 07:52:14107 run_loop.Run();
108
109 ASSERT_EQ(1234, vibrate_milliseconds());
110}
111
Seung Jae Limc16c5e12024-05-10 23:30:00112IN_PROC_BROWSER_TEST_F(VibrationTest, VibrateNotifiesListener) {
113 VibrationObserver observer(shell()->web_contents());
114 EXPECT_FALSE(observer.DidVibrate());
115
116 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html")));
117 base::RunLoop run_loop;
118
119 TriggerVibrate(1234, run_loop.QuitClosure());
120 run_loop.Run();
121 observer.Wait();
122 EXPECT_TRUE(observer.DidVibrate());
123}
124
leon.han31c48e202017-05-19 07:52:14125} // namespace
126
127} // namespace content