17405867cSopenharmony_ci/*
27405867cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37405867cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47405867cSopenharmony_ci * you may not use this file except in compliance with the License.
57405867cSopenharmony_ci * You may obtain a copy of the License at
67405867cSopenharmony_ci *
77405867cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87405867cSopenharmony_ci *
97405867cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107405867cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117405867cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127405867cSopenharmony_ci * See the License for the specific language governing permissions and
137405867cSopenharmony_ci * limitations under the License.
147405867cSopenharmony_ci */
157405867cSopenharmony_ci#include <iostream>
167405867cSopenharmony_ci#include <map>
177405867cSopenharmony_ci
187405867cSopenharmony_ci#include <gtest/gtest.h>
197405867cSopenharmony_ci
207405867cSopenharmony_ci#include "app_event_store.h"
217405867cSopenharmony_ci#include "hiappevent_base.h"
227405867cSopenharmony_ci#include "hiappevent_config.h"
237405867cSopenharmony_ci#include "hiappevent_userinfo.h"
247405867cSopenharmony_ci
257405867cSopenharmony_ciusing namespace testing::ext;
267405867cSopenharmony_ciusing namespace OHOS::HiviewDFX;
277405867cSopenharmony_ci
287405867cSopenharmony_cinamespace {
297405867cSopenharmony_ciconst std::string TEST_DIR = "/data/test/hiappevent/";
307405867cSopenharmony_ciconst std::string TEST_USER_ID_NAME = "testUserName";
317405867cSopenharmony_ciconst std::string TEST_USER_ID_VALUE = "testUserId";
327405867cSopenharmony_ciconst std::string TEST_USER_PROP_NAME = "testUserName";
337405867cSopenharmony_ciconst std::string TEST_USER_PROP_VALUE = "testUserProperty";
347405867cSopenharmony_ci
357405867cSopenharmony_ciclass HiAppEventUserInfoTest : public testing::Test {
367405867cSopenharmony_cipublic:
377405867cSopenharmony_ci    void SetUp();
387405867cSopenharmony_ci    void TearDown();
397405867cSopenharmony_ci};
407405867cSopenharmony_ci
417405867cSopenharmony_civoid HiAppEventUserInfoTest::SetUp()
427405867cSopenharmony_ci{
437405867cSopenharmony_ci    HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
447405867cSopenharmony_ci    (void)AppEventStore::GetInstance().InitDbStore();
457405867cSopenharmony_ci    HiAppEvent::UserInfo::GetInstance().RemoveUserId(TEST_USER_ID_NAME);
467405867cSopenharmony_ci    HiAppEvent::UserInfo::GetInstance().RemoveUserProperty(TEST_USER_PROP_NAME);
477405867cSopenharmony_ci}
487405867cSopenharmony_ci
497405867cSopenharmony_civoid HiAppEventUserInfoTest::TearDown()
507405867cSopenharmony_ci{
517405867cSopenharmony_ci    (void)AppEventStore::GetInstance().DestroyDbStore();
527405867cSopenharmony_ci}
537405867cSopenharmony_ci}
547405867cSopenharmony_ci
557405867cSopenharmony_ci/**
567405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest001
577405867cSopenharmony_ci * @tc.desc: Test to set and get user id.
587405867cSopenharmony_ci * @tc.type: FUNC
597405867cSopenharmony_ci * @tc.require: issueI8EOLQ
607405867cSopenharmony_ci */
617405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest001, TestSize.Level3)
627405867cSopenharmony_ci{
637405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest001 start" << std::endl;
647405867cSopenharmony_ci
657405867cSopenharmony_ci    {
667405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserId(TEST_USER_ID_NAME, TEST_USER_ID_VALUE);
677405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
687405867cSopenharmony_ci    }
697405867cSopenharmony_ci    {
707405867cSopenharmony_ci        std::string strUserId;
717405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserId(TEST_USER_ID_NAME, strUserId);
727405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
737405867cSopenharmony_ci        ASSERT_NE(strUserId, "");
747405867cSopenharmony_ci    }
757405867cSopenharmony_ci
767405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest001 end" << std::endl;
777405867cSopenharmony_ci}
787405867cSopenharmony_ci
797405867cSopenharmony_ci/**
807405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest002
817405867cSopenharmony_ci * @tc.desc: Test failed to get user id.
827405867cSopenharmony_ci * @tc.type: FUNC
837405867cSopenharmony_ci * @tc.require: issueI8EOLQ
847405867cSopenharmony_ci */
857405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest002, TestSize.Level3)
867405867cSopenharmony_ci{
877405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest002 start" << std::endl;
887405867cSopenharmony_ci
897405867cSopenharmony_ci    {
907405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserId(TEST_USER_ID_NAME, TEST_USER_ID_VALUE);
917405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
927405867cSopenharmony_ci    }
937405867cSopenharmony_ci    {
947405867cSopenharmony_ci        std::string strUserId;
957405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserId("", strUserId);
967405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
977405867cSopenharmony_ci        ASSERT_EQ(strUserId, "");
987405867cSopenharmony_ci    }
997405867cSopenharmony_ci
1007405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest002 end" << std::endl;
1017405867cSopenharmony_ci}
1027405867cSopenharmony_ci
1037405867cSopenharmony_ci/**
1047405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest003
1057405867cSopenharmony_ci * @tc.desc: Test to replace and get user id.
1067405867cSopenharmony_ci * @tc.type: FUNC
1077405867cSopenharmony_ci * @tc.require: issueI8EOLQ
1087405867cSopenharmony_ci */
1097405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest003, TestSize.Level3)
1107405867cSopenharmony_ci{
1117405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest003 start" << std::endl;
1127405867cSopenharmony_ci
1137405867cSopenharmony_ci    {
1147405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserId(TEST_USER_ID_NAME, TEST_USER_ID_VALUE);
1157405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1167405867cSopenharmony_ci
1177405867cSopenharmony_ci        ret = HiAppEvent::UserInfo::GetInstance().SetUserId(TEST_USER_ID_NAME, "testUserId1");
1187405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1197405867cSopenharmony_ci    }
1207405867cSopenharmony_ci    {
1217405867cSopenharmony_ci        std::string strUserId;
1227405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserId(TEST_USER_ID_NAME, strUserId);
1237405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1247405867cSopenharmony_ci        ASSERT_EQ(strUserId, "testUserId1");
1257405867cSopenharmony_ci    }
1267405867cSopenharmony_ci
1277405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest003 end" << std::endl;
1287405867cSopenharmony_ci}
1297405867cSopenharmony_ci
1307405867cSopenharmony_ci/**
1317405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest004
1327405867cSopenharmony_ci * @tc.desc: Test to remove and get user id.
1337405867cSopenharmony_ci * @tc.type: FUNC
1347405867cSopenharmony_ci * @tc.require: issueI8EOLQ
1357405867cSopenharmony_ci */
1367405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest004, TestSize.Level3)
1377405867cSopenharmony_ci{
1387405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest004 start" << std::endl;
1397405867cSopenharmony_ci
1407405867cSopenharmony_ci    {
1417405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserId(TEST_USER_ID_NAME, TEST_USER_ID_VALUE);
1427405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1437405867cSopenharmony_ci
1447405867cSopenharmony_ci        ret = HiAppEvent::UserInfo::GetInstance().SetUserId(TEST_USER_ID_NAME, "");
1457405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1467405867cSopenharmony_ci    }
1477405867cSopenharmony_ci    {
1487405867cSopenharmony_ci        std::string strUserId;
1497405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserId(TEST_USER_ID_NAME, strUserId);
1507405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1517405867cSopenharmony_ci        ASSERT_EQ(strUserId, "");
1527405867cSopenharmony_ci    }
1537405867cSopenharmony_ci
1547405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest004 end" << std::endl;
1557405867cSopenharmony_ci}
1567405867cSopenharmony_ci
1577405867cSopenharmony_ci/**
1587405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest005
1597405867cSopenharmony_ci * @tc.desc: Test to set and get user property.
1607405867cSopenharmony_ci * @tc.type: FUNC
1617405867cSopenharmony_ci * @tc.require: issueI8EOLQ
1627405867cSopenharmony_ci */
1637405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest005, TestSize.Level3)
1647405867cSopenharmony_ci{
1657405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest005 start" << std::endl;
1667405867cSopenharmony_ci
1677405867cSopenharmony_ci    {
1687405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserProperty(TEST_USER_PROP_NAME, TEST_USER_PROP_VALUE);
1697405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1707405867cSopenharmony_ci    }
1717405867cSopenharmony_ci    {
1727405867cSopenharmony_ci        std::string strUserProperty;
1737405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserProperty(TEST_USER_PROP_NAME, strUserProperty);
1747405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1757405867cSopenharmony_ci        ASSERT_NE(strUserProperty, "");
1767405867cSopenharmony_ci    }
1777405867cSopenharmony_ci
1787405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest005 end" << std::endl;
1797405867cSopenharmony_ci}
1807405867cSopenharmony_ci
1817405867cSopenharmony_ci/**
1827405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest006
1837405867cSopenharmony_ci * @tc.desc: Test failed to get user property.
1847405867cSopenharmony_ci * @tc.type: FUNC
1857405867cSopenharmony_ci * @tc.require: issueI8EOLQ
1867405867cSopenharmony_ci */
1877405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest006, TestSize.Level3)
1887405867cSopenharmony_ci{
1897405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest006 start" << std::endl;
1907405867cSopenharmony_ci
1917405867cSopenharmony_ci    {
1927405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserProperty(TEST_USER_PROP_NAME, TEST_USER_PROP_VALUE);
1937405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1947405867cSopenharmony_ci    }
1957405867cSopenharmony_ci    {
1967405867cSopenharmony_ci        std::string strUserProperty;
1977405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserProperty("", strUserProperty);
1987405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
1997405867cSopenharmony_ci        ASSERT_EQ(strUserProperty, "");
2007405867cSopenharmony_ci    }
2017405867cSopenharmony_ci
2027405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest006 end" << std::endl;
2037405867cSopenharmony_ci}
2047405867cSopenharmony_ci
2057405867cSopenharmony_ci/**
2067405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest007
2077405867cSopenharmony_ci * @tc.desc: Test to set and get user property.
2087405867cSopenharmony_ci * @tc.type: FUNC
2097405867cSopenharmony_ci * @tc.require: issueI8EOLQ
2107405867cSopenharmony_ci */
2117405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest007, TestSize.Level3)
2127405867cSopenharmony_ci{
2137405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest007 start" << std::endl;
2147405867cSopenharmony_ci
2157405867cSopenharmony_ci    {
2167405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserProperty(TEST_USER_PROP_NAME, TEST_USER_PROP_VALUE);
2177405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
2187405867cSopenharmony_ci
2197405867cSopenharmony_ci        ret = HiAppEvent::UserInfo::GetInstance().SetUserProperty(TEST_USER_PROP_NAME, "testUserProperty1");
2207405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
2217405867cSopenharmony_ci    }
2227405867cSopenharmony_ci    {
2237405867cSopenharmony_ci        std::string strUserProperty;
2247405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserProperty(TEST_USER_PROP_NAME, strUserProperty);
2257405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
2267405867cSopenharmony_ci        ASSERT_EQ(strUserProperty, "testUserProperty1");
2277405867cSopenharmony_ci    }
2287405867cSopenharmony_ci
2297405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest007 end" << std::endl;
2307405867cSopenharmony_ci}
2317405867cSopenharmony_ci
2327405867cSopenharmony_ci/**
2337405867cSopenharmony_ci * @tc.name: HiAppEventUserInfoTest008
2347405867cSopenharmony_ci * @tc.desc: Test to remove and get user property.
2357405867cSopenharmony_ci * @tc.type: FUNC
2367405867cSopenharmony_ci * @tc.require: issueI8EOLQ
2377405867cSopenharmony_ci */
2387405867cSopenharmony_ciHWTEST_F(HiAppEventUserInfoTest, HiAppEventUserInfoTest008, TestSize.Level3)
2397405867cSopenharmony_ci{
2407405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest008 start" << std::endl;
2417405867cSopenharmony_ci
2427405867cSopenharmony_ci    {
2437405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().SetUserProperty(TEST_USER_PROP_NAME, TEST_USER_PROP_VALUE);
2447405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
2457405867cSopenharmony_ci
2467405867cSopenharmony_ci        ret = HiAppEvent::UserInfo::GetInstance().SetUserProperty(TEST_USER_PROP_NAME, "");
2477405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
2487405867cSopenharmony_ci    }
2497405867cSopenharmony_ci    {
2507405867cSopenharmony_ci        std::string strUserProperty;
2517405867cSopenharmony_ci        int ret = HiAppEvent::UserInfo::GetInstance().GetUserProperty(TEST_USER_PROP_NAME, strUserProperty);
2527405867cSopenharmony_ci        ASSERT_EQ(ret, 0);
2537405867cSopenharmony_ci        ASSERT_EQ(strUserProperty, "");
2547405867cSopenharmony_ci    }
2557405867cSopenharmony_ci
2567405867cSopenharmony_ci    std::cout << "HiAppEventUserInfoTest008 end" << std::endl;
2577405867cSopenharmony_ci}