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