OSDN Git Service

非アクティブなタブのListViewを空にする
[opentween/open-tween.git] / OpenTween.Tests / MemoryImageTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Drawing;
25 using System.Drawing.Imaging;
26 using System.IO;
27 using System.Linq;
28 using System.Text;
29 using System.Threading.Tasks;
30 using Xunit;
31
32 namespace OpenTween
33 {
34     public class MemoryImageTest
35     {
36         [Fact]
37         public async Task ImageFormat_GifTest()
38         {
39             using var imgStream = File.OpenRead("Resources/re.gif");
40             using var image = await MemoryImage.CopyFromStreamAsync(imgStream).ConfigureAwait(false);
41             Assert.Equal(ImageFormat.Gif, image.ImageFormat);
42             Assert.Equal(".gif", image.ImageFormatExt);
43         }
44
45         [Fact]
46         public void ImageFormat_CopyFromImageTest()
47         {
48             using var bitmap = new Bitmap(width: 200, height: 200);
49             using var image = MemoryImage.CopyFromImage(bitmap);
50
51             // CopyFromImage から作成した MemoryImage は PNG 画像として扱われる
52             Assert.Equal(ImageFormat.Png, image.ImageFormat);
53             Assert.Equal(".png", image.ImageFormatExt);
54         }
55
56         [Fact]
57         public async Task CopyFromStream_Test()
58         {
59             using var stream = File.OpenRead("Resources/re.gif");
60             using var memstream = new MemoryStream();
61             await stream.CopyToAsync(memstream)
62                 .ConfigureAwait(false);
63
64             stream.Seek(0, SeekOrigin.Begin);
65
66             using var image = MemoryImage.CopyFromStream(stream);
67             Assert.Equal(memstream.ToArray(), image.Stream.ToArray());
68         }
69
70         [Fact]
71         public async Task CopyFromStreamAsync_Test()
72         {
73             using var stream = File.OpenRead("Resources/re.gif");
74             using var memstream = new MemoryStream();
75             await stream.CopyToAsync(memstream)
76                 .ConfigureAwait(false);
77
78             stream.Seek(0, SeekOrigin.Begin);
79
80             using var image = await MemoryImage.CopyFromStreamAsync(stream)
81                 .ConfigureAwait(false);
82             Assert.Equal(memstream.ToArray(), image.Stream.ToArray());
83         }
84
85         [Fact]
86         public async Task CopyFromBytes_Test()
87         {
88             using var stream = File.OpenRead("Resources/re.gif");
89             using var memstream = new MemoryStream();
90             await stream.CopyToAsync(memstream)
91                 .ConfigureAwait(false);
92             var imageBytes = memstream.ToArray();
93
94             using var image = MemoryImage.CopyFromBytes(imageBytes);
95             Assert.Equal(imageBytes, image.Stream.ToArray());
96         }
97
98         [Fact]
99         public void CopyFromImage_Test()
100         {
101             using var bitmap = new Bitmap(width: 200, height: 200);
102
103             // MemoryImage をエラー無く作成できることをテストする
104             using var image = MemoryImage.CopyFromImage(bitmap);
105         }
106
107         [Fact]
108         public void Dispose_Test()
109         {
110             using var image = TestUtils.CreateDummyImage();
111             Assert.False(image.IsDisposed);
112
113             image.Dispose();
114
115             Assert.True(image.IsDisposed);
116             Assert.Throws<ObjectDisposedException>(() => image.Image);
117             Assert.Throws<ObjectDisposedException>(() => image.ImageFormat);
118         }
119
120         [Fact]
121         public async Task Equals_Test()
122         {
123             using var imgStream1 = File.OpenRead("Resources/re.gif");
124             using var image1 = await MemoryImage.CopyFromStreamAsync(imgStream1).ConfigureAwait(false);
125
126             using var imgStream2 = File.OpenRead("Resources/re.gif");
127             using var image2 = await MemoryImage.CopyFromStreamAsync(imgStream2).ConfigureAwait(false);
128             Assert.True(image1.Equals(image2));
129             Assert.True(image2.Equals(image1));
130
131             using var image3 = TestUtils.CreateDummyImage();
132             Assert.False(image1.Equals(image3));
133             Assert.False(image3.Equals(image1));
134         }
135     }
136 }