1 /*
2  * Copyright (c) 2021 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 #ifndef UNITTEST_OHOS_ABILITY_RUNTIME_LIFECYCLE_TEST_BASE_H
17 #define UNITTEST_OHOS_ABILITY_RUNTIME_LIFECYCLE_TEST_BASE_H
18 
19 #include <ctime>
20 #include <pthread.h>
21 #include <semaphore.h>
22 #include <iremote_object.h>
23 #include "ability_manager_service.h"
24 #include "ability_record.h"
25 #include "ability_scheduler.h"
26 #include "sa_mgr_client.h"
27 
28 /**
29  * @class LifeTestCommand
30  * asynchronous test thread use LifeTestCommand to call AbilityManagerService API.
31  */
32 class LifeTestCommand {
33 public:
LifeTestCommand()34     LifeTestCommand()
35         : state_(OHOS::AAFwk::AbilityState::INITIAL),
36         expectState_(OHOS::AAFwk::AbilityState::INITIAL),
37         abnormalState_(OHOS::AAFwk::AbilityState::INITIAL),
38         callback_(true)
39     {
40         sem_init(&sem_, 0, 0);
41     }
42 
~LifeTestCommand()43     ~LifeTestCommand()
44     {
45         sem_destroy(&sem_);
46     }
47 
48     OHOS::AAFwk::AbilityState state_;          // actual life
49     OHOS::AAFwk::AbilityState expectState_;    // expect life
50     OHOS::AAFwk::AbilityState abnormalState_;  // test abnormal condition life
51     bool callback_;                            // if false client won't callback_ to construct timeout case.
52     sem_t sem_;
53 };
54 
55 class LifecycleTestBase {
56 public:
57     virtual bool StartNextAbility() = 0;
58     virtual int AttachAbility(
59         const OHOS::sptr<OHOS::AAFwk::AbilityScheduler>& scheduler, const OHOS::sptr<OHOS::IRemoteObject>& token) = 0;
60 
61     static constexpr uint32_t DELAY_TEST_TIME = 1000;  // ms
62     static constexpr long MILLISECONDS = 1000;
63     static constexpr long NANOSECONDS = 1000000000;
64 
AbilityStartThread(void * command)65     static void* AbilityStartThread(void* command)
66     {
67         auto c = static_cast<LifeTestCommand*>(command);
68         if (c == nullptr) {
69             return nullptr;
70         }
71         if (c->callback_) {
72             if (c->abnormalState_ != OHOS::AAFwk::AbilityState::INITIAL) {
73                 c->state_ = c->abnormalState_;
74             } else {
75                 c->state_ = c->expectState_;
76             }
77             sem_post(&(c->sem_));
78         }
79         return c;
80     }
81 
SemTimedWaitMillis(long msecs,sem_t & sem)82     static int SemTimedWaitMillis(long msecs, sem_t& sem)
83     {
84         if (msecs <= 0) {
85             return 0;
86         }
87         msecs = msecs % MILLISECONDS;
88         long secs = msecs / MILLISECONDS;
89         struct timespec ts = { 0, 0 };
90         clock_gettime(CLOCK_REALTIME, &ts);
91         msecs = msecs * MILLISECONDS * MILLISECONDS + ts.tv_nsec;
92         long add = msecs / NANOSECONDS;
93         ts.tv_sec += (add + secs);
94         ts.tv_nsec = msecs % NANOSECONDS;
95         return sem_timedwait(&sem, &ts);
96     }
97 };
98 
99 #endif  // UNITTEST_OHOS_ABILITY_RUNTIME_LIFECYCLE_TEST_BASE_H
100