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 #include "context_pool.h"
27
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 using namespace std;
32 using namespace testing;
33 using namespace testing::ext;
SetUpTestCase()34 void ContextAppStateObserverTest::SetUpTestCase()
35 {
36 }
37
TearDownTestCase()38 void ContextAppStateObserverTest::TearDownTestCase()
39 {
40 }
41
SetUp()42 void ContextAppStateObserverTest::SetUp()
43 {
44 }
45
TearDown()46 void ContextAppStateObserverTest::TearDown()
47 {
48 }
49
50 HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_001, TestSize.Level0)
51 {
52 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
53 ASSERT_NE(contextCallback, nullptr);
54 uint64_t contextId = 1;
55 EXPECT_CALL(*contextCallback, GetCallerName())
__anon2a4036cb0102() 56 .WillRepeatedly([]() {
57 return "com.homs.settings";
58 }
59 );
60 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
61 ASSERT_NE(appStateObserverManager, nullptr);
62 appStateObserverManager->SubscribeAppState(contextCallback, contextId);
63 appStateObserverManager->UnSubscribeAppState();
64 }
65
66 HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_002, TestSize.Level0)
67 {
68 uint64_t contextId = 1;
69 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
70 ASSERT_NE(appStateObserverManager, nullptr);
71 appStateObserverManager->SubscribeAppState(nullptr, contextId);
72 }
73
74 HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_003, TestSize.Level0)
75 {
76 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
77 ASSERT_NE(contextCallback, nullptr);
78 uint64_t contextId = 1;
79 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
80 ASSERT_NE(appStateObserverManager, nullptr);
81 appStateObserverManager->SubscribeAppState(contextCallback, contextId);
82 }
83
84 HWTEST_F(ContextAppStateObserverTest, UnSubscribeAppStateTest_001, TestSize.Level0)
85 {
86 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
87 ASSERT_NE(appStateObserverManager, nullptr);
88 appStateObserverManager->UnSubscribeAppState();
89 }
90
91 HWTEST_F(ContextAppStateObserverTest, UnSubscribeAppStateTest_002, TestSize.Level0)
92 {
93 auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
94 ASSERT_NE(appStateObserverManager, nullptr);
95 uint64_t contextId = 1;
96 std::string bundelName = "com.homs.settings";
97 appStateObserverManager->appStateObserver_ = new (std::nothrow) ContextAppStateObserver(contextId, bundelName);
98 appStateObserverManager->UnSubscribeAppState();
99 appStateObserverManager->appStateObserver_ = nullptr;
100 }
101
102 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_001, TestSize.Level0)
103 {
104 uint64_t contextId = 1;
105 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
106 ASSERT_NE(appStateObserver, nullptr);
107 AppStateData appStateData;
108 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
109 appStateData.bundleName = "com.homs.settings";
110 appStateObserver->OnAppStateChanged(appStateData);
111 }
112
113 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_002, TestSize.Level0)
114 {
115 uint64_t contextId = 1;
116 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
117 ASSERT_NE(appStateObserver, nullptr);
118 AppStateData appStateData;
119 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
120 appStateData.bundleName = "com.homs.settings";
121 appStateObserver->OnAppStateChanged(appStateData);
122 }
123
124 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_003, TestSize.Level0)
125 {
126 uint64_t contextId = 1;
127 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
128 ASSERT_NE(appStateObserver, nullptr);
129 AppStateData appStateData;
130 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
131 appStateData.bundleName = "com.homs.settings";
132 appStateObserver->OnAppStateChanged(appStateData);
133 }
134
135 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_001, TestSize.Level0)
136 {
137 uint64_t contextId = 1;
138 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
139 ASSERT_NE(appStateObserver, nullptr);
140 AppStateData appStateData;
141 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
142 appStateData.bundleName = "com.homs.settings";
143 appStateObserver->OnForegroundApplicationChanged(appStateData);
144 }
145
146 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_002, TestSize.Level0)
147 {
148 uint64_t contextId = 1;
149 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
150 ASSERT_NE(appStateObserver, nullptr);
151 AppStateData appStateData;
152 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
153 appStateData.bundleName = "com.homs.settings";
154 appStateObserver->OnForegroundApplicationChanged(appStateData);
155 }
156
157 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_003, TestSize.Level0)
158 {
159 uint64_t contextId = 1;
160 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
161 ASSERT_NE(appStateObserver, nullptr);
162 AppStateData appStateData;
163 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
164 appStateData.bundleName = "com.homs.settings";
165 appStateObserver->OnForegroundApplicationChanged(appStateData);
166 }
167
168 HWTEST_F(ContextAppStateObserverTest, ProcAppStateChangedTest_001, TestSize.Level0)
169 {
170 int32_t userId = 100;
171 uint64_t contextId = 1;
172 uint32_t tokenId = 0x1234;
173 auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
174 ASSERT_NE(appStateObserver, nullptr);
175 AppStateData appStateData;
176 appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
177 appStateData.bundleName = "com.homs.settings";
178 auto context = Common::MakeShared<MockContext>();
179 EXPECT_NE(context, nullptr);
180 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
181 EXPECT_CALL(*context, GetLatestError()).WillRepeatedly(Return(GENERAL_ERROR));
182 EXPECT_CALL(*context, GetTokenId()).WillRepeatedly(Return(tokenId));
183 EXPECT_CALL(*context, GetUserId())
184 .WillOnce(Return(101))
185 .WillRepeatedly(Return(userId));
186 EXPECT_CALL(*context, Stop())
187 .WillRepeatedly(Return(true));
188 EXPECT_TRUE(ContextPool::Instance().Insert(context));
189 appStateObserver->ProcAppStateChanged(userId);
190 appStateObserver->ProcAppStateChanged(userId);
191 appStateObserver->ProcAppStateChanged(userId);
192 EXPECT_TRUE(ContextPool::Instance().Delete(contextId));
193 }
194 } // namespace UserAuth
195 } // namespace UserIam
196 } // namespace OHOS
197