diff --git a/bundle.json b/bundle.json index b8db11934c12d7c539aedb123641b1245a7c71ab..78c998182825884d9ed995ad98391e36858402d5 100644 --- a/bundle.json +++ b/bundle.json @@ -62,7 +62,8 @@ "test": [ "//base/sensors/miscdevice/interfaces/plugin/vibrator/test/unittest:unittest", "//base/sensors/miscdevice/interfaces/native/vibrator/test/fuzztest:fuzztest", - "//base/sensors/miscdevice/interfaces/native/vibrator/test:unittest" + "//base/sensors/miscdevice/interfaces/native/vibrator/test:unittest", + "//base/sensors/miscdevice/interfaces/native/light/test:unittest" ] } } diff --git a/interfaces/native/light/test/BUILD.gn b/interfaces/native/light/test/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..15a3f92d0cfc03a734151ba977e33c177f69ce19 --- /dev/null +++ b/interfaces/native/light/test/BUILD.gn @@ -0,0 +1,47 @@ +# Copyright (c) 2021 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. + +import("//build/test.gni") + +SUBSYSTEM_DIR = "//base/sensors" +module_output_path = "sensors/miscdevice/interfaces" + +###########################LightAgentTest########################### +ohos_unittest("LightAgentTest") { + module_out_path = module_output_path + + sources = [ "unittest/light_agent_test.cpp" ] + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "$SUBSYSTEM_DIR/miscdevice/interfaces/native/light/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/miscdevice/interfaces/native/light:light_interface_native", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] +} + +###########################end########################### +group("unittest") { + testonly = true + deps = [ ":LightAgentTest" ] +} diff --git a/interfaces/native/light/test/unittest/light_agent_test.cpp b/interfaces/native/light/test/unittest/light_agent_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..dbf83f77536fe09f4e2bf00a0655da74ae00fee8 --- /dev/null +++ b/interfaces/native/light/test/unittest/light_agent_test.cpp @@ -0,0 +1,298 @@ +/* + * Copyright (c) 2021 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 "light_agent.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { + constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "LightAgentTest" }; +} // namespace + +class LightAgentTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +LightInfo *lightInfo_ = nullptr; +int32_t lightId_ = -1; +int32_t invalidLightId_ = -1; +int32_t lightType_ = -1; + +/** + * @tc.name: StartLightTest_001 + * @tc.desc: Verify GetLightList + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_001, TestSize.Level1) +{ + CALL_LOG_ENTER; + int32_t count = -1; + int32_t ret = GetLightList(&lightInfo_, count); + for (int32_t i = 0; i < count; ++i) { + MISC_HILOGI("lightId: %{public}d, lightName: %{public}s, lightNumber: %{public}d, lightType: %{public}d", + lightInfo_[i].lightId, lightInfo_[i].lightName, lightInfo_[i].lightNumber, lightInfo_[i].lightType); + lightId_ = lightInfo_[i].lightId; + lightType_ = lightInfo_[i].lightType; + } + ASSERT_EQ(ret, 0); +} + +/** + * @tc.name: StartLightTest_002 + * @tc.desc: Verify GetLightList + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_002, TestSize.Level1) +{ + CALL_LOG_ENTER; + int32_t count = -1; + int32_t ret = GetLightList(nullptr, count); + ASSERT_EQ(ret, -1); +} + +bool GetLightColor(LightColor &color, int32_t lightType) +{ + switch (lightType) { + case LIGHT_TYPE_SINGLE_COLOR: { + color.singleColor = 0Xff; + return true; + } + case LIGHT_TYPE_RGB_COLOR: { + color.rgbColor = { + .r = 0Xff, + .g = 0Xff, + .b = 0Xff + }; + return true; + } + case LIGHT_TYPE_WRGB_COLOR: { + color.wrgbColor = { + .w = 0Xff, + .r = 0Xff, + .g = 0Xff, + .b = 0Xff + }; + return true; + } + default: { + MISC_HILOGE("lightType: %{public}d invalid", lightType); + return false; + } + } +} + +/** + * @tc.name: StartLightTest_003 + * @tc.desc: Verify TurnOn + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_003, TestSize.Level1) +{ + CALL_LOG_ENTER; + LightColor color; + bool ret = GetLightColor(color, lightType_); + if (!ret) { + ASSERT_EQ(ret, -1); + } else { + LightAnimation animation; + animation.mode = LIGHT_MODE_DEFAULT; + animation.onTime = 50; + animation.offTime = 50; + int32_t ret = TurnOn(lightId_, color, animation); + sleep(5); + ASSERT_EQ(ret, 0); + } +} + +/** + * @tc.name: StartLightTest_004 + * @tc.desc: Verify TurnOn + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_004, TestSize.Level1) +{ + CALL_LOG_ENTER; + LightColor color; + bool ret = GetLightColor(color, lightType_); + if (!ret) { + ASSERT_EQ(ret, -1); + } else { + LightAnimation animation; + animation.mode = LIGHT_MODE_BUTT; + animation.onTime = 50; + animation.offTime = 50; + int32_t ret = TurnOn(lightId_, color, animation); + ASSERT_EQ(ret, -1); + } +} + +/** + * @tc.name: StartLightTest_005 + * @tc.desc: Verify TurnOn + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_005, TestSize.Level1) +{ + CALL_LOG_ENTER; + LightColor color; + bool ret = GetLightColor(color, lightType_); + if (!ret) { + ASSERT_EQ(ret, -1); + } else { + LightAnimation animation; + animation.mode = -1; + animation.onTime = 50; + animation.offTime = 50; + int32_t ret = TurnOn(lightId_, color, animation); + ASSERT_EQ(ret, -1); + } +} + +/** + * @tc.name: StartLightTest_006 + * @tc.desc: Verify TurnOn + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_006, TestSize.Level1) +{ + CALL_LOG_ENTER; + LightColor color; + bool ret = GetLightColor(color, lightType_); + if (!ret) { + ASSERT_EQ(ret, -1); + } else { + LightAnimation animation; + animation.mode = LIGHT_MODE_DEFAULT; + animation.onTime = -1; + animation.offTime = 50; + int32_t ret = TurnOn(lightId_, color, animation); + ASSERT_EQ(ret, -1); + } +} + +/** + * @tc.name: StartLightTest_007 + * @tc.desc: Verify TurnOn + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_007, TestSize.Level1) +{ + CALL_LOG_ENTER; + LightColor color; + bool ret = GetLightColor(color, lightType_); + if (!ret) { + ASSERT_EQ(ret, -1); + } else { + LightAnimation animation; + animation.mode = LIGHT_MODE_DEFAULT; + animation.onTime = 50; + animation.offTime = -1; + int32_t ret = TurnOn(lightId_, color, animation); + ASSERT_EQ(ret, -1); + } +} + +/** + * @tc.name: StartLightTest_008 + * @tc.desc: Verify TurnOn + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_008, TestSize.Level1) +{ + CALL_LOG_ENTER; + LightColor color; + bool ret = GetLightColor(color, lightType_); + if (!ret) { + ASSERT_EQ(ret, -1); + } else { + LightAnimation animation; + animation.mode = LIGHT_MODE_DEFAULT; + animation.onTime = 2; + animation.offTime = 2; + int32_t ret = TurnOn(lightId_, color, animation); + sleep(5); + ASSERT_EQ(ret, 0); + } +} + +/** + * @tc.name: StartLightTest_009 + * @tc.desc: Verify TurnOn + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_009, TestSize.Level1) +{ + CALL_LOG_ENTER; + LightColor color; + bool ret = GetLightColor(color, lightType_); + if (!ret) { + ASSERT_EQ(ret, -1); + } else { + LightAnimation animation; + animation.mode = LIGHT_MODE_DEFAULT; + animation.onTime = 2; + animation.offTime = 2; + int32_t ret = TurnOn(invalidLightId_, color, animation); + ASSERT_EQ(ret, -1); + } +} + +/** + * @tc.name: StartLightTest_010 + * @tc.desc: Verify TurnOff + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_010, TestSize.Level1) +{ + CALL_LOG_ENTER; + int32_t ret = TurnOff(lightId_); + ASSERT_EQ(ret, 0); +} + +/** + * @tc.name: StartLightTest_011 + * @tc.desc: Verify TurnOff + * @tc.type: FUNC + * @tc.require: I63TFA + */ +HWTEST_F(LightAgentTest, StartLightTest_011, TestSize.Level1) +{ + CALL_LOG_ENTER; + int32_t ret = TurnOff(invalidLightId_); + ASSERT_EQ(ret, -1); +} +} // namespace Sensors +} // namespace OHOS