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 <iostream>
18 #include <memory>
19 #include "sched_controller.h"
20 #include "window_state_observer.h"
21 #include "app_startup_scene_rec.h"
22 #include "window_manager.h"
23 #include "wm_common.h"
24 #include "sched_policy.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::ResourceSchedule::CgroupSetting;
28 using OHOS::Rosen::WindowModeType;
29 using OHOS::Rosen::IWindowModeChangedListener;
30 
31 namespace OHOS {
32 namespace ResourceSchedule {
33 namespace {
34     static const int32_t APP_START_UP = 0;
35 }
36 namespace CgroupSetting {
37 class CGroupSchedTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp();
42     void TearDown();
43 };
44 
SetUpTestCase(void)45 void CGroupSchedTest::SetUpTestCase(void)
46 {
47 }
48 
TearDownTestCase(void)49 void CGroupSchedTest::TearDownTestCase(void)
50 {
51 }
52 
SetUp(void)53 void CGroupSchedTest::SetUp(void)
54 {
55     AppStartupSceneRec::GetInstance().Init();
56 }
57 
TearDown(void)58 void CGroupSchedTest::TearDown(void)
59 {
60     AppStartupSceneRec::GetInstance().Deinit();
61 }
62 
63 /**
64  * @tc.name: CGroupSchedTest_WindowStateObserver_001
65  * @tc.desc: Window Mode Observer Test
66  * @tc.type: FUNC
67  * @tc.require: issuesI9BU37
68  * @tc.desc:
69  */
70 HWTEST_F(CGroupSchedTest, CGroupSchedTest_WindowStateObserver_001, Function | MediumTest | Level1)
71 {
72     std::shared_ptr<WindowModeObserver> windowModeObserver_ =
73         std::make_shared<WindowModeObserver>();
74     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT);
75     EXPECT_NE(windowModeObserver_, nullptr);
76 
77     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_FLOATING);
78     EXPECT_NE(windowModeObserver_, nullptr);
79 
80     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT);
81     EXPECT_NE(windowModeObserver_, nullptr);
82 
83     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT_FLOATING);
84     EXPECT_NE(windowModeObserver_, nullptr);
85 
86     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT);
87     EXPECT_NE(windowModeObserver_, nullptr);
88 
89     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_OTHER);
90     EXPECT_NE(windowModeObserver_, nullptr);
91 
92     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_FLOATING);
93     EXPECT_NE(windowModeObserver_, nullptr);
94 
95     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT_FLOATING);
96     EXPECT_NE(windowModeObserver_, nullptr);
97 
98     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_FLOATING);
99     EXPECT_NE(windowModeObserver_, nullptr);
100 
101     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_OTHER);
102     EXPECT_NE(windowModeObserver_, nullptr);
103 
104     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT_FLOATING);
105     EXPECT_NE(windowModeObserver_, nullptr);
106 
107     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_OTHER);
108     EXPECT_NE(windowModeObserver_, nullptr);
109 
110     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT);
111     EXPECT_NE(windowModeObserver_, nullptr);
112 
113     windowModeObserver_->OnWindowModeUpdate(Rosen::WindowModeType::WINDOW_MODE_SPLIT);
114 
115     windowModeObserver_ = nullptr;
116 }
117 
118 /**
119  * @tc.name: CGroupSchedTest_WindowStateObserver_002
120  * @tc.desc: Window Mode Observer Test
121  * @tc.type: FUNC
122  * @tc.require: issuesI9BU37
123  * @tc.desc:
124  */
125 HWTEST_F(CGroupSchedTest, CGroupSchedTest_WindowStateObserver_002, Function | MediumTest | Level1)
126 {
127     std::shared_ptr<WindowModeObserver> windowModeObserver_ =
128         std::make_shared<WindowModeObserver>();
129     uint8_t nowWindowMode = windowModeObserver_->MarshallingWindowModeType(
130         Rosen::WindowModeType::WINDOW_MODE_SPLIT_FLOATING);
131     EXPECT_EQ(nowWindowMode, RSSWindowMode::WINDOW_MODE_SPLIT_FLOATING);
132 
133     nowWindowMode = windowModeObserver_->MarshallingWindowModeType(Rosen::WindowModeType::WINDOW_MODE_SPLIT);
134     EXPECT_EQ(nowWindowMode, RSSWindowMode::WINDOW_MODE_SPLIT);
135 
136     nowWindowMode = windowModeObserver_->MarshallingWindowModeType(Rosen::WindowModeType::WINDOW_MODE_FLOATING);
137     EXPECT_EQ(nowWindowMode, RSSWindowMode::WINDOW_MODE_FLOATING);
138     nowWindowMode = windowModeObserver_->MarshallingWindowModeType(
139         Rosen::WindowModeType::WINDOW_MODE_FULLSCREEN_FLOATING);
140     EXPECT_EQ(nowWindowMode, RSSWindowMode::WINDOW_MODE_FLOATING);
141 
142     nowWindowMode = windowModeObserver_->MarshallingWindowModeType(Rosen::WindowModeType::WINDOW_MODE_OTHER);
143     EXPECT_EQ(nowWindowMode, RSSWindowMode::WINDOW_MODE_OTHER);
144     windowModeObserver_ = nullptr;
145 }
146 
147 /**
148  * @tc.name: CGroupSchedTest_WindowStateObserver_002
149  * @tc.desc: Window Mode Observer Test
150  * @tc.type: FUNC
151  * @tc.require: issuesI9BU37
152  * @tc.desc:
153  */
154 HWTEST_F(CGroupSchedTest, CGroupSchedTest_SchedController_001, Function | MediumTest | Level1)
155 {
156     auto &schedController = SchedController::GetInstance();
157     schedController.SubscribeWindowState();
158     schedController.UnsubscribeWindowState();
159     EXPECT_EQ(schedController.windowModeObserver_, nullptr);
160 }
161 
162 /**
163  * @tc.name: CGroupSchedTest_AppStartupSceneRec_001
164  * @tc.desc: application startup scene Test
165  * @tc.type: FUNC
166  * @tc.require: issuesI9IR2I
167  * @tc.desc:
168  */
169 HWTEST_F(CGroupSchedTest, CGroupSchedTest_AppStartupSceneRec_001, Function | MediumTest | Level1)
170 {
171     AppStartupSceneRec::GetInstance().CleanRecordSceneData();
172     std::string bundleName = "test101";
173     std::string uid = "101";
174     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(-1, uid, bundleName);
175     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
176     bool isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
177     EXPECT_EQ(isReportContinuousStartup, false);
178     uid = "102";
179     bundleName = "test102";
180     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
181     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
182     EXPECT_EQ(isReportContinuousStartup, false);
183     uid = "103";
184     bundleName = "test103";
185     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
186     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
187     EXPECT_EQ(isReportContinuousStartup, false);
188     uid = "104";
189     bundleName = "test104";
190     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
191     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
192     EXPECT_EQ(isReportContinuousStartup, false);
193     uid = "105";
194     bundleName = "test105";
195     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
196     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
197     EXPECT_EQ(isReportContinuousStartup, true);
198     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
199     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
200     EXPECT_EQ(isReportContinuousStartup, true);
201     uid = "106";
202     bundleName = "test106";
203     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
204     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
205     EXPECT_EQ(isReportContinuousStartup, true);
206 }
207 
208 /**
209  * @tc.name: CGroupSchedTest_AppStartupSceneRec_002
210  * @tc.desc: application startup scene Test
211  * @tc.type: FUNC
212  * @tc.require: issuesI9IR2I
213  * @tc.desc:
214  */
215 HWTEST_F(CGroupSchedTest, CGroupSchedTest_AppStartupSceneRec_002, Function | MediumTest | Level1)
216 {
217     AppStartupSceneRec::GetInstance().CleanRecordSceneData();
218     bool isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
219     EXPECT_EQ(isReportContinuousStartup, false);
220     std::string bundleName = "test101";
221     std::string uid = "101";
222     AppStartupSceneRec::GetInstance().startIgnorePkgs_.emplace("test101");
223     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
224     uid = "106";
225     bundleName = "test106";
226     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
227     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
228     EXPECT_EQ(isReportContinuousStartup, false);
229     AppStartupSceneRec::GetInstance().RecordIsContinuousStartup(APP_START_UP, uid, bundleName);
230     isReportContinuousStartup = AppStartupSceneRec::GetInstance().isReportContinuousStartup_.load();
231     EXPECT_EQ(isReportContinuousStartup, false);
232 }
233 } // namespace CgroupSetting
234 } // namespace ResourceSchedule
235 } // namespace OHOS
236