1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "gtest/gtest.h"
7 #include "BasicLayers.h"
9 #include "imgIContainer.h"
10 #include "imgITools.h"
11 #include "ImageFactory.h"
12 #include "ImageContainer.h"
13 #include "mozilla/gfx/2D.h"
14 #include "mozilla/RefPtr.h"
15 #include "nsIInputStream.h"
17 #include "ProgressTracker.h"
19 using namespace mozilla
;
20 using namespace mozilla::gfx
;
21 using namespace mozilla::image
;
23 class ImageContainers
: public ::testing::Test
{
25 AutoInitializeImageLib mInit
;
28 TEST_F(ImageContainers
, RasterImageContainer
) {
29 ImageTestCase testCase
= GreenPNGTestCase();
32 RefPtr
<Image
> image
= ImageFactory::CreateAnonymousImage(
33 nsDependentCString(testCase
.mMimeType
));
34 ASSERT_TRUE(!image
->HasError());
36 nsCOMPtr
<nsIInputStream
> inputStream
= LoadFile(testCase
.mPath
);
37 ASSERT_TRUE(inputStream
);
39 // Figure out how much data we have.
41 nsresult rv
= inputStream
->Available(&length
);
42 ASSERT_TRUE(NS_SUCCEEDED(rv
));
44 // Write the data into the image.
45 rv
= image
->OnImageDataAvailable(nullptr, nullptr, inputStream
, 0,
46 static_cast<uint32_t>(length
));
47 ASSERT_TRUE(NS_SUCCEEDED(rv
));
49 // Let the image know we've sent all the data.
50 rv
= image
->OnImageDataComplete(nullptr, nullptr, NS_OK
, true);
51 ASSERT_TRUE(NS_SUCCEEDED(rv
));
53 RefPtr
<ProgressTracker
> tracker
= image
->GetProgressTracker();
54 tracker
->SyncNotifyProgress(FLAG_LOAD_COMPLETE
);
56 RefPtr
<layers::LayerManager
> layerManager
=
57 new layers::BasicLayerManager(layers::BasicLayerManager::BLM_OFFSCREEN
);
59 // Get at native size.
60 RefPtr
<layers::ImageContainer
> nativeContainer
=
61 image
->GetImageContainer(layerManager
, imgIContainer::FLAG_SYNC_DECODE
);
62 ASSERT_TRUE(nativeContainer
!= nullptr);
63 IntSize containerSize
= nativeContainer
->GetCurrentSize();
64 EXPECT_EQ(testCase
.mSize
.width
, containerSize
.width
);
65 EXPECT_EQ(testCase
.mSize
.height
, containerSize
.height
);
67 // Upscaling should give the native size.
68 ImgDrawResult drawResult
;
69 IntSize requestedSize
= testCase
.mSize
;
70 requestedSize
.Scale(2, 2);
71 RefPtr
<layers::ImageContainer
> upscaleContainer
;
72 drawResult
= image
->GetImageContainerAtSize(
73 layerManager
, requestedSize
, Nothing(),
74 imgIContainer::FLAG_SYNC_DECODE
|
75 imgIContainer::FLAG_HIGH_QUALITY_SCALING
,
76 getter_AddRefs(upscaleContainer
));
77 EXPECT_EQ(drawResult
, ImgDrawResult::SUCCESS
);
78 ASSERT_TRUE(upscaleContainer
!= nullptr);
79 containerSize
= upscaleContainer
->GetCurrentSize();
80 EXPECT_EQ(testCase
.mSize
.width
, containerSize
.width
);
81 EXPECT_EQ(testCase
.mSize
.height
, containerSize
.height
);
83 // Downscaling should give the downscaled size.
84 requestedSize
= testCase
.mSize
;
85 requestedSize
.width
/= 2;
86 requestedSize
.height
/= 2;
87 RefPtr
<layers::ImageContainer
> downscaleContainer
;
88 drawResult
= image
->GetImageContainerAtSize(
89 layerManager
, requestedSize
, Nothing(),
90 imgIContainer::FLAG_SYNC_DECODE
|
91 imgIContainer::FLAG_HIGH_QUALITY_SCALING
,
92 getter_AddRefs(downscaleContainer
));
93 EXPECT_EQ(drawResult
, ImgDrawResult::SUCCESS
);
94 ASSERT_TRUE(downscaleContainer
!= nullptr);
95 containerSize
= downscaleContainer
->GetCurrentSize();
96 EXPECT_EQ(requestedSize
.width
, containerSize
.width
);
97 EXPECT_EQ(requestedSize
.height
, containerSize
.height
);
99 // Get at native size again. Should give same container.
100 RefPtr
<layers::ImageContainer
> againContainer
;
101 drawResult
= image
->GetImageContainerAtSize(
102 layerManager
, testCase
.mSize
, Nothing(), imgIContainer::FLAG_SYNC_DECODE
,
103 getter_AddRefs(againContainer
));
104 EXPECT_EQ(drawResult
, ImgDrawResult::SUCCESS
);
105 ASSERT_EQ(nativeContainer
.get(), againContainer
.get());