1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/run_loop.h"
6 #include "build/build_config.h"
7 #include "mojo/public/cpp/bindings/remote.h"
8 #include "services/device/device_service_test_base.h"
9 #include "services/device/public/mojom/vibration_manager.mojom.h"
11 #if BUILDFLAG(IS_ANDROID)
12 #include "base/android/jni_android.h"
13 #include "services/device/vibration/android/vibration_jni_headers/VibrationManagerImpl_jni.h"
15 #include "services/device/vibration/vibration_manager_impl.h"
22 class VibrationManagerImplTest : public DeviceServiceTestBase {
24 VibrationManagerImplTest() = default;
26 VibrationManagerImplTest(const VibrationManagerImplTest&) = delete;
27 VibrationManagerImplTest& operator=(const VibrationManagerImplTest&) = delete;
29 ~VibrationManagerImplTest() override = default;
32 void SetUp() override {
33 DeviceServiceTestBase::SetUp();
35 device_service()->BindVibrationManager(
36 vibration_manager_.BindNewPipeAndPassReceiver());
39 void Vibrate(int64_t milliseconds) {
40 base::RunLoop run_loop;
41 vibration_manager_->Vibrate(milliseconds, run_loop.QuitClosure());
46 base::RunLoop run_loop;
47 vibration_manager_->Cancel(run_loop.QuitClosure());
51 int64_t GetVibrationMilliSeconds() {
52 #if BUILDFLAG(IS_ANDROID)
53 return Java_VibrationManagerImpl_getVibrateMilliSecondsForTesting(
54 base::android::AttachCurrentThread());
56 return VibrationManagerImpl::milli_seconds_for_testing_;
60 bool GetVibrationCancelled() {
61 #if BUILDFLAG(IS_ANDROID)
62 return Java_VibrationManagerImpl_getVibrateCancelledForTesting(
63 base::android::AttachCurrentThread());
65 return VibrationManagerImpl::cancelled_for_testing_;
70 mojo::Remote<mojom::VibrationManager> vibration_manager_;
73 TEST_F(VibrationManagerImplTest, VibrateThenCancel) {
74 EXPECT_NE(10000, GetVibrationMilliSeconds());
76 EXPECT_EQ(10000, GetVibrationMilliSeconds());
78 EXPECT_FALSE(GetVibrationCancelled());
80 EXPECT_TRUE(GetVibrationCancelled());