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