diff --git a/services/abilitymgr_lite/.gitignore b/services/abilitymgr_lite/.gitignore deleted file mode 100644 index b075466b417a8f786ddd4587566721798d921711..0000000000000000000000000000000000000000 --- a/services/abilitymgr_lite/.gitignore +++ /dev/null @@ -1 +0,0 @@ -unittest diff --git a/services/abilitymgr_lite/BUILD.gn b/services/abilitymgr_lite/BUILD.gn index 36509148906002d33c4b0194b858f7478027a190..ba3479bb09a6f1d987cc7febb099685feca453fc 100644 --- a/services/abilitymgr_lite/BUILD.gn +++ b/services/abilitymgr_lite/BUILD.gn @@ -119,5 +119,6 @@ lite_component("aafwk_services_lite") { features = [ ":abilityms", "tools:aa", + "unittest:ability_test", ] } diff --git a/services/abilitymgr_lite/unittest/BUILD.gn b/services/abilitymgr_lite/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8e7523e47de18f36577ffd1a8be0f5877c4f57a1 --- /dev/null +++ b/services/abilitymgr_lite/unittest/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2020 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/lite/config/component/lite_component.gni") +import("//build/lite/config/test.gni") + +group("ability_test") { + deps = [ + "test_lv0/page_ability_test:ability_test_pageAbilityTest_group_lv0", + ] +} \ No newline at end of file diff --git a/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/BUILD.gn b/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b259a0a7f15869bdfed735d30b559f448cbd1f89 --- /dev/null +++ b/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2020 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/lite/config/component/lite_component.gni") +import("//build/lite/config/test.gni") + +unittest("ability_test_pageAbilityTest_lv0") { + output_extension = "bin" + output_dir = "$root_out_dir/test/unittest/PageAbilityTest_lv0" + + ldflags = ["-lstdc++"] + + sources = [ + "../../utils/ability_test_helper.cpp", + "page_ability_test.cpp", + ] + + include_dirs = [ + "${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite", + "${aafwk_lite_path}/interfaces/kits/ability_lite", + "${aafwk_lite_path}/interfaces/kits/want_lite", + "${aafwk_lite_path}/frameworks/want_lite/include", + "${aafwk_lite_path}/services/abilitymgr_lite/include", + "${aafwk_lite_path}/services/abilitymgr_lite/unittest/util", + "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", + "${appexecfwk_lite_path}/utils/bundle_lite", + "${appexecfwk_lite_path}/interfaces/innerkits/bundlemgr_lite", + "${appexecfwk_lite_path}/frameworks/bundle_lite/include", + "${appexecfwk_lite_path}/kits/appkit_lite/appkit_utils/include", + "//foundation/distributedschedule/samgr_lite/interfaces/innerkits/samgr", + "//foundation/distributedschedule/samgr_lite/interfaces/innerkits/registry", + "//kernel/liteos_a/kernel/include", + "//kernel/liteos_a/kernel/common", + "//third_party/cJSON", + "//utils/native/lite/include", + ] + + deps = [ + "${aafwk_lite_path}/frameworks/abilitymgr_lite:aafwk_abilityManager_lite", + "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//build/lite/config/component/cJSON:cjson_shared", + "//foundation/communication/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + ] + + defines = ["OHOS_APPEXECFWK_BMS_BUNDLEMANAGER"] +} + +group("ability_test_pageAbilityTest_group_lv0") { + deps = [ ":ability_test_pageAbilityTest_lv0" ] +} \ No newline at end of file diff --git a/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/page_ability_test.cpp b/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/page_ability_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec11113e7055bd886e1f4248be70c548440c7a6e --- /dev/null +++ b/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/page_ability_test.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2020 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 "gtest/gtest.h" + +#include "../../utils/ability_test_helper.h" + +using namespace testing::ext; + +namespace OHOS { + constexpr char HAP_PATH[] = "/system/internal/setting.hap"; + constexpr char BUNDLE_NAME[] = "com.huawei.setting"; + constexpr char ABILITY_NAME[] = "SettingMainAbility"; + + static Want g_want = {}; + static ElementName g_element = {}; + + class PageAbilityTest : public testing::Test { + public : + static void SetUpTestCase() + { + AbilityTestHelper::Initialize(); + } + + static void TearDownTestCase() + { + AbilityTestHelper::UnInitialize(); + } + + void SetUp() override + { + SetElementBundleName(&g_element, BUNDLE_NAME); + SetElementAbilityName(&g_element, ABILITY_NAME); + SetWantElement(&g_want, g_element); + } + + void TearDown() override + { + ASSERT_TRUE(AbilityTestHelper::TestTerminateApp(g_element.bundleName)); + ASSERT_NE(STATE_ACTIVE, AbilityTestHelper::GetAbilityState(g_element)); + ClearElement(&g_element); + ClearWant(&g_want); + } + }; + + /** + * @tc.name: StartAbilityTest001 + * @tc.desc: test StartAbility. + * @tc.require: AR000DSMBM + * @tc.type: FUNC + */ + HWTEST_F(PageAbilityTest, startAbility001, TestSize.Level0) + { + /** + * @tc.steps: step1. Start ability. + * @tc.expected: step1. Start ability success. + */ + ASSERT_TRUE(AbilityTestHelper::TestStartAbility(g_want)); + + /** + * @tc.expected: step1. Ability state is STATE_ACTIVE. + */ + ASSERT_EQ(STATE_ACTIVE, AbilityTestHelper::GetAbilityState(g_element)); + } +} // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr_lite/unittest/utils/ability_test_helper.cpp b/services/abilitymgr_lite/unittest/utils/ability_test_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bde64ad37f3e9a7dbda08b754eca3ff98f63f3e4 --- /dev/null +++ b/services/abilitymgr_lite/unittest/utils/ability_test_helper.cpp @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2020 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 "ability_test_helper.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace OHOS { + constexpr uint32_t WAIT_TIMEOUT = 30; + constexpr int MAX_BUFFER_SIZE_PER = 1024; // 1024 byte + constexpr char ABILITY_STATE[] = "Ability State: ["; + constexpr char NO_ABILITY[] = "Ability not found"; + constexpr char SLICE_STACK[] = "\n ["; + constexpr char SLICE_STATE[] = "] State: ["; + + static sem_t g_sem; + static bool g_result = false; + static std::string g_resultString; + + SvcIdentity AbilityTestHelper::identity_ = {}; + IClientProxy *AbilityTestHelper::proxy_ = nullptr; + + void AbilityTestHelper::Initialize() + { + if (RegisterIpcCallback(AbilityCallback, 0, IPC_WAIT_FOREVER, &identity_, nullptr) != 0) + { + printf("registerIpcCallback failed\n"); + exit(-1); + } + proxy_ = GetAbilityInnerFeature(); + if (proxy_ == nullptr) + { + exit(-1); + } + sleep(1); + } + + void AbilityTestHelper::UnInitialize() + { + UnregisterIpcCallback(identity_); + sleep(1); + } + + void AbilityTestHelper::InstallCallback(const uint8_t resultCode, const void *resultMessage) + { + std::string strMessage = reinterpret_cast(resultMessage); + if (!strMessage.empty()) + { + printf("install resultMessage is %s\n", strMessage.c_str()); + } + + g_result = (resultCode == ERR_OK); + SemPost(); + } + + void AbilityTestHelper::UninstallCallback(const uint8_t resultCode, const void *resultMessage) + { + std::string strMessage = reinterpret_cast(resultMessage); + if (!strMessage.empty()) + { + printf("[INFO] [AbilityTestHelper] uninstall resultMessage is %s\n", strMessage.c_str()); + } + + g_result = (resultCode == ERR_OK); + SemPost(); + } + + int32_t AbilityTestHelper::AbilityCallback(const IpcContext* context, void *ipcMsg, IpcIo *data, void *arg) + { + if (ipcMsg == nullptr) + { + printf("ams call back error, ipcMsg is null\n"); + return -1; + } + + uint32_t funcId = 0; + GetCode(ipcMsg, &funcId); + uint32_t flag = 0; + GetFlag(ipcMsg, &flag); + if (flag == LITEIPC_FLAG_ONEWAY) + { + FreeBuffer(nullptr, ipcMsg); + } + switch (funcId) + { + case SCHEDULER_APP_INIT: { + ElementName element = {}; + DeserializeElement(&element, data); + int ret = IpcIoPopInt32(data); + printf("ams call back, start %s.%s ret = %d\n", element.bundleName, element.abilityName, ret); + ClearElement(&element); + g_result = (ret == EC_SUCCESS); + break; + } + case SCHEDULER_DUMP_ABILITY: { + BuffPtr *buff = IpcIoPopDataBuff(data); + if ((buff == nullptr) || (buff->buff == nullptr)) + { + printf("ams call back error, buff is empty\n"); + return false; + } + g_resultString = static_cast(buff->buff); + FreeBuffer(nullptr, buff->buff); + break; + } + default: { + printf("ams call back error, funcId: %u\n", funcId); + break; + } + } + + SemPost(); + return 0; + } + + bool AbilityTestHelper::TestInstall(const std::string &hap) + { + InstallParam installParam = { + .installLocation = 1, + .keepData = false + }; + if (!Install(hap.c_str(), &installParam, InstallCallback)) + { + printf("[ERROR] [AbilityTestHelper] Install hap failed!\n"); + exit(-1); + } + g_result = false; + SemWait(); + return g_result; + } + + bool AbilityTestHelper::TestUnInstall(const std::string &bundleName) + { + InstallParam installParam = { + .installLocation = 1, + .keepData = false + }; + Uninstall(bundleName.c_str(), &installParam, UninstallCallback); + g_result = false; + SemWait(); + return g_result; + } + + bool AbilityTestHelper::TestStartAbility(const Want &want) + { + SetWantSvcIdentity(const_cast(&want), identity_); + StartAbility(&want); + g_result = false; + SemWait(); + sleep(1); + return g_result; + } + + bool AbilityTestHelper::TestTerminateApp(const std::string &bundleName) + { + IpcIo req; + char data[IPC_IO_DATA_MAX]; + IpcIoInit(&req, data, IPC_IO_DATA_MAX, 0); + IpcIoPushString(&req, bundleName.c_str()); + int32_t ret = proxy_->Invoke(proxy_, TERMINATE_APP, &req, nullptr, nullptr); + sleep(2); + return ret == EC_SUCCESS; + } + + State AbilityTestHelper::GetAbilityState(const ElementName &elementName) + { + TestDumpAbility(elementName); + + auto position = g_resultString.find(ABILITY_STATE); + if (position != std::string::npos) + { + return static_cast(g_resultString[position + strlen(ABILITY_STATE)] - '0'); + } + + if (g_resultString.find(NO_ABILITY) != std::string::npos) + { + return STATE_INITIAL; + } + printf("[ERROR] [AbilityTestHelper] Failed to GetAbilityState\n"); + return STATE_UNINITIALIZED; + } + + std::list> AbilityTestHelper::GetSliceStack(const ElementName &elementName) + { + TestDumpAbility(elementName); + std::list> sliceList; + std::string::size_type begin; + std::string::size_type end = 0; + + while (((begin = g_resultString.find(SLICE_STACK, end)) != std::string::npos) && + ((end = g_resultString.find(SLICE_STATE, begin)) != std::string::npos)) { + auto record = std::make_shared(); + record->name = g_resultString.substr(begin + strlen(SLICE_STACK), end); + record->state = static_cast(g_resultString[end + strlen(SLICE_STATE)] - '0'); + sliceList.push_back(record); + } + + return sliceList; + } + + IClientProxy *AbilityTestHelper::GetAbilityInnerFeature() + { + IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_INNER_FEATURE); + if (iUnknown == nullptr) + { + printf("ams inner unknown is null\n"); + return nullptr; + } + IClientProxy *innerProxy = nullptr; + (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&innerProxy); + if (innerProxy == nullptr) + { + printf("ams inner feature is null\n"); + return nullptr; + } + return innerProxy; + } + + void AbilityTestHelper::TestDumpAbility(const ElementName &elementName) + { + IpcIo req; + char data[IPC_IO_DATA_MAX]; + IpcIoInit(&req, data, IPC_IO_DATA_MAX, 2); + Want want = {}; + SetWantElement(&want, elementName); + SetWantSvcIdentity(&want, identity_); + if (!SerializeWant(&req, &want)) + { + printf("SerializeWant failed\n"); + ClearWant(&want); + exit(-1); + } + ClearWant(&want); + proxy_->Invoke(proxy_, DUMP_ABILITY, &req, nullptr, nullptr); + SemWait(); + + printf("[Dump]\n%s\n", g_resultString.c_str()); + } + + void AbilityTestHelper::SemWait() + { + printf("waiting callback\n"); + sem_init(&g_sem, 0, 0); + struct timespec ts = {}; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += WAIT_TIMEOUT; + sem_timedwait(&g_sem, &ts); + } + + void AbilityTestHelper::SemPost() + { + printf("receive callback\n"); + sem_post(&g_sem); + } +} + diff --git a/services/abilitymgr_lite/unittest/utils/ability_test_helper.h b/services/abilitymgr_lite/unittest/utils/ability_test_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..815afddbaab0fa37a0b144f9ad8cf0f72fc0bfc5 --- /dev/null +++ b/services/abilitymgr_lite/unittest/utils/ability_test_helper.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_ABILITY_TEST_HELPER_H +#define OHOS_ABILITY_TEST_HELPER_H + +#include +#include +#include +#include +#include +#include +#include "liteipc_adapter.h" +namespace OHOS { + struct SliceRecord { + std::string name; + State state; + }; + + class AbilityTestHelper { + public: + AbilityTestHelper() = delete; + ~AbilityTestHelper() = delete; + static void Initialize(); + static void UnInitialize(); + + static void InstallCallback(uint8_t resultCode, const void *resultMessage); + static void UninstallCallback(uint8_t resultCode, const void *resultMessage); + static int32_t AbilityCallback(const IpcContext* context, void *ipcMsg, IpcIo *data, void *arg); + static bool TestInstall(const std::string &hap); + static bool TestUnInstall(const std::string &bundleName); + static bool TestStartAbility(const Want &want); + static bool TestTerminateApp(const std::string &bundleName); + static State GetAbilityState(const ElementName &elementName); + static std::list> GetSliceStack(const ElementName &elementName); + + private: + static IClientProxy *GetAbilityInnerFeature(); + static void TestDumpAbility(const ElementName &elementName); + static void SemWait(); + static void SemPost(); + + static SvcIdentity identity_; + static IClientProxy *proxy_; + }; +} + +#endif // OHOS_ABILITY_TEST_HELPER_H \ No newline at end of file