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 <unistd.h>
18
19 #include <thread>
20 #include "client_adaptor.h"
21 #include "iservice_registry.h"
22 #include "itypes_util.h"
23 #include "logger.h"
24 #include "objectstore_errors.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::ObjectStore;
28 using namespace OHOS;
29 using namespace std;
30
31 namespace {
32 class ClientAdaptorTest : public testing::Test {
33 public:
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase(void)40 void ClientAdaptorTest::SetUpTestCase(void)
41 {
42 // input testsuit setup step,setup invoked before all testcases
43 }
44
TearDownTestCase(void)45 void ClientAdaptorTest::TearDownTestCase(void)
46 {
47 // input testsuit teardown step,teardown invoked after all testcases
48 }
49
SetUp(void)50 void ClientAdaptorTest::SetUp(void)
51 {
52 // input testcase setup step,setup invoked before each testcases
53 }
54
TearDown(void)55 void ClientAdaptorTest::TearDown(void)
56 {
57 // input testcase teardown step,teardown invoked after each testcases
58 }
59
60 /**
61 * @tc.name: OnRemoteDied_001
62 * @tc.desc: Normal test for OnRemoteDied
63 * @tc.type: FUNC
64 */
65
66 HWTEST_F(ClientAdaptorTest, OnRemoteDied_001, TestSize.Level1)
67 {
68 ClientAdaptor clientAdaptor;
69 sptr<IRemoteObject> impl = nullptr;
70 clientAdaptor.distributedDataMgr_ = std::make_shared<ObjectStoreDataServiceProxy>(impl);
71 wptr<IRemoteObject> remote = nullptr;
72 ClientAdaptor::ServiceDeathRecipient serviceDeathRecipient;
73 serviceDeathRecipient.OnRemoteDied(remote);
74 EXPECT_EQ(clientAdaptor.distributedDataMgr_, nullptr);
75 }
76
77 /**
78 * @tc.name: RegisterClientDeathListener_001
79 * @tc.desc: Abnormal test for RegisterClientDeathListener, remoteObject is nullptr
80 * @tc.type: FUNC
81 */
82
83 HWTEST_F(ClientAdaptorTest, RegisterClientDeathListener_001, TestSize.Level1)
84 {
85 ClientAdaptor clientAdaptor;
86 sptr<IRemoteObject> impl = nullptr;
87 clientAdaptor.distributedDataMgr_ = nullptr;
88 std::string appId = "123";
89 sptr<IRemoteObject> remoteObject = nullptr;
90 uint32_t ret = clientAdaptor.RegisterClientDeathListener(appId, remoteObject);
91 EXPECT_EQ(ret, ERR_EXIST);
92 }
93
94 /**
95 * @tc.name: RegisterClientDeathListener_002
96 * @tc.desc: Normal test for RegisterClientDeathListener
97 * @tc.type: FUNC
98 */
99
100 HWTEST_F(ClientAdaptorTest, RegisterClientDeathListener_002, TestSize.Level1)
101 {
102 ClientAdaptor clientAdaptor;
103 sptr<IRemoteObject> impl = nullptr;
104 clientAdaptor.distributedDataMgr_ = nullptr;
105 std::string appId = "123";
106 sptr<IRemoteObject> remoteObject = new IPCObjectStub();
107 uint32_t ret = clientAdaptor.RegisterClientDeathListener(appId, remoteObject);
108 EXPECT_EQ(ret, SUCCESS);
109 }
110 }
111