Skip to content

Commit cc9d30f

Browse files
committed
Enable rest of input file tests
1 parent d4d8d5c commit cc9d30f

File tree

1 file changed

+143
-143
lines changed

1 file changed

+143
-143
lines changed

src/Components/test/E2ETestMigration/Tests/InputFileTest.cs

+143-143
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
67
using System.IO;
78
using System.Linq;
@@ -51,20 +52,14 @@ protected override async Task InitializeCoreAsync(TestContext context)
5152
Output.WriteLine("Loaded page");
5253

5354
await MountTestComponentAsync<InputFileComponent>(page);
54-
//Browser.MountTestComponent<InputFileComponent>();
5555

5656
_page = page;
5757
}
5858

59-
[Fact]
60-
public async Task CanUploadSingleSmallFile()
59+
private async Task VerifyFile(TempFile file)
6160
{
62-
// Create a temporary text file
63-
var file = TempFile.Create(_tempDirectory, "txt", "This file was uploaded to the browser and read from .NET.");
64-
6561
// Upload the file
66-
var inputFile = await _page.QuerySelectorAsync("#input-file");
67-
await inputFile.SetInputFilesAsync(file.Path);
62+
await _page.SetInputFilesAsync("#input-file", file.Path);
6863

6964
var fileContainer = await _page.WaitForSelectorAsync($"[id='file-{file.Name}']");
7065
Assert.NotNull(fileContainer);
@@ -87,141 +82,146 @@ public async Task CanUploadSingleSmallFile()
8782
Assert.Equal(file.Text, await fileContentElement.GetTextContentAsync());
8883
}
8984

90-
//[Fact]
91-
//[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/26331")]
92-
//public void CanUploadSingleLargeFile()
93-
//{
94-
// // Create a large text file
95-
// var fileContentSizeInBytes = 1024 * 1024;
96-
// var contentBuilder = new StringBuilder();
97-
98-
// for (int i = 0; i < fileContentSizeInBytes; i++)
99-
// {
100-
// contentBuilder.Append((i % 10).ToString(CultureInfo.InvariantCulture));
101-
// }
102-
103-
// var file = TempFile.Create(_tempDirectory, "txt", contentBuilder.ToString());
104-
105-
// // Upload the file
106-
// var inputFile = Browser.Exists(By.Id("input-file"));
107-
// inputFile.SendKeys(file.Path);
108-
109-
// var fileContainer = Browser.Exists(By.Id($"file-{file.Name}"));
110-
// var fileNameElement = fileContainer.FindElement(By.Id("file-name"));
111-
// var fileLastModifiedElement = fileContainer.FindElement(By.Id("file-last-modified"));
112-
// var fileSizeElement = fileContainer.FindElement(By.Id("file-size"));
113-
// var fileContentTypeElement = fileContainer.FindElement(By.Id("file-content-type"));
114-
// var fileContentElement = fileContainer.FindElement(By.Id("file-content"));
115-
116-
// // Validate that the file was uploaded correctly and all fields are present
117-
// Browser.False(() => string.IsNullOrWhiteSpace(fileNameElement.Text));
118-
// Browser.NotEqual(default, () => DateTimeOffset.Parse(fileLastModifiedElement.Text, CultureInfo.InvariantCulture));
119-
// Browser.Equal(file.Contents.Length.ToString(CultureInfo.InvariantCulture), () => fileSizeElement.Text);
120-
// Browser.Equal("text/plain", () => fileContentTypeElement.Text);
121-
// Browser.Equal(file.Text, () => fileContentElement.Text);
122-
//}
123-
124-
//[Fact]
125-
//public void CanUploadMultipleFiles()
126-
//{
127-
// // Create multiple small text files
128-
// var files = Enumerable.Range(1, 3)
129-
// .Select(i => TempFile.Create(_tempDirectory, "txt", $"Contents of file {i}."))
130-
// .ToList();
131-
132-
// // Upload each file
133-
// var inputFile = Browser.Exists(By.Id("input-file"));
134-
// inputFile.SendKeys(string.Join("\n", files.Select(f => f.Path)));
135-
136-
// // Validate that each file was uploaded correctly
137-
// Assert.All(files, file =>
138-
// {
139-
// var fileContainer = Browser.Exists(By.Id($"file-{file.Name}"));
140-
// var fileNameElement = fileContainer.FindElement(By.Id("file-name"));
141-
// var fileLastModifiedElement = fileContainer.FindElement(By.Id("file-last-modified"));
142-
// var fileSizeElement = fileContainer.FindElement(By.Id("file-size"));
143-
// var fileContentTypeElement = fileContainer.FindElement(By.Id("file-content-type"));
144-
// var fileContentElement = fileContainer.FindElement(By.Id("file-content"));
145-
146-
// // Validate that the file was uploaded correctly and all fields are present
147-
// Browser.False(() => string.IsNullOrWhiteSpace(fileNameElement.Text));
148-
// Browser.NotEqual(default, () => DateTimeOffset.Parse(fileLastModifiedElement.Text, CultureInfo.InvariantCulture));
149-
// Browser.Equal(file.Contents.Length.ToString(CultureInfo.InvariantCulture), () => fileSizeElement.Text);
150-
// Browser.Equal("text/plain", () => fileContentTypeElement.Text);
151-
// Browser.Equal(file.Text, () => fileContentElement.Text);
152-
// });
153-
//}
154-
155-
//[Fact]
156-
//[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/25929")]
157-
//public void CanUploadAndConvertImageFile()
158-
//{
159-
// var sourceImageId = "image-source";
160-
161-
// // Get the source image base64
162-
// var base64 = Browser.ExecuteJavaScript<string>($@"
163-
// const canvas = document.createElement('canvas');
164-
// const context = canvas.getContext('2d');
165-
// const image = document.getElementById('{sourceImageId}');
166-
167-
// canvas.width = image.naturalWidth;
168-
// canvas.height = image.naturalHeight;
169-
// context.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight);
170-
171-
// return canvas.toDataURL().split(',').pop();");
172-
173-
// // Save the image file locally
174-
// var file = TempFile.Create(_tempDirectory, "png", Convert.FromBase64String(base64));
175-
176-
// // Re-upload the image file (it will be converted to a JPEG and scaled to fix 640x480)
177-
// var inputFile = Browser.Exists(By.Id("input-image"));
178-
// inputFile.SendKeys(file.Path);
179-
180-
// // Validate that the image was converted without error and is the correct size
181-
// var uploadedImage = Browser.Exists(By.Id("image-uploaded"));
182-
183-
// Browser.Equal(480, () => uploadedImage.Size.Width);
184-
// Browser.Equal(480, () => uploadedImage.Size.Height);
185-
//}
186-
187-
//[Fact]
188-
//public void ThrowsWhenTooManyFilesAreSelected()
189-
//{
190-
// var maxAllowedFilesElement = Browser.Exists(By.Id("max-allowed-files"));
191-
// maxAllowedFilesElement.Clear();
192-
// maxAllowedFilesElement.SendKeys("1\n");
193-
194-
// // Save two files locally
195-
// var file1 = TempFile.Create(_tempDirectory, "txt", "This is file 1.");
196-
// var file2 = TempFile.Create(_tempDirectory, "txt", "This is file 2.");
197-
198-
// // Select both files
199-
// var inputFile = Browser.Exists(By.Id("input-file"));
200-
// inputFile.SendKeys($"{file1.Path}\n{file2.Path}");
201-
202-
// // Validate that the proper exception is thrown
203-
// var exceptionMessage = Browser.Exists(By.Id("exception-message"));
204-
// Browser.Equal("The maximum number of files accepted is 1, but 2 were supplied.", () => exceptionMessage.Text);
205-
//}
206-
207-
//[Fact]
208-
//public void ThrowsWhenOversizedFileIsSelected()
209-
//{
210-
// var maxFileSizeElement = Browser.Exists(By.Id("max-file-size"));
211-
// maxFileSizeElement.Clear();
212-
// maxFileSizeElement.SendKeys("10\n");
213-
214-
// // Save a file that exceeds the specified file size limit
215-
// var file = TempFile.Create(_tempDirectory, "txt", "This file is over 10 bytes long.");
216-
217-
// // Select the file
218-
// var inputFile = Browser.Exists(By.Id("input-file"));
219-
// inputFile.SendKeys(file.Path);
220-
221-
// // Validate that the proper exception is thrown
222-
// var exceptionMessage = Browser.Exists(By.Id("exception-message"));
223-
// Browser.Equal("Supplied file with size 32 bytes exceeds the maximum of 10 bytes.", () => exceptionMessage.Text);
224-
//}
85+
private async Task VerifyFiles(IEnumerable<TempFile> files)
86+
{
87+
// Upload the files
88+
var filePaths = files
89+
.Select(i => i.Path)
90+
.ToArray();
91+
92+
await _page.SetInputFilesAsync("#input-file", filePaths);
93+
94+
foreach (var file in files)
95+
{
96+
var fileContainer = await _page.WaitForSelectorAsync($"[id='file-{file.Name}']");
97+
Assert.NotNull(fileContainer);
98+
var fileNameElement = await fileContainer.QuerySelectorAsync("#file-name");
99+
Assert.NotNull(fileNameElement);
100+
var fileLastModifiedElement = await fileContainer.QuerySelectorAsync("#file-last-modified");
101+
Assert.NotNull(fileLastModifiedElement);
102+
var fileSizeElement = await fileContainer.QuerySelectorAsync("#file-size");
103+
Assert.NotNull(fileSizeElement);
104+
var fileContentTypeElement = await fileContainer.QuerySelectorAsync("#file-content-type");
105+
Assert.NotNull(fileContentTypeElement);
106+
var fileContentElement = await fileContainer.QuerySelectorAsync("#file-content");
107+
Assert.NotNull(fileContentElement);
108+
109+
// Validate that the file was uploaded correctly and all fields are present
110+
Assert.False(string.IsNullOrWhiteSpace(await fileNameElement.GetTextContentAsync()));
111+
Assert.NotEqual(default, DateTimeOffset.Parse(await fileLastModifiedElement.GetTextContentAsync(), CultureInfo.InvariantCulture));
112+
Assert.Equal(file.Contents.Length.ToString(CultureInfo.InvariantCulture), await fileSizeElement.GetTextContentAsync());
113+
Assert.Equal("text/plain", await fileContentTypeElement.GetTextContentAsync());
114+
Assert.Equal(file.Text, await fileContentElement.GetTextContentAsync());
115+
}
116+
}
117+
118+
[Fact]
119+
public async Task CanUploadSingleSmallFile()
120+
{
121+
// Create a temporary text file
122+
var file = TempFile.Create(_tempDirectory, "txt", "This file was uploaded to the browser and read from .NET.");
123+
await VerifyFile(file);
124+
}
125+
126+
[Fact]
127+
public async Task CanUploadSingleLargeFile()
128+
{
129+
// Create a large text file
130+
var fileContentSizeInBytes = 1024 * 1024;
131+
var contentBuilder = new StringBuilder();
132+
133+
for (int i = 0; i < fileContentSizeInBytes; i++)
134+
{
135+
contentBuilder.Append((i % 10).ToString(CultureInfo.InvariantCulture));
136+
}
137+
138+
var file = TempFile.Create(_tempDirectory, "txt", contentBuilder.ToString());
139+
140+
await VerifyFile(file);
141+
}
142+
143+
[Fact]
144+
public async Task CanUploadMultipleFiles()
145+
{
146+
// Create multiple small text files
147+
var files = Enumerable.Range(1, 3)
148+
.Select(i => TempFile.Create(_tempDirectory, "txt", $"Contents of file {i}."))
149+
.ToList();
150+
151+
await VerifyFiles(files);
152+
}
153+
154+
[Fact]
155+
public async Task CanUploadAndConvertImageFile()
156+
{
157+
var sourceImageId = "image-source";
158+
159+
// Get the source image base64
160+
var base64 = await _page.EvaluateAsync<string>($@"
161+
const canvas = document.createElement('canvas');
162+
const context = canvas.getContext('2d');
163+
const image = document.getElementById('{sourceImageId}');
164+
165+
canvas.width = image.naturalWidth;
166+
canvas.height = image.naturalHeight;
167+
context.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight);
168+
169+
return canvas.toDataURL().split(',').pop();");
170+
171+
// Save the image file locally
172+
var file = TempFile.Create(_tempDirectory, "png", Convert.FromBase64String(base64));
173+
174+
// Re-upload the image file (it will be converted to a JPEG and scaled to fix 640x480)
175+
var inputFile = await _page.QuerySelectorAsync("#input-image");
176+
await inputFile.SetInputFilesAsync(file.Path);
177+
178+
// Validate that the image was converted without error and is the correct size
179+
var uploadedImage = await _page.WaitForSelectorAsync("#image-uploaded");
180+
Assert.NotNull(uploadedImage);
181+
var box = await uploadedImage.GetBoundingBoxAsync();
182+
Assert.Equal(480, box.Height);
183+
Assert.Equal(480, box.Width);
184+
}
185+
186+
protected async Task ClearAndType(string selector, string value)
187+
{
188+
await _page.EvalOnSelectorAsync(selector, "e => e.Value = ''");
189+
var element = await _page.QuerySelectorAsync(selector);
190+
await element.TypeAsync(value);
191+
}
192+
193+
[Fact]
194+
public async Task ThrowsWhenTooManyFilesAreSelected()
195+
{
196+
await ClearAndType("#max-allowed-files", "1\n");
197+
198+
// Save two files locally
199+
var file1 = TempFile.Create(_tempDirectory, "txt", "This is file 1.");
200+
var file2 = TempFile.Create(_tempDirectory, "txt", "This is file 2.");
201+
202+
// Select both files
203+
await _page.SetInputFilesAsync("#input-file", new string[] { file1.Path, file2.Path });
204+
205+
// Validate that the proper exception is thrown
206+
var exceptionMessage = await _page.QuerySelectorAsync("#exception-message");
207+
Assert.Equal("The maximum number of files accepted is 1, but 2 were supplied.", await exceptionMessage.GetTextContentAsync());
208+
}
209+
210+
[Fact]
211+
public async Task ThrowsWhenOversizedFileIsSelected()
212+
{
213+
await ClearAndType("#max-file-size", "10\n");
214+
215+
// Save a file that exceeds the specified file size limit
216+
var file = TempFile.Create(_tempDirectory, "txt", "This file is over 10 bytes long.");
217+
218+
// Select the file
219+
await _page.SetInputFilesAsync("#input-file", file.Path);
220+
221+
// Validate that the proper exception is thrown
222+
var exceptionMessage = await _page.QuerySelectorAsync("#exception-message");
223+
Assert.Equal("Supplied file with size 32 bytes exceeds the maximum of 10 bytes.", await exceptionMessage.GetTextContentAsync());
224+
}
225225

226226
public override void Dispose()
227227
{

0 commit comments

Comments
 (0)