|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using PuppeteerSharp.Tests.Attributes; |
| 5 | +using PuppeteerSharp.Xunit; |
| 6 | +using Xunit; |
| 7 | +using Xunit.Abstractions; |
| 8 | + |
| 9 | +namespace PuppeteerSharp.Tests.IdleOverrideTests |
| 10 | +{ |
| 11 | + [Collection(TestConstants.TestFixtureCollectionName)] |
| 12 | + public class IdleOverrideTests : PuppeteerPageBaseTest |
| 13 | + { |
| 14 | + public IdleOverrideTests(ITestOutputHelper output) : base(output) |
| 15 | + { |
| 16 | + } |
| 17 | + |
| 18 | + private async Task<string> GetIdleStateAsync() |
| 19 | + { |
| 20 | + var stateElement = await Page.QuerySelectorAsync("#state"); |
| 21 | + return await Page.EvaluateFunctionAsync<string>( |
| 22 | + @"(element) => { |
| 23 | + return element.innerText; |
| 24 | + }", |
| 25 | + stateElement); |
| 26 | + } |
| 27 | + |
| 28 | + private async Task VerifyStateAsync(string expectedState) |
| 29 | + { |
| 30 | + var actualState = await GetIdleStateAsync(); |
| 31 | + Assert.Equal(expectedState, actualState); |
| 32 | + } |
| 33 | + |
| 34 | + [PuppeteerTest("idle_override.spec.ts", "Emulate idle state", "changing idle state emulation causes change of the IdleDetector state")] |
| 35 | + [SkipBrowserFact(skipFirefox: true)] |
| 36 | + public async Task ChangingIdleStateEmulationCausesChangeOfTheIdleDetectorState() |
| 37 | + { |
| 38 | + await Context.OverridePermissionsAsync( |
| 39 | + TestConstants.ServerUrl + "/idle-detector.html", |
| 40 | + new[] |
| 41 | + { |
| 42 | + OverridePermission.IdleDetection, |
| 43 | + }); |
| 44 | + |
| 45 | + await Page.GoToAsync(TestConstants.ServerUrl+ "/idle-detector.html"); |
| 46 | + |
| 47 | + // Store initial state, as soon as it is not guaranteed to be `active, unlocked`. |
| 48 | + var initialState = await GetIdleStateAsync(); |
| 49 | + |
| 50 | + // Emulate Idle states and verify IdleDetector updates state accordingly. |
| 51 | + await Page.EmulateIdleStateAsync(new EmulateIdleOverrides { |
| 52 | + IsUserActive = false, |
| 53 | + IsScreenUnlocked = false, |
| 54 | + }); |
| 55 | + |
| 56 | + await VerifyStateAsync("Idle state: idle, locked."); |
| 57 | + |
| 58 | + await Page.EmulateIdleStateAsync(new EmulateIdleOverrides { |
| 59 | + IsUserActive = true, |
| 60 | + IsScreenUnlocked = false, |
| 61 | + }); |
| 62 | + await VerifyStateAsync("Idle state: active, locked."); |
| 63 | + |
| 64 | + await Page.EmulateIdleStateAsync(new EmulateIdleOverrides { |
| 65 | + IsUserActive = true, |
| 66 | + IsScreenUnlocked = true, |
| 67 | + }); |
| 68 | + await VerifyStateAsync("Idle state: active, unlocked."); |
| 69 | + |
| 70 | + await Page.EmulateIdleStateAsync(new EmulateIdleOverrides { |
| 71 | + IsUserActive = false, |
| 72 | + IsScreenUnlocked = true, |
| 73 | + }); |
| 74 | + await VerifyStateAsync("Idle state: idle, unlocked."); |
| 75 | + |
| 76 | + // Remove Idle emulation and verify IdleDetector is in initial state. |
| 77 | + await Page.EmulateIdleStateAsync(); |
| 78 | + await VerifyStateAsync(initialState); |
| 79 | + |
| 80 | + // Emulate idle state again after removing emulation. |
| 81 | + await Page.EmulateIdleStateAsync(new EmulateIdleOverrides { |
| 82 | + IsUserActive = false, |
| 83 | + IsScreenUnlocked = false, |
| 84 | + }); |
| 85 | + await VerifyStateAsync("Idle state: idle, locked."); |
| 86 | + |
| 87 | + // Remove emulation second time. |
| 88 | + await Page.EmulateIdleStateAsync(); |
| 89 | + await VerifyStateAsync(initialState); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments