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_0500 96 * @tc.name : AbilityManager 97 * @tc.desc : Test Function AbilityManager::KillProcessesByBundleName 98 */ 99 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0500, TestSize.Level1) 100 { 101 HILOG_INFO("AbilityManagerTest_0500 is start"); 102 const std::string bundleName = "test"; 103 auto res = AbilityManager::GetInstance().KillProcessesByBundleName(bundleName); 104 EXPECT_NE(res, ERR_OK); 105 HILOG_INFO("AbilityManagerTest_0500 is end"); 106 } 107 108 /* 109 * @tc.number : AbilityManagerTest_0600 110 * @tc.name : AbilityManager 111 * @tc.desc : Test Function AbilityManager::KillProcessesByBundleName 112 */ 113 HWTEST_F(AbilityManagerTest, AbilityManagerTest_0600, TestSize.Level1) 114 { 115 HILOG_INFO("AbilityManagerTest_0600 is start"); 116 const std::string bundleName = "test"; 117 sptr<AAFwk::AbilityManagerStubTestMock> mock = new AAFwk::AbilityManagerStubTestMock(); 118 AAFwk::AbilityManagerClient::GetInstance()->proxy_ = mock; 119 auto res = AbilityManager::GetInstance().KillProcessesByBundleName(bundleName); 120 EXPECT_EQ(res, ERR_OK); 121 AAFwk::AbilityManagerClient::GetInstance()->proxy_ = nullptr; 122 HILOG_INFO("AbilityManagerTest_0600 is end"); 123 } 124 } // namespace AAFwk 125 } // namespace OHOS