1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 #define private public 18 #define protected public 19 #include "ability_manager.h" 20 #include "ability_manager_stub_mock.h" 21 #undef private 22 #undef protected 23 #include "hilog_wrapper.h" 24 using namespace testing::ext; 25 using namespace testing; 26 namespace OHOS { 27 namespace AAFwk { 28 namespace { 29 constexpr int32_t ONE = 1; 30 constexpr int32_t NEGATIVE = -1; 31 } 32 using namespace OHOS::AppExecFwk; 33 class AbilityManagerTest : public testing::Test { 34 public: SetUpTestCase(void)35 static void SetUpTestCase(void) {}; TearDownTestCase(void)36 static void TearDownTestCase(void) {}; SetUp()37 void SetUp() {}; TearDown()38 void TearDown() {}; 39 }; 40 41 /* 42 * @tc.number : AbilityManagerTest_0100 43 * @tc.name : AbilityManager 44 * @tc.desc : Test Function AbilityManager::GetInstance() and AbilityManager::StartAbility 45 */ 46 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0100, TestSize.Level1) 47 { 48 HILOG_INFO("AbilityManagerTest_0100 is start"); 49 int32_t requestCode = NEGATIVE; 50 Want want; 51 ElementName element("device", "com.ix.hiMusic", "MusicSAbility"); 52 want.SetElement(element); 53 sptr<AAFwk::AbilityManagerStubTestMock> mock = new AAFwk::AbilityManagerStubTestMock(); 54 AAFwk::AbilityManagerClient::GetInstance()->proxy_ = mock; 55 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, requestCode); 56 EXPECT_EQ(error, ERR_OK); 57 AbilityManager::GetInstance().StartAbility(want, requestCode); 58 AAFwk::AbilityManagerClient::GetInstance()->proxy_ = nullptr; 59 HILOG_INFO("AbilityManagerTest_0100 is end"); 60 } 61 62 /* 63 * @tc.number : AbilityManagerTest_0200 64 * @tc.name : AbilityManager 65 * @tc.desc : Test Function AbilityManager::GetInstance() and AbilityManager::StartAbility 66 */ 67 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0200, TestSize.Level1) 68 { 69 HILOG_INFO("AbilityManagerTest_0200 is start"); 70 int32_t requestCode = NEGATIVE; 71 Want want; 72 ElementName element("device", "com.ix.hiMusic", "MusicSAbility"); 73 want.SetElement(element); 74 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, requestCode); 75 EXPECT_NE(error, ERR_OK); 76 AbilityManager::GetInstance().StartAbility(want, requestCode); 77 HILOG_INFO("AbilityManagerTest_0200 is end"); 78 } 79 80 /* 81 * @tc.number : AbilityManagerTest_0300 82 * @tc.name : AbilityManager 83 * @tc.desc : Test Function AbilityManager::ClearUpApplicationData 84 */ 85 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0300, TestSize.Level1) 86 { 87 HILOG_INFO("AbilityManagerTest_0300 is start"); 88 const std::string bundleName = "test"; 89 auto res = AbilityManager::GetInstance().ClearUpApplicationData(bundleName); 90 EXPECT_EQ(res, ONE); 91 HILOG_INFO("AbilityManagerTest_0300 is end"); 92 } 93 94 /* 95 * @tc.number : AbilityManagerTest_0400 96 * @tc.name : AbilityManager 97 * @tc.desc : Test Function AbilityManager::GetAllRunningProcesses 98 */ 99 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0400, TestSize.Level1) 100 { 101 HILOG_INFO("AbilityManagerTest_0400 is start"); 102 auto res = AbilityManager::GetInstance().GetAllRunningProcesses(); 103 EXPECT_TRUE(res.empty()); 104 HILOG_INFO("AbilityManagerTest_0400 is end"); 105 } 106 107 /* 108 * @tc.number : AbilityManagerTest_0500 109 * @tc.name : AbilityManager 110 * @tc.desc : Test Function AbilityManager::KillProcessesByBundleName 111 */ 112 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0500, TestSize.Level1) 113 { 114 HILOG_INFO("AbilityManagerTest_0500 is start"); 115 const std::string bundleName = "test"; 116 auto res = AbilityManager::GetInstance().KillProcessesByBundleName(bundleName); 117 EXPECT_NE(res, ERR_OK); 118 HILOG_INFO("AbilityManagerTest_0500 is end"); 119 } 120 121 /* 122 * @tc.number : AbilityManagerTest_0600 123 * @tc.name : AbilityManager 124 * @tc.desc : Test Function AbilityManager::KillProcessesByBundleName 125 */ 126 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0600, TestSize.Level1) 127 { 128 HILOG_INFO("AbilityManagerTest_0600 is start"); 129 const std::string bundleName = "test"; 130 sptr<AAFwk::AbilityManagerStubTestMock> mock = new AAFwk::AbilityManagerStubTestMock(); 131 AAFwk::AbilityManagerClient::GetInstance()->proxy_ = mock; 132 auto res = AbilityManager::GetInstance().KillProcessesByBundleName(bundleName); 133 EXPECT_EQ(res, ERR_OK); 134 AAFwk::AbilityManagerClient::GetInstance()->proxy_ = nullptr; 135 HILOG_INFO("AbilityManagerTest_0600 is end"); 136 } 137 } // namespace AAFwk 138 } // namespace OHOS