1 /*
2 * Copyright (c) 2025 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
18 #define private public
19
20 #include "ans_inner_errors.h"
21 #include "mock_distributed_operation_callback.h"
22 #include "notification_operation_service.h"
23 #include "mock_distributed_operation_callback.h"
24
25 namespace OHOS {
26 namespace Notification {
27
28 using namespace testing::ext;
29
30 class DistributedOperationServiceTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
SetUp()34 void SetUp() {};
TearDown()35 void TearDown() {};
36 };
37
SetUpTestCase()38 void DistributedOperationServiceTest::SetUpTestCase()
39 {
40 GTEST_LOG_(INFO) << "SetUp Case start";
41 GTEST_LOG_(INFO) << "SetUp end";
42 }
43
TearDownTestCase()44 void DistributedOperationServiceTest::TearDownTestCase()
45 {
46 GTEST_LOG_(INFO) << "TearDown case";
47 }
48
49 /**
50 * @tc.name: Device sync switch check
51 * @tc.desc: Test device data service
52 * @tc.type: FUNC
53 * @tc.require: issue
54 */
55 HWTEST_F(DistributedOperationServiceTest, DeviceData_00001, Function | SmallTest | Level1)
56 {
57 MockOperationCallback::ResetOperationResult();
58 // add operation.
59 sptr<MockOperationCallback> callback = new (std::nothrow) MockOperationCallback();
60 DistributedOperationService::GetInstance().AddOperation("abcd", callback);
61 // remove operation.
62 DistributedOperationService::GetInstance().RemoveOperationResponse("abcd");
63 // not return operation result.
64 int32_t result = MockOperationCallback::GetOperationResult();
65 ASSERT_EQ(result, -1);
66 }
67
68 /**
69 * @tc.name: Device sync switch check
70 * @tc.desc: Test device data service
71 * @tc.type: FUNC
72 * @tc.require: issue
73 */
74 HWTEST_F(DistributedOperationServiceTest, DeviceData_00002, Function | SmallTest | Level1)
75 {
76 MockOperationCallback::ResetOperationResult();
77 // add operation.
78 sptr<MockOperationCallback> callback1 = new (std::nothrow) MockOperationCallback();
79 sptr<MockOperationCallback> callback2 = new (std::nothrow) MockOperationCallback();
80 DistributedOperationService::GetInstance().AddOperation("abcd", callback1);
81 DistributedOperationService::GetInstance().AddOperation("abcd", callback2);
82 // invoke time out operation.
83 DistributedOperationService::GetInstance().HandleOperationTimeOut("abcd");
84 // time out operation result.
85 sleep(1);
86 int32_t result = MockOperationCallback::GetOperationResult();
87 ASSERT_EQ(result, (int)ERR_ANS_OPERATION_TIMEOUT);
88 }
89
90 /**
91 * @tc.name: Device sync switch check
92 * @tc.desc: Test device data service
93 * @tc.type: FUNC
94 * @tc.require: issue
95 */
96 HWTEST_F(DistributedOperationServiceTest, DeviceData_00003, Function | SmallTest | Level1)
97 {
98 MockOperationCallback::ResetOperationResult();
99 // add operation.
100 sptr<MockOperationCallback> callback = new (std::nothrow) MockOperationCallback();
101 DistributedOperationService::GetInstance().AddOperation("abcd", callback);
102 // invoke successful operation.
103 DistributedOperationService::GetInstance().ReplyOperationResponse("abcd", 0);
104 // invoke successful.
105 int32_t result = MockOperationCallback::GetOperationResult();
106 ASSERT_EQ(result, 0);
107 }
108
109 /**
110 * @tc.name: Device sync switch check
111 * @tc.desc: Test device data service
112 * @tc.type: FUNC
113 * @tc.require: issue
114 */
115 HWTEST_F(DistributedOperationServiceTest, DeviceData_00004, Function | SmallTest | Level1)
116 {
117 MockOperationCallback::ResetOperationResult();
118 // add operation.
119 sptr<MockOperationCallback> callback = new (std::nothrow) MockOperationCallback();
120 DistributedOperationService::GetInstance().AddOperation("abcd", callback);
121 DistributedOperationService::GetInstance().operationQueue_ = nullptr;
122 OperationTimerInfo TimerInfo = OperationTimerInfo("hashCode");
123 TimerInfo.OnTrigger();
124 // invoke successful operation.
125 sleep(4);
126 int32_t result = MockOperationCallback::GetOperationResult();
127 ASSERT_EQ(result, -1);
128 }
129 }
130 }
131