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