Skip to content

Commit

Permalink
Add EmulateMediaTypeAsync and EmulateMediaFeaturesAsync (hardkoded#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Nov 5, 2019
1 parent 4e6d984 commit 2721e9b
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 14 deletions.
46 changes: 46 additions & 0 deletions lib/PuppeteerSharp.Tests/PageTests/EmulateMediaFeaturesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Threading.Tasks;
using PuppeteerSharp.Media;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.PageTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class EmulateMediaFeaturesAsyncTests : PuppeteerPageBaseTest
{
public EmulateMediaFeaturesAsyncTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
public async Task ShouldWork()
{
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" },
});
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: no-preference)').matches"));
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "light" },
});
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches"));
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" },
});
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches"));
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" },
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "light" },
});
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: no-preference)').matches"));
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches"));
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
namespace PuppeteerSharp.Tests.PageTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class EmulateMediaTests : PuppeteerPageBaseTest
public class EmulateMediaTypeTests : PuppeteerPageBaseTest
{
public EmulateMediaTests(ITestOutputHelper output) : base(output)
public EmulateMediaTypeTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
public async Task ShouldWork()
{
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));
Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));
await Page.EmulateMediaAsync(MediaType.Print);
Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));
await Page.EmulateMediaAsync(MediaType.None);
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));
Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));
Assert.True(await Page.EvaluateExpressionAsync<bool>("matchMedia('screen').matches"));
Assert.False(await Page.EvaluateExpressionAsync<bool>("matchMedia('print').matches"));
await Page.EmulateMediaTypeAsync(MediaType.Print);
Assert.False(await Page.EvaluateExpressionAsync<bool>("matchMedia('screen').matches"));
Assert.True(await Page.EvaluateExpressionAsync<bool>("matchMedia('print').matches"));
await Page.EmulateMediaTypeAsync(MediaType.None);
Assert.True(await Page.EvaluateExpressionAsync<bool>("matchMedia('screen').matches"));
Assert.False(await Page.EvaluateExpressionAsync<bool>("matchMedia('print').matches"));
}
}
}
24 changes: 24 additions & 0 deletions lib/PuppeteerSharp/MediaFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace PuppeteerSharp
{
/// <summary>
/// Meadia Feature. See <see cref="Page.EmulateMediaFeaturesAsync(System.Collections.Generic.IEnumerable{MediaFeatureValue})"/>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum MediaFeature
{
/// <summary>
/// prefers-color-scheme media feature.
/// </summary>
[EnumMember(Value = "prefers-color-scheme")]
PrefersColorScheme,
/// <summary>
/// prefers-reduced-motion media feature.
/// </summary>
[EnumMember(Value = "prefers-reduced-motion")]
PrefersReducedMotion
}
}
20 changes: 20 additions & 0 deletions lib/PuppeteerSharp/MediaFeatureValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Newtonsoft.Json;

namespace PuppeteerSharp
{
/// <summary>
/// Media Feature. <see cref="Page.EmulateMediaFeaturesAsync(System.Collections.Generic.IEnumerable{MediaFeatureValue})"/>
/// </summary>
public class MediaFeatureValue
{
/// <summary>
/// The CSS media feature name. Supported names are `'prefers-colors-scheme'` and `'prefers-reduced-motion'`.
/// </summary>
[JsonProperty(PropertyName = "name")]
public MediaFeature MediaFeature { get; set; }
/// <summary>
/// The value for the given CSS media feature.
/// </summary>
public string Value { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using PuppeteerSharp.Media;

namespace PuppeteerSharp.Messaging
{
internal class EmulationSetEmulatedMediaFeatureRequest
{
public IEnumerable<MediaFeatureValue> Features { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using PuppeteerSharp.Media;
using System.Collections.Generic;
using PuppeteerSharp.Media;

namespace PuppeteerSharp.Messaging
{
internal class EmulationSetEmulatedMediaRequest
internal class EmulationSetEmulatedMediaTypeRequest
{
public MediaType Media { get; set; }
}
Expand Down
73 changes: 71 additions & 2 deletions lib/PuppeteerSharp/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,77 @@ public Task SetJavaScriptEnabledAsync(bool enabled)
/// </summary>
/// <returns>Task.</returns>
/// <param name="media">Media to set.</param>
public Task EmulateMediaAsync(MediaType media)
=> Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaRequest { Media = media });
[Obsolete("User EmulateMediaTypeAsync instead")]
public Task EmulateMediaAsync(MediaType media) => EmulateMediaTypeAsync(media);

/// <summary>
/// Emulates a media such as screen or print.
/// </summary>
/// <param name="type">Media to set.</param>
/// <example>
/// <code>
/// <![CDATA[
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");
/// // → true
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");
/// // → true
/// await page.EmulateMediaTypeAsync(MediaType.Print);
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");
/// // → false
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");
/// // → true
/// await page.EmulateMediaTypeAsync(MediaType.None);
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");
/// // → true
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");
/// // → true
/// ]]>
/// </code>
/// </example>
/// <returns>Emulate media type task.</returns>
public Task EmulateMediaTypeAsync(MediaType type)
=> Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaTypeRequest { Media = type });

/// <summary>
/// Given an array of media feature objects, emulates CSS media features on the page.
/// </summary>
/// <param name="features">Features to apply</param>
/// <example>
/// <code>
/// <![CDATA[
/// await page.EmulateMediaFeaturesAsync(new MediaFeature[]{ new MediaFeature { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" }});
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches)");
/// // → true
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches)");
/// // → false
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
/// // → false
/// await page.EmulateMediaFeaturesAsync(new MediaFeature[]{ new MediaFeature { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" }});
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches)");
/// // → true
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
/// // → false
/// await page.EmulateMediaFeaturesAsync(new MediaFeature[]
/// {
/// new MediaFeature { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" },
/// new MediaFeature { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" },
/// });
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches)");
/// // → true
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches)");
/// // → false
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
/// // → false
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches)");
/// // → true
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
/// // → false
/// ]]>
/// </code>
/// </example>
/// <returns>Emulate features task</returns>
public Task EmulateMediaFeaturesAsync(IEnumerable<MediaFeatureValue> features)
=> Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaFeatureRequest { Features = features });

/// <summary>
/// Sets the viewport.
Expand Down

0 comments on commit 2721e9b

Please sign in to comment.