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 <gtest/gtest.h>
17 #include <string>
18 #include "app_manager_adapter.h"
19 #include "avsession_log.h"
20
21 using OHOS::AppExecFwk::AppData;
22 using OHOS::AppExecFwk::AppProcessData;
23 using OHOS::AVSession::AppManagerAdapter;
24
25 static int32_t g_expectedUid;
26 static AppData g_appData;
27 static AppProcessData g_appProcessData;
28
29 class AppManagerAdapterTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35 static constexpr int32_t TEST_UID = 3;
36 static constexpr int32_t OTHER_UID_1 = 5;
37 static constexpr int32_t OTHER_UID_2 = 4;
38 static constexpr int32_t TEST_PID = 1;
39 static constexpr int32_t INVALID_UID = -1;
40 };
41
SetUpTestCase()42 void AppManagerAdapterTest::SetUpTestCase()
43 {
44 std::string appName = "zhifubao";
45 g_appData.appName = appName;
46 g_appData.uid = AppManagerAdapterTest::TEST_UID ;
47 g_appProcessData.processName = appName;
48 g_appProcessData.pid = AppManagerAdapterTest::TEST_PID;
49 g_appProcessData.appDatas.push_back(g_appData);
50 OHOS::AVSession::AppManagerAdapter::GetInstance().SetAppBackgroundStateObserver([](int32_t uid, int32_t pid) {
51 g_expectedUid = uid;
52 });
53 OHOS::AVSession::AppManagerAdapter::GetInstance().SetServiceCallbackForAppStateChange([] (int uid, int state) {
54 SLOGI("serviceCallback For AppManagerAdapterTest uid = %{public}d, state = %{public}d", uid, state);
55 });
56 }
57
TearDownTestCase()58 void AppManagerAdapterTest::TearDownTestCase()
59 {
60 }
61
TearDown()62 void AppManagerAdapterTest::TearDown()
63 {
64 }
65
SetUp()66 void AppManagerAdapterTest::SetUp()
67 {
68 g_expectedUid = INVALID_UID;
69 g_appProcessData.appState = OHOS::AppExecFwk::ApplicationState::APP_STATE_BACKGROUND;
70 }
71
72 /**
73 * @tc.name: OnAppStateChanged001
74 * @tc.desc: Verify successfully received changes
75 * @tc.type: FUNC
76 * @tc.require: AR000H31KJ
77 */
78 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged001, testing::ext::TestSize.Level1)
79 {
80 SLOGI("OnAppStateChanged001, start");
81 AppManagerAdapter::GetInstance().Init();
82 AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
83 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
84 AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
85 EXPECT_EQ(g_expectedUid, g_appData.uid);
86 SLOGI("OnAppStateChanged001, end");
87 }
88
89 /**
90 * @tc.name: OnAppStateChanged002
91 * @tc.desc: Validation failed to receive changes
92 * @tc.type: FUNC
93 * @tc.require: AR000H31KJ
94 */
95 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged002, testing::ext::TestSize.Level1)
96 {
97 SLOGI("OnAppStateChanged002, start");
98 AppManagerAdapter::GetInstance().Init();
99 AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
100 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
101 AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
102 EXPECT_NE(g_expectedUid, g_appData.uid);
103 SLOGI("OnAppStateChanged002, end");
104 }
105
106 /**
107 * @tc.name: OnAppStateChanged003
108 * @tc.desc: Influence of state on change
109 * @tc.type: FUNC
110 * @tc.require: AR000H31KJ
111 */
112 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged003, testing::ext::TestSize.Level1)
113 {
114 SLOGI("OnAppStateChanged003, start");
115 g_appProcessData.appState = OHOS::AppExecFwk::ApplicationState::APP_STATE_FOREGROUND;
116 AppManagerAdapter::GetInstance().Init();
117 AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
118 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
119 AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
120 EXPECT_NE(g_expectedUid, g_appData.uid);
121 SLOGI("OnAppStateChanged003, end");
122 }
123
124 /**
125 * @tc.name: OnAppStateChanged004
126 * @tc.desc: Influence of state on change
127 * @tc.type: FUNC
128 * @tc.require: AR000H31KJ
129 */
130 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged004, testing::ext::TestSize.Level1)
131 {
132 SLOGI("OnAppStateChanged004, start");
133 g_appProcessData.appState = OHOS::AppExecFwk::ApplicationState::APP_STATE_FOREGROUND;
134 AppManagerAdapter::GetInstance().Init();
135 AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
136 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
137 AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
138 EXPECT_NE(g_expectedUid, g_appData.uid);
139 SLOGI("OnAppStateChanged004, end");
140 }
141
142 /**
143 * @tc.name: OnAppStateChanged005
144 * @tc.desc: Impact of deleting data on change
145 * @tc.type: FUNC
146 * @tc.require: AR000H31KJ
147 */
148 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged005, testing::ext::TestSize.Level1)
149 {
150 SLOGI("OnAppStateChanged005, start");
151 AppManagerAdapter::GetInstance().Init();
152 AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
153 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
154 EXPECT_EQ(g_expectedUid, g_appData.uid);
155 g_expectedUid = INVALID_UID;
156 AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
157 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
158 EXPECT_NE(g_expectedUid, g_appData.uid);
159 SLOGI("OnAppStateChanged005, end");
160 }
161
162 /**
163 * @tc.name: OnAppStateChanged006
164 * @tc.desc: Impact of more data on change
165 * @tc.type: FUNC
166 * @tc.require: AR000H31KJ
167 */
168 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged006, testing::ext::TestSize.Level1)
169 {
170 SLOGI("OnAppStateChanged006, start");
171 AppManagerAdapter::GetInstance().Init();
172 AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
173 AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
174 AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_2);
175 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
176 AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
177 AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
178 EXPECT_EQ(g_expectedUid, g_appData.uid);
179 SLOGI("OnAppStateChanged006, end");
180 }
181
182 /**
183 * @tc.name: OnAppStateChanged007
184 * @tc.desc: The impact of more data but no correct data on change
185 * @tc.type: FUNC
186 * @tc.require: AR000H31KJ
187 */
188 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged007, testing::ext::TestSize.Level1)
189 {
190 SLOGI("OnAppStateChanged007, start");
191 AppManagerAdapter::GetInstance().Init();
192 AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
193 AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_2);
194 AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
195 AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
196 AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_2);
197 EXPECT_NE(g_expectedUid, g_appData.uid);
198 SLOGI("OnAppStateChanged007, end");
199 }