1 /*
2 * Copyright (C) 2024 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 <gtest/gtest.h>
17 #include <string>
18 #include "account_observer.h"
19 #include <cstdlib>
20 #include "account_listener.h"
21
22 using namespace std;
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::Media;
26
27 namespace OHOS {
28 namespace Media {
29 namespace AccountObserverFuncUT {
30
31 class AccountObserverTestCallBack : public AccountObserverCallBack {
32 public:
AccountObserverTestCallBack()33 AccountObserverTestCallBack() {}
~AccountObserverTestCallBack()34 ~AccountObserverTestCallBack() {}
StopAndRelease(AVScreenCaptureStateCode state)35 bool StopAndRelease(AVScreenCaptureStateCode state)
36 {
37 return true;
38 }
NotifyStopAndRelease(AVScreenCaptureStateCode state)39 bool NotifyStopAndRelease(AVScreenCaptureStateCode state)
40 {
41 return true;
42 }
Release()43 void Release() {}
44 };
45
46 class AccountObserverTestFalseCallBack : public AccountObserverCallBack {
47 public:
AccountObserverTestFalseCallBack()48 AccountObserverTestFalseCallBack() {}
~AccountObserverTestFalseCallBack()49 ~AccountObserverTestFalseCallBack() {}
StopAndRelease(AVScreenCaptureStateCode state)50 bool StopAndRelease(AVScreenCaptureStateCode state)
51 {
52 return false;
53 }
NotifyStopAndRelease(AVScreenCaptureStateCode state)54 bool NotifyStopAndRelease(AVScreenCaptureStateCode state)
55 {
56 return false;
57 }
Release()58 void Release() {}
59 };
60
61 class AccountObserverInnerUnitTest : public testing::Test {
62 public:
63 static void SetUpTestCase(void);
64
65 static void TearDownTestCase(void);
66
67 void SetUp(void);
68
69 void TearDown(void);
70 };
71
SetUpTestCase(void)72 void AccountObserverInnerUnitTest::SetUpTestCase(void) {}
73
TearDownTestCase(void)74 void AccountObserverInnerUnitTest::TearDownTestCase(void) {}
75
SetUp(void)76 void AccountObserverInnerUnitTest::SetUp(void)
77 {
78 std::cout << "[SetUp]: SetUp!!!, test: "<< std::endl;
79 }
80
TearDown(void)81 void AccountObserverInnerUnitTest::TearDown(void)
82 {
83 std::cout << "[TearDown]: over!!!" << std::endl;
84 }
85
86 /**
87 * @tc.name: RegisterObserver_01
88 * @tc.desc: RegisterObserver_01
89 * @tc.type: FUNC
90 */
91 HWTEST_F(AccountObserverInnerUnitTest, RegisterObserver_01, TestSize.Level1)
92 {
93 ASSERT_TRUE(AccountObserver::GetInstance().RegisterObserver());
94 AccountObserver::GetInstance().UnregisterObserver();
95 }
96
97 /**
98 * @tc.name: RegisterAccountObserverCallBack_01
99 * @tc.desc: RegisterAccountObserverCallBack_01
100 * @tc.type: FUNC
101 */
102 HWTEST_F(AccountObserverInnerUnitTest, RegisterAccountObserverCallBack_01, TestSize.Level1)
103 {
104 auto accountObserverCallBack = std::make_shared<AccountObserverTestCallBack>();
105 ASSERT_TRUE(AccountObserver::GetInstance().RegisterAccountObserverCallBack(accountObserverCallBack));
106 AccountObserver::GetInstance().UnregisterAccountObserverCallBack(accountObserverCallBack);
107 ASSERT_TRUE(accountObserverCallBack->StopAndRelease(
108 AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER_SWITCHES));
109 }
110
111 /**
112 * @tc.name: AccountCallBackReturn_01
113 * @tc.desc: AccountCallBackReturn_01
114 * @tc.type: FUNC
115 */
116 HWTEST_F(AccountObserverInnerUnitTest, AccountCallBackReturn_01, TestSize.Level1)
117 {
118 AccountObserver::GetInstance().UnregisterObserver();
119 ASSERT_TRUE(AccountObserver::GetInstance().RegisterObserver());
120 auto accountObserverCallBack = std::make_shared<AccountObserverTestCallBack>();
121 ASSERT_TRUE(AccountObserver::GetInstance().RegisterAccountObserverCallBack(accountObserverCallBack));
122 ASSERT_TRUE(AccountObserver::GetInstance().OnAccountsSwitch());
123 AccountObserver::GetInstance().UnregisterObserver();
124 }
125 } // namespace AccountObserverFuncUT
126 } // namespace Media
127 } // namespace OHOS
128