• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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())
__anon156bde690102() 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, ScreenLockStateTest_001, TestSize.Level0)
103 {
104     auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
105     ASSERT_NE(appStateObserverManager, nullptr);
106     bool screenLockState = true;
107     int32_t userId = 1;
108     appStateObserverManager->RemoveScreenLockState(userId);
109     screenLockState = appStateObserverManager->GetScreenLockState(userId);
110     ASSERT_EQ(screenLockState, false);
111     screenLockState = true;
112     appStateObserverManager->SetScreenLockState(screenLockState, userId);
113     int32_t userId2 = 2;
114     appStateObserverManager->SetScreenLockState(screenLockState, userId2);
115     screenLockState = appStateObserverManager->GetScreenLockState(userId);
116     ASSERT_EQ(screenLockState, true);
117     screenLockState = false;
118     appStateObserverManager->SetScreenLockState(screenLockState, userId);
119     screenLockState = appStateObserverManager->GetScreenLockState(userId);
120     ASSERT_EQ(screenLockState, false);
121     appStateObserverManager->RemoveScreenLockState(userId2);
122     screenLockState = appStateObserverManager->GetScreenLockState(userId2);
123     ASSERT_EQ(screenLockState, false);
124 }
125 
126 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_001, TestSize.Level0)
127 {
128     uint64_t contextId = 1;
129     auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
130     ASSERT_NE(appStateObserver, nullptr);
131     AppStateData appStateData;
132     appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
133     appStateData.bundleName = "com.homs.settings";
134     appStateObserver->OnAppStateChanged(appStateData);
135 }
136 
137 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_002, TestSize.Level0)
138 {
139     uint64_t contextId = 1;
140     auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
141     ASSERT_NE(appStateObserver, nullptr);
142     AppStateData appStateData;
143     appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
144     appStateData.bundleName = "com.homs.settings";
145     appStateObserver->OnAppStateChanged(appStateData);
146 }
147 
148 HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_003, TestSize.Level0)
149 {
150     uint64_t contextId = 1;
151     auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
152     ASSERT_NE(appStateObserver, nullptr);
153     AppStateData appStateData;
154     appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
155     appStateData.bundleName = "com.homs.settings";
156     appStateObserver->OnAppStateChanged(appStateData);
157 }
158 
159 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_001, TestSize.Level0)
160 {
161     uint64_t contextId = 1;
162     auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
163     ASSERT_NE(appStateObserver, nullptr);
164     AppStateData appStateData;
165     appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
166     appStateData.bundleName = "com.homs.settings";
167     appStateObserver->OnForegroundApplicationChanged(appStateData);
168 }
169 
170 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_002, TestSize.Level0)
171 {
172     uint64_t contextId = 1;
173     auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
174     ASSERT_NE(appStateObserver, nullptr);
175     AppStateData appStateData;
176     appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
177     appStateData.bundleName = "com.homs.settings";
178     appStateObserver->OnForegroundApplicationChanged(appStateData);
179 }
180 
181 HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_003, TestSize.Level0)
182 {
183     uint64_t contextId = 1;
184     auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
185     ASSERT_NE(appStateObserver, nullptr);
186     AppStateData appStateData;
187     appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
188     appStateData.bundleName = "com.homs.settings";
189     appStateObserver->OnForegroundApplicationChanged(appStateData);
190 }
191 
192 HWTEST_F(ContextAppStateObserverTest, ProcAppStateChangedTest_001, TestSize.Level0)
193 {
194     int32_t userId = 100;
195     uint64_t contextId = 1;
196     uint32_t tokenId = 0x1234;
197     auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
198     ASSERT_NE(appStateObserver, nullptr);
199     AppStateData appStateData;
200     appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
201     appStateData.bundleName = "com.homs.settings";
202     auto context = Common::MakeShared<MockContext>();
203     EXPECT_NE(context, nullptr);
204     EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
205     EXPECT_CALL(*context, GetLatestError()).WillRepeatedly(Return(GENERAL_ERROR));
206     EXPECT_CALL(*context, GetTokenId()).WillRepeatedly(Return(tokenId));
207     EXPECT_CALL(*context, GetUserId())
208         .WillOnce(Return(101))
209         .WillRepeatedly(Return(userId));
210     EXPECT_CALL(*context, Stop())
211         .WillOnce(Return(false))
212         .WillRepeatedly(Return(true));
213     EXPECT_TRUE(ContextPool::Instance().Insert(context));
214     appStateObserver->ProcAppStateChanged(userId);
215     appStateObserver->ProcAppStateChanged(userId);
216     appStateObserver->ProcAppStateChanged(userId);
217     EXPECT_TRUE(ContextPool::Instance().Delete(contextId));
218 }
219 } // namespace UserAuth
220 } // namespace UserIam
221 } // namespace OHOS
222