Skip to content

Commit 78f96e0

Browse files
committed
test: CookieManager sample
1 parent 3628d08 commit 78f96e0

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Page
2+
x:Class="UITests.Toolkit.Web.CookieManagerTests"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:UITests.Toolkit.Web"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
9+
mc:Ignorable="d"
10+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
11+
12+
<StackPanel Spacing="8" Padding="12">
13+
<TextBox x:Name="NameTextBox" Text="{x:Bind ViewModel.Name, Mode=TwoWay}" Header="Name" />
14+
<TextBox x:Name="VlaueTextBox" Text="{x:Bind ViewModel.Value, Mode=TwoWay}" Header="Value" />
15+
<TextBox Text="{x:Bind ViewModel.Path, Mode=TwoWay}" Header="Path" />
16+
<TextBox Text="{x:Bind ViewModel.Domain, Mode=TwoWay}" Header="Domain" />
17+
<DatePicker Date="{x:Bind ViewModel.Expires, Mode=TwoWay}" Header="Expires" />
18+
<winui:NumberBox Value="{x:Bind ViewModel.MaxAge, Mode=TwoWay}" SpinButtonPlacementMode="Inline" Minimum="-1" Maximum="1000000" Header="Max age" />
19+
<CheckBox Content="Secure" IsChecked="{x:Bind ViewModel.Secure, Mode=TwoWay}" />
20+
<ComboBox ItemsSource="{x:Bind ViewModel.SameSiteOptions}" SelectedItem="{x:Bind ViewModel.SameSite, Mode=TwoWay}" Header="Same site" />
21+
<Button x:Name="SetCookieButton" Command="{x:Bind ViewModel.SetCommand}">Set cookie</Button>
22+
<Button x:Name="DeleteCookieButton" Command="{x:Bind ViewModel.DeleteCommand}">Delete cookie</Button>
23+
<Button x:Name="GetCookiesButton" Command="{x:Bind ViewModel.GetCommand}">Get cookies</Button>
24+
<TextBlock x:Name="CookiesTextBlock" Text="{x:Bind ViewModel.CookiesText, Mode=OneWay}" />
25+
</StackPanel>
26+
</Page>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

src/SamplesApp/UITests.Shared/UITests.Shared.projitems

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@
297297
<SubType>Designer</SubType>
298298
<Generator>MSBuild:Compile</Generator>
299299
</Page>
300+
<Page Include="$(MSBuildThisFileDirectory)Toolkit\Web\CookieManagerTests.xaml">
301+
<SubType>Designer</SubType>
302+
<Generator>MSBuild:Compile</Generator>
303+
</Page>
300304
<Page Include="$(MSBuildThisFileDirectory)Utilities\ControlStateViewer.xaml">
301305
<SubType>Designer</SubType>
302306
<Generator>MSBuild:Compile</Generator>
@@ -4229,6 +4233,9 @@
42294233
<Compile Include="$(MSBuildThisFileDirectory)Toolkit\ElevationView_Clipping.xaml.cs">
42304234
<DependentUpon>ElevationView_Clipping.xaml</DependentUpon>
42314235
</Compile>
4236+
<Compile Include="$(MSBuildThisFileDirectory)Toolkit\Web\CookieManagerTests.xaml.cs">
4237+
<DependentUpon>CookieManagerTests.xaml</DependentUpon>
4238+
</Compile>
42324239
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ControlStateViewer.xaml.cs">
42334240
<DependentUpon>ControlStateViewer.xaml</DependentUpon>
42344241
</Compile>

0 commit comments

Comments
 (0)