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().SetAppStateChangeObserver(
51         [] (int32_t uid, int32_t pid, bool isBackground) {
52         g_expectedUid = uid;
53     });
54     OHOS::AVSession::AppManagerAdapter::GetInstance().SetServiceCallbackForAppStateChange([](int uid, int state) {
55         SLOGI("serviceCallback For AppManagerAdapterTest uid = %{public}d, state = %{public}d", uid, state);
56     });
57 }
58 
TearDownTestCase()59 void AppManagerAdapterTest::TearDownTestCase()
60 {
61 }
62 
TearDown()63 void AppManagerAdapterTest::TearDown()
64 {
65 }
66 
SetUp()67 void AppManagerAdapterTest::SetUp()
68 {
69     g_expectedUid = INVALID_UID;
70     g_appProcessData.appState = OHOS::AppExecFwk::ApplicationState::APP_STATE_BACKGROUND;
71 }
72 
73 /**
74 * @tc.name: OnAppStateChanged001
75 * @tc.desc: Verify successfully received changes
76 * @tc.type: FUNC
77 * @tc.require: AR000H31KJ
78 */
HWTEST_F(AppManagerAdapterTest, OnAppStateChanged001, testing::ext::TestSize.Level1)79 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged001, testing::ext::TestSize.Level1)
80 {
81     SLOGI("OnAppStateChanged001, start");
82     AppManagerAdapter::GetInstance().Init();
83     AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
84     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
85     AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
86     EXPECT_EQ(g_expectedUid, g_appData.uid);
87     SLOGI("OnAppStateChanged001, end");
88 }
89 
90 /**
91 * @tc.name: OnAppStateChanged002
92 * @tc.desc: Validation failed to receive changes
93 * @tc.type: FUNC
94 * @tc.require: AR000H31KJ
95 */
HWTEST_F(AppManagerAdapterTest, OnAppStateChanged002, testing::ext::TestSize.Level1)96 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged002, testing::ext::TestSize.Level1)
97 {
98     SLOGI("OnAppStateChanged002, start");
99     AppManagerAdapter::GetInstance().Init();
100     AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
101     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
102     AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
103     EXPECT_NE(g_expectedUid, g_appData.uid);
104     SLOGI("OnAppStateChanged002, end");
105 }
106 
107 /**
108 * @tc.name: OnAppStateChanged003
109 * @tc.desc: Influence of state on change
110 * @tc.type: FUNC
111 * @tc.require: AR000H31KJ
112 */
HWTEST_F(AppManagerAdapterTest, OnAppStateChanged003, testing::ext::TestSize.Level1)113 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged003, testing::ext::TestSize.Level1)
114 {
115     SLOGI("OnAppStateChanged003, start");
116     g_appProcessData.appState = OHOS::AppExecFwk::ApplicationState::APP_STATE_FOREGROUND;
117     AppManagerAdapter::GetInstance().Init();
118     AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
119     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
120     AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
121     EXPECT_NE(g_expectedUid, g_appData.uid);
122     SLOGI("OnAppStateChanged003, end");
123 }
124 
125 /**
126 * @tc.name: OnAppStateChanged004
127 * @tc.desc: Influence of state on change
128 * @tc.type: FUNC
129 * @tc.require: AR000H31KJ
130 */
HWTEST_F(AppManagerAdapterTest, OnAppStateChanged004, testing::ext::TestSize.Level1)131 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged004, testing::ext::TestSize.Level1)
132 {
133     SLOGI("OnAppStateChanged004, start");
134     g_appProcessData.appState = OHOS::AppExecFwk::ApplicationState::APP_STATE_FOREGROUND;
135     AppManagerAdapter::GetInstance().Init();
136     AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
137     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
138     AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
139     EXPECT_NE(g_expectedUid, g_appData.uid);
140     SLOGI("OnAppStateChanged004, end");
141 }
142 
143 /**
144 * @tc.name: OnAppStateChanged005
145 * @tc.desc: Impact of deleting data on change
146 * @tc.type: FUNC
147 * @tc.require: AR000H31KJ
148 */
HWTEST_F(AppManagerAdapterTest, OnAppStateChanged005, testing::ext::TestSize.Level1)149 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged005, testing::ext::TestSize.Level1)
150 {
151     SLOGI("OnAppStateChanged005, start");
152     AppManagerAdapter::GetInstance().Init();
153     AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
154     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
155     EXPECT_EQ(g_expectedUid, g_appData.uid);
156     g_expectedUid = INVALID_UID;
157     AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
158     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
159     EXPECT_NE(g_expectedUid, g_appData.uid);
160     SLOGI("OnAppStateChanged005, end");
161 }
162 
163 /**
164 * @tc.name: OnAppStateChanged006
165 * @tc.desc: Impact of more data on change
166 * @tc.type: FUNC
167 * @tc.require: AR000H31KJ
168 */
HWTEST_F(AppManagerAdapterTest, OnAppStateChanged006, testing::ext::TestSize.Level1)169 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged006, testing::ext::TestSize.Level1)
170 {
171     SLOGI("OnAppStateChanged006, start");
172     AppManagerAdapter::GetInstance().Init();
173     AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
174     AppManagerAdapter::GetInstance().AddObservedApp(TEST_UID);
175     AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_2);
176     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
177     AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
178     AppManagerAdapter::GetInstance().RemoveObservedApp(TEST_UID);
179     EXPECT_EQ(g_expectedUid, g_appData.uid);
180     SLOGI("OnAppStateChanged006, end");
181 }
182 
183 /**
184 * @tc.name: OnAppStateChanged007
185 * @tc.desc: The impact of more data but no correct data on change
186 * @tc.type: FUNC
187 * @tc.require: AR000H31KJ
188 */
HWTEST_F(AppManagerAdapterTest, OnAppStateChanged007, testing::ext::TestSize.Level1)189 static HWTEST_F(AppManagerAdapterTest, OnAppStateChanged007, testing::ext::TestSize.Level1)
190 {
191     SLOGI("OnAppStateChanged007, start");
192     AppManagerAdapter::GetInstance().Init();
193     AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_1);
194     AppManagerAdapter::GetInstance().AddObservedApp(OTHER_UID_2);
195     AppManagerAdapter::GetInstance().HandleAppStateChanged(g_appProcessData);
196     AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_1);
197     AppManagerAdapter::GetInstance().RemoveObservedApp(OTHER_UID_2);
198     EXPECT_NE(g_expectedUid, g_appData.uid);
199     SLOGI("OnAppStateChanged007, end");
200 }