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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "account_status_listener.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 
23 namespace OHOS::FileManagement::CloudDisk::Test {
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace std;
27 using Want = OHOS::AAFwk::Want;
28 
29 constexpr int32_t USER_ID = 101;
30 
31 class AccountStatusListenerTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp();
36     void TearDown();
37     static inline shared_ptr<AccountStatusListener> accountStatusListener_ = nullptr;
38     static inline shared_ptr<AccountStatusSubscriber> accountStatusSubscriber_ = nullptr;
39     static inline shared_ptr<EventFwk::CommonEventSubscribeInfo> subscribeInfo_ = nullptr;
40 };
41 
SetUpTestCase(void)42 void AccountStatusListenerTest::SetUpTestCase(void)
43 {
44     GTEST_LOG_(INFO) << "SetUpTestCase";
45     EventFwk::MatchingSkills matchingSkills;
46     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
47     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
48     subscribeInfo_ = make_shared<EventFwk::CommonEventSubscribeInfo>(matchingSkills);
49     accountStatusSubscriber_ = make_shared<AccountStatusSubscriber>(*subscribeInfo_);
50     accountStatusListener_ = make_shared<AccountStatusListener>();
51 }
52 
TearDownTestCase(void)53 void AccountStatusListenerTest::TearDownTestCase(void)
54 {
55     GTEST_LOG_(INFO) << "TearDownTestCase";
56 }
57 
SetUp(void)58 void AccountStatusListenerTest::SetUp(void)
59 {
60     GTEST_LOG_(INFO) << "SetUp";
61 }
62 
TearDown(void)63 void AccountStatusListenerTest::TearDown(void)
64 {
65     GTEST_LOG_(INFO) << "TearDown";
66 }
67 
68 /**
69  * @tc.name: OnReceiveEventTest001
70  * @tc.desc: Verify the OnReceiveEvent function
71  * @tc.type: FUNC
72  * @tc.require: issuesI90JZZ
73  */
74 HWTEST_F(AccountStatusListenerTest, OnReceiveEventTest001, TestSize.Level1)
75 {
76     GTEST_LOG_(INFO) << "OnReceiveEventTest001 Start";
77     try {
78         Want want;
79         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
80         string data(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
81         EventFwk::CommonEventData eventData(want, USER_ID, data);
82         accountStatusSubscriber_->OnReceiveEvent(eventData);
83         EXPECT_TRUE(true);
84     } catch (...) {
85         EXPECT_TRUE(false);
86         GTEST_LOG_(INFO) << "OnReceiveEventTest001  ERROR";
87     }
88     GTEST_LOG_(INFO) << "OnReceiveEventTest001 End";
89 }
90 
91 /**
92  * @tc.name: OnReceiveEventTest002
93  * @tc.desc: Verify the OnReceiveEvent function
94  * @tc.type: FUNC
95  * @tc.require: issuesI90JZZ
96  */
97 HWTEST_F(AccountStatusListenerTest, OnReceiveEventTest002, TestSize.Level1)
98 {
99     GTEST_LOG_(INFO) << "OnReceiveEventTest002 Start";
100     try {
101         Want want;
102         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
103         string data(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
104         EventFwk::CommonEventData eventData(want, USER_ID, data);
105         accountStatusSubscriber_->OnReceiveEvent(eventData);
106         EXPECT_TRUE(true);
107     } catch (...) {
108         EXPECT_TRUE(false);
109         GTEST_LOG_(INFO) << "OnReceiveEventTest002  ERROR";
110     }
111     GTEST_LOG_(INFO) << "OnReceiveEventTest002 End";
112 }
113 
114 /**
115  * @tc.name: StopTest001
116  * @tc.desc: Verify the Stop function
117  * @tc.type: FUNC
118  * @tc.require: issuesI90JZZ
119  */
120 HWTEST_F(AccountStatusListenerTest, StopTest001, TestSize.Level1)
121 {
122     GTEST_LOG_(INFO) << "StopTest001 Start";
123     try {
124         accountStatusListener_->commonEventSubscriber_ = std::make_shared<AccountStatusSubscriber>(*subscribeInfo_);
125         accountStatusListener_->Stop();
126         EXPECT_TRUE(accountStatusListener_->commonEventSubscriber_ == nullptr);
127     } catch (...) {
128         EXPECT_TRUE(false);
129         GTEST_LOG_(INFO) << "StopTest001  ERROR";
130     }
131     GTEST_LOG_(INFO) << "StopTest001 End";
132 }
133 
134 /**
135  * @tc.name: StopTest002
136  * @tc.desc: Verify the Stop function
137  * @tc.type: FUNC
138  * @tc.require: issuesI90JZZ
139  */
140 HWTEST_F(AccountStatusListenerTest, StopTest002, TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "StopTest002 Start";
143     try {
144         accountStatusListener_->commonEventSubscriber_ = nullptr;
145         accountStatusListener_->Stop();
146         EXPECT_TRUE(accountStatusListener_->commonEventSubscriber_ == nullptr);
147     } catch (...) {
148         EXPECT_TRUE(false);
149         GTEST_LOG_(INFO) << "StopTest002  ERROR";
150     }
151     GTEST_LOG_(INFO) << "StopTest002 End";
152 }
153 } // namespace OHOS::FileManagement::CloudDisk::Test