• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include <string>
19 #include <thread>
20 #include "group_impl.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 using namespace testing::ext;
25 using namespace OHOS::AppExecFwk;
26 
27 class GroupImplTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void GroupImplTest::SetUpTestCase(void)
36 {}
37 
TearDownTestCase(void)38 void GroupImplTest::TearDownTestCase(void)
39 {}
40 
SetUp(void)41 void GroupImplTest::SetUp(void)
42 {}
43 
TearDown(void)44 void GroupImplTest::TearDown(void)
45 {}
46 
47 /**
48  * @tc.number: AppExecFwk_GroupImpl_ConstructorTest_001
49  * @tc.name: GroupImplConstructor
50  * @tc.desc: Test GroupImpl Constructor.
51  */
52 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_ConstructorTest_001, TestSize.Level0)
53 {
54     auto name = std::string("AppExecFwk_GroupImpl_ConstructorTest_001");
55     GTEST_LOG_(INFO) << name << " start";
56     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
57     EXPECT_TRUE((groupPtr != nullptr));
58     GTEST_LOG_(INFO) << name << " end";
59 }
60 
61 /**
62  * @tc.number: AppExecFwk_GroupImpl_AwaitAllTasks_001
63  * @tc.name: AwaitAllTasks
64  * @tc.desc: Test GroupImpl await all tasks done
65  */
66 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_AwaitAllTasks_001, TestSize.Level0)
67 {
68     auto name = std::string("AppExecFwk_GroupImpl_AwaitAllTasks_001");
69     GTEST_LOG_(INFO) << name << " start";
70     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
71     // add task
72     groupPtr->Associate();
73     groupPtr->Associate();
74     // add notification
75     std::shared_ptr<Runnable> notification1 =
__anona175f3e70102() 76         std::make_shared<Runnable>([]() { GTEST_LOG_(INFO) << "GroupImpl notification1"; });
77     std::shared_ptr<Runnable> notification2 =
__anona175f3e70202() 78         std::make_shared<Runnable>([]() { GTEST_LOG_(INFO) << "GroupImpl notification2"; });
79     groupPtr->AddNotification(notification1);
80     groupPtr->AddNotification(notification2);
81     // excute task
__anona175f3e70302() 82     std::thread excuteTask1([&]() { groupPtr->NotifyTaskDone(); });
__anona175f3e70402() 83     std::thread excuteTask2([&]() { groupPtr->NotifyTaskDone(); });
84     excuteTask1.detach();
85     excuteTask2.detach();
86     groupPtr->AwaitAllTasks(1000);
87     std::this_thread::sleep_for(std::chrono::milliseconds(3000));
88     GTEST_LOG_(INFO) << name << " end";
89 }
90 
91 /**
92  * @tc.number: AppExecFwk_Task_Run_002
93  * @tc.name: AwaitAllTasks
94  * @tc.desc: Test GroupImpl await all tasks done
95  */
96 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_AwaitAllTasks_002, TestSize.Level0)
97 {
98     auto name = std::string("AppExecFwk_Task_Run_002");
99     GTEST_LOG_(INFO) << name << " start";
100     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
101     // add task
102     groupPtr->Associate();
103     // add notification
104     std::shared_ptr<Runnable> notification =
__anona175f3e70502() 105         std::make_shared<Runnable>([]() { GTEST_LOG_(INFO) << "GroupImpl notification1"; });
106     groupPtr->AddNotification(notification);
107     bool result = groupPtr->AwaitAllTasks(1000);
108     EXPECT_FALSE(result);
109     GTEST_LOG_(INFO) << name << " end";
110 }
111 
112 /**
113  * @tc.number: AppExecFwk_GroupImpl_Associate_001
114  * @tc.name: Associate
115  * @tc.desc: Test GroupImpl add task
116  */
117 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_Associate_001, TestSize.Level0)
118 {
119     auto name = std::string("AppExecFwk_GroupImpl_Associate_001");
120     GTEST_LOG_(INFO) << name << " start";
121     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
122     // add task
123     groupPtr->Associate();
124     bool result = groupPtr->AwaitAllTasks(1000);
125     EXPECT_FALSE(result);
126     GTEST_LOG_(INFO) << name << " end";
127 }
128 
129 /**
130  * @tc.number: AppExecFwk_GroupImpl_NotifyTaskDone_001
131  * @tc.name: NotifyTaskDone
132  * @tc.desc: Test GroupImpl notify task done
133  */
134 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_NotifyTaskDone_001, TestSize.Level0)
135 {
136     auto name = std::string("AppExecFwk_GroupImpl_NotifyTaskDone_001");
137     GTEST_LOG_(INFO) << name << " start";
138     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
139     groupPtr->Associate();
140     groupPtr->NotifyTaskDone();
141     bool result = groupPtr->AwaitAllTasks(1000);
142     EXPECT_TRUE(result);
143     GTEST_LOG_(INFO) << name << " end";
144 }
145 
146 /**
147  * @tc.number: AppExecFwk_GroupImpl_AddNotification_001
148  * @tc.name: AddNotification
149  * @tc.desc: Test GroupImpl add a notification
150  */
151 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_AddNotification_001, TestSize.Level0)
152 {
153     auto name = std::string("AppExecFwk_GroupImpl_AddNotification_001");
154     GTEST_LOG_(INFO) << name << " start";
155     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
156     std::shared_ptr<Runnable> notification =
__anona175f3e70602() 157         std::make_shared<Runnable>([]() { GTEST_LOG_(INFO) << "GroupImpl AddNotification"; });
158     bool result = groupPtr->AddNotification(notification);
159     EXPECT_TRUE(result);
160     GTEST_LOG_(INFO) << name << " end";
161 }
162 
163 /**
164  * @tc.number: AppExecFwk_GroupImpl_AddNotification_002
165  * @tc.name: AddNotification
166  * @tc.desc: Test GroupImpl max number of notifications
167  */
168 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_AddNotification_002, TestSize.Level0)
169 {
170     auto name = std::string("AppExecFwk_GroupImpl_AddNotification_002");
171     GTEST_LOG_(INFO) << name << " start";
172     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
173     for (int i = 0; i < 1000; i++) {
174         std::shared_ptr<Runnable> notification =
__anona175f3e70702() 175             std::make_shared<Runnable>([&]() { GTEST_LOG_(INFO) << "GroupImpl AddNotification"; });
176         groupPtr->AddNotification(notification);
177     }
178     std::shared_ptr<Runnable> notificationTask =
__anona175f3e70802() 179         std::make_shared<Runnable>([&]() { GTEST_LOG_(INFO) << "GroupImpl AddNotification"; });
180     bool result = groupPtr->AddNotification(notificationTask);
181     EXPECT_TRUE(result);
182     GTEST_LOG_(INFO) << name << " end";
183 }
184 
185 /**
186  * @tc.number: AppExecFwk_GroupImpl_AddNotification_003
187  * @tc.name: AddNotification
188  * @tc.desc: Test GroupImpl max number of notifications
189  */
190 HWTEST(GroupImplTest, AppExecFwk_GroupImpl_AddNotification_003, TestSize.Level0)
191 {
192     auto name = std::string("AppExecFwk_GroupImpl_AddNotification_003");
193     GTEST_LOG_(INFO) << name << " start";
194     std::shared_ptr<GroupImpl> groupPtr = std::make_shared<GroupImpl>();
195     groupPtr->Associate();
196     for (int i = 0; i < 1000; i++) {
197         std::shared_ptr<Runnable> notification =
__anona175f3e70902() 198             std::make_shared<Runnable>([&]() { GTEST_LOG_(INFO) << "GroupImpl AddNotification"; });
199         groupPtr->AddNotification(notification);
200     }
201     std::shared_ptr<Runnable> notificationTask =
__anona175f3e70a02() 202         std::make_shared<Runnable>([&]() { GTEST_LOG_(INFO) << "GroupImpl AddNotification"; });
203     bool result = groupPtr->AddNotification(notificationTask);
204     EXPECT_FALSE(result);
205     GTEST_LOG_(INFO) << name << " end";
206 }
207 }  // namespace AppExecFwk
208 }  // namespace OHOS