Skip to content

Add unit test for ImageListImage #11736

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System.Drawing;

namespace System.Windows.Forms.Design.Tests;

public sealed class ImageListImageTests
{
[Fact]
public void Constructor_WithImage_ShouldSetImageProperty()
{
using Bitmap bitmap = new(10, 10);
ImageListImage imageListImage = new(bitmap);

imageListImage.Image.Should().Be(bitmap);
imageListImage.Name.Should().BeEmpty();
}

[Fact]
public void Constructor_WithImageAndName_ShouldSetImageAndNameProperties()
{
using Bitmap bitmap = new(10, 10);
string name = "TestImage";
ImageListImage imageListImage = new(bitmap, name);

imageListImage.Image.Should().Be(bitmap);
imageListImage.Name.Should().Be(name);
}

[Fact]
public void NameProperty_ShouldGetAndSetCorrectly()
{
using Bitmap bitmap = new(10, 10);
string name = "TestImage";
ImageListImage imageListImage = new(bitmap)
{
Name = name
};

imageListImage.Name.Should().Be(name);
}

[Fact]
public void NameProperty_ShouldReturnEmptyString_WhenNameIsNull()
{
using Bitmap bitmap = new(10, 10);
ImageListImage imageListImage = new(bitmap)
{
Name = null
};

imageListImage.Name.Should().BeEmpty();
}

[Fact]
public void ImageListImageFromStream_ShouldCreateImageListImageFromBitmapStream()
{
using Bitmap bitmap = new(10, 10);
using MemoryStream memoryStream = new();
bitmap.Save(memoryStream, Drawing.Imaging.ImageFormat.Bmp);
memoryStream.Seek(0, SeekOrigin.Begin);

var result = ImageListImage.ImageListImageFromStream(memoryStream, imageIsIcon: false);

result.Should().NotBeNull();
result.Image.Should().NotBeNull();
result.Image.Size.Should().Be(bitmap.Size);
result.Image.RawFormat.Should().Be(expected: Drawing.Imaging.ImageFormat.Bmp);
}

[Fact]
public void ImageListImageFromStream_ShouldCreateImageListImageFromIconStream()
{
using Icon icon = SystemIcons.Application;
using MemoryStream memoryStream = new();
icon.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

var result = ImageListImage.ImageListImageFromStream(memoryStream, imageIsIcon: true);

result.Should().NotBeNull();
result.Image.Should().NotBeNull();
result.Image.Size.Should().Be(icon.Size);
result.Image.RawFormat.Should().Be(Drawing.Imaging.ImageFormat.MemoryBmp);
}
}