• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #ifndef SCREEN_CAPTURE_SERVER_FUNCTION_UNITTEST_H
16 #define SCREEN_CAPTURE_SERVER_FUNCTION_UNITTEST_H
17 
18 #include <fcntl.h>
19 #include <iostream>
20 #include <string>
21 #include <nativetoken_kit.h>
22 #include "media_errors.h"
23 #include "media_utils.h"
24 #include "accesstoken_kit.h"
25 #include "token_setproc.h"
26 #include "screen_capture_listener_proxy.h"
27 #include "screen_capture_server.h"
28 #include "gtest/gtest.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace ScreenCaptureTestParam {
33 constexpr uint32_t RECORDER_TIME = 2;
34 }
35 class ScreenCaptureServerFunctionTest : public testing::Test {
36 public:
37     virtual void SetUp();
38     virtual void TearDown();
39     int32_t SetInvalidConfig();
40     int32_t SetValidConfig();
41     int32_t SetInvalidConfigFile(RecorderInfo &recorderInfo);
42     int32_t SetValidConfigFile(RecorderInfo &recorderInfo);
43     int32_t SetRecorderInfo(std::string name, RecorderInfo &recorderInfo);
44     void OpenFileFd(std::string name);
45     int32_t InitFileScreenCaptureServer();
46     int32_t InitStreamScreenCaptureServer();
47     void SetHapPermission();
48     int32_t SetScreenCaptureObserver();
49     int32_t StartFileAudioCapture(AVScreenCaptureMixMode mixMode);
50     int32_t StartStreamAudioCapture();
51     void SetSCInnerAudioCaptureAndPushData(std::shared_ptr<AudioBuffer> innerAudioBuffer);
52     void SetSCMicAudioCaptureAndPushData(std::shared_ptr<AudioBuffer> micAudioBuffer);
53 
54 protected:
55     std::shared_ptr<ScreenCaptureServer> screenCaptureServer_;
56     AVScreenCaptureConfig config_;
57     int32_t outputFd_ = -1;
58 private:
59     const std::string ScreenRecorderBundleName =
60         GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"];
61     const std::string HiviewCareBundleName =
62         GetScreenCaptureSystemParam()["const.multimedia.screencapture.hiviewcarebundlename"];
63     Security::AccessToken::HapInfoParams info_ = {
64         .userID = 100, // 100 UserID
65         .bundleName = "com.ohos.test.screencapturetdd",
66         .instIndex = 0, // 0 index
67         .appIDDesc = "com.ohos.test.screencapturetdd",
68         .isSystemApp = true
69     };
70     Security::AccessToken::HapPolicyParams policy_ = {
71         .apl = Security::AccessToken::APL_SYSTEM_BASIC,
72         .domain = "test.domain.screencapturetdd",
73         .permList = {},
74         .permStateList = {
75             {
76                 .permissionName = "ohos.permission.MICROPHONE",
77                 .isGeneral = true,
78                 .resDeviceID = { "local" },
79                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
80                 .grantFlags = { 1 }
81             },
82             {
83                 .permissionName = "ohos.permission.READ_MEDIA",
84                 .isGeneral = true,
85                 .resDeviceID = { "local" },
86                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
87                 .grantFlags = { 1 }
88             },
89             {
90                 .permissionName = "ohos.permission.WRITE_MEDIA",
91                 .isGeneral = true,
92                 .resDeviceID = { "local" },
93                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
94                 .grantFlags = { 1 }
95             },
96             {
97                 .permissionName = "ohos.permission.KEEP_BACKGROUND_RUNNING",
98                 .isGeneral = true,
99                 .resDeviceID = { "local" },
100                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
101                 .grantFlags = { 1 }
102             },
103             {
104                 .permissionName = "ohos.permission.CAPTURE_SCREEN",
105                 .isGeneral = true,
106                 .resDeviceID = { "local" },
107                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
108                 .grantFlags = { 1 }
109             }
110         }
111     };
112 };
113 
114 class StandardScreenCaptureServerUnittestCallback : public IStandardScreenCaptureListener {
115 public:
116     virtual ~StandardScreenCaptureServerUnittestCallback() = default;
AsObject()117     sptr<IRemoteObject> AsObject() { return nullptr; };
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)118     void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) {};
OnAudioBufferAvailable(bool isReady,AudioCaptureSourceType type)119     void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) {};
OnVideoBufferAvailable(bool isReady)120     void OnVideoBufferAvailable(bool isReady) {};
OnStateChange(AVScreenCaptureStateCode stateCode)121     void OnStateChange(AVScreenCaptureStateCode stateCode) {};
OnDisplaySelected(uint64_t displayId)122     void OnDisplaySelected(uint64_t displayId) {};
OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event,ScreenCaptureRect * area)123     void OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event, ScreenCaptureRect* area) {};
OnUserSelected(ScreenCaptureUserSelectionInfo selectionInfo)124     void OnUserSelected(ScreenCaptureUserSelectionInfo selectionInfo) {};
125 };
126 
127 class ScreenCaptureServerUnittestCallbackMock : public ScreenCaptureListenerCallback {
128 public:
ScreenCaptureServerUnittestCallbackMock(const sptr<IStandardScreenCaptureListener> & listener)129     explicit ScreenCaptureServerUnittestCallbackMock(const sptr<IStandardScreenCaptureListener> &listener)
130         : ScreenCaptureListenerCallback(listener) {}
131     virtual ~ScreenCaptureServerUnittestCallbackMock() = default;
132     void OnError(ScreenCaptureErrorType errorType, int32_t errorCode);
133     void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type);
134     void OnVideoBufferAvailable(bool isReady);
135     void OnStateChange(AVScreenCaptureStateCode stateCode);
136     void OnDisplaySelected(uint64_t displayId);
137     void OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event, ScreenCaptureRect* area);
138     void OnUserSelected(ScreenCaptureUserSelectionInfo selectionInfo);
139     void Stop();
140 };
141 } // Media
142 } // OHOS
143 #endif