-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to combine AvaloniaResources with the same system path #15302
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
tests/Avalonia.Base.UnitTests/Utilities/AvaloniaResourcesIndexTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using Avalonia.Utilities; | ||
using Xunit; | ||
|
||
namespace Avalonia.Base.UnitTests; | ||
|
||
public class AvaloniaResourcesIndexTests | ||
{ | ||
[Fact] | ||
public void Should_Write_And_Read_The_Same_Resources() | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
|
||
var fooBytes = Encoding.UTF8.GetBytes("foo"); | ||
var booBytes = Encoding.UTF8.GetBytes("boo"); | ||
AvaloniaResourcesIndexReaderWriter.WriteResources(memoryStream, | ||
new[] | ||
{ | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "foo.xaml", Size = fooBytes.Length, Open = () => new MemoryStream(fooBytes) | ||
}, | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "boo.xaml", Size = booBytes.Length, Open = () => new MemoryStream(booBytes) | ||
} | ||
}); | ||
|
||
memoryStream.Seek(4, SeekOrigin.Begin); // skip 4 bytes for "index size" field. | ||
|
||
var index = AvaloniaResourcesIndexReaderWriter.ReadIndex(memoryStream); | ||
var resourcesBasePosition = memoryStream.Position; | ||
|
||
Span<byte> buffer = stackalloc byte[index[0].Size]; | ||
|
||
Assert.Equal("foo.xaml", index[0].Path); | ||
Assert.Equal(0, index[0].Offset); | ||
Assert.Equal(fooBytes.Length, index[0].Size); | ||
|
||
memoryStream.Seek(resourcesBasePosition + index[0].Offset, SeekOrigin.Begin); | ||
memoryStream.ReadExactly(buffer); | ||
Assert.Equal(fooBytes, buffer.ToArray()); | ||
|
||
Assert.Equal("boo.xaml", index[1].Path); | ||
Assert.Equal(fooBytes.Length, index[1].Offset); | ||
Assert.Equal(booBytes.Length, index[1].Size); | ||
|
||
memoryStream.Seek(resourcesBasePosition + index[1].Offset, SeekOrigin.Begin); | ||
memoryStream.ReadExactly(buffer); | ||
Assert.Equal(booBytes, buffer.ToArray()); | ||
} | ||
|
||
[Fact] | ||
public void Should_Combined_Same_Physical_Path_Resources() | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
|
||
var resourceBytes = Encoding.UTF8.GetBytes("resource-data"); | ||
AvaloniaResourcesIndexReaderWriter.WriteResources(memoryStream, new[] | ||
{ | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "app.xaml", | ||
SystemPath = "app.ico", | ||
Size = resourceBytes.Length, | ||
Open = () => new MemoryStream(resourceBytes) | ||
}, | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "!__AvaloniaDefaultWindowIcon", | ||
SystemPath = "app.ico", | ||
Size = resourceBytes.Length, | ||
Open = () => new MemoryStream(resourceBytes) | ||
} | ||
}); | ||
|
||
memoryStream.Seek(4, SeekOrigin.Begin); // skip 4 bytes for "index size" field. | ||
|
||
var index = AvaloniaResourcesIndexReaderWriter.ReadIndex(memoryStream); | ||
|
||
Assert.Equal("app.xaml", index[0].Path); | ||
Assert.Equal(0, index[0].Offset); | ||
Assert.Equal(resourceBytes.Length, index[0].Size); | ||
|
||
Assert.Equal("!__AvaloniaDefaultWindowIcon", index[1].Path); | ||
Assert.Equal(0, index[1].Offset); | ||
Assert.Equal(resourceBytes.Length, index[1].Size); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this method is part of public API, I made it obsolete, and added a new overload with AvaloniaResourcesEntry items.