|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | | -using System; |
5 | | -using System.Collections.Generic; |
6 | | -using System.Linq; |
| 4 | +using System.Buffers; |
7 | 5 | using System.Text; |
8 | 6 | using System.Text.Json; |
9 | 7 | using Microsoft.AspNetCore.Components.Infrastructure; |
10 | | -using Microsoft.AspNetCore.Components.PersistentState; |
11 | 8 | using Microsoft.AspNetCore.Components.Rendering; |
12 | 9 | using Microsoft.AspNetCore.Components.RenderTree; |
13 | 10 | using Microsoft.Extensions.DependencyInjection; |
@@ -604,6 +601,57 @@ public async Task PersistAsync_CanPersistValueTypes_NullableTupleProperty() |
604 | 601 | Assert.Equal(("test2", 789), retrievedValue); |
605 | 602 | } |
606 | 603 |
|
| 604 | + [Fact] |
| 605 | + public void PersistAsync_CanUseCustomSerializer() |
| 606 | + { |
| 607 | + // Arrange |
| 608 | + var currentState = new Dictionary<string, byte[]>(); |
| 609 | + var state = new PersistentComponentState(currentState, [], []); |
| 610 | + var serviceProvider = new ServiceCollection().BuildServiceProvider(); |
| 611 | + var stateValueProvider = new PersistentStateValueProvider(state, NullLogger<PersistentStateValueProvider>.Instance, serviceProvider); |
| 612 | + var customSerializer = new TestStringSerializer(); |
| 613 | + var testValue = "Hello, World!"; |
| 614 | + |
| 615 | + state.PersistingState = true; |
| 616 | + |
| 617 | + // Act |
| 618 | + stateValueProvider.PersistAsync("test-key", testValue, customSerializer); |
| 619 | + |
| 620 | + // Assert |
| 621 | + state.PersistingState = false; |
| 622 | + |
| 623 | + // Simulate the state transfer that happens between persist and restore phases |
| 624 | + var newState = new PersistentComponentState(new Dictionary<string, byte[]>(), [], []); |
| 625 | + newState.InitializeExistingState(currentState, RestoreContext.InitialValue); |
| 626 | + var newStateValueProvider = new PersistentStateValueProvider(newState, NullLogger<PersistentStateValueProvider>.Instance, serviceProvider); |
| 627 | + |
| 628 | + Assert.True(newStateValueProvider.TryTake("test-key", customSerializer, out var retrievedValue)); |
| 629 | + Assert.Equal(testValue, retrievedValue); |
| 630 | + } |
| 631 | + |
| 632 | + [Fact] |
| 633 | + public void TryTake_CanUseCustomSerializer() |
| 634 | + { |
| 635 | + // Arrange |
| 636 | + var customData = "Custom Data"; |
| 637 | + var customBytes = Encoding.UTF8.GetBytes(customData); |
| 638 | + var existingState = new Dictionary<string, byte[]> { { "test-key", customBytes } }; |
| 639 | + |
| 640 | + var state = new PersistentComponentState(new Dictionary<string, byte[]>(), [], []); |
| 641 | + state.InitializeExistingState(existingState, RestoreContext.InitialValue); |
| 642 | + |
| 643 | + var serviceProvider = new ServiceCollection().BuildServiceProvider(); |
| 644 | + var stateValueProvider = new PersistentStateValueProvider(state, NullLogger<PersistentStateValueProvider>.Instance, serviceProvider); |
| 645 | + var customSerializer = new TestStringSerializer(); |
| 646 | + |
| 647 | + // Act |
| 648 | + var success = stateValueProvider.TryTake("test-key", customSerializer, out var retrievedValue); |
| 649 | + |
| 650 | + // Assert |
| 651 | + Assert.True(success); |
| 652 | + Assert.Equal(customData, retrievedValue); |
| 653 | + } |
| 654 | + |
607 | 655 | private static void InitializeState(PersistentComponentState state, List<(ComponentState componentState, string propertyName, string value)> items) |
608 | 656 | { |
609 | 657 | var dictionary = new Dictionary<string, byte[]>(); |
@@ -723,4 +771,19 @@ private class ParentComponent : IComponent |
723 | 771 | public void Attach(RenderHandle renderHandle) => throw new NotImplementedException(); |
724 | 772 | public Task SetParametersAsync(ParameterView parameters) => throw new NotImplementedException(); |
725 | 773 | } |
| 774 | + |
| 775 | + private class TestStringSerializer : PersistentComponentStateSerializer<string> |
| 776 | + { |
| 777 | + public override void Persist(string value, IBufferWriter<byte> writer) |
| 778 | + { |
| 779 | + var bytes = Encoding.UTF8.GetBytes(value); |
| 780 | + writer.Write(bytes); |
| 781 | + } |
| 782 | + |
| 783 | + public override string Restore(ReadOnlySequence<byte> data) |
| 784 | + { |
| 785 | + var bytes = data.ToArray(); |
| 786 | + return Encoding.UTF8.GetString(bytes); |
| 787 | + } |
| 788 | + } |
726 | 789 | } |
0 commit comments