1 /*
2 * Copyright (c) 2025 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 "sec_comp_monitor_test.h"
17
18 using namespace testing::ext;
19 using namespace OHOS;
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 constexpr int32_t APP_STATE_CACHED = 100;
26 }
27
SetUpTestCase()28 void SecCompMonitorTest::SetUpTestCase()
29 {
30 }
31
TearDownTestCase()32 void SecCompMonitorTest::TearDownTestCase()
33 {
34 sleep(3); // delay 3 minutes
35 }
36
SetUp()37 void SecCompMonitorTest::SetUp()
38 {
39 if (appStateObserver_ != nullptr) {
40 return;
41 }
42 appStateObserver_ = std::make_shared<SecCompUsageObserver>();
43 }
44
TearDown()45 void SecCompMonitorTest::TearDown()
46 {
47 appStateObserver_ = nullptr;
48 }
49
50 /**
51 * @tc.name: ProcessFromForegroundList001
52 * @tc.desc: Monitor foreground list for process after process state changed
53 * @tc.type: FUNC
54 * @tc.require:
55 */
56 HWTEST_F(SecCompMonitorTest, ProcessFromForegroundList001, TestSize.Level0)
57 {
58 EXPECT_EQ(true, SecCompMonitor::GetInstance().IsToastShownNeeded(10));
59 EXPECT_EQ(false, SecCompMonitor::GetInstance().IsToastShownNeeded(10));
60 ASSERT_NE(nullptr, appStateObserver_);
61 EXPECT_EQ(1, SecCompMonitor::GetInstance().appsInForeground_.size());
62 ProcessData processData;
63 processData.state = AppProcessState::APP_STATE_BACKGROUND;
64 processData.pid = 10;
65 // change to background
66 appStateObserver_->OnProcessStateChanged(processData);
67 EXPECT_EQ(0, SecCompMonitor::GetInstance().appsInForeground_.size());
68
69 EXPECT_EQ(true, SecCompMonitor::GetInstance().IsToastShownNeeded(10));
70 EXPECT_EQ(1, SecCompMonitor::GetInstance().appsInForeground_.size());
71 // change to die
72 appStateObserver_->OnProcessDied(processData);
73 EXPECT_EQ(0, SecCompMonitor::GetInstance().appsInForeground_.size());
74
75 EXPECT_EQ(true, SecCompMonitor::GetInstance().IsToastShownNeeded(10));
76 EXPECT_EQ(1, SecCompMonitor::GetInstance().appsInForeground_.size());
77 AppStateData appStateData;
78 appStateData.state = APP_STATE_CACHED;
79 appStateData.pid = 10;
80 // change to background
81 appStateObserver_->OnAppCacheStateChanged(appStateData);
82 EXPECT_EQ(0, SecCompMonitor::GetInstance().appsInForeground_.size());
83 }
84 } // namespace AccessToken
85 } // namespace Security
86 } // namespace OHOS
87