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