summaryrefslogtreecommitdiffstats
path: root/image/test/gtest/TestContainers.cpp
blob: 98326e8764fce9195c8b7f679b14df105806996c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "BasicLayers.h"
#include "Common.h"
#include "imgIContainer.h"
#include "ImageFactory.h"
#include "ImageContainer.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/RefPtr.h"
#include "nsIInputStream.h"
#include "nsString.h"
#include "ProgressTracker.h"

using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::image;

class ImageContainers : public ::testing::Test {
 protected:
  AutoInitializeImageLib mInit;
};

TEST_F(ImageContainers, RasterImageContainer) {
  ImageTestCase testCase = GreenPNGTestCase();

  // Create an image.
  RefPtr<Image> image = ImageFactory::CreateAnonymousImage(
      nsDependentCString(testCase.mMimeType));
  ASSERT_TRUE(!image->HasError());

  nsCOMPtr<nsIInputStream> inputStream = LoadFile(testCase.mPath);
  ASSERT_TRUE(inputStream);

  // Figure out how much data we have.
  uint64_t length;
  nsresult rv = inputStream->Available(&length);
  ASSERT_TRUE(NS_SUCCEEDED(rv));

  // Write the data into the image.
  rv = image->OnImageDataAvailable(nullptr, nullptr, inputStream, 0,
                                   static_cast<uint32_t>(length));
  ASSERT_TRUE(NS_SUCCEEDED(rv));

  // Let the image know we've sent all the data.
  rv = image->OnImageDataComplete(nullptr, nullptr, NS_OK, true);
  ASSERT_TRUE(NS_SUCCEEDED(rv));

  RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
  tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);

  RefPtr<layers::LayerManager> layerManager =
      new layers::BasicLayerManager(layers::BasicLayerManager::BLM_OFFSCREEN);

  // Get at native size.
  RefPtr<layers::ImageContainer> nativeContainer =
      image->GetImageContainer(layerManager, imgIContainer::FLAG_SYNC_DECODE);
  ASSERT_TRUE(nativeContainer != nullptr);
  IntSize containerSize = nativeContainer->GetCurrentSize();
  EXPECT_EQ(testCase.mSize.width, containerSize.width);
  EXPECT_EQ(testCase.mSize.height, containerSize.height);

  // Upscaling should give the native size.
  ImgDrawResult drawResult;
  IntSize requestedSize = testCase.mSize;
  requestedSize.Scale(2, 2);
  RefPtr<layers::ImageContainer> upscaleContainer;
  drawResult = image->GetImageContainerAtSize(
      layerManager, requestedSize, Nothing(),
      imgIContainer::FLAG_SYNC_DECODE |
          imgIContainer::FLAG_HIGH_QUALITY_SCALING,
      getter_AddRefs(upscaleContainer));
  EXPECT_EQ(drawResult, ImgDrawResult::SUCCESS);
  ASSERT_TRUE(upscaleContainer != nullptr);
  containerSize = upscaleContainer->GetCurrentSize();
  EXPECT_EQ(testCase.mSize.width, containerSize.width);
  EXPECT_EQ(testCase.mSize.height, containerSize.height);

  // Downscaling should give the downscaled size.
  requestedSize = testCase.mSize;
  requestedSize.width /= 2;
  requestedSize.height /= 2;
  RefPtr<layers::ImageContainer> downscaleContainer;
  drawResult = image->GetImageContainerAtSize(
      layerManager, requestedSize, Nothing(),
      imgIContainer::FLAG_SYNC_DECODE |
          imgIContainer::FLAG_HIGH_QUALITY_SCALING,
      getter_AddRefs(downscaleContainer));
  EXPECT_EQ(drawResult, ImgDrawResult::SUCCESS);
  ASSERT_TRUE(downscaleContainer != nullptr);
  containerSize = downscaleContainer->GetCurrentSize();
  EXPECT_EQ(requestedSize.width, containerSize.width);
  EXPECT_EQ(requestedSize.height, containerSize.height);

  // Get at native size again. Should give same container.
  RefPtr<layers::ImageContainer> againContainer;
  drawResult = image->GetImageContainerAtSize(
      layerManager, testCase.mSize, Nothing(), imgIContainer::FLAG_SYNC_DECODE,
      getter_AddRefs(againContainer));
  EXPECT_EQ(drawResult, ImgDrawResult::SUCCESS);
  ASSERT_EQ(nativeContainer.get(), againContainer.get());
}