1 /*
2  * Copyright (c) 2022 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 <iostream>
17 #include <memory>
18 
19 #include "gtest/gtest.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "multiuser/os_account_observer.h"
23 #include "utils_log.h"
24 
25 namespace OHOS {
26 namespace Storage {
27 namespace DistributedFile {
28 namespace Test {
29 using namespace testing::ext;
30 using Want = OHOS::AAFwk::Want;
31 constexpr int32_t USER_ID = 101;
32 std::shared_ptr<OsAccountObserver> g_subScriber = nullptr;
33 
34 class OsAccountObserverTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
37     static void TearDownTestCase(void);
38     void SetUp();
39     void TearDown();
40 };
41 
SetUpTestCase(void)42 void OsAccountObserverTest::SetUpTestCase(void)
43 {
44     // input testsuit setup step,setup invoked before all testcases
45     if (g_subScriber == nullptr) {
46         EventFwk::MatchingSkills matchingSkills;
47         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
48         EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
49         g_subScriber = std::make_shared<OsAccountObserver>(subscribeInfo);
50         if (g_subScriber == nullptr) {
51             LOGE("subscriber is nullptr");
52             return;
53         }
54 
55         bool subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(g_subScriber);
56         if (!subRet) {
57             LOGE("Subscribe common event failed");
58         }
59     }
60 }
61 
TearDownTestCase(void)62 void OsAccountObserverTest::TearDownTestCase(void)
63 {
64     // input testsuit teardown step,teardown invoked after all testcases
65     bool subRet = EventFwk::CommonEventManager::UnSubscribeCommonEvent(g_subScriber);
66     if (!subRet) {
67         LOGE("UnSubscribe common event failed");
68     }
69     g_subScriber = nullptr;
70 }
71 
SetUp(void)72 void OsAccountObserverTest::SetUp(void)
73 {
74     // input testcase setup step,setup invoked before each testcases
75 }
76 
TearDown(void)77 void OsAccountObserverTest::TearDown(void)
78 {
79     // input testcase teardown step,teardown invoked after each testcases
80 }
81 
82 /**
83  * @tc.name: OsAccountObserverTest_OnReceiveEvent_0100
84  * @tc.desc: Verify the OnReceiveEvent function.
85  * @tc.type: FUNC
86  * @tc.require: SR000H0387
87  */
88 HWTEST_F(OsAccountObserverTest, OsAccountObserverTest_OnReceiveEvent_0100, TestSize.Level1)
89 {
90     GTEST_LOG_(INFO) << "OsAccountObserverTest_OnReceiveEvent_0100 start";
91     bool res = true;
92     try {
93         if (g_subScriber != nullptr) {
94             Want want;
95             want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
96             int32_t code = USER_ID;
97             std::string data(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
98             EventFwk::CommonEventData eventData(want, code, data);
99             g_subScriber->OnReceiveEvent(eventData);
100         }
101     } catch (const std::exception &e) {
102         res = false;
103         LOGE("OsAccountObserverTest_OnReceiveEvent_0100 : %{public}s", e.what());
104     }
105     EXPECT_TRUE(res == true);
106     GTEST_LOG_(INFO) << "OsAccountObserverTest_OnReceiveEvent_0100 end";
107 }
108 } // namespace Test
109 } // namespace DistributedFile
110 } // namespace Storage
111 } // namespace OHOS