1 /*
2  * Copyright (c) 2022-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 <condition_variable>
17 #include <cstdio>
18 
19 #include <gtest/gtest.h>
20 
21 #include "iservice_registry.h"
22 #include "service_reverse_mock.h"
23 #include "svc_death_recipient.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27 using namespace testing;
28 
29 const string FILE_NAME = "temp.json";
30 static condition_variable g_cv;
31 static atomic<bool> g_serviceDie = false;
32 
33 class SvcDeathRecipientTest : public testing::Test {
34 public:
SetUpTestCase(void)35     static void SetUpTestCase(void) {};
TearDownTestCase()36     static void TearDownTestCase() {};
SetUp()37     void SetUp() override {};
TearDown()38     void TearDown() override {};
39 
40 public:
41     mutex lock_;
42 };
43 
CallBack(const wptr<IRemoteObject> & obj)44 static void CallBack(const wptr<IRemoteObject> &obj)
45 {
46     g_serviceDie.store(true);
47     g_cv.notify_all();
48     GTEST_LOG_(INFO) << "SvcSessionManagerTest-CallBack success";
49 }
50 
51 /**
52  * @tc.number: SUB_backup_sa_deathecipient_OnRemoteDied_0100
53  * @tc.name: SUB_backup_sa_deathecipient_OnRemoteDied_0100
54  * @tc.desc: 测试 OnRemoteDied 接口调用成功
55  * @tc.size: MEDIUM
56  * @tc.type: FUNC
57  * @tc.level Level 1
58  * @tc.require: I6F3GV
59  */
60 HWTEST_F(SvcDeathRecipientTest, SUB_backup_sa_deathecipient_OnRemoteDied_0100, testing::ext::TestSize.Level1)
61 {
62     GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_deathecipient_OnRemoteDied_0100";
63     sptr<ServiceReverseMock> remote = sptr(new ServiceReverseMock());
64     sptr<SvcDeathRecipient> deathRecipient = sptr(new SvcDeathRecipient(CallBack));
65     remote->AddDeathRecipient(deathRecipient);
66     deathRecipient->OnRemoteDied(remote);
67     remote = nullptr;
68     deathRecipient = nullptr;
69     unique_lock<mutex> lk(lock_);
__anonc818dd0d0102null70     g_cv.wait(lk, [&] { return true; });
71     ASSERT_TRUE(g_serviceDie);
72     GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_deathecipient_OnRemoteDied_0100";
73 }
74 } // namespace OHOS::FileManagement::Backup