/src/mozilla-central/image/test/gtest/TestContainers.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
4 | | |
5 | | #include "gtest/gtest.h" |
6 | | |
7 | | #include "BasicLayers.h" |
8 | | #include "Common.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" |
16 | | #include "nsString.h" |
17 | | #include "ProgressTracker.h" |
18 | | |
19 | | using namespace mozilla; |
20 | | using namespace mozilla::gfx; |
21 | | using namespace mozilla::image; |
22 | | |
23 | | class ImageContainers : public ::testing::Test |
24 | | { |
25 | | protected: |
26 | | AutoInitializeImageLib mInit; |
27 | | }; |
28 | | |
29 | | TEST_F(ImageContainers, RasterImageContainer) |
30 | 0 | { |
31 | 0 | ImageTestCase testCase = GreenPNGTestCase(); |
32 | 0 |
|
33 | 0 | // Create an image. |
34 | 0 | RefPtr<Image> image = |
35 | 0 | ImageFactory::CreateAnonymousImage(nsDependentCString(testCase.mMimeType)); |
36 | 0 | ASSERT_TRUE(!image->HasError()); |
37 | 0 |
|
38 | 0 | nsCOMPtr<nsIInputStream> inputStream = LoadFile(testCase.mPath); |
39 | 0 | ASSERT_TRUE(inputStream); |
40 | 0 |
|
41 | 0 | // Figure out how much data we have. |
42 | 0 | uint64_t length; |
43 | 0 | nsresult rv = inputStream->Available(&length); |
44 | 0 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
45 | 0 |
|
46 | 0 | // Write the data into the image. |
47 | 0 | rv = image->OnImageDataAvailable(nullptr, nullptr, inputStream, 0, |
48 | 0 | static_cast<uint32_t>(length)); |
49 | 0 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
50 | 0 |
|
51 | 0 | // Let the image know we've sent all the data. |
52 | 0 | rv = image->OnImageDataComplete(nullptr, nullptr, NS_OK, true); |
53 | 0 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
54 | 0 |
|
55 | 0 | RefPtr<ProgressTracker> tracker = image->GetProgressTracker(); |
56 | 0 | tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE); |
57 | 0 |
|
58 | 0 | RefPtr<layers::LayerManager> layerManager = |
59 | 0 | new layers::BasicLayerManager(layers::BasicLayerManager::BLM_OFFSCREEN); |
60 | 0 |
|
61 | 0 | // Get at native size. |
62 | 0 | RefPtr<layers::ImageContainer> nativeContainer = |
63 | 0 | image->GetImageContainer(layerManager, |
64 | 0 | imgIContainer::FLAG_SYNC_DECODE); |
65 | 0 | ASSERT_TRUE(nativeContainer != nullptr); |
66 | 0 | IntSize containerSize = nativeContainer->GetCurrentSize(); |
67 | 0 | EXPECT_EQ(testCase.mSize.width, containerSize.width); |
68 | 0 | EXPECT_EQ(testCase.mSize.height, containerSize.height); |
69 | 0 |
|
70 | 0 | // Upscaling should give the native size. |
71 | 0 | ImgDrawResult drawResult; |
72 | 0 | IntSize requestedSize = testCase.mSize; |
73 | 0 | requestedSize.Scale(2, 2); |
74 | 0 | RefPtr<layers::ImageContainer> upscaleContainer; |
75 | 0 | drawResult = image->GetImageContainerAtSize(layerManager, |
76 | 0 | requestedSize, |
77 | 0 | Nothing(), |
78 | 0 | imgIContainer::FLAG_SYNC_DECODE | |
79 | 0 | imgIContainer::FLAG_HIGH_QUALITY_SCALING, |
80 | 0 | getter_AddRefs(upscaleContainer)); |
81 | 0 | EXPECT_EQ(drawResult, ImgDrawResult::SUCCESS); |
82 | 0 | ASSERT_TRUE(upscaleContainer != nullptr); |
83 | 0 | containerSize = upscaleContainer->GetCurrentSize(); |
84 | 0 | EXPECT_EQ(testCase.mSize.width, containerSize.width); |
85 | 0 | EXPECT_EQ(testCase.mSize.height, containerSize.height); |
86 | 0 |
|
87 | 0 | // Downscaling should give the downscaled size. |
88 | 0 | requestedSize = testCase.mSize; |
89 | 0 | requestedSize.width /= 2; |
90 | 0 | requestedSize.height /= 2; |
91 | 0 | RefPtr<layers::ImageContainer> downscaleContainer; |
92 | 0 | drawResult = image->GetImageContainerAtSize(layerManager, |
93 | 0 | requestedSize, |
94 | 0 | Nothing(), |
95 | 0 | imgIContainer::FLAG_SYNC_DECODE | |
96 | 0 | imgIContainer::FLAG_HIGH_QUALITY_SCALING, |
97 | 0 | getter_AddRefs(downscaleContainer)); |
98 | 0 | EXPECT_EQ(drawResult, ImgDrawResult::SUCCESS); |
99 | 0 | ASSERT_TRUE(downscaleContainer != nullptr); |
100 | 0 | containerSize = downscaleContainer->GetCurrentSize(); |
101 | 0 | EXPECT_EQ(requestedSize.width, containerSize.width); |
102 | 0 | EXPECT_EQ(requestedSize.height, containerSize.height); |
103 | 0 |
|
104 | 0 | // Get at native size again. Should give same container. |
105 | 0 | RefPtr<layers::ImageContainer> againContainer; |
106 | 0 | drawResult = image->GetImageContainerAtSize(layerManager, |
107 | 0 | testCase.mSize, |
108 | 0 | Nothing(), |
109 | 0 | imgIContainer::FLAG_SYNC_DECODE, |
110 | 0 | getter_AddRefs(againContainer)); |
111 | 0 | EXPECT_EQ(drawResult, ImgDrawResult::SUCCESS); |
112 | 0 | ASSERT_EQ(nativeContainer.get(), againContainer.get()); |
113 | 0 | } |