From 37b867c71fd1b83e4f2fc8ebcfd806fbf6c6400d Mon Sep 17 00:00:00 2001 From: zhubx0704 Date: Tue, 25 Jul 2023 19:37:07 +0800 Subject: [PATCH] Add ScreenCapture Unittest and Fuzztest Signed-off-by: zhubx0704 --- test/BUILD.gn | 2 + test/fuzztest/BUILD.gn | 3 + test/fuzztest/common/test_screen_capture.cpp | 127 +++ test/fuzztest/common/test_screen_capture.h | 74 ++ .../BUILD.gn | 76 ++ .../corpus/init | 16 + .../project.xml | 25 + .../screencaptureaudioconfigure_fuzzer.cpp | 106 ++ .../screencaptureaudioconfigure_fuzzer.h | 42 + .../screencapturecapturemode_fuzzer/BUILD.gn | 76 ++ .../corpus/init | 16 + .../project.xml | 25 + .../screencapturecapturemode_fuzzer.cpp | 102 ++ .../screencapturecapturemode_fuzzer.h | 42 + .../BUILD.gn | 76 ++ .../corpus/init | 16 + .../project.xml | 25 + .../screencapturevideoconfigure_fuzzer.cpp | 105 ++ .../screencapturevideoconfigure_fuzzer.h | 42 + test/unittest/screen_capture_test/BUILD.gn | 93 ++ .../capi/include/screen_capture_capi_mock.h | 58 + .../capi/screen_capture_mock_factory.cpp | 31 + .../capi/src/screen_capture_capi_mock.cpp | 227 ++++ .../include/screen_capture_native_mock.h | 60 ++ .../native/screen_capture_mock_factory.cpp | 31 + .../native/src/screen_capture_native_mock.cpp | 113 ++ .../screen_capture_test/screen_capture_mock.h | 68 ++ .../include/screen_capture_unit_test.h | 65 ++ .../src/screen_capture_unit_test.cpp | 996 ++++++++++++++++++ 29 files changed, 2738 insertions(+) create mode 100644 test/fuzztest/common/test_screen_capture.cpp create mode 100644 test/fuzztest/common/test_screen_capture.h create mode 100644 test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/BUILD.gn create mode 100644 test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/corpus/init create mode 100644 test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/project.xml create mode 100644 test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.cpp create mode 100644 test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.h create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/BUILD.gn create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/corpus/init create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/project.xml create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.cpp create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.h create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/BUILD.gn create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/corpus/init create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/project.xml create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.cpp create mode 100644 test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.h create mode 100644 test/unittest/screen_capture_test/BUILD.gn create mode 100644 test/unittest/screen_capture_test/capi/include/screen_capture_capi_mock.h create mode 100644 test/unittest/screen_capture_test/capi/screen_capture_mock_factory.cpp create mode 100644 test/unittest/screen_capture_test/capi/src/screen_capture_capi_mock.cpp create mode 100644 test/unittest/screen_capture_test/native/include/screen_capture_native_mock.h create mode 100644 test/unittest/screen_capture_test/native/screen_capture_mock_factory.cpp create mode 100644 test/unittest/screen_capture_test/native/src/screen_capture_native_mock.cpp create mode 100644 test/unittest/screen_capture_test/screen_capture_mock.h create mode 100644 test/unittest/screen_capture_test/screen_capture_unittest/include/screen_capture_unit_test.h create mode 100644 test/unittest/screen_capture_test/screen_capture_unittest/src/screen_capture_unit_test.cpp diff --git a/test/BUILD.gn b/test/BUILD.gn index 25cd695c0..ff08204c1 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -27,6 +27,8 @@ group("media_unit_test") { "unittest/avcodec_test:avcodec_list_native_unit_test", "unittest/avcodec_test:format_native_unit_test", "unittest/avcodec_test:vcodec_native_unit_test", + "unittest/screen_capture_test:screen_capture_capi_unit_test", + "unittest/screen_capture_test:screen_capture_native_unit_test", ] } } diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 18c35416f..39c9425d1 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -32,6 +32,9 @@ group("fuzztest") { "player_fuzztest/playerservicestub_fuzzer:PlayerServiceStubFuzzTest", "player_fuzztest/playersetvolume_fuzzer:PlayerSetVolumeFuzzTest", "player_fuzztest/playerstub_fuzzer:PlayerStubFuzzTest", + "screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer:ScreenCaptureAudioConfigureFuzzTest", + "screen_capture_fuzztest/screencapturecapturemode_fuzzer:ScreenCaptureCaptureModeFuzzTest", + "screen_capture_fuzztest/screencapturevideoconfigure_fuzzer:ScreenCaptureVideoConfigureFuzzTest", ] } ############################################################################### diff --git a/test/fuzztest/common/test_screen_capture.cpp b/test/fuzztest/common/test_screen_capture.cpp new file mode 100644 index 000000000..89b123716 --- /dev/null +++ b/test/fuzztest/common/test_screen_capture.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test_screen_capture.h" +#include +#include "securec.h" + +using namespace std; +using namespace OHOS; +using namespace OHOS::Media; + +void TestScreenCaptureCallbackTest::OnError(ScreenCaptureErrorType errorType, int32_t errorCode) +{ + cout << "Error received, errorType:" << errorType << " errorCode:" << errorCode << endl; +} + +void TestScreenCaptureCallbackTest::OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) +{ + if (isReady) { + std::shared_ptr audioBuffer; + if (screenCapture->AcquireAudioBuffer(audioBuffer, type) == MSERR_OK) { + cout << "AcquireAudioBuffer, audioBufferLen:" << audioBuffer->length << ", timestampe:" + << audioBuffer->timestamp << ", audioSourceType:" << audioBuffer->sourcetype << endl; + } + screenCapture->ReleaseAudioBuffer(type); + } else { + cout << "AcquireAudioBuffer failed" << endl; + } +} + +void TestScreenCaptureCallbackTest::OnVideoBufferAvailable(bool isReady) +{ + if (isReady) { + int32_t fence = 0; + int64_t timestamp = 0; + OHOS::Rect damage; + sptr surfacebuffer = screenCapture->AcquireVideoBuffer(fence, timestamp, damage); + if (surfacebuffer != nullptr) { + int32_t length = surfacebuffer->GetSize(); + cout << "AcquireVideoBuffer, videoBufferLen:" << surfacebuffer->GetSize() << "timestamp:" + << timestamp << "size:"<< length << endl; + screenCapture->ReleaseVideoBuffer(); + } else { + cout << "AcquireVideoBuffer failed" << endl; + } + } +} + +TestScreenCapture::TestScreenCapture() +{ +} + +TestScreenCapture::~TestScreenCapture() +{ +} + +bool TestScreenCapture::CreateScreenCapture() +{ + screenCapture = ScreenCaptureFactory::CreateScreenCapture(); + if (screenCapture == nullptr) { + screenCapture->Release(); + return false; + } + return true; +} + +int32_t TestScreenCapture::SetScreenCaptureCallback(const std::shared_ptr& callback) +{ + return screenCapture->SetScreenCaptureCallback(callback); +} + +int32_t TestScreenCapture::Init(AVScreenCaptureConfig config) +{ + return screenCapture->Init(config); +} + +int32_t TestScreenCapture::StartScreenCapture() +{ + return screenCapture->StartScreenCapture(); +} + +int32_t TestScreenCapture::StopScreenCapture() +{ + return screenCapture->StopScreenCapture(); +} + +int32_t TestScreenCapture::Release() +{ + return screenCapture->Release(); +} + +int32_t TestScreenCapture::SetMicrophoneEnabled(bool isMicrophone) +{ + return screenCapture->SetMicrophoneEnabled(isMicrophone); +} + +int32_t TestScreenCapture::AcquireAudioBuffer(std::shared_ptr &audioBuffer, AudioCaptureSourceType type) +{ + return screenCapture->AcquireAudioBuffer(audioBuffer, type); +} + +sptr TestScreenCapture::AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, Rect &damage) +{ + return screenCapture->AcquireVideoBuffer(fence, timestamp, damage); +} + +int32_t TestScreenCapture::ReleaseAudioBuffer(AudioCaptureSourceType type) +{ + return screenCapture->ReleaseAudioBuffer(type); +} + +int32_t TestScreenCapture::ReleaseVideoBuffer() +{ + return screenCapture->ReleaseVideoBuffer(); +} diff --git a/test/fuzztest/common/test_screen_capture.h b/test/fuzztest/common/test_screen_capture.h new file mode 100644 index 000000000..987d9f50c --- /dev/null +++ b/test/fuzztest/common/test_screen_capture.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_SCREEN_CAPTURE_H +#define TEST_SCREEN_CAPTURE_H + +#include +#include +#include +#include +#include "nativetoken_kit.h" +#include "accesstoken_kit.h" +#include "token_setproc.h" +#include "screen_capture.h" +#include "aw_common.h" +#include "media_errors.h" + +namespace OHOS { +namespace Media { +#define RETURN_IF(cond, ret, ...) \ +do { \ + if (!(cond)) { \ + return ret; \ + } \ +} while (0) + +class TestScreenCapture : public NoCopyable { +public: + TestScreenCapture(); + ~TestScreenCapture(); + std::shared_ptr screenCapture = nullptr; + bool CreateScreenCapture(); + int32_t SetScreenCaptureCallback(const std::shared_ptr& callback); + int32_t Init(AVScreenCaptureConfig config); + int32_t StartScreenCapture(); + int32_t StopScreenCapture(); + int32_t Release(); + int32_t SetMicrophoneEnabled(bool isMicrophone); + int32_t AcquireAudioBuffer(std::shared_ptr &audioBuffer, AudioCaptureSourceType type); + sptr AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, Rect &damage); + int32_t ReleaseAudioBuffer(AudioCaptureSourceType type); + int32_t ReleaseVideoBuffer(); +}; + +class TestScreenCaptureCallbackTest : public ScreenCaptureCallBack, public NoCopyable { +public: + explicit TestScreenCaptureCallbackTest(std::shared_ptr screenCapture_) + { + screenCapture = screenCapture_; + } + ~TestScreenCaptureCallbackTest() + { + screenCapture = nullptr; + } + std::shared_ptr screenCapture; + void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) override; + void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) override; + void OnVideoBufferAvailable(bool isReady) override; +}; +} // namespace Media +} // namespace OHOS +#endif diff --git a/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/BUILD.gn b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/BUILD.gn new file mode 100644 index 000000000..46231f4c4 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/BUILD.gn @@ -0,0 +1,76 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/multimedia/player_framework/config.gni") +module_output_path = "player_framework/screen_capture" +MEDIA_ROOT_DIR = "//foundation/multimedia/player_framework/" + +##############################fuzztest########################################## +ohos_fuzztest("ScreenCaptureAudioConfigureFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "$MEDIA_ROOT_DIR/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer" + resource_config_file = "$MEDIA_ROOT_DIR/test/fuzztest/resource/ohos_test.xml" + + include_dirs = [ + "$MEDIA_ROOT_DIR/interfaces/inner_api/native", + "$MEDIA_ROOT_DIR/services/utils/include", + "$MEDIA_ROOT_DIR/test/fuzztest/common/", + "$MEDIA_PLAYER_GRAPHIC/utils/sync_fence/export", + ] + cflags = [ + "-std=c++17", + "-fno-rtti", + "-fno-exceptions", + "-Wall", + "-fno-common", + "-fstack-protector-strong", + "-Wshadow", + "-FPIC", + "-FS", + "-O2", + "-D_FORTIFY_SOURCE=2", + "-fvisibility=hidden", + "-Wformat=2", + "-Wfloat-equal", + "-Wdate-time", + "-Werror", + "-Wextra", + "-Wimplicit-fallthrough", + "-Wsign-compare", + "-Wunused-parameter", + ] + if (multimedia_player_framework_support_screen_capture) { + sources = [ + "$MEDIA_ROOT_DIR/test/fuzztest/common/aw_common.cpp", + "$MEDIA_ROOT_DIR/test/fuzztest/common/test_screen_capture.cpp", + "screencaptureaudioconfigure_fuzzer.cpp", + ] + } + deps = [ + "$MEDIA_PLAYER_GRAPHIC:libsurface", + "$MEDIA_PLAYER_GRAPHIC/frameworks/surface:surface", + "$MEDIA_PLAYER_GRAPHIC/rosen/modules/render_service_client:librender_service_client", + "$MEDIA_PLAYER_GRAPHIC/utils:sync_fence", + "$MEDIA_ROOT_DIR/interfaces/inner_api/native:media_client", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "hilog:libhilog", + "player_framework:media_client", + ] +} diff --git a/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/corpus/init b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/corpus/init new file mode 100644 index 000000000..2b595da0c --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/project.xml b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/project.xml new file mode 100644 index 000000000..bac4974e9 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.cpp b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.cpp new file mode 100644 index 000000000..0cb9ee25e --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "aw_common.h" +#include "string_ex.h" +#include "media_errors.h" +#include "directory_ex.h" +#include "screen_capture.h" +#include "screencaptureaudioconfigure_fuzzer.h" + +using namespace std; +using namespace OHOS; +using namespace Media; + +namespace OHOS { +namespace Media { +ScreenCaptureAudioConfigureFuzzer::ScreenCaptureAudioConfigureFuzzer() +{ +} + +ScreenCaptureAudioConfigureFuzzer::~ScreenCaptureAudioConfigureFuzzer() +{ +} + +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0; +} + +bool ScreenCaptureAudioConfigureFuzzer::FuzzScreenCaptureAudioConfigure(uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(AudioCaptureInfo)) { + return false; + } + size_t startPos = 0; + bool retFlags = TestScreenCapture::CreateScreenCapture(); + RETURN_IF(retFlags, false); + + AVScreenCaptureConfig config; + constexpr int32_t audioSourceTypesList = 5; + constexpr uint32_t recorderTime = 3; + startPos += GetObject(config.audioInfo.micCapInfo.audioSampleRate, data + startPos, size - startPos); + startPos += GetObject(config.audioInfo.micCapInfo.audioChannels, data + startPos, size - startPos); + AudioCaptureSourceType audioSourceType[audioSourceTypesList] { + SOURCE_INVALID, + SOURCE_DEFAULT, + MIC, + ALL_PLAYBACK, + APP_PLAYBACK, + }; + int32_t asourcesubscript = *reinterpret_cast(data) % (audioSourceTypesList); + config.audioInfo.micCapInfo.audioSource = audioSourceType[asourcesubscript]; + + std::shared_ptr callbackobj + = std::make_shared(screenCapture); + TestScreenCapture::SetMicrophoneEnabled(true); + TestScreenCapture::SetScreenCaptureCallback(callbackobj); + TestScreenCapture::Init(config); + TestScreenCapture::StartScreenCapture(); + sleep(recorderTime); + TestScreenCapture::StopScreenCapture(); + TestScreenCapture::Release(); + return true; +} +} // namespace Media + +bool FuzzTestScreenCaptureAudioConfigure(uint8_t *data, size_t size) +{ + if (data == nullptr) { + return true; + } + + if (size < sizeof(AudioCaptureInfo)) { + return true; + } + ScreenCaptureAudioConfigureFuzzer testScreenCapture; + return testScreenCapture.FuzzScreenCaptureAudioConfigure(data, size); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzTestScreenCaptureAudioConfigure(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.h b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.h new file mode 100644 index 000000000..0f23c0330 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencaptureaudioconfigure_fuzzer/screencaptureaudioconfigure_fuzzer.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SCREENCAPTUREAUDIOCONFIGURE_FUZZER +#define SCREENCAPTUREAUDIOCONFIGURE_FUZZER + +#include +#include +#include +#include +#include +#include +#include +#include "test_screen_capture.h" + +#define FUZZ_PROJECT_NAME "screencaptureaudioconfigure_fuzzer" + +namespace OHOS { +namespace Media { +class ScreenCaptureAudioConfigureFuzzer : public TestScreenCapture { +public: + ScreenCaptureAudioConfigureFuzzer(); + ~ScreenCaptureAudioConfigureFuzzer(); + bool FuzzScreenCaptureAudioConfigure(uint8_t *data, size_t size); + std::shared_ptr screenCapture = nullptr; +}; +} // namespace Media +bool FuzzTestScreenCaptureAudioConfigure(uint8_t *data, size_t size); +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/BUILD.gn b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/BUILD.gn new file mode 100644 index 000000000..44526f2f2 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/BUILD.gn @@ -0,0 +1,76 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/multimedia/player_framework/config.gni") +module_output_path = "player_framework/screen_capture" +MEDIA_ROOT_DIR = "//foundation/multimedia/player_framework/" + +##############################fuzztest########################################## +ohos_fuzztest("ScreenCaptureCaptureModeFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "$MEDIA_ROOT_DIR/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer" + resource_config_file = "$MEDIA_ROOT_DIR/test/fuzztest/resource/ohos_test.xml" + + include_dirs = [ + "$MEDIA_ROOT_DIR/interfaces/inner_api/native", + "$MEDIA_ROOT_DIR/services/utils/include", + "$MEDIA_ROOT_DIR/test/fuzztest/common/", + "$MEDIA_PLAYER_GRAPHIC/utils/sync_fence/export", + ] + cflags = [ + "-std=c++17", + "-fno-rtti", + "-fno-exceptions", + "-Wall", + "-fno-common", + "-fstack-protector-strong", + "-Wshadow", + "-FPIC", + "-FS", + "-O2", + "-D_FORTIFY_SOURCE=2", + "-fvisibility=hidden", + "-Wformat=2", + "-Wfloat-equal", + "-Wdate-time", + "-Werror", + "-Wextra", + "-Wimplicit-fallthrough", + "-Wsign-compare", + "-Wunused-parameter", + ] + if (multimedia_player_framework_support_screen_capture) { + sources = [ + "$MEDIA_ROOT_DIR/test/fuzztest/common/aw_common.cpp", + "$MEDIA_ROOT_DIR/test/fuzztest/common/test_screen_capture.cpp", + "screencapturecapturemode_fuzzer.cpp", + ] + } + deps = [ + "$MEDIA_PLAYER_GRAPHIC:libsurface", + "$MEDIA_PLAYER_GRAPHIC/frameworks/surface:surface", + "$MEDIA_PLAYER_GRAPHIC/rosen/modules/render_service_client:librender_service_client", + "$MEDIA_PLAYER_GRAPHIC/utils:sync_fence", + "$MEDIA_ROOT_DIR/interfaces/inner_api/native:media_client", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "hilog:libhilog", + "player_framework:media_client", + ] +} diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/corpus/init b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/corpus/init new file mode 100644 index 000000000..2b595da0c --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/project.xml b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/project.xml new file mode 100644 index 000000000..bac4974e9 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.cpp b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.cpp new file mode 100644 index 000000000..ce7bf8e5f --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "aw_common.h" +#include "string_ex.h" +#include "media_errors.h" +#include "directory_ex.h" +#include "screen_capture.h" +#include "screencapturecapturemode_fuzzer.h" + +using namespace std; +using namespace OHOS; +using namespace Media; + +namespace OHOS { +namespace Media { +ScreenCaptureCaptureModeFuzzer::ScreenCaptureCaptureModeFuzzer() +{ +} + +ScreenCaptureCaptureModeFuzzer::~ScreenCaptureCaptureModeFuzzer() +{ +} + +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0; +} + +bool ScreenCaptureCaptureModeFuzzer::FuzzScreenCaptureCaptureMode(uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(int32_t)) { + return false; + } + bool retFlags = TestScreenCapture::CreateScreenCapture(); + RETURN_IF(retFlags, false); + + AVScreenCaptureConfig config; + constexpr int32_t captureModeList = 4; + constexpr uint32_t recorderTime = 3; + CaptureMode captureMode_[captureModeList] { + CAPTURE_HOME_SCREEN, + CAPTURE_SPECIFIED_SCREEN, + CAPTURE_SPECIFIED_WINDOW, + CAPTURE_INVAILD + }; + int32_t capturemodesubscript = *reinterpret_cast(data) % (captureModeList); + config.captureMode = captureMode_[capturemodesubscript]; + + std::shared_ptr callbackobj + = std::make_shared(screenCapture); + TestScreenCapture::SetMicrophoneEnabled(true); + TestScreenCapture::SetScreenCaptureCallback(callbackobj); + TestScreenCapture::Init(config); + TestScreenCapture::StartScreenCapture(); + sleep(recorderTime); + TestScreenCapture::StopScreenCapture(); + TestScreenCapture::Release(); + return true; +} +} // namespace Media + +bool FuzzTestScreenCaptureCaptureMode(uint8_t *data, size_t size) +{ + if (data == nullptr) { + return true; + } + + if (size < sizeof(int32_t)) { + return true; + } + ScreenCaptureCaptureModeFuzzer testScreenCapture; + return testScreenCapture.FuzzScreenCaptureCaptureMode(data, size); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzTestScreenCaptureCaptureMode(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.h b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.h new file mode 100644 index 000000000..64f8fcbbb --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturecapturemode_fuzzer/screencapturecapturemode_fuzzer.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SCREENCAPTURECAPTUREMODE_FUZZER +#define SCREENCAPTURECAPTUREMODE_FUZZER + +#include +#include +#include +#include +#include +#include +#include +#include "test_screen_capture.h" + +#define FUZZ_PROJECT_NAME "screencapturecapturemode_fuzzer" + +namespace OHOS { +namespace Media { +class ScreenCaptureCaptureModeFuzzer : public TestScreenCapture { +public: + ScreenCaptureCaptureModeFuzzer(); + ~ScreenCaptureCaptureModeFuzzer(); + bool FuzzScreenCaptureCaptureMode(uint8_t *data, size_t size); + std::shared_ptr screenCapture = nullptr; +}; +} // namespace Media +bool FuzzTestScreenCaptureCaptureMode(uint8_t *data, size_t size); +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/BUILD.gn b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/BUILD.gn new file mode 100644 index 000000000..6bc7cd153 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/BUILD.gn @@ -0,0 +1,76 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/multimedia/player_framework/config.gni") +module_output_path = "player_framework/screen_capture" +MEDIA_ROOT_DIR = "//foundation/multimedia/player_framework/" + +##############################fuzztest########################################## +ohos_fuzztest("ScreenCaptureVideoConfigureFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "$MEDIA_ROOT_DIR/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer" + resource_config_file = "$MEDIA_ROOT_DIR/test/fuzztest/resource/ohos_test.xml" + + include_dirs = [ + "$MEDIA_ROOT_DIR/interfaces/inner_api/native", + "$MEDIA_ROOT_DIR/services/utils/include", + "$MEDIA_ROOT_DIR/test/fuzztest/common/", + "$MEDIA_PLAYER_GRAPHIC/utils/sync_fence/export", + ] + cflags = [ + "-std=c++17", + "-fno-rtti", + "-fno-exceptions", + "-Wall", + "-fno-common", + "-fstack-protector-strong", + "-Wshadow", + "-FPIC", + "-FS", + "-O2", + "-D_FORTIFY_SOURCE=2", + "-fvisibility=hidden", + "-Wformat=2", + "-Wfloat-equal", + "-Wdate-time", + "-Werror", + "-Wextra", + "-Wimplicit-fallthrough", + "-Wsign-compare", + "-Wunused-parameter", + ] + if (multimedia_player_framework_support_screen_capture) { + sources = [ + "$MEDIA_ROOT_DIR/test/fuzztest/common/aw_common.cpp", + "$MEDIA_ROOT_DIR/test/fuzztest/common/test_screen_capture.cpp", + "screencapturevideoconfigure_fuzzer.cpp", + ] + } + deps = [ + "$MEDIA_PLAYER_GRAPHIC:libsurface", + "$MEDIA_PLAYER_GRAPHIC/frameworks/surface:surface", + "$MEDIA_PLAYER_GRAPHIC/rosen/modules/render_service_client:librender_service_client", + "$MEDIA_PLAYER_GRAPHIC/utils:sync_fence", + "$MEDIA_ROOT_DIR/interfaces/inner_api/native:media_client", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "hilog:libhilog", + "player_framework:media_client", + ] +} diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/corpus/init b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/corpus/init new file mode 100644 index 000000000..2b595da0c --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/project.xml b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/project.xml new file mode 100644 index 000000000..bac4974e9 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.cpp b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.cpp new file mode 100644 index 000000000..048b67c5f --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "aw_common.h" +#include "string_ex.h" +#include "media_errors.h" +#include "directory_ex.h" +#include "screen_capture.h" +#include "screencapturevideoconfigure_fuzzer.h" + +using namespace std; +using namespace OHOS; +using namespace Media; + +namespace OHOS { +namespace Media { +ScreenCaptureVideoConfigureFuzzer::ScreenCaptureVideoConfigureFuzzer() +{ +} + +ScreenCaptureVideoConfigureFuzzer::~ScreenCaptureVideoConfigureFuzzer() +{ +} + +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0; +} + +bool ScreenCaptureVideoConfigureFuzzer::FuzzScreenCaptureVideoConfigure(uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(VideoCaptureInfo)) { + return false; + } + size_t startPos = 0; + bool retFlags = TestScreenCapture::CreateScreenCapture(); + RETURN_IF(retFlags, false); + + AVScreenCaptureConfig config; + constexpr int32_t videoSourceTypeList = 4; + constexpr uint32_t recorderTime = 3; + startPos += GetObject(config.videoInfo.videoCapInfo.videoFrameWidth, data + startPos, size - startPos); + startPos += GetObject(config.videoInfo.videoCapInfo.videoFrameHeight, data + startPos, size - startPos); + VideoSourceType videoSourceType[videoSourceTypeList] { + VIDEO_SOURCE_SURFACE_YUV, + VIDEO_SOURCE_SURFACE_ES, + VIDEO_SOURCE_SURFACE_RGBA, + VIDEO_SOURCE_BUTT + }; + int32_t vsourcesubscript = *reinterpret_cast(data) % (videoSourceTypeList); + config.videoInfo.videoCapInfo.videoSource = videoSourceType[vsourcesubscript]; + + std::shared_ptr callbackobj + = std::make_shared(screenCapture); + TestScreenCapture::SetMicrophoneEnabled(true); + TestScreenCapture::SetScreenCaptureCallback(callbackobj); + TestScreenCapture::Init(config); + TestScreenCapture::StartScreenCapture(); + sleep(recorderTime); + TestScreenCapture::StopScreenCapture(); + TestScreenCapture::Release(); + return true; +} +} // namespace Media + +bool FuzzTestScreenCaptureVideoConfigure(uint8_t *data, size_t size) +{ + if (data == nullptr) { + return true; + } + + if (size < sizeof(VideoCaptureInfo)) { + return true; + } + ScreenCaptureVideoConfigureFuzzer testScreenCapture; + return testScreenCapture.FuzzScreenCaptureVideoConfigure(data, size); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzTestScreenCaptureVideoConfigure(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.h b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.h new file mode 100644 index 000000000..b1e277805 --- /dev/null +++ b/test/fuzztest/screen_capture_fuzztest/screencapturevideoconfigure_fuzzer/screencapturevideoconfigure_fuzzer.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SCREENCAPTUREVIDEOCONFIGURE_FUZZER +#define SCREENCAPTUREVIDEOCONFIGURE_FUZZER + +#include +#include +#include +#include +#include +#include +#include +#include "test_screen_capture.h" + +#define FUZZ_PROJECT_NAME "screencapturevideoconfigure_fuzzer" + +namespace OHOS { +namespace Media { +class ScreenCaptureVideoConfigureFuzzer : public TestScreenCapture { +public: + ScreenCaptureVideoConfigureFuzzer(); + ~ScreenCaptureVideoConfigureFuzzer(); + bool FuzzScreenCaptureVideoConfigure(uint8_t *data, size_t size); + std::shared_ptr screenCapture = nullptr; +}; +} // namespace Media +bool FuzzTestScreenCaptureVideoConfigure(uint8_t *data, size_t size); +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/screen_capture_test/BUILD.gn b/test/unittest/screen_capture_test/BUILD.gn new file mode 100644 index 000000000..74f14c7dc --- /dev/null +++ b/test/unittest/screen_capture_test/BUILD.gn @@ -0,0 +1,93 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/multimedia/player_framework/config.gni") + +module_output_path = "player_framework/screen_capture" + +ohos_unittest("screen_capture_native_unit_test") { + module_out_path = module_output_path + include_dirs = [ + "./", + "./screen_capture_unittest/include", + "./native/include", + "$MEDIA_PLAYER_ROOT_DIR/test/unittest/common/include", + "$MEDIA_PLAYER_ROOT_DIR/interfaces/inner_api/native", + ] + + cflags = [ + "-Wall", + "-Werror", + ] + + if (multimedia_player_framework_support_screen_capture) { + sources = [ + "native/screen_capture_mock_factory.cpp", + "native/src/screen_capture_native_mock.cpp", + "screen_capture_unittest/src/screen_capture_unit_test.cpp", + ] + } + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "graphic_2d:surface", + "hilog:libhilog", + "player_framework:media_client", + "window_manager:libdm", + ] +} + +################################################################################################################## + +ohos_unittest("screen_capture_capi_unit_test") { + module_out_path = module_output_path + include_dirs = [ + "./", + "./screen_capture_unittest/include", + "./capi/include", + "$MEDIA_PLAYER_ROOT_DIR/interfaces/kits/c", + "$MEDIA_PLAYER_ROOT_DIR/test/unittest/common/include", + "$MEDIA_PLAYER_ROOT_DIR/frameworks/native/capi/common", + "$MEDIA_PLAYER_ROOT_DIR/interfaces/inner_api/native", + ] + + cflags = [ + "-Wall", + "-Werror", + ] + + if (multimedia_player_framework_support_screen_capture) { + sources = [ + "capi/screen_capture_mock_factory.cpp", + "capi/src/screen_capture_capi_mock.cpp", + "screen_capture_unittest/src/screen_capture_unit_test.cpp", + ] + } + + deps = [ "$MEDIA_PLAYER_ROOT_DIR/interfaces/kits/c:native_avscreen_capture" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "graphic_2d:surface", + "hilog:libhilog", + "player_framework:media_client", + "window_manager:libdm", + ] +} diff --git a/test/unittest/screen_capture_test/capi/include/screen_capture_capi_mock.h b/test/unittest/screen_capture_test/capi/include/screen_capture_capi_mock.h new file mode 100644 index 000000000..090c170ae --- /dev/null +++ b/test/unittest/screen_capture_test/capi/include/screen_capture_capi_mock.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SCREEN_CAPTURE_CAPI_MOCK_H +#define SCREEN_CAPTURE_CAPI_MOCK_H + +#include "native_avscreen_capture.h" +#include "screen_capture_mock.h" + +namespace OHOS { +namespace Media { +class ScreenCaptureCapiMock : public ScreenCaptureMock { +public: + explicit ScreenCaptureCapiMock(OH_AVScreenCapture* screencapture) : screenCapture_(screencapture) {} + ~ScreenCaptureCapiMock() = default; + int32_t SetScreenCaptureCallback(const std::shared_ptr& cb) override; + int32_t Init(AVScreenCaptureConfig config) override; + int32_t StartScreenCapture() override; + int32_t StopScreenCapture() override; + int32_t Release() override; + int32_t SetMicrophoneEnabled(bool isMicrophone) override; + int32_t AcquireAudioBuffer(std::shared_ptr &audioBuffer, + OHOS::Media::AudioCaptureSourceType type) override; + sptr AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, OHOS::Rect &damage) override; + int32_t ReleaseAudioBuffer(OHOS::Media::AudioCaptureSourceType type) override; + int32_t ReleaseVideoBuffer() override; + +private: + static void SetScreenCaptureCallback(OH_AVScreenCapture *screencapture, + std::shared_ptr cb); + static std::shared_ptr GetCallback(OH_AVScreenCapture *screenCapture); + static void DelCallback(OH_AVScreenCapture *screenCapture); + static void OnError(OH_AVScreenCapture *screenCapture, int32_t errorCode); + static void OnAudioBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady, OH_AudioCaptureSourceType type); + static void OnVideoBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady); + OH_AVScreenCaptureConfig Convert(AVScreenCaptureConfig config); + + static std::mutex mutex_; + static std::map> mockCbMap_; + + OH_AVScreenCapture* screenCapture_ = nullptr; + std::atomic isExit_ { false }; + AVScreenCaptureConfig config_; +}; +} // namespace Media +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/screen_capture_test/capi/screen_capture_mock_factory.cpp b/test/unittest/screen_capture_test/capi/screen_capture_mock_factory.cpp new file mode 100644 index 000000000..037ae4e3e --- /dev/null +++ b/test/unittest/screen_capture_test/capi/screen_capture_mock_factory.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "native_avscreen_capture.h" +#include "screen_capture_mock.h" +#include "screen_capture_capi_mock.h" + +namespace OHOS { +namespace Media { +std::shared_ptr ScreenCaptureMockFactory::CreateScreenCapture() +{ + OH_AVScreenCapture* screencap = OH_AVScreenCapture_Create(); + if (screencap != nullptr) { + return std::make_shared(screencap); + } + return nullptr; +} +} // namespace Media +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/screen_capture_test/capi/src/screen_capture_capi_mock.cpp b/test/unittest/screen_capture_test/capi/src/screen_capture_capi_mock.cpp new file mode 100644 index 000000000..8cb524d53 --- /dev/null +++ b/test/unittest/screen_capture_test/capi/src/screen_capture_capi_mock.cpp @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_capture_capi_mock.h" + +using namespace std; +using namespace OHOS; +using namespace OHOS::Media; +using namespace testing::ext; +using namespace OHOS::Media::ScreenCaptureTestParam; +std::mutex ScreenCaptureCapiMock::mutex_; +std::map> ScreenCaptureCapiMock::mockCbMap_; + +void ScreenCaptureCapiMock::OnError(OH_AVScreenCapture *screenCapture, int32_t errorCode) +{ + std::shared_ptr mockCb = GetCallback(screenCapture); + if (mockCb != nullptr) { + mockCb->OnError(errorCode); + } +} + +void ScreenCaptureCapiMock::OnAudioBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady, + OH_AudioCaptureSourceType type) +{ + std::shared_ptr mockCb = GetCallback(screenCapture); + if (mockCb != nullptr) { + mockCb->OnAudioBufferAvailable(isReady, static_cast(type)); + } +} + +void ScreenCaptureCapiMock::OnVideoBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady) +{ + std::shared_ptr mockCb = GetCallback(screenCapture); + if (mockCb != nullptr) { + mockCb->OnVideoBufferAvailable(isReady); + } +} + +OH_AVScreenCaptureConfig ScreenCaptureCapiMock::Convert(AVScreenCaptureConfig config) +{ + OH_AVScreenCaptureConfig config_; + config_.captureMode = static_cast(config.captureMode); + config_.dataType = static_cast(config.dataType); + config_.audioInfo.micCapInfo = { + .audioSampleRate = config.audioInfo.micCapInfo.audioSampleRate, + .audioChannels = config.audioInfo.micCapInfo.audioChannels, + .audioSource = static_cast(config.audioInfo.micCapInfo.audioSource) + }; + config_.audioInfo.innerCapInfo = { + .audioSampleRate = config.audioInfo.innerCapInfo.audioSampleRate, + .audioChannels = config.audioInfo.innerCapInfo.audioChannels, + .audioSource = static_cast(config.audioInfo.innerCapInfo.audioSource) + }; + config_.audioInfo.audioEncInfo.audioBitrate = config.audioInfo.audioEncInfo.audioBitrate; + config_.audioInfo.audioEncInfo.audioCodecformat = + static_cast(config.audioInfo.audioEncInfo.audioCodecformat); + config_.videoInfo.videoCapInfo.displayId = config.videoInfo.videoCapInfo.displayId; + std::list taskIds = config.videoInfo.videoCapInfo.taskIDs; + if (taskIds.size() > 0) { + int32_t *taskIds_temp = (int*)malloc(sizeof(int)); + for (std::list::iterator its = taskIds.begin(); its != taskIds.end(); ++its) { + *taskIds_temp = *its; + taskIds_temp++; + } + config_.videoInfo.videoCapInfo.missionIDs = taskIds_temp; + } + config_.videoInfo.videoCapInfo.missionIDsLen = taskIds.size(); + config_.videoInfo.videoCapInfo.videoFrameWidth = config.videoInfo.videoCapInfo.videoFrameWidth; + config_.videoInfo.videoCapInfo.videoFrameHeight = config.videoInfo.videoCapInfo.videoFrameHeight; + config_.videoInfo.videoCapInfo.videoSource = + static_cast(config.videoInfo.videoCapInfo.videoSource); + config_.videoInfo.videoEncInfo = { + .videoCodec = static_cast(config_.videoInfo.videoEncInfo.videoCodec), + .videoBitrate = static_cast(config_.videoInfo.videoEncInfo.videoBitrate), + .videoFrameRate = static_cast(config_.videoInfo.videoEncInfo.videoFrameRate) + }; + std::string url = config.recorderInfo.url; + if (!(url.empty())) { + config_.recorderInfo.url = (char*)(url.data()); + config_.recorderInfo.urlLen = url.size(); + } + if (config.recorderInfo.fileFormat == ContainerFormatType::CFT_MPEG_4A) { + config_.recorderInfo.fileFormat = OH_ContainerFormatType::CFT_MPEG_4A; + } else if (config.recorderInfo.fileFormat == ContainerFormatType::CFT_MPEG_4) { + config_.recorderInfo.fileFormat = OH_ContainerFormatType::CFT_MPEG_4; + } + return config_; +} + +std::shared_ptr ScreenCaptureCapiMock::GetCallback(OH_AVScreenCapture *screenCapture) +{ + std::lock_guard lock(mutex_); + if (mockCbMap_.empty()) { + return nullptr; + } + if (mockCbMap_.find(screenCapture) != mockCbMap_.end()) { + return mockCbMap_.at(screenCapture); + } + return nullptr; +} + +void ScreenCaptureCapiMock::DelCallback(OH_AVScreenCapture *screenCapture) +{ + std::lock_guard lock(mutex_); + if (mockCbMap_.empty()) { + return; + } + auto it = mockCbMap_.find(screenCapture); + if (it != mockCbMap_.end()) { + mockCbMap_.erase(it); + } +} + +void ScreenCaptureCapiMock::SetScreenCaptureCallback(OH_AVScreenCapture *screencapture, + std::shared_ptr cb) +{ + std::lock_guard lock(mutex_); + mockCbMap_[screencapture] = cb; +} + +int32_t ScreenCaptureCapiMock::SetScreenCaptureCallback(const std::shared_ptr& cb) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + if (cb != nullptr) { + SetScreenCaptureCallback(screenCapture_, cb); + struct OH_AVScreenCaptureCallback callback; + callback.onError = ScreenCaptureCapiMock::OnError; + callback.onAudioBufferAvailable = ScreenCaptureCapiMock::OnAudioBufferAvailable; + callback.onVideoBufferAvailable = ScreenCaptureCapiMock::OnVideoBufferAvailable; + return OH_AVScreenCapture_SetCallback(screenCapture_, callback); + } + return MSERR_INVALID_OPERATION; +} + +int32_t ScreenCaptureCapiMock::StartScreenCapture() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return OH_AVScreenCapture_StartScreenCapture(screenCapture_); +} + +int32_t ScreenCaptureCapiMock::Init(AVScreenCaptureConfig config) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + OH_AVScreenCaptureConfig config_ = Convert(config); + return OH_AVScreenCapture_Init(screenCapture_, config_); +} + +int32_t ScreenCaptureCapiMock::StopScreenCapture() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return OH_AVScreenCapture_StopScreenCapture(screenCapture_); +} + +int32_t ScreenCaptureCapiMock::Release() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + DelCallback(screenCapture_); + return OH_AVScreenCapture_Release(screenCapture_); +} + +int32_t ScreenCaptureCapiMock::SetMicrophoneEnabled(bool isMicrophone) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture_, isMicrophone); +} + +int32_t ScreenCaptureCapiMock::AcquireAudioBuffer(std::shared_ptr &audioBuffer, + AudioCaptureSourceType type) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + OH_AudioBuffer *buffer = (OH_AudioBuffer *)malloc(sizeof(OH_AudioBuffer)); + if (buffer == nullptr) { + return MSERR_INVALID_OPERATION; + } + auto t_type = static_cast(type); + int32_t ret = OH_AVScreenCapture_AcquireAudioBuffer(screenCapture_, &buffer, t_type); + audioBuffer = + std::make_shared(buffer->buf, buffer->size, buffer->timestamp, + static_cast(buffer->type)); + free(buffer); + buffer = nullptr; + return ret; +} + +sptr ScreenCaptureCapiMock::AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, + OHOS::Rect &damage) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, nullptr, "screenCapture_ == nullptr"); + OH_Rect damage_ = { + .x = damage.x, + .y = damage.y, + .width = damage.w, + .height = damage.h, + }; + OH_NativeBuffer* buffer = OH_AVScreenCapture_AcquireVideoBuffer(screenCapture_, &fence, ×tamp, &damage_); + sptr surfacebuffer; + if (buffer != nullptr) { + surfacebuffer = OHOS::SurfaceBuffer::NativeBufferToSurfaceBuffer(buffer); + OH_NativeBuffer_Unreference(buffer); + } + return surfacebuffer; +} + +int32_t ScreenCaptureCapiMock::ReleaseAudioBuffer(OHOS::Media::AudioCaptureSourceType type) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + auto t_type = static_cast(type); + return OH_AVScreenCapture_ReleaseAudioBuffer(screenCapture_, t_type); +} + +int32_t ScreenCaptureCapiMock::ReleaseVideoBuffer() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return OH_AVScreenCapture_ReleaseVideoBuffer(screenCapture_); +} \ No newline at end of file diff --git a/test/unittest/screen_capture_test/native/include/screen_capture_native_mock.h b/test/unittest/screen_capture_test/native/include/screen_capture_native_mock.h new file mode 100644 index 000000000..4ea79dfac --- /dev/null +++ b/test/unittest/screen_capture_test/native/include/screen_capture_native_mock.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SCREEN_CAPTURE_NATIVE_MOCK_H +#define SCREEN_CAPTURE_NATIVE_MOCK_H + +#include "screen_capture_mock.h" + +namespace OHOS { +namespace Media { +class ScreenCaptureNativeMock : public ScreenCaptureMock { +public: + explicit ScreenCaptureNativeMock(std::shared_ptr screencapture) : screenCapture_(screencapture) {} + ~ScreenCaptureNativeMock() = default; + int32_t SetScreenCaptureCallback(const std::shared_ptr& callback) override; + int32_t Init(AVScreenCaptureConfig config) override; + int32_t StartScreenCapture() override; + int32_t StopScreenCapture() override; + int32_t Release() override; + int32_t SetMicrophoneEnabled(bool isMicrophone) override; + int32_t AcquireAudioBuffer(std::shared_ptr &audioBuffer, AudioCaptureSourceType type) override; + sptr AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, OHOS::Rect &damage) override; + int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) override; + int32_t ReleaseVideoBuffer() override; + +private: + std::shared_ptr screenCapture_ = nullptr; + AVScreenCaptureConfig config_; +}; + +class ScreenCaptureNativeCallbackMock : public ScreenCaptureCallBack, public NoCopyable { +public: + ScreenCaptureNativeCallbackMock(std::shared_ptr cb, + std::weak_ptr vd) : mockCb_(cb), screenCapture_(vd) {} + ~ScreenCaptureNativeCallbackMock() + { + mockCb_ = nullptr; + } + void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) override; + void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) override; + void OnVideoBufferAvailable(bool isReady) override; + +private: + std::shared_ptr mockCb_ = nullptr; + std::weak_ptr screenCapture_; +}; +} // namespace Media +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/screen_capture_test/native/screen_capture_mock_factory.cpp b/test/unittest/screen_capture_test/native/screen_capture_mock_factory.cpp new file mode 100644 index 000000000..7e0e4b027 --- /dev/null +++ b/test/unittest/screen_capture_test/native/screen_capture_mock_factory.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_capture.h" +#include "screen_capture_mock.h" +#include "screen_capture_native_mock.h" + +namespace OHOS { +namespace Media { +std::shared_ptr ScreenCaptureMockFactory::CreateScreenCapture() +{ + auto screencap = ScreenCaptureFactory::CreateScreenCapture(); + if (screencap != nullptr) { + return std::make_shared(screencap); + } + return nullptr; +} +} // namespace Media +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/screen_capture_test/native/src/screen_capture_native_mock.cpp b/test/unittest/screen_capture_test/native/src/screen_capture_native_mock.cpp new file mode 100644 index 000000000..5c3b9dd6c --- /dev/null +++ b/test/unittest/screen_capture_test/native/src/screen_capture_native_mock.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_capture_native_mock.h" + +using namespace std; +using namespace OHOS; +using namespace testing::ext; +using namespace OHOS::Media::ScreenCaptureTestParam; + +namespace OHOS { +namespace Media { +void ScreenCaptureNativeCallbackMock::OnError(ScreenCaptureErrorType errorType, int32_t errorCode) +{ + (void)errorType; + if (mockCb_ != nullptr) { + mockCb_->OnError(errorCode); + } +} + +void ScreenCaptureNativeCallbackMock::OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) +{ + if (mockCb_ != nullptr) { + mockCb_->OnAudioBufferAvailable(isReady, type); + } +} + +void ScreenCaptureNativeCallbackMock::OnVideoBufferAvailable(bool isReady) +{ + if (mockCb_ != nullptr) { + mockCb_->OnVideoBufferAvailable(isReady); + } +} + +int32_t ScreenCaptureNativeMock::SetScreenCaptureCallback(const std::shared_ptr& callback) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + if (callback != nullptr) { + auto cb = std::make_shared(callback, screenCapture_); + return screenCapture_->SetScreenCaptureCallback(cb); + } + return MSERR_INVALID_OPERATION; +} + +int32_t ScreenCaptureNativeMock::StartScreenCapture() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->StartScreenCapture(); +} + +int32_t ScreenCaptureNativeMock::Init(AVScreenCaptureConfig config) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->Init(config); +} + +int32_t ScreenCaptureNativeMock::StopScreenCapture() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->StopScreenCapture(); +} + +int32_t ScreenCaptureNativeMock::Release() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->Release(); +} + +int32_t ScreenCaptureNativeMock::SetMicrophoneEnabled(bool isMicrophone) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->SetMicrophoneEnabled(isMicrophone); +} + +int32_t ScreenCaptureNativeMock::AcquireAudioBuffer(std::shared_ptr &audioBuffer, + AudioCaptureSourceType type) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->AcquireAudioBuffer(audioBuffer, type); +} + +sptr ScreenCaptureNativeMock::AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, + OHOS::Rect &damage) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, nullptr, "screenCapture_ == nullptr"); + return screenCapture_->AcquireVideoBuffer(fence, timestamp, damage); +} + +int32_t ScreenCaptureNativeMock::ReleaseAudioBuffer(AudioCaptureSourceType type) +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->ReleaseAudioBuffer(type); +} + +int32_t ScreenCaptureNativeMock::ReleaseVideoBuffer() +{ + UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr"); + return screenCapture_->ReleaseVideoBuffer(); +} +} // namespace Media +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/screen_capture_test/screen_capture_mock.h b/test/unittest/screen_capture_test/screen_capture_mock.h new file mode 100644 index 000000000..2e1d19234 --- /dev/null +++ b/test/unittest/screen_capture_test/screen_capture_mock.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SCREEN_CAPTURE_MOCK_H +#define SCREEN_CAPTURE_MOCK_H + +#include +#include +#include +#include +#include +#include +#include +#include "gtest/gtest.h" +#include "screen_capture.h" +#include "unittest_log.h" +#include "media_errors.h" +#include "display_manager.h" + +namespace OHOS { +namespace Media { +namespace ScreenCaptureTestParam { + constexpr uint32_t RECORDER_TIME = 3; +} // namespace ScreenCaptureTestParam + +class ScreenCaptureCallBackMock : public NoCopyable { +public: + virtual void OnError(int32_t errorCode) = 0; + virtual void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) = 0; + virtual void OnVideoBufferAvailable(bool isReady) = 0; +}; + +class ScreenCaptureMock { +public: + virtual ~ScreenCaptureMock() = default; + virtual int32_t SetScreenCaptureCallback(const std::shared_ptr& callback) = 0; + virtual int32_t Init(AVScreenCaptureConfig config) = 0; + virtual int32_t StartScreenCapture() = 0; + virtual int32_t StopScreenCapture() = 0; + virtual int32_t Release() = 0; + virtual int32_t SetMicrophoneEnabled(bool isMicrophone) = 0; + virtual int32_t AcquireAudioBuffer(std::shared_ptr &audioBuffer, AudioCaptureSourceType type) = 0; + virtual sptr AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, OHOS::Rect &damage) = 0; + virtual int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) = 0; + virtual int32_t ReleaseVideoBuffer() = 0; +}; + +class __attribute__((visibility("default"))) ScreenCaptureMockFactory { +public: + static std::shared_ptr CreateScreenCapture(); +private: + ScreenCaptureMockFactory() = delete; + ~ScreenCaptureMockFactory() = delete; +}; +} // namespace Media +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/screen_capture_test/screen_capture_unittest/include/screen_capture_unit_test.h b/test/unittest/screen_capture_test/screen_capture_unittest/include/screen_capture_unit_test.h new file mode 100644 index 000000000..9785e6630 --- /dev/null +++ b/test/unittest/screen_capture_test/screen_capture_unittest/include/screen_capture_unit_test.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SCREEN_CAPTURE_UNIT_TEST_H +#define SCREEN_CAPTURE_UNIT_TEST_H + +#include "gtest/gtest.h" +#include "screen_capture_mock.h" + +namespace OHOS { +namespace Media { +class ScreenCaptureUnitTestCallback : public ScreenCaptureCallBackMock { +public: + explicit ScreenCaptureUnitTestCallback(std::shared_ptr ScreenCapture, FILE *aFile, FILE *vFile, + int32_t aFlag, int32_t vFlag) + : screenCapture_(ScreenCapture), aFile_(aFile), vFile_(vFile), aFlag_(aFlag), vFlag_(vFlag) {} + ~ScreenCaptureUnitTestCallback() = default; + void OnError(int32_t errorCode) override; + void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) override; + void OnVideoBufferAvailable(bool isReady) override; + void DumpAudioBuffer(std::shared_ptr audioBuffer); + void DumpVideoBuffer(sptr surfacebuffer); +private: + std::shared_ptr screenCapture_; + FILE *aFile_ = nullptr; + FILE *vFile_ = nullptr; + int32_t aFlag_ = 0; + int32_t vFlag_ = 0; +}; + +class ScreenCaptureUnitTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(void); + void TearDown(void); + void AudioLoop(void); + void AudioLoopWithoutRelease(void); + int32_t SetConfig(AVScreenCaptureConfig &config); + void OpenFile(std::string filename_); + void CloseFile(void); + char filename[100] = {0}; + FILE *aFile = nullptr; + FILE *vFile = nullptr; + int32_t aFlag = 0; + int32_t vFlag = 0; +protected: + std::shared_ptr screenCapture_ = nullptr; + std::shared_ptr screenCaptureCb_ = nullptr; + std::unique_ptr audioLoop_ = nullptr; +}; +} // namespace Media +} // namespace OHOS +#endif \ No newline at end of file diff --git a/test/unittest/screen_capture_test/screen_capture_unittest/src/screen_capture_unit_test.cpp b/test/unittest/screen_capture_test/screen_capture_unittest/src/screen_capture_unit_test.cpp new file mode 100644 index 000000000..cce3044a4 --- /dev/null +++ b/test/unittest/screen_capture_test/screen_capture_unittest/src/screen_capture_unit_test.cpp @@ -0,0 +1,996 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_capture_unit_test.h" +#include "media_errors.h" + +using namespace OHOS; +using namespace OHOS::Media; +using namespace testing::ext; +using namespace std; +using namespace OHOS::Rosen; +using namespace OHOS::Media::ScreenCaptureTestParam; + +namespace OHOS { +namespace Media { +void ScreenCaptureUnitTestCallback::OnError(int32_t errorCode) +{ + cout << "Error received, errorCode:" << errorCode << endl; +} + +void ScreenCaptureUnitTestCallback::OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) +{ + if (isReady) { + std::shared_ptr audioBuffer = nullptr; + if (screenCapture_->AcquireAudioBuffer(audioBuffer, type) == MSERR_OK) { + if (audioBuffer == nullptr) { + cout << "AcquireAudioBuffer failed, audio buffer empty" << endl; + } + cout << "AcquireAudioBuffer, audioBufferLen:" << audioBuffer->length << ", timestampe:" + << audioBuffer->timestamp << ", audioSourceType:" << audioBuffer->sourcetype << endl; + DumpAudioBuffer(audioBuffer); + } + if (aFlag_ == 1) { + screenCapture_->ReleaseAudioBuffer(type); + } + } else { + cout << "AcquireAudioBuffer failed" << endl; + } +} + +void ScreenCaptureUnitTestCallback::DumpAudioBuffer(std::shared_ptr audioBuffer) +{ + if ((aFile_ != nullptr) && (audioBuffer->buffer != nullptr)) { + if (fwrite(audioBuffer->buffer, 1, audioBuffer->length, aFile_) != audioBuffer->length) { + cout << "error occurred in fwrite:" << strerror(errno) < surfacebuffer = screenCapture_->AcquireVideoBuffer(fence, timestamp, damage); + if (surfacebuffer != nullptr) { + int32_t length = surfacebuffer->GetSize(); + cout << "AcquireVideoBuffer, videoBufferLen:" << surfacebuffer->GetSize() << ", timestamp:" + << timestamp << ", size:"<< length << endl; + DumpVideoBuffer(surfacebuffer); + if (vFlag_ == 1) { + screenCapture_->ReleaseVideoBuffer(); + } + } else { + cout << "AcquireVideoBuffer failed" << endl; + } + } +} + +void ScreenCaptureUnitTestCallback::DumpVideoBuffer(sptr surfacebuffer) +{ + if (vFile_ != nullptr) { + if (fwrite(surfacebuffer->GetVirAddr(), 1, surfacebuffer->GetSize(), vFile_) != surfacebuffer->GetSize()) { + cout << "error occurred in fwrite:" << strerror(errno) <= 0) { + aFile = fopen(filename, "w+"); + if (aFile == nullptr) { + cout << "aFile audio open failed, " << strerror(errno) << endl; + } + } else { + cout << "snprintf audio file failed, " << strerror(errno) << endl; + return; + } + if (snprintf_s(filename, sizeof(filename), sizeof(filename) - 1, "/data/screen_capture/%s.yuv", + filename_.c_str()) >= 0) { + vFile = fopen(filename, "w+"); + if (vFile == nullptr) { + cout << "vFile video open failed, " << strerror(errno) << endl; + } + } else { + cout << "snprintf video file failed, " << strerror(errno) << endl; + return; + } +} + +void ScreenCaptureUnitTest::CloseFile(void) +{ + if (aFile != nullptr) { + fclose(aFile); + aFile = nullptr; + } + if (vFile != nullptr) { + fclose(vFile); + vFile = nullptr; + } +} + +void ScreenCaptureUnitTest::AudioLoop(void) +{ + int index_ = 200; + int index_audio_frame = 0; + while (index_) { + if (screenCapture_ == nullptr) { + break; + } + std::shared_ptr audioBuffer = nullptr; + AudioCaptureSourceType type = MIC; + if (screenCapture_->AcquireAudioBuffer(audioBuffer, type) == MSERR_OK) { + if (audioBuffer == nullptr) { + cout << "AcquireAudioBuffer failed, audio buffer is nullptr" << endl; + continue; + } + cout << "index audio:" << index_audio_frame++ << ", AcquireAudioBuffer, audioBufferLen:" + << audioBuffer->length << ", timestampe:" << audioBuffer->timestamp << ", audioSourceType:" + << audioBuffer->sourcetype << endl; + screenCapture_->ReleaseAudioBuffer(type); + } else { + cout << "AcquireAudioBuffer failed" << endl; + } + index_--; + } +} + +void ScreenCaptureUnitTest::AudioLoopWithoutRelease(void) +{ + int index_ = 200; + int index_audio_frame = 0; + while (index_) { + if (screenCapture_ == nullptr) { + break; + } + std::shared_ptr audioBuffer = nullptr; + AudioCaptureSourceType type = MIC; + if (screenCapture_->AcquireAudioBuffer(audioBuffer, type) == MSERR_OK) { + if (audioBuffer == nullptr) { + cout << "AcquireAudioBuffer failed, audio buffer is nullptr" << endl; + continue; + } + cout << "index audio:" << index_audio_frame++ << ", AcquireAudioBuffer, audioBufferLen:" + << audioBuffer->length << ", timestampe:" << audioBuffer->timestamp << ", audioSourceType:" + << audioBuffer->sourcetype << endl; + } else { + cout << "AcquireAudioBuffer failed" << endl; + } + index_--; + } +} + +/** + * @tc.name: screen_capture_video_configure_0001 + * @tc.desc: init with videoFrameWidth -1 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_video_configure_0001, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoFrameWidth = -1; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = false; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_video_configure_0002 + * @tc.desc: init with videoFrameHeight -1 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_video_configure_0002, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoFrameHeight = -1; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = false; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_video_configure_0003 + * @tc.desc: init with videoSource yuv + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_video_configure_0003, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_YUV; + + bool isMicrophone = false; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_video_configure_0004 + * @tc.desc: init with videoSource es + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_video_configure_0004, TestSize.Level0) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_ES; + + bool isMicrophone = false; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_without_audio_data + * @tc.desc: close microphone + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_without_audio_data, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + OpenFile("screen_capture_without_audio_data"); + + aFlag = 0; + vFlag = 1; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + bool isMicrophone = false; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(RECORDER_TIME); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_audio_configure_0001 + * @tc.desc: init with audioSampleRate -1 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_audio_configure_0001, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.audioInfo.micCapInfo.audioSampleRate = -1; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_audio_configure_0002 + * @tc.desc: init with audioChannels -1 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_audio_configure_0002, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.audioInfo.micCapInfo.audioChannels = -1; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_audio_configure_0003 + * @tc.desc: init with audioSource SOURCE_INVALID + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_audio_configure_0003, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.audioInfo.micCapInfo.audioSource = SOURCE_INVALID; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_avconfigure + * @tc.desc: init with both audioinfo and videoinfo invaild + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_avconfigure, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.audioInfo.micCapInfo.audioSource = SOURCE_INVALID; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_YUV; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_with_audio_data + * @tc.desc: open microphone + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_with_audio_data, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + OpenFile("screen_capture_with_audio_data"); + + aFlag = 1; + vFlag = 1; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(RECORDER_TIME); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_captureMode_0001 + * @tc.desc: screen capture with captureMode -1 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_captureMode_0001, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.captureMode = static_cast(-1); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_captureMode_0002 + * @tc.desc: screen capture with captureMode 5 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_captureMode_0002, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.captureMode = static_cast(5); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_init_datatype_0001 + * @tc.desc: screen capture init with ENCODED_STREAM + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_init_datatype_0001, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + config_.dataType = ENCODED_STREAM; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_init_datatype_0002 + * @tc.desc: screen capture init with CAPTURE_FILE + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_init_datatype_0002, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + config_.dataType = CAPTURE_FILE; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_init_datatype_0003 + * @tc.desc: screen capture init with INVAILD + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_init_datatype_0003, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + config_.dataType = INVAILD; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_NE(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_audioSampleRate_48000 + * @tc.desc: screen capture with audioSampleRate 48000 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_audioSampleRate_48000, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.audioInfo.micCapInfo.audioSampleRate = 48000; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + aFlag = 1; + vFlag = 1; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(RECORDER_TIME); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_video_size_0001 + * @tc.desc: screen capture with 160x160 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_video_size_0001, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoFrameWidth = 160; + config_.videoInfo.videoCapInfo.videoFrameHeight = 160; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + vFile = fopen("/data/screen_capture/screen_capture_video_size_0001.yuv", "w+"); + if (vFile == nullptr) { + cout << "vFile video open failed, " << strerror(errno) << endl; + } + + aFlag = 1; + vFlag = 1; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(RECORDER_TIME); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_video_size_0002 + * @tc.desc: screen capture with 640x480 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_video_size_0002, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoFrameWidth = 640; + config_.videoInfo.videoCapInfo.videoFrameHeight = 480; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + vFile = fopen("/data/screen_capture/screen_capture_video_size_0002.yuv", "w+"); + if (vFile == nullptr) { + cout << "vFile video open failed, " << strerror(errno) << endl; + } + + aFlag = 1; + vFlag = 1; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(RECORDER_TIME); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_video_size_0003 + * @tc.desc: screen capture with 1920x1080 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_video_size_0003, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoFrameWidth = 1920; + config_.videoInfo.videoCapInfo.videoFrameHeight = 1080; + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + vFile = fopen("/data/screen_capture/screen_capture_video_size_0003.yuv", "w+"); + if (vFile == nullptr) { + cout << "vFile video open failed, " << strerror(errno) << endl; + } + + aFlag = 1; + vFlag = 1; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(RECORDER_TIME); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_from_display + * @tc.desc: screen capture from display + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_from_display, TestSize.Level0) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + sptr display = DisplayManager::GetInstance().GetDefaultDisplaySync(); + ASSERT_NE(display, nullptr); + cout << "get displayinfo: " << endl; + cout << "width: " << display->GetWidth() << "; height: " << display->GetHeight() << "; density: " + << display->GetDpi() << "; refreshRate: " << display->GetRefreshRate() << endl; + + config_.videoInfo.videoCapInfo.videoFrameWidth = display->GetWidth(); + config_.videoInfo.videoCapInfo.videoFrameHeight = display->GetHeight(); + + aFlag = 1; + vFlag = 1; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(RECORDER_TIME); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_buffertest_0001 + * @tc.desc: screen capture buffer test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_buffertest_0001, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + int index = 200; + int index_video_frame = 0; + audioLoop_ = std::make_unique(&ScreenCaptureUnitTest::AudioLoopWithoutRelease, this); + while (index) { + int32_t fence = 0; + int64_t timestamp = 0; + OHOS::Rect damage; + sptr surfacebuffer = screenCapture_->AcquireVideoBuffer(fence, timestamp, damage); + if (surfacebuffer != nullptr) { + int32_t length = surfacebuffer->GetSize(); + cout << "index video:" << index_video_frame++ << "; AcquireVideoBuffer, videoBufferLen:" + << surfacebuffer->GetSize() << ", timestamp:" << timestamp << ", size:"<< length << endl; + } else { + cout << "AcquireVideoBuffer failed" << endl; + } + index--; + } + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + if (audioLoop_ != nullptr && audioLoop_->joinable()) { + audioLoop_->join(); + audioLoop_.reset(); + audioLoop_ = nullptr; + } + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_buffertest_0002 + * @tc.desc: screen capture buffer test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_buffertest_0002, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + bool isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + int index = 200; + int index_video_frame = 0; + audioLoop_ = std::make_unique(&ScreenCaptureUnitTest::AudioLoop, this); + while (index) { + int32_t fence = 0; + int64_t timestamp = 0; + OHOS::Rect damage; + sptr surfacebuffer = screenCapture_->AcquireVideoBuffer(fence, timestamp, damage); + if (surfacebuffer != nullptr) { + int32_t length = surfacebuffer->GetSize(); + cout << "index video:" << index_video_frame++ << "; AcquireVideoBuffer, videoBufferLen:" + << surfacebuffer->GetSize() << ", timestamp:" << timestamp << ", size:"<< length << endl; + screenCapture_->ReleaseVideoBuffer(); + } else { + cout << "AcquireVideoBuffer failed" << endl; + } + index--; + } + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + if (audioLoop_ != nullptr && audioLoop_->joinable()) { + audioLoop_->join(); + audioLoop_.reset(); + audioLoop_ = nullptr; + } + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_buffertest_0003 + * @tc.desc: screen capture buffer test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_buffertest_0003, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + aFlag = 0; + vFlag = 1; + bool isMicrophone = true; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(15); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_buffertest_0004 + * @tc.desc: screen capture buffer test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_buffertest_0004, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + aFlag = 1; + vFlag = 0; + bool isMicrophone = true; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(10); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_buffertest_0005 + * @tc.desc: screen capture buffer test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_buffertest_0005, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + + aFlag = 0; + vFlag = 0; + bool isMicrophone = true; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(10); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); +} + +/** + * @tc.name: screen_capture_mic_open_close_open + * @tc.desc: screen capture mic test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_mic_open_close_open, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + aFile = fopen("/data/screen_capture/screen_capture_mic_open_close_open.pcm", "w+"); + if (aFile == nullptr) { + cout << "aFile audio open failed, " << strerror(errno) << endl; + } + + aFlag = 1; + vFlag = 1; + bool isMicrophone = true; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(5); + isMicrophone = false; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + sleep(3); + isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + sleep(3); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_mic_close_open_close + * @tc.desc: screen capture mic test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_mic_close_open_close, TestSize.Level2) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + aFile = fopen("/data/screen_capture/screen_capture_mic_close_open_close.pcm", "w+"); + if (aFile == nullptr) { + cout << "aFile audio open failed, " << strerror(errno) << endl; + } + + aFlag = 1; + vFlag = 1; + bool isMicrophone = false; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(5); + isMicrophone = true; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + sleep(3); + isMicrophone = false; + screenCapture_->SetMicrophoneEnabled(isMicrophone); + sleep(3); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_displayId + * @tc.desc: screen capture displayId test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_displayId, TestSize.Level1) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + config_.videoInfo.videoCapInfo.displayId = 10; + OpenFile("screen_capture_displayId"); + + aFlag = 1; + vFlag = 1; + bool isMicrophone = true; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(3); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} + +/** + * @tc.name: screen_capture_taskIDs + * @tc.desc: screen capture taskIDs test + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ScreenCaptureUnitTest, screen_capture_taskIDs, TestSize.Level1) +{ + AVScreenCaptureConfig config_; + SetConfig(config_); + config_.videoInfo.videoCapInfo.videoSource = VIDEO_SOURCE_SURFACE_RGBA; + int32_t num[] = { 111, 222, 333, 444, 555 }; + list listInt_A(num, num + size(num)); + + (config_.videoInfo.videoCapInfo.taskIDs).assign(++listInt_A.begin(), --listInt_A.end()); + cout << "taskIDs: "; + for (list::iterator it = (config_.videoInfo.videoCapInfo.taskIDs).begin(); + it != (config_.videoInfo.videoCapInfo.taskIDs).end(); it++) + { + cout << *it << " "; + } + cout << endl; + OpenFile("screen_capture_taskIDs"); + + aFlag = 1; + vFlag = 1; + bool isMicrophone = true; + screenCaptureCb_ = std::make_shared(screenCapture_, aFile, vFile, aFlag, vFlag); + ASSERT_NE(nullptr, screenCaptureCb_); + screenCapture_->SetMicrophoneEnabled(isMicrophone); + EXPECT_EQ(MSERR_OK, screenCapture_->SetScreenCaptureCallback(screenCaptureCb_)); + EXPECT_EQ(MSERR_OK, screenCapture_->Init(config_)); + EXPECT_EQ(MSERR_OK, screenCapture_->StartScreenCapture()); + sleep(3); + EXPECT_EQ(MSERR_OK, screenCapture_->StopScreenCapture()); + EXPECT_EQ(MSERR_OK, screenCapture_->Release()); + CloseFile(); +} +} // namespace Media +} // namespace OHOS \ No newline at end of file -- Gitee