• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 #include <memory>
18 
19 #include "cloud_sync_callback_manager.h"
20 #include "cloud_sync_callback_proxy.h"
21 
22 #include "dfs_error.h"
23 #include "iservice_registry.h"
24 #include "service_callback_mock.h"
25 
26 namespace OHOS {
27 namespace FileManagement::CloudSync {
28 namespace Test {
29 using namespace testing::ext;
30 using namespace testing;
31 using namespace std;
32 
33 std::shared_ptr<CloudSyncCallbackManager> g_managePtr_;
34 
35 class CloudSyncCallbackManagerTest : 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 CloudSyncCallbackManagerTest::SetUpTestCase(void)
44 {
45     std::cout << "SetUpTestCase" << std::endl;
46 }
47 
TearDownTestCase(void)48 void CloudSyncCallbackManagerTest::TearDownTestCase(void)
49 {
50     std::cout << "TearDownTestCase" << std::endl;
51 }
52 
SetUp(void)53 void CloudSyncCallbackManagerTest::SetUp(void)
54 {
55     if (g_managePtr_ == nullptr) {
56         g_managePtr_ = make_shared<CloudSyncCallbackManager>();
57         ASSERT_TRUE(g_managePtr_ != nullptr) << "CallbackManager failed";
58     }
59     std::cout << "SetUp" << std::endl;
60 }
61 
TearDown(void)62 void CloudSyncCallbackManagerTest::TearDown(void)
63 {
64     g_managePtr_ = nullptr;
65     std::cout << "TearDown" << std::endl;
66 }
67 
68 /*
69  * @tc.name: RemoveCallbackTest
70  * @tc.desc: Verify the RemoveCallback function.
71  * @tc.type: FUNC
72  * @tc.require: I6H5MH
73  */
74 HWTEST_F(CloudSyncCallbackManagerTest, RemoveCallbackTest, TestSize.Level1)
75 {
76     GTEST_LOG_(INFO) << "RemoveCallbackTest Start";
77     try {
78         const string bundleName = "com.ohos.photos";
79         sptr<CloudSyncCallbackMock> callback = sptr(new CloudSyncCallbackMock());
80         const int userId = 0;
81         g_managePtr_->AddCallback(bundleName, userId, callback);
82         CloudSyncCallbackManager::CallbackInfo cbInfo;
83         int res = g_managePtr_->callbackListMap_.Find(bundleName, cbInfo);
84         EXPECT_EQ(res, true);
85         EXPECT_NE(cbInfo.callbackProxy, nullptr);
86         g_managePtr_->RemoveCallback(bundleName);
87     } catch (...) {
88         EXPECT_TRUE(false);
89         GTEST_LOG_(INFO) << " RemoveCallbackTest FAILED";
90     }
91     GTEST_LOG_(INFO) << "RemoveCallbackTest End";
92 }
93 
94 /*
95  * @tc.name: AddCallbackTest
96  * @tc.desc: Verify the AddCallback function.
97  * @tc.type: FUNC
98  * @tc.require: I6H5MH
99  */
100 HWTEST_F(CloudSyncCallbackManagerTest, AddCallbackTest, TestSize.Level1)
101 {
102     GTEST_LOG_(INFO) << "AddCallbackTest Start";
103     try {
104         const string bundleName = "com.ohos.photos";
105         sptr<CloudSyncCallbackMock> callback = sptr(new CloudSyncCallbackMock());
106         const int userId = 0;
107         g_managePtr_->AddCallback(bundleName, userId, callback);
108         CloudSyncCallbackManager::CallbackInfo cbInfo;
109         int res = g_managePtr_->callbackListMap_.Find(bundleName, cbInfo);
110         EXPECT_EQ(res, true);
111         EXPECT_NE(cbInfo.callbackProxy, nullptr);
112     } catch (...) {
113         EXPECT_TRUE(false);
114         GTEST_LOG_(INFO) << " AddCallbackTest FAILED";
115     }
116     GTEST_LOG_(INFO) << "AddCallbackTest End";
117 }
118 
119 /*
120  * @tc.name: SetDeathRecipientTest
121  * @tc.desc: Verify the SetDeathRecipient function.
122  * @tc.type: FUNC
123  * @tc.require: I6H5MH
124  */
125 HWTEST_F(CloudSyncCallbackManagerTest, SetDeathRecipientTest, TestSize.Level1)
126 {
127     GTEST_LOG_(INFO) << "SetDeathRecipient Start";
128     try {
129         const string bundleName = "com.ohos.photos";
130         sptr<CloudSyncCallbackMock> callback = sptr(new CloudSyncCallbackMock());
131         const int userId = 0;
132         g_managePtr_->AddCallback(bundleName, userId, callback);
133         CloudSyncCallbackManager::CallbackInfo cbInfo;
134         int res = g_managePtr_->callbackListMap_.Find(bundleName, cbInfo);
135         EXPECT_TRUE(res);
136         g_managePtr_->SetDeathRecipient(bundleName, cbInfo);
137         cbInfo.callbackProxy = nullptr;
138     } catch (...) {
139         EXPECT_TRUE(false);
140         GTEST_LOG_(INFO) << " SetDeathRecipient ERROR";
141     }
142     GTEST_LOG_(INFO) << "SetDeathRecipient End";
143 }
144 
145 /*
146  * @tc.name: NotifyTest
147  * @tc.desc: Verify the Notify function.
148  * @tc.type: FUNC
149  * @tc.require: I6H5MH
150  */
151 HWTEST_F(CloudSyncCallbackManagerTest, NotifyTest, TestSize.Level1)
152 {
153     GTEST_LOG_(INFO) << "Notify Start";
154     try {
155         const string bundleName = "com.ohos.photos";
156         CloudSyncState state = CloudSyncState::COMPLETED;
157         ErrorType error = ErrorType::NO_ERROR;
158         sptr<CloudSyncCallbackMock> callback = sptr(new CloudSyncCallbackMock());
159         const int userId = 0;
160         g_managePtr_->AddCallback(bundleName, userId, callback);
161         CloudSyncCallbackManager::CallbackInfo cbInfo;
162         int res = g_managePtr_->callbackListMap_.Find(bundleName, cbInfo);
163         EXPECT_TRUE(res);
164         auto callbackProxy = cbInfo.callbackProxy;
165         EXPECT_NE(callbackProxy, nullptr);
166         callbackProxy->OnSyncStateChanged(state, error);
167         g_managePtr_->Notify(bundleName, cbInfo, state, error);
168     } catch (...) {
169         EXPECT_TRUE(false);
170         GTEST_LOG_(INFO) << " Notify ERROR";
171     }
172     GTEST_LOG_(INFO) << "Notify End";
173 }
174 
175 } // namespace Test
176 } // namespace FileManagement::CloudSync
177 } // namespace OHOS
178