|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Runtime.InteropServices.WindowsRuntime; |
| 4 | +using Uno.UI.Samples.Controls; |
| 5 | +using Uno.UI.Samples.UITests.Helpers; |
| 6 | +using Uno.UI.Toolkit.Web; |
| 7 | +using Windows.UI.Core; |
| 8 | +using Windows.UI.Xaml.Controls; |
| 9 | +using System.Windows.Input; |
| 10 | + |
| 11 | +namespace UITests.Toolkit.Web |
| 12 | +{ |
| 13 | +#if true |
| 14 | + [Sample("Toolkit", ViewModelType = typeof(CookieManagerTestsViewModel))] |
| 15 | +#endif |
| 16 | + public sealed partial class CookieManagerTests : Page |
| 17 | + { |
| 18 | + public CookieManagerTests() |
| 19 | + { |
| 20 | + this.InitializeComponent(); |
| 21 | + this.DataContextChanged += CookieManagerTests_DataContextChanged; |
| 22 | + } |
| 23 | + |
| 24 | + private void CookieManagerTests_DataContextChanged(Windows.UI.Xaml.DependencyObject sender, Windows.UI.Xaml.DataContextChangedEventArgs args) |
| 25 | + { |
| 26 | + ViewModel = args.NewValue as CookieManagerTestsViewModel; |
| 27 | + } |
| 28 | + |
| 29 | + public CookieManagerTestsViewModel ViewModel { get; private set; } |
| 30 | + } |
| 31 | + |
| 32 | + public class CookieManagerTestsViewModel : ViewModelBase |
| 33 | + { |
| 34 | + private string _cookiesText = string.Empty; |
| 35 | + |
| 36 | + public CookieManagerTestsViewModel(CoreDispatcher dispatcher) : base(dispatcher) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + public string Name { get; set; } |
| 41 | + |
| 42 | + public string Value { get; set; } |
| 43 | + |
| 44 | + public string Path { get; set; } |
| 45 | + |
| 46 | + public string Domain { get; set; } |
| 47 | + |
| 48 | + public int MaxAge { get; set; } |
| 49 | + |
| 50 | + public DateTimeOffset Expires { get; set; } = DateTimeOffset.UtcNow.AddDays(1); |
| 51 | + |
| 52 | + public bool Secure { get; set; } |
| 53 | + |
| 54 | + public CookieSameSite[] SameSiteOptions { get; } = Enum.GetValues(typeof(CookieSameSite)).OfType<CookieSameSite>().ToArray(); |
| 55 | + |
| 56 | + public CookieSameSite SameSite { get; set; } |
| 57 | + |
| 58 | + public string CookiesText |
| 59 | + { |
| 60 | + get => _cookiesText; |
| 61 | + set => Set(ref _cookiesText, value); |
| 62 | + } |
| 63 | + |
| 64 | + public ICommand SetCommand => GetOrCreateCommand(SetCookie); |
| 65 | + |
| 66 | + private void SetCookie() |
| 67 | + { |
| 68 | + var request = new SetCookieRequest(Name, Value); |
| 69 | + if (!string.IsNullOrEmpty(Path)) |
| 70 | + { |
| 71 | + request.Path = Path; |
| 72 | + } |
| 73 | + |
| 74 | + if (!string.IsNullOrEmpty(Domain)) |
| 75 | + { |
| 76 | + request.Domain = Domain; |
| 77 | + } |
| 78 | + |
| 79 | + if (MaxAge > 0) |
| 80 | + { |
| 81 | + request.MaxAge = MaxAge; |
| 82 | + } |
| 83 | + |
| 84 | + if (Expires >= DateTimeOffset.UtcNow) |
| 85 | + { |
| 86 | + request.Expires = Expires; |
| 87 | + } |
| 88 | + |
| 89 | + if (SameSite != CookieSameSite.Lax) |
| 90 | + { |
| 91 | + request.SameSite = SameSite; |
| 92 | + } |
| 93 | + |
| 94 | + request.Secure = Secure; |
| 95 | + |
| 96 | + CookieManager.GetDefault().SetCookie(request); |
| 97 | + } |
| 98 | + |
| 99 | + public ICommand GetCommand => GetOrCreateCommand(GetCookies); |
| 100 | + |
| 101 | + private void GetCookies() |
| 102 | + { |
| 103 | + var cookies = CookieManager.GetDefault().GetCookies(); |
| 104 | + CookiesText = string.Join(",", cookies.Select(cookie => $"{cookie.Name}:{cookie.Value}")); |
| 105 | + } |
| 106 | + |
| 107 | + public ICommand DeleteCommand => GetOrCreateCommand(DeleteCookie); |
| 108 | + |
| 109 | + private void DeleteCookie() |
| 110 | + { |
| 111 | + CookieManager.GetDefault().DeleteCookie(Name, !string.IsNullOrEmpty(Path) ? Path : null); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments