• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <cstdlib>
17 #include <gtest/gtest.h>
18 
19 #define private public
20 #include "hilog_tag_wrapper.h"
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "main_thread.h"
24 #include "mock_bundle_installer_service.h"
25 #include "mock_bundle_manager.h"
26 #include "mock_overlay_manager.h"
27 #include "mock_system_ability_manager.h"
28 #include "ohos_application.h"
29 #include "process_info.h"
30 #include "sys_mgr_client.h"
31 #include "system_ability_definition.h"
32 #undef private
33 
34 using namespace testing;
35 using namespace testing::ext;
36 
37 namespace OHOS {
38 namespace AppExecFwk {
39 sptr<MockBundleInstallerService> mockBundleInstaller = new (std::nothrow) MockBundleInstallerService();
40 sptr<BundleMgrService> mockBundleMgr = new (std::nothrow) BundleMgrService();
41 
42 class MainThreadByMockBmsTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp() override;
47     void TearDown() override;
48     void MockBundleInstaller();
49     sptr<MainThread> mainThread_ = nullptr;
50     sptr<ISystemAbilityManager> iSystemAbilityMgr_ = nullptr;
51     sptr<AppExecFwk::MockSystemAbilityManager> mockSystemAbility_ = nullptr;
52 };
53 
SetUpTestCase()54 void MainThreadByMockBmsTest::SetUpTestCase() {}
55 
TearDownTestCase()56 void MainThreadByMockBmsTest::TearDownTestCase() {}
57 
SetUp()58 void MainThreadByMockBmsTest::SetUp()
59 {
60     std::shared_ptr<EventRunner> runner = EventRunner::GetMainEventRunner();
61     ASSERT_NE(runner, nullptr);
62     mainThread_ = sptr<MainThread>(new (std::nothrow) MainThread());
63     ASSERT_NE(mainThread_, nullptr);
64     mainThread_->Init(runner);
65     mockSystemAbility_ = new (std::nothrow) AppExecFwk::MockSystemAbilityManager();
66     iSystemAbilityMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67     SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_;
68 }
69 
TearDown()70 void MainThreadByMockBmsTest::TearDown()
71 {
72     mainThread_->applicationForDump_.reset();
73     SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr_;
74 }
75 
MockBundleInstaller()76 void MainThreadByMockBmsTest::MockBundleInstaller()
77 {
78     auto mockGetBundleInstaller = []() { return mockBundleInstaller; };
79     auto mockGetSystemAbility = [bms = mockBundleMgr, saMgr = iSystemAbilityMgr_](int32_t systemAbilityId) {
80         GTEST_LOG_(INFO) << "MockBundleInstaller systemAbilityId: " << systemAbilityId << " )";
81         if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID || systemAbilityId == APP_MGR_SERVICE_ID) {
82             return bms->AsObject();
83         } else {
84             return saMgr->GetSystemAbility(systemAbilityId);
85         }
86     };
87     EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).WillOnce(testing::Invoke(mockGetBundleInstaller));
88     EXPECT_CALL(*mockSystemAbility_, GetSystemAbility(testing::_))
89         .WillRepeatedly(testing::Invoke(mockGetSystemAbility));
90 }
91 
92 /**
93  * @tc.name: SetNativeLibPath_0100
94  * @tc.desc: set native lib path.
95  * @tc.type: FUNC
96  * @tc.require: issueI64MUJ
97  */
98 HWTEST_F(MainThreadByMockBmsTest, SetNativeLibPath_0100, TestSize.Level1)
99 {
100     TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
101     Configuration config;
102     AppLaunchData launchData;
103     ProcessInfo processInfo("test_quickfix", 9999);
104     ApplicationInfo appInfo;
105     appInfo.name = "MainAbility";
106     appInfo.bundleName = "com.ohos.quickfix";
107     launchData.SetApplicationInfo(appInfo);
108     launchData.SetProcessInfo(processInfo);
109     MockBundleInstaller();
110     // SetNativeLibPath is implemented in anonymous space, called by HandleLaunchApplication
111     mainThread_->HandleLaunchApplication(launchData, config);
112     ASSERT_NE(mainThread_->application_, nullptr);
113     EXPECT_NE(mainThread_->application_->abilityRuntimeContext_, nullptr);
114     TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
115 }
116 
117 /**
118  * @tc.name: GetOverlayModuleInfos_0100
119  * @tc.desc: Get overlay paths form mock bms.
120  * @tc.type: FUNC
121  * @tc.require: issueI6SAQC
122  */
123 HWTEST_F(MainThreadByMockBmsTest, GetOverlayModuleInfos_0100, TestSize.Level1)
124 {
125     EXPECT_TRUE(mainThread_ != nullptr);
126     std::string bundleName = "com.ohos.demo";
127     std::string moduleName = "entry";
128     std::vector<OverlayModuleInfo> overlayModuleInfos;
129     OverlayModuleInfo overlayModuleInfo;
130     overlayModuleInfo.bundleName = "com.ohos.demo";
131     overlayModuleInfo.moduleName = "entry";
132     overlayModuleInfo.hapPath = "test";
133     overlayModuleInfo.priority = 99;
134     overlayModuleInfo.state = OverlayState::OVERLAY_ENABLE;
135     overlayModuleInfos.emplace_back(overlayModuleInfo);
136     overlayModuleInfos[0].state = OverlayState::OVERLAY_DISABLED;
137     MockBundleInstaller();
138     int result = mainThread_->GetOverlayModuleInfos(bundleName, moduleName, overlayModuleInfos);
139     EXPECT_TRUE(result == 0);
140     EXPECT_TRUE(overlayModuleInfos.size() == 1);
141 }
142 } // namespace AppExecFwk
143 } // namespace OHOS