• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 <gmock/gmock.h>
18 #include "user_delegate.h"
19 #include "device_manager_adapter_mock.h"
20 #include "account_delegate_mock.h"
21 #include "metadata/meta_data_manager.h"
22 
23 using namespace OHOS::DistributedData;
24 using namespace testing::ext;
25 using namespace testing;
26 using namespace std;
Subscribe(std::string prefix,Observer observer,bool isLocal)27 bool MetaDataManager::Subscribe(std::string prefix, Observer observer, bool isLocal)
28 {
29     if (observer) {
30         for (int i = 0; i <= (MetaDataManager::DELETE + 1); ++i) {
31             static int32_t flag = MetaDataManager::INSERT;
32             static std::string key = prefix + "1";
33             static std::string value = "";
34             observer(key, value, flag);
35             ++flag;
36         }
37         return true;
38     }
39     return false;
40 }
41 
42 namespace OHOS::Test {
43 namespace DistributedDataTest {
44 class UserDelegateMockTest : public testing::Test {
45 public:
46     static void SetUpTestCase(void);
47     static void TearDownTestCase(void);
SetUp()48     void SetUp() {}
TearDown()49     void TearDown() {}
50     static inline shared_ptr<DeviceManagerAdapterMock> devMgrAdapterMock = nullptr;
51 };
52 
SetUpTestCase(void)53 void UserDelegateMockTest::SetUpTestCase(void)
54 {
55     devMgrAdapterMock = make_shared<DeviceManagerAdapterMock>();
56     BDeviceManagerAdapter::deviceManagerAdapter = devMgrAdapterMock;
57 }
58 
TearDownTestCase(void)59 void UserDelegateMockTest::TearDownTestCase(void)
60 {
61     BDeviceManagerAdapter::deviceManagerAdapter = nullptr;
62     devMgrAdapterMock = nullptr;
63 }
64 
65 /**
66 * @tc.name: GetLocalUserStatus
67 * @tc.desc: test GetLocalUserStatus normal branch.
68 * @tc.type: FUNC
69 * @tc.require:
70 */
71 HWTEST_F(UserDelegateMockTest, GetLocalUserStatus, TestSize.Level0)
72 {
73     DeviceInfo devInfo = { .uuid = "AFAGA45WF3663FAGA" };
74     EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillOnce(Return(devInfo));
75     std::vector<UserStatus> vec = UserDelegate::GetInstance().GetLocalUserStatus();
76     EXPECT_TRUE(vec.empty());
77 }
78 
79 /**
80 * @tc.name: InitLocalUserMeta
81 * @tc.desc: test InitLocalUserMeta
82 * @tc.type: FUNC
83 * @tc.require:
84 */
85 HWTEST_F(UserDelegateMockTest, InitLocalUserMeta, TestSize.Level0)
86 {
87     EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)).WillOnce(Return(false));
88     bool ret = UserDelegate::GetInstance().InitLocalUserMeta();
89     EXPECT_FALSE(ret);
90 
91     std::vector<int> users;
92     EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_))
93         .WillRepeatedly(DoAll(SetArgReferee<0>(users), Return(true)));
94     ret = UserDelegate::GetInstance().InitLocalUserMeta();
95     UserDelegate::GetInstance().DeleteUsers("users");
96     EXPECT_FALSE(ret);
97 }
98 
99 /**
100 * @tc.name: GetLocalUsers
101 * @tc.desc: test GetLocalUsers
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(UserDelegateMockTest, GetLocalUsers, TestSize.Level0)
106 {
107     DeviceInfo devInfo = { .uuid = "TESTGHJJ46785FAGA9323FGAGATEST" };
108     EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillOnce(Return(devInfo));
109     std::map<int, bool> value;
110     bool ret = UserDelegate::GetInstance().deviceUser_.Insert(devInfo.uuid, value);
111     EXPECT_TRUE(ret);
112     std::set<std::string> user = UserDelegate::GetInstance().GetLocalUsers();
113     EXPECT_TRUE(user.empty());
114 }
115 
116 /**
117 * @tc.name: GetUsers
118 * @tc.desc: test GetUsers
119 * @tc.type: FUNC
120 * @tc.require:
121 */
122 HWTEST_F(UserDelegateMockTest, GetUsers, TestSize.Level0)
123 {
124     std::string deviceId = "45677afatghfrttWISKKM";
125     std::map<int, bool> val;
126     bool ret = UserDelegate::GetInstance().deviceUser_.Insert(deviceId, val);
127     EXPECT_TRUE(ret);
128     std::vector<UserStatus> statusVec = UserDelegate::GetInstance().GetUsers(deviceId);
129     EXPECT_TRUE(statusVec.empty());
130 }
131 
132 /**
133 * @tc.name: Init
134 * @tc.desc: test Init
135 * @tc.type: FUNC
136 * @tc.require:
137 */
138 HWTEST_F(UserDelegateMockTest, Init, TestSize.Level0)
139 {
140     EXPECT_CALL(AccountDelegateMock::Init(), Subscribe(_)).WillRepeatedly(Return(0));
141     DeviceInfo devInfo = { .uuid = "HJJ4FGAGAAGA45WF3663FAGA" };
142     EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillRepeatedly(Return(devInfo));
143     std::shared_ptr<ExecutorPool> poolPtr = std::make_shared<ExecutorPool>(0, 1);
144     ASSERT_NE(poolPtr, nullptr);
145     UserDelegate::GetInstance().executors_ = poolPtr;
146     ASSERT_NE(UserDelegate::GetInstance().executors_, nullptr);
147     UserDelegate::GetInstance().Init(poolPtr);
148     ASSERT_TRUE(UserDelegate::GetInstance().executors_ != nullptr);
149 }
150 }
151 }