-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
WALMA-133: Adding fake video capture file configuration to chrome
- Loading branch information
Showing
8 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Fake video capture source | ||
|
||
## Preparing video file | ||
|
||
You can use video files as a fake video capture source in Chrome browser of format `y4m` or `mjpeg`. | ||
|
||
If you have a video file in e.g., `mp4` format, use your preferred video tool to convert it to one of the formats mentioned above. If you don't have a preferred tool, simply use `ffmpeg`. | ||
|
||
_Suggestion: use `mjpeg`, it will result in a smaller file size._ | ||
|
||
```bash | ||
# Convert mp4 to y4m. | ||
ffmpeg -y -i test.mp4 -pix_fmt yuv420p test.y4m | ||
|
||
# Convert with resize to 480p. | ||
ffmpeg -y -i test.mp4 -filter:v scale=480:-1 -pix_fmt yuv420p test.y4m | ||
|
||
# Convert mp4 to mjpeg. | ||
ffmpeg -y -i test.mp4 test.mjpeg | ||
|
||
# Convert with resize to 480p. | ||
ffmpeg -y -i test.mp4 -filter:v scale=480:-1 test.mjpeg | ||
|
||
``` | ||
|
||
### Configuring test | ||
|
||
Add the prepared video file to your `csproj` project as an `EmbeddedResource`, and then configure the test method as shown below. | ||
|
||
```csharp | ||
using Lombiq.Tests.UI.Attributes; | ||
using Lombiq.Tests.UI.Constants; | ||
using Lombiq.Tests.UI.Extensions; | ||
using Lombiq.Tests.UI.Models; | ||
using Lombiq.Tests.UI.Services; | ||
using OpenQA.Selenium; | ||
using Shouldly; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Lombiq.Tests.UI.Samples.Tests; | ||
|
||
public class BasicTests : UITestBase | ||
{ | ||
public BasicTests(ITestOutputHelper testOutputHelper) | ||
: base(testOutputHelper) | ||
{ | ||
} | ||
|
||
[Theory, Chrome] | ||
public Task TestMethod(Browser browser) => | ||
ExecuteTestAfterSetupAsync( | ||
async context => | ||
{ | ||
// ... | ||
}, | ||
browser, | ||
configuration => | ||
// Here we set the fake video source configuration. | ||
configuration.BrowserConfiguration.FakeVideoSource = new FakeBrowserVideoSource | ||
{ | ||
StreamProvider = () => | ||
// Load video file content from resources. | ||
typeof(BasicTests).Assembly.GetManifestResourceStream(typeof(BasicTests), "BasicTest.mjpeg"), | ||
// Set the video format. | ||
Format = FakeBrowserVideoSourceFileFormat.MJpeg, | ||
}); | ||
} | ||
``` |
30 changes: 30 additions & 0 deletions
30
Lombiq.Tests.UI/Extensions/FakeBrowserVideoSourceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Lombiq.Tests.UI.Constants; | ||
using Lombiq.Tests.UI.Models; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Lombiq.Tests.UI.Extensions; | ||
|
||
public static class FakeBrowserVideoSourceExtensions | ||
{ | ||
public static string SaveVideoToTempFolder(this FakeBrowserVideoSource source) | ||
{ | ||
using var fakeCameraSource = source.StreamProvider(); | ||
var fakeCameraSourcePath = Path.ChangeExtension( | ||
DirectoryPaths.GetTempDirectoryPath(Guid.NewGuid().ToString()), | ||
GetExtension(source.Format)); | ||
using var fakeCameraSourceFile = new FileStream(fakeCameraSourcePath, FileMode.CreateNew, FileAccess.Write); | ||
|
||
fakeCameraSource.CopyTo(fakeCameraSourceFile); | ||
|
||
return fakeCameraSourcePath; | ||
} | ||
|
||
private static string GetExtension(FakeBrowserVideoSourceFileFormat format) => | ||
format switch | ||
{ | ||
FakeBrowserVideoSourceFileFormat.MJpeg => "mjpeg", | ||
FakeBrowserVideoSourceFileFormat.Y4m => "y4m", | ||
_ => throw new ArgumentOutOfRangeException(nameof(format)), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace Lombiq.Tests.UI.Models; | ||
|
||
public class FakeBrowserVideoSource | ||
{ | ||
/// <summary> | ||
/// Gets or sets a callback that will be used to obtain the video content <see cref="Stream"/> and will be saved and used | ||
/// as a fake video capture file for the Chrome browser. The consumer will dispose of the returned <see cref="Stream"/> | ||
/// after the callback is called. | ||
/// </summary> | ||
public Func<Stream> StreamProvider { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the video format of the provided stream. | ||
/// </summary> | ||
public FakeBrowserVideoSourceFileFormat Format { get; set; } = FakeBrowserVideoSourceFileFormat.MJpeg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Lombiq.Tests.UI.Models; | ||
|
||
public enum FakeBrowserVideoSourceFileFormat | ||
{ | ||
MJpeg, | ||
Y4m, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters