1 /*
2 * Copyright (C) 2024 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 "context_appstate_observer_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "accesstoken_kit.h"
21 #include "app_state_data.h"
22 #include "context_appstate_observer.h"
23 #include "mock_context.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 using namespace std;
31 using namespace testing;
32 using namespace testing::ext;
SetUpTestCase()33 void ContextAppStateObserverTest::SetUpTestCase()
34 {
35 }
36
TearDownTestCase()37 void ContextAppStateObserverTest::TearDownTestCase()
38 {
39 }
40
SetUp()41 void ContextAppStateObserverTest::SetUp()
42 {
43 }
44
TearDown()45 void ContextAppStateObserverTest::TearDown()
46 {
47 }
48
49 HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_001, TestSize.Level0)
50 {
51 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
52 ASSERT_NE(contextCallback, nullptr);
53 uint64_t contextId = 1;
54 EXPECT_CALL(*contextCallback, GetCallerName())
__anonf0f4d4610102() 55 .WillRepeatedly([]() {
56 return "com.homs.settings";
57 }
58 );
59 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
60 ASSERT_NE(appStateObserverManager, nullptr);
61 appStateObserverManager->SubscribeAppState(contextCallback, contextId);
62 appStateObserverManager->UnSubscribeAppState();
63 }
64
65 HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_002, TestSize.Level0)
66 {
67 uint64_t contextId = 1;
68 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
69 ASSERT_NE(appStateObserverManager, nullptr);
70 appStateObserverManager->SubscribeAppState(nullptr, contextId);
71 }
72
73 HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_003, TestSize.Level0)
74 {
75 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
76 ASSERT_NE(contextCallback, nullptr);
77 uint64_t contextId = 1;
78 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
79 ASSERT_NE(appStateObserverManager, nullptr);
80 appStateObserverManager->SubscribeAppState(contextCallback, contextId);
81 }
82
83 HWTEST_F(ContextAppStateObserverTest, UnSubscribeAppStateTest_001, TestSize.Level0)
84 {
85 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
86 ASSERT_NE(appStateObserverManager, nullptr);
87 appStateObserverManager->UnSubscribeAppState();
88 }
89
90 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_001, TestSize.Level0)
91 {
92 uint64_t contextId = 1;
93 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
94 ASSERT_NE(appStateObserver, nullptr);
95 AppStateData appStateData;
96 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
97 appStateData.bundleName = "com.homs.settings";
98 appStateObserver->OnAppStateChanged(appStateData);
99 }
100
101 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_002, TestSize.Level0)
102 {
103 uint64_t contextId = 1;
104 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
105 ASSERT_NE(appStateObserver, nullptr);
106 AppStateData appStateData;
107 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
108 appStateData.bundleName = "com.homs.settings";
109 appStateObserver->OnAppStateChanged(appStateData);
110 }
111
112 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_003, TestSize.Level0)
113 {
114 uint64_t contextId = 1;
115 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
116 ASSERT_NE(appStateObserver, nullptr);
117 AppStateData appStateData;
118 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
119 appStateData.bundleName = "com.homs.settings";
120 appStateObserver->OnAppStateChanged(appStateData);
121 }
122
123 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_001, TestSize.Level0)
124 {
125 uint64_t contextId = 1;
126 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
127 ASSERT_NE(appStateObserver, nullptr);
128 AppStateData appStateData;
129 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
130 appStateData.bundleName = "com.homs.settings";
131 appStateObserver->OnForegroundApplicationChanged(appStateData);
132 }
133
134 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_002, TestSize.Level0)
135 {
136 uint64_t contextId = 1;
137 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
138 ASSERT_NE(appStateObserver, nullptr);
139 AppStateData appStateData;
140 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
141 appStateData.bundleName = "com.homs.settings";
142 appStateObserver->OnForegroundApplicationChanged(appStateData);
143 }
144
145 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_003, TestSize.Level0)
146 {
147 uint64_t contextId = 1;
148 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
149 ASSERT_NE(appStateObserver, nullptr);
150 AppStateData appStateData;
151 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
152 appStateData.bundleName = "com.homs.settings";
153 appStateObserver->OnForegroundApplicationChanged(appStateData);
154 }
155
156 HWTEST_F(ContextAppStateObserverTest, OnAbilityStateChangedTest_001, TestSize.Level0)
157 {
158 uint64_t contextId = 1;
159 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
160 ASSERT_NE(appStateObserver, nullptr);
161 AbilityStateData abilityStateData;
162 abilityStateData.abilityState = static_cast<int32_t>(AbilityState::ABILITY_STATE_BACKGROUND);
163 abilityStateData.bundleName = "com.homs.settings";
164 appStateObserver->OnAbilityStateChanged(abilityStateData);
165 }
166
167 HWTEST_F(ContextAppStateObserverTest, OnAbilityStateChangedTest_002, TestSize.Level0)
168 {
169 uint64_t contextId = 1;
170 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
171 ASSERT_NE(appStateObserver, nullptr);
172 AbilityStateData abilityStateData;
173 abilityStateData.abilityState = static_cast<int32_t>(AbilityState::ABILITY_STATE_FOREGROUND);
174 abilityStateData.bundleName = "com.homs.settings";
175 appStateObserver->OnAbilityStateChanged(abilityStateData);
176 }
177
178 HWTEST_F(ContextAppStateObserverTest, OnAbilityStateChangedTest_003, TestSize.Level0)
179 {
180 uint64_t contextId = 1;
181 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
182 ASSERT_NE(appStateObserver, nullptr);
183 AbilityStateData abilityStateData;
184 abilityStateData.abilityState = static_cast<int32_t>(AbilityState::ABILITY_STATE_BACKGROUND);
185 abilityStateData.bundleName = "com.homs.settings";
186 appStateObserver->OnAbilityStateChanged(abilityStateData);
187 }
188 } // namespace UserAuth
189 } // namespace UserIam
190 } // namespace OHOS
191