• 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 
52 protected:
53     std::shared_ptr<ScreenCaptureServer> screenCaptureServer_;
54     AVScreenCaptureConfig config_;
55     int32_t outputFd_ = -1;
56 private:
57     const std::string ScreenRecorderBundleName =
58         GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"];
59     const std::string HiviewCareBundleName =
60         GetScreenCaptureSystemParam()["const.multimedia.screencapture.hiviewcarebundlename"];
61     Security::AccessToken::HapInfoParams info_ = {
62         .userID = 100, // 100 UserID
63         .bundleName = "com.ohos.test.screencapturetdd",
64         .instIndex = 0, // 0 index
65         .appIDDesc = "com.ohos.test.screencapturetdd",
66         .isSystemApp = true
67     };
68     Security::AccessToken::HapPolicyParams policy_ = {
69         .apl = Security::AccessToken::APL_SYSTEM_BASIC,
70         .domain = "test.domain.screencapturetdd",
71         .permList = {},
72         .permStateList = {
73             {
74                 .permissionName = "ohos.permission.MICROPHONE",
75                 .isGeneral = true,
76                 .resDeviceID = { "local" },
77                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
78                 .grantFlags = { 1 }
79             },
80             {
81                 .permissionName = "ohos.permission.READ_MEDIA",
82                 .isGeneral = true,
83                 .resDeviceID = { "local" },
84                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
85                 .grantFlags = { 1 }
86             },
87             {
88                 .permissionName = "ohos.permission.WRITE_MEDIA",
89                 .isGeneral = true,
90                 .resDeviceID = { "local" },
91                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
92                 .grantFlags = { 1 }
93             },
94             {
95                 .permissionName = "ohos.permission.KEEP_BACKGROUND_RUNNING",
96                 .isGeneral = true,
97                 .resDeviceID = { "local" },
98                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
99                 .grantFlags = { 1 }
100             },
101             {
102                 .permissionName = "ohos.permission.CAPTURE_SCREEN",
103                 .isGeneral = true,
104                 .resDeviceID = { "local" },
105                 .grantStatus = { Security::AccessToken::PermissionState::PERMISSION_GRANTED },
106                 .grantFlags = { 1 }
107             }
108         }
109     };
110 };
111 
112 class StandardScreenCaptureServerUnittestCallback : public IStandardScreenCaptureListener {
113 public:
114     virtual ~StandardScreenCaptureServerUnittestCallback() = default;
AsObject()115     sptr<IRemoteObject> AsObject() { return nullptr; };
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)116     void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) {};
OnAudioBufferAvailable(bool isReady,AudioCaptureSourceType type)117     void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) {};
OnVideoBufferAvailable(bool isReady)118     void OnVideoBufferAvailable(bool isReady) {};
OnStateChange(AVScreenCaptureStateCode stateCode)119     void OnStateChange(AVScreenCaptureStateCode stateCode) {};
OnDisplaySelected(uint64_t displayId)120     void OnDisplaySelected(uint64_t displayId) {};
121 };
122 
123 class ScreenCaptureServerUnittestCallbackMock : public ScreenCaptureListenerCallback {
124 public:
ScreenCaptureServerUnittestCallbackMock(const sptr<IStandardScreenCaptureListener> & listener)125     explicit ScreenCaptureServerUnittestCallbackMock(const sptr<IStandardScreenCaptureListener> &listener)
126         : ScreenCaptureListenerCallback(listener) {}
127     virtual ~ScreenCaptureServerUnittestCallbackMock() = default;
128     void OnError(ScreenCaptureErrorType errorType, int32_t errorCode);
129     void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type);
130     void OnVideoBufferAvailable(bool isReady);
131     void OnStateChange(AVScreenCaptureStateCode stateCode);
132     void OnDisplaySelected(uint64_t displayId);
133     void Stop();
134 };
135 } // Media
136 } // OHOS
137 #endif