/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include "hdf_base.h" #include "osal_time.h" #include "v1_1/ivibrator_interface.h" using namespace OHOS::HDI::Vibrator::V1_1; using namespace testing::ext; namespace { uint32_t g_duration = 1000; uint32_t g_noDuration = 0; uint32_t g_sleepTime1 = 2000; uint32_t g_sleepTime2 = 5000; int32_t g_intensity1 = 30; int32_t g_intensity2 = -30; int32_t g_frequency1 = 200; int32_t g_frequency2 = -200; std::string g_timeSequence = "haptic.clock.timer"; std::string g_builtIn = "haptic.default.effect"; std::string g_arbitraryStr = "arbitraryString"; sptr g_vibratorInterface = nullptr; class HdfVibratorHdiServiceTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp(); void TearDown(); }; void HdfVibratorHdiServiceTest::SetUpTestCase() { g_vibratorInterface = IVibratorInterface::Get(); } void HdfVibratorHdiServiceTest::TearDownTestCase() { } void HdfVibratorHdiServiceTest::SetUp() { } void HdfVibratorHdiServiceTest::TearDown() { } /** * @tc.name: CheckVibratorInstanceIsEmpty * @tc.desc: Creat a vibrator instance. The instance is not empty. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0100, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); } /** * @tc.name: PerformOneShotVibratorDuration001 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at a given duration. * Controls this vibrator to stop the vibrator * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0200, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->StartOnce(g_duration); EXPECT_EQ(startRet, HDF_SUCCESS); OsalMSleep(g_sleepTime1); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: PerformOneShotVibratorDuration002 * @tc.desc: Controls this vibrator to perform a one-shot vibrator at 0 millisecond. * Controls this vibrator to stop the vibrator * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0300, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->StartOnce(g_noDuration); EXPECT_EQ(startRet, HDF_SUCCESS); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: ExecuteVibratorEffect001 * @tc.desc: Controls this Performing Time Series Vibrator Effects. * Controls this vibrator to stop the vibrator * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0400, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->Start(g_timeSequence); EXPECT_EQ(startRet, HDF_SUCCESS); OsalMSleep(g_sleepTime2); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: ExecuteVibratorEffect002 * @tc.desc: Controls this Performing built-in Vibrator Effects. * Controls this vibrator to stop the vibrator. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0500, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->Start(g_builtIn); EXPECT_EQ(startRet, HDF_SUCCESS); OsalMSleep(g_sleepTime1); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: ExecuteVibratorEffect004 * @tc.desc: Controls this Performing Time Series Vibrator Effects. * Controls this vibrator to stop the vibrator. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0600, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->Start(g_timeSequence); EXPECT_EQ(startRet, HDF_SUCCESS); OsalMSleep(g_sleepTime2); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_BUTT); EXPECT_EQ(endRet, HDF_ERR_INVALID_PARAM); endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: ExecuteVibratorEffect005 * @tc.desc: Controls this vibrator to stop the vibrator. * Controls this Performing Time Series Vibrator Effects. * Controls this vibrator to stop the vibrator. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0700, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->Start(g_timeSequence); EXPECT_EQ(startRet, HDF_SUCCESS); OsalMSleep(g_sleepTime2); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: ExecuteVibratorEffect006 * @tc.desc: Controls this vibrator to stop the vibrator. * Controls this Perform built-in Vibrator Effects. * Controls this vibrator to stop the vibrator. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0800, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->Start(g_builtIn); EXPECT_EQ(startRet, HDF_SUCCESS); OsalMSleep(g_sleepTime2); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: ExecuteVibratorEffect007 * @tc.desc: Controls this Perform a one-shot vibrator with a arbitrary string. * Controls this vibrator to stop the vibrator. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_0900, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); int32_t startRet = g_vibratorInterface->Start(g_arbitraryStr); EXPECT_EQ(startRet, HDF_ERR_INVALID_PARAM); int32_t endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE); EXPECT_EQ(endRet, HDF_SUCCESS); } /** * @tc.name: GetVibratorInfo_001 * @tc.desc: Obtain the vibrator setting strength, frequency capability and * range in the system. Validity check of input parameters. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1000, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); std::vector info; int32_t startRet = g_vibratorInterface->GetVibratorInfo(info); EXPECT_EQ(startRet, HDF_SUCCESS); printf("intensity = %d, intensityMaxValue = %d, intensityMinValue = %d\n\t", info[0].isSupportIntensity, info[0].intensityMaxValue, info[0].intensityMinValue); printf("frequency = %d, intensityMaxValue = %d, intensityMinValue = %d\n\t", info[0].isSupportFrequency, info[0].frequencyMaxValue, info[0].frequencyMinValue); } /** * @tc.name: EnableVibratorModulation_001 * @tc.desc: Start vibrator based on the setting vibration effect. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1100, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); std::vector info; int32_t startRet = g_vibratorInterface->GetVibratorInfo(info); EXPECT_EQ(startRet, HDF_SUCCESS); if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) { EXPECT_GT(g_duration, 0); EXPECT_GE(g_intensity1, info[0].intensityMinValue); EXPECT_LE(g_intensity1, info[0].intensityMaxValue); EXPECT_GE(g_frequency1, info[0].frequencyMinValue); EXPECT_LE(g_frequency1, info[0].frequencyMaxValue); startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency1); EXPECT_EQ(startRet, HDF_SUCCESS); OsalMSleep(g_sleepTime1); startRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE); EXPECT_EQ(startRet, HDF_SUCCESS); } } /** * @tc.name: EnableVibratorModulation_002 * @tc.desc: Start vibrator based on the setting vibration effect. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1200, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); std::vector info; int32_t startRet = g_vibratorInterface->GetVibratorInfo(info); EXPECT_EQ(startRet, HDF_SUCCESS); if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) { startRet = g_vibratorInterface->EnableVibratorModulation(g_noDuration, g_intensity1, g_frequency1); EXPECT_EQ(startRet, -1); } } /** * @tc.name: EnableVibratorModulation_003 * @tc.desc: Start vibrator based on the setting vibration effect. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1300, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); std::vector info; int32_t startRet = g_vibratorInterface->GetVibratorInfo(info); EXPECT_EQ(startRet, HDF_SUCCESS); if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) { startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity2, g_frequency1); EXPECT_EQ(startRet, -2); } } /** * @tc.name: EnableVibratorModulation_004 * @tc.desc: Start vibrator based on the setting vibration effect. * @tc.type: FUNC */ HWTEST_F(HdfVibratorHdiServiceTest, SUB_Driver_Sensor_HdiVibrator_1400, Function | MediumTest | Level1) { ASSERT_NE(nullptr, g_vibratorInterface); std::vector info; int32_t startRet = g_vibratorInterface->GetVibratorInfo(info); EXPECT_EQ(startRet, HDF_SUCCESS); if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) { startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency2); EXPECT_EQ(startRet, -3); } } }