Skip to content

Conversation

@johnedocampo
Copy link
Contributor

@johnedocampo johnedocampo commented Feb 11, 2026

Currently, creating unittests for MediaCapabilitiesCache::FindVideoDecoder() is difficult due to the function's reliance on VideoCodecCapabilities. This class (along with its sibling class AudioCodecCapabilities and parent class CodecCapabilities) are heavily coupled with JNI calls, which makes mocking them in a unit testing environment difficult.

This PR addresses this issue by updating VideoCodecCapabilities and AudioCodecCapabilities to now be able to create instances of each class for testing purposes. Each class has a new CreateForTesting static method, which will allow developers to create these objects with their own values. These new methods are completely decoupled from JNI initialization, making them suitable for a unit testing environment.

The class function VideoCodecCapability::AreResolutionAndRateSupported has been altered slightly to now check if the |j_video_capabilities_| value is not null. In production, this change is no-op as |j_video_capabilities_| is always assigned. However, in a unit testing environment, this value is set to null as we have no means of properly retrieving the corresponding java object.

Follow-up PRs will be added that will make use of these new testing ctors.

Bug: 433773029

@github-actions
Copy link

🤖 Gemini Suggested Commit Message


media: Add test support for codec capabilities

Add new constructors for CodecCapability, AudioCodecCapability, and
VideoCodecCapability that take parameters directly instead of relying on
JNIEnv and Java objects. Introduce CreateForTesting static methods
to simplify creation of these objects in unit tests. This enables
isolated testing of media codec capabilities logic.
Also, add a null check for j_video_capabilities_ in
VideoCodecCapability::AreResolutionAndRateSupported to prevent
crashes when this field is uninitialized during unit tests.

Bug: 433773029

💡 Pro Tips for a Better Commit Message:

  1. Influence the Result: Want to change the output? You can write custom prompts or instructions directly in the Pull Request description. The model uses that text to generate the message.
  2. Re-run the Generator: Post a comment with: /generate-commit-message

@johnedocampo johnedocampo changed the title [WIP]Add testing To Audio/VideoCodecCapabilities media: Refactor Audio/Video CodecCapabilities for testing Feb 11, 2026
@johnedocampo
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors CodecCapability and its derived classes to facilitate unit testing by decoupling them from JNI dependencies. This is achieved by introducing CreateForTesting static factory methods and corresponding constructors that don't rely on Java objects. The overall approach is sound and effectively addresses the problem described. My review includes suggestions to adopt modern C++ best practices, specifically using std::make_unique, which aligns with the project's referenced Chromium style guide and improves code safety and readability.

@johnedocampo johnedocampo force-pushed the add_capabilities_testing branch from df422a0 to b409152 Compare February 11, 2026 01:16
@johnedocampo johnedocampo changed the title media: Refactor Audio/Video CodecCapabilities for testing media: Update Audio/Video CodecCapabilities for testing Feb 11, 2026
@johnedocampo johnedocampo force-pushed the add_capabilities_testing branch from b409152 to 995e0f7 Compare February 11, 2026 17:35
@johnedocampo johnedocampo marked this pull request as ready for review February 11, 2026 17:36
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces new constructors and CreateForTesting static methods for CodecCapability, AudioCodecCapability, and VideoCodecCapability classes. These changes aim to facilitate unit testing by decoupling the object creation from JNI calls. Additionally, a null check for j_video_capabilities_ has been added in VideoCodecCapability::AreResolutionAndRateSupported to prevent crashes in unit test environments. The changes are well-explained and address a clear need for improved testability.

Comment on lines +259 to +261
return std::unique_ptr<AudioCodecCapability>(new AudioCodecCapability(
std::move(name), is_secure_req, is_secure_sup, is_tunnel_req,
is_tunnel_sup, supported_bitrates));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The CreateForTesting method uses new AudioCodecCapability(...) directly. While this is functional, it's generally preferred to use std::make_unique for creating std::unique_ptr instances. std::make_unique provides exception safety and can be more efficient by performing a single allocation for both the object and the unique_ptr's control block.

  return std::make_unique<AudioCodecCapability>(std::move(name), is_secure_req,
                                                is_secure_sup, is_tunnel_req,
                                                is_tunnel_sup, supported_bitrates);

Comment on lines +333 to +337
return std::unique_ptr<VideoCodecCapability>(new VideoCodecCapability(
std::move(name), is_secure_req, is_secure_sup, is_tunnel_req,
is_tunnel_sup, is_software_decoder, is_hdr_capable,
/*j_video_capabilities=*/std::move(null_ref), supported_widths,
supported_heights, supported_bitrates, supported_frame_rates));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the AudioCodecCapability::CreateForTesting method, it's recommended to use std::make_unique here for consistency and best practices.

  return std::make_unique<VideoCodecCapability>(std::move(name), is_secure_req,
                                                is_secure_sup, is_tunnel_req,
                                                is_tunnel_sup, is_software_decoder,
                                                is_hdr_capable, std::move(null_ref),
                                                supported_widths, supported_heights,
                                                supported_bitrates, supported_frame_rates);

@johnedocampo johnedocampo force-pushed the add_capabilities_testing branch from 995e0f7 to 665deaa Compare February 11, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant