1 /*
2 * Copyright (c) 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 "app_state_observer_test.h"
17
18 #include "test_log.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace DistributedSchedule {
25 namespace {
26 constexpr int32_t FOREGROUND = 2;
27 constexpr int32_t BACKGROUND = 4;
28 }
29
SetUpTestCase()30 void AppStateObserverTest::SetUpTestCase()
31 {
32 DTEST_LOG << "AppStateObserverTest::SetUpTestCase" << std::endl;
33 }
34
TearDownTestCase()35 void AppStateObserverTest::TearDownTestCase()
36 {
37 DTEST_LOG << "AppStateObserverTest::TearDownTestCase" << std::endl;
38 }
39
TearDown()40 void AppStateObserverTest::TearDown()
41 {
42 DTEST_LOG << "AppStateObserverTest::TearDown" << std::endl;
43 }
44
SetUp()45 void AppStateObserverTest::SetUp()
46 {
47 DTEST_LOG << "AppStateObserverTest::SetUp" << std::endl;
48 appStateObserver_ = new AppStateObserver();
49 }
50
51 /**
52 * @tc.name: OnAbilityStateChanged_001
53 * @tc.desc: call OnAbilityStateChanged from distributedsched
54 * @tc.type: FUNC
55 * @tc.require: I6SJQ6
56 */
57 HWTEST_F(AppStateObserverTest, OnAbilityStateChanged_001, TestSize.Level3)
58 {
59 DTEST_LOG << "AppStateObserverTest OnAbilityStateChanged_001 begin" << std::endl;
60
61 ASSERT_NE(appStateObserver_, nullptr);
62 AppExecFwk::AppStateData appStateData;
63 appStateObserver_->OnForegroundApplicationChanged(appStateData);
64
65 AppExecFwk::AbilityStateData extensionStateData;
66 appStateObserver_->OnExtensionStateChanged(extensionStateData);
67
68 AppExecFwk::ProcessData processData;
69 appStateObserver_->OnProcessCreated(processData);
70 appStateObserver_->OnProcessDied(processData);
71
72 AppExecFwk::AbilityStateData abilityStateData;
73 appStateObserver_->OnAbilityStateChanged(abilityStateData);
74 EXPECT_NE(appStateObserver_, nullptr);
75 DTEST_LOG << "AppStateObserverTest OnAbilityStateChanged_001 end" << std::endl;
76 }
77
78 /**
79 * @tc.name: OnAbilityStateChanged_002
80 * @tc.desc: call OnAbilityStateChanged from distributedsched
81 * @tc.type: FUNC
82 * @tc.require: I6SJQ6
83 */
84 HWTEST_F(AppStateObserverTest, OnAbilityStateChanged_002, TestSize.Level3)
85 {
86 DTEST_LOG << "AppStateObserverTest OnAbilityStateChanged_001 begin" << std::endl;
87 ASSERT_NE(appStateObserver_, nullptr);
88 AppExecFwk::AbilityStateData abilityStateData;
89 abilityStateData.abilityState = FOREGROUND;
90 appStateObserver_->OnAbilityStateChanged(abilityStateData);
91 EXPECT_NE(appStateObserver_, nullptr);
92
93 appStateObserver_->OnAbilityStateChanged(abilityStateData);
94 EXPECT_NE(appStateObserver_, nullptr);
95 DTEST_LOG << "AppStateObserverTest OnAbilityStateChanged_001 end" << std::endl;
96 }
97
98 /**
99 * @tc.name: OnAbilityStateChanged_003
100 * @tc.desc: call OnAbilityStateChanged from distributedsched
101 * @tc.type: FUNC
102 * @tc.require: I6SJQ6
103 */
104 HWTEST_F(AppStateObserverTest, OnAbilityStateChanged_003, TestSize.Level3)
105 {
106 DTEST_LOG << "AppStateObserverTest OnAbilityStateChanged_001 begin" << std::endl;
107 ASSERT_NE(appStateObserver_, nullptr);
108 AppExecFwk::AbilityStateData abilityStateData;
109 abilityStateData.abilityState = BACKGROUND;
110 appStateObserver_->OnAbilityStateChanged(abilityStateData);
111 EXPECT_NE(appStateObserver_, nullptr);
112
113 appStateObserver_->OnAbilityStateChanged(abilityStateData);
114 EXPECT_NE(appStateObserver_, nullptr);
115 DTEST_LOG << "AppStateObserverTest OnAbilityStateChanged_003 end" << std::endl;
116 }
117 }
118 }