Skip to content

Commit

Permalink
test: UWP and Android PdfDocument Sample and Runtime test
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Mar 11, 2023
1 parent 319ea60 commit a1e526d
Show file tree
Hide file tree
Showing 15 changed files with 475 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/SamplesApp/SamplesApp.Droid/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
[assembly: UsesPermission("android.permission.MANAGE_MEDIA")]
[assembly: UsesPermission("android.permission.USE_FULL_SCREEN_INTENT")]
[assembly: UsesPermission("android.permission.WRITE_EXTERNAL_STORAGE")]
[assembly: UsesPermission("android.permission.READ_EXTERNAL_STORAGE")]
[assembly: UsesPermission("android.permission.CAMERA")]
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
[assembly: UsesPermission("android.permission.ACCESS_NETWORK_STATE")]
[assembly: UsesPermission("android.permission.SET_WALLPAPER")]
[assembly: UsesPermission("android.permission.READ_CONTACTS")]
[assembly: UsesPermission("android.permission.WRITE_EXTERNAL_STORAGE")]
[assembly: UsesPermission("android.permission.READ_EXTERNAL_STORAGE")]
9 changes: 9 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -8855,5 +8855,14 @@
<Content Include="$(MSBuildThisFileDirectory)Assets\Diagnostics\avatar.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\libre-camera-panorama.svg" />
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)Windows_Data\Pdf\PdfDocumentRenderTest.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Compile Include="$(MSBuildThisFileDirectory)Windows_Data\Pdf\PdfDocumentRenderTest.xaml.cs">
<DependentUpon>PdfDocumentRenderTest.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="ItemExclusions.props" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<UserControl
x:Class="UITests.Shared.Windows_Data_Pdf.PdfDocumentRenderTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="using:Uno.UI.Toolkit"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal"
Margin="0,10,0,0">
<Button x:Name="LoadButton" Content="Load document" Click="LoadDocument" Margin="5,0"/>
<PasswordBox x:Name="PasswordBox" Width="200" PlaceholderText="Optional password"/>
</StackPanel>
<StackPanel x:Name="RenderingPanel" Visibility="Collapsed"
Grid.Row="1">
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Name="ViewPageLabel" VerticalAlignment="center">View page</TextBlock>
<!--
Always give a TextBox a name that's accessible to a screen reader. In this case,
reference the labeling TextBlock to have the accessible name set on the TextBox.
-->
<TextBox x:Name="PageNumberBox" InputScope="Number" Width="30" Text="1" TextAlignment="Right" Margin="5,0,5,0"
AutomationProperties.LabeledBy="{Binding ElementName=ViewPageLabel}"/>
<TextBlock VerticalAlignment="Center">of <Run x:Name="PageCountText"/>.</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<!--
Always give a ComboBox a name that's accessible to a screen reader. Given that there's no labeling
TextBlock to reference, explicitly set the accessible name on the ComboBox. A shipping app would
localize this name.
-->
<ComboBox x:Name="Options" AutomationProperties.Name="View page size" SelectedIndex="0">
<ComboBoxItem>Arrange to Width</ComboBoxItem>
<ComboBoxItem>Actual size</ComboBoxItem>
<ComboBoxItem>Half size on beige background</ComboBoxItem>
<ComboBoxItem>Crop to center of page</ComboBoxItem>
</ComboBox>
<Button Click="ViewPage" Content="View" Margin="10,0,0,0"/>
</StackPanel>
</StackPanel>
<ScrollViewer Margin="12,20,12,12"
HorizontalScrollBarVisibility="Auto"
Grid.Row="2"
x:Name="viewer">
<!--
Always give an Image a name that's accessible to screen reader, (unless the Image is purely decorative.)
A shipping app would localize this name.
-->
<Border Background="White"
toolkit:UIElementExtensions.Elevation="2"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="10" >
<Image x:Name="Output"
AutomationProperties.Name="PDF document content"
Stretch="None"
/>
</Border>
</ScrollViewer>
<SymbolIcon Symbol="Permissions"
Margin="12"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Width="25"
Visibility="Collapsed"
Grid.Row="2"
x:Name="Protected"/>
<ProgressRing x:Name="ProgressControl"
Grid.Row="2"
Height="50"
Width="50"
IsActive="True"
Visibility="Collapsed"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Uno.UI.Samples.Controls;
using Windows.Data.Pdf;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;

namespace UITests.Shared.Windows_Data_Pdf
{
[SampleControlInfo("Windows.Data.Pdf", "PdfDocument", description: "Demonstrates use of Windows.Data.Pdf.PdfDocument", ignoreInSnapshotTests: true)]
public sealed partial class PdfDocumentRenderTest : UserControl
{
const int WrongPassword = unchecked((int)0x8007052b); // HRESULT_FROM_WIN32(ERROR_WRONG_PASSWORD)
const int GenericFail = unchecked((int)0x80004005); // E_FAIL
private PdfDocument pdfDocument;

public PdfDocumentRenderTest()
{
this.InitializeComponent();
}

private async void LoadDocument(object sender, RoutedEventArgs args)
{
LoadButton.IsEnabled = false;

pdfDocument = null;
Output.Source = null;
PageNumberBox.Text = "1";
RenderingPanel.Visibility = Visibility.Collapsed;

var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".pdf");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
ProgressControl.Visibility = Visibility.Visible;
try
{
var stream = await file.OpenAsync(FileAccessMode.Read);
pdfDocument = await PdfDocument.LoadFromStreamAsync(stream, PasswordBox.Password);
}
catch (Exception ex)
{
Windows.UI.Popups.MessageDialog dialog = ex.HResult switch
{
WrongPassword => new("Document is password-protected and password is incorrect.", "Error"),
GenericFail => new("Document is not a valid PDF.", "Error"),
_ => new(ex.Message, "Error")
};
await dialog.ShowAsync();
}

if (pdfDocument != null)
{
RenderingPanel.Visibility = Visibility.Visible;
if (pdfDocument.IsPasswordProtected)
{
Protected.Visibility = Visibility.Visible;
}
else
{
Protected.Visibility = Visibility.Collapsed;
}
PageCountText.Text = pdfDocument.PageCount.ToString();
}
ProgressControl.Visibility = Visibility.Collapsed;
}
LoadButton.IsEnabled = true;
}

private async void ViewPage(object sender, RoutedEventArgs args)
{
uint pageNumber;
if (!uint.TryParse(PageNumberBox.Text, out pageNumber) || (pageNumber < 1) || (pageNumber > pdfDocument.PageCount))
{
Windows.UI.Popups.MessageDialog dialog = new("Invalid page number.", "Error");
await dialog.ShowAsync();
return;
}

Output.Source = null;
ProgressControl.Visibility = Visibility.Visible;

// Convert from 1-based page number to 0-based page index.
uint pageIndex = pageNumber - 1;

using (PdfPage page = pdfDocument.GetPage(pageIndex))
{
var stream = new InMemoryRandomAccessStream();
switch (Options.SelectedIndex)
{
// Arrange size to viewport with
case 0:
await page.RenderToStreamAsync(stream, new() { DestinationWidth = (uint)(viewer.ViewportWidth - 20) });
break;
// View actual size.
case 1:
await page.RenderToStreamAsync(stream);
break;

// View half size on beige background.
case 2:
var options1 = new PdfPageRenderOptions();
options1.BackgroundColor = Windows.UI.Colors.Beige;
options1.DestinationHeight = (uint)(page.Size.Height / 2);
options1.DestinationWidth = (uint)(page.Size.Width / 2);
await page.RenderToStreamAsync(stream, options1);
break;

// Crop to center.
case 3:
var options2 = new PdfPageRenderOptions();
var rect = page.Dimensions.TrimBox;
options2.SourceRect = new Rect(rect.X + rect.Width / 4, rect.Y + rect.Height / 4, rect.Width / 2, rect.Height / 2);
await page.RenderToStreamAsync(stream, options2);
break;
}
BitmapImage src = new BitmapImage();
await src.SetSourceAsync(stream);
Output.Source = src;
}
ProgressControl.Visibility = Visibility.Collapsed;
}
}
}

Binary file added src/Uno.UI.RuntimeTests/Assets/UnoA4.pdf
Binary file not shown.
Binary file added src/Uno.UI.RuntimeTests/Assets/UnoA4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Uno.UI.RuntimeTests/Assets/UnoA4_Crop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a1e526d

Please sign in to comment.