1 /*
2 * Copyright (c) 2022-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 <gtest/gtest.h>
17 #include <string>
18 #include <system_ability_definition.h>
19
20 #include "device_info_proxy.h"
21 #include "enterprise_device_mgr_stub_mock.h"
22 #include "edm_sys_manager_mock.h"
23 #include "utils.h"
24
25 using namespace testing::ext;
26 using namespace testing;
27
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
32 class DeviceInfoProxyTest : public testing::Test {
33 protected:
34 void SetUp() override;
35
36 void TearDown() override;
37
38 static void TearDownTestSuite(void);
39 std::shared_ptr<DeviceInfoProxy> deviceInfoProxy = nullptr;
40 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
41 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
42 };
43
SetUp()44 void DeviceInfoProxyTest::SetUp()
45 {
46 deviceInfoProxy = DeviceInfoProxy::GetDeviceInfoProxy();
47 edmSysManager_ = std::make_shared<EdmSysManager>();
48 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
49 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
50 Utils::SetEdmServiceEnable();
51 }
52
TearDown()53 void DeviceInfoProxyTest::TearDown()
54 {
55 deviceInfoProxy.reset();
56 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
57 object_ = nullptr;
58 Utils::SetEdmServiceDisable();
59 }
60
TearDownTestSuite()61 void DeviceInfoProxyTest::TearDownTestSuite()
62 {
63 ASSERT_FALSE(Utils::GetEdmServiceState());
64 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
65 }
66
67 /**
68 * @tc.name: TestGetDeviceInfoSuc
69 * @tc.desc: Test GetDeviceInfo func.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoSuc, TestSize.Level1)
73 {
74 AppExecFwk::ElementName admin;
75 admin.SetBundleName(ADMIN_PACKAGENAME);
76 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
77 .Times(1)
78 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy));
79 std::string info;
80 int32_t ret = deviceInfoProxy->GetDeviceSerial(admin, info);
81 ASSERT_TRUE(ret == ERR_OK);
82 ASSERT_TRUE(info == RETURN_STRING);
83 }
84
85 /**
86 * @tc.name: TestGetDeviceInfoFail
87 * @tc.desc: Test GetDeviceInfo func.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoFail, TestSize.Level1)
91 {
92 Utils::SetEdmServiceDisable();
93 AppExecFwk::ElementName admin;
94 admin.SetBundleName(ADMIN_PACKAGENAME);
95 std::string info;
96 int32_t ret = deviceInfoProxy->GetDeviceSerial(admin, info);
97 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
98 }
99 } // namespace TEST
100 } // namespace EDM
101 } // namespace OHOS
102