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 }
39 };
40
41 class AccountObserverTestFalseCallBack : public AccountObserverCallBack {
42 public:
AccountObserverTestFalseCallBack()43 AccountObserverTestFalseCallBack() {}
~AccountObserverTestFalseCallBack()44 ~AccountObserverTestFalseCallBack() {}
StopAndRelease(AVScreenCaptureStateCode state)45 bool StopAndRelease(AVScreenCaptureStateCode state)
46 {
47 return false;
48 }
49 };
50
51 class AccountObserverInnerUnitTest : public testing::Test {
52 public:
53 static void SetUpTestCase(void);
54
55 static void TearDownTestCase(void);
56
57 void SetUp(void);
58
59 void TearDown(void);
60 };
61
SetUpTestCase(void)62 void AccountObserverInnerUnitTest::SetUpTestCase(void) {}
63
TearDownTestCase(void)64 void AccountObserverInnerUnitTest::TearDownTestCase(void) {}
65
SetUp(void)66 void AccountObserverInnerUnitTest::SetUp(void)
67 {
68 std::cout << "[SetUp]: SetUp!!!, test: "<< std::endl;
69 }
70
TearDown(void)71 void AccountObserverInnerUnitTest::TearDown(void)
72 {
73 std::cout << "[TearDown]: over!!!" << std::endl;
74 }
75
76 /**
77 * @tc.name: RegisterObserver_01
78 * @tc.desc: RegisterObserver_01
79 * @tc.type: FUNC
80 */
81 HWTEST_F(AccountObserverInnerUnitTest, RegisterObserver_01, TestSize.Level1)
82 {
83 ASSERT_TRUE(AccountObserver::GetInstance().RegisterObserver());
84 AccountObserver::GetInstance().UnregisterObserver();
85 }
86
87 /**
88 * @tc.name: RegisterAccountObserverCallBack_01
89 * @tc.desc: RegisterAccountObserverCallBack_01
90 * @tc.type: FUNC
91 */
92 HWTEST_F(AccountObserverInnerUnitTest, RegisterAccountObserverCallBack_01, TestSize.Level1)
93 {
94 auto accountObserverCallBack = std::make_shared<AccountObserverTestCallBack>();
95 ASSERT_TRUE(AccountObserver::GetInstance().RegisterAccountObserverCallBack(accountObserverCallBack));
96 AccountObserver::GetInstance().UnregisterAccountObserverCallBack();
97 ASSERT_TRUE(accountObserverCallBack->StopAndRelease(
98 AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER_SWITCHES));
99 }
100
101 /**
102 * @tc.name: AccountCallBackReturn_01
103 * @tc.desc: AccountCallBackReturn_01
104 * @tc.type: FUNC
105 */
106 HWTEST_F(AccountObserverInnerUnitTest, AccountCallBackReturn_01, TestSize.Level1)
107 {
108 AccountObserver::GetInstance().UnregisterObserver();
109 ASSERT_TRUE(AccountObserver::GetInstance().RegisterObserver());
110 auto accountObserverCallBack = std::make_shared<AccountObserverTestCallBack>();
111 ASSERT_TRUE(AccountObserver::GetInstance().RegisterAccountObserverCallBack(accountObserverCallBack));
112 ASSERT_TRUE(AccountObserver::GetInstance().OnAccountsSwitch());
113 AccountObserver::GetInstance().UnregisterObserver();
114 }
115 } // namespace AccountObserverFuncUT
116 } // namespace Media
117 } // namespace OHOS
118