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