File: DeferredImageDecoderTestWoPlatform.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (124 lines) | stat: -rw-r--r-- 4,968 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/graphics/DeferredImageDecoder.h"

#include "platform/SharedBuffer.h"
#include "platform/image-decoders/ImageDecoderTestHelpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "wtf/RefPtr.h"
#include <memory>

namespace blink {

/**
 *  Used to test decoding SkImages out of order.
 *  e.g.
 *  SkImage* imageA = decoder.createFrameAtIndex(0);
 *  // supply more (but not all) data to the decoder
 *  SkImage* imageB = decoder.createFrameAtIndex(laterFrame);
 *  draw(imageB);
 *  draw(imageA);
 *
 *  This results in using the same ImageDecoder (in the ImageDecodingStore) to
 *  decode less data the second time. This test ensures that it is safe to do
 *  so.
 *
 *  @param fileName File to decode
 *  @param bytesForFirstFrame Number of bytes needed to return an SkImage
 *  @param laterFrame Frame to decode with almost complete data. Can be 0.
 */
static void mixImages(const char* fileName,
                      size_t bytesForFirstFrame,
                      size_t laterFrame) {
  RefPtr<SharedBuffer> file = readFile(fileName);
  ASSERT_NE(file, nullptr);

  RefPtr<SharedBuffer> partialFile =
      SharedBuffer::create(file->data(), bytesForFirstFrame);
  std::unique_ptr<DeferredImageDecoder> decoder = DeferredImageDecoder::create(
      partialFile, false, ImageDecoder::AlphaPremultiplied,
      ColorBehavior::ignore());
  ASSERT_NE(decoder, nullptr);
  sk_sp<SkImage> partialImage = decoder->createFrameAtIndex(0);

  RefPtr<SharedBuffer> almostCompleteFile =
      SharedBuffer::create(file->data(), file->size() - 1);
  decoder->setData(almostCompleteFile, false);
  sk_sp<SkImage> imageWithMoreData = decoder->createFrameAtIndex(laterFrame);

  // we now want to ensure we don't crash if we access these in this order
  SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
  sk_sp<SkSurface> surf = SkSurface::MakeRaster(info);
  surf->getCanvas()->drawImage(imageWithMoreData, 0, 0);
  surf->getCanvas()->drawImage(partialImage, 0, 0);
}

TEST(DeferredImageDecoderTestWoPlatform, mixImagesGif) {
  mixImages("/LayoutTests/images/resources/animated.gif", 818u, 1u);
}

TEST(DeferredImageDecoderTestWoPlatform, mixImagesPng) {
  mixImages("/LayoutTests/images/resources/mu.png", 910u, 0u);
}

TEST(DeferredImageDecoderTestWoPlatform, mixImagesJpg) {
  mixImages("/LayoutTests/images/resources/2-dht.jpg", 177u, 0u);
}

TEST(DeferredImageDecoderTestWoPlatform, mixImagesWebp) {
  mixImages("/LayoutTests/images/resources/webp-animated.webp", 142u, 1u);
}

TEST(DeferredImageDecoderTestWoPlatform, mixImagesBmp) {
  mixImages("/LayoutTests/images/resources/lenna.bmp", 122u, 0u);
}

TEST(DeferredImageDecoderTestWoPlatform, mixImagesIco) {
  mixImages("/LayoutTests/images/resources/wrong-frame-dimensions.ico", 1376u,
            1u);
}

TEST(DeferredImageDecoderTestWoPlatform, fragmentedSignature) {
  const char* testFiles[] = {
      "/LayoutTests/images/resources/animated.gif",
      "/LayoutTests/images/resources/mu.png",
      "/LayoutTests/images/resources/2-dht.jpg",
      "/LayoutTests/images/resources/webp-animated.webp",
      "/LayoutTests/images/resources/lenna.bmp",
      "/LayoutTests/images/resources/wrong-frame-dimensions.ico",
  };

  for (size_t i = 0; i < SK_ARRAY_COUNT(testFiles); ++i) {
    RefPtr<SharedBuffer> fileBuffer = readFile(testFiles[i]);
    ASSERT_NE(fileBuffer, nullptr);
    // We need contiguous data, which SharedBuffer doesn't guarantee.
    sk_sp<SkData> skData = fileBuffer->getAsSkData();
    EXPECT_EQ(skData->size(), fileBuffer->size());
    const char* data = reinterpret_cast<const char*>(skData->bytes());

    // Truncated signature (only 1 byte).  Decoder instantiation should fail.
    RefPtr<SharedBuffer> buffer = SharedBuffer::create<size_t>(data, 1u);
    EXPECT_FALSE(ImageDecoder::hasSufficientDataToSniffImageType(*buffer));
    EXPECT_EQ(nullptr, DeferredImageDecoder::create(
                           buffer, false, ImageDecoder::AlphaPremultiplied,
                           ColorBehavior::ignore()));

    // Append the rest of the data.  We should be able to sniff the signature
    // now, even if segmented.
    buffer->append<size_t>(data + 1, skData->size() - 1);
    EXPECT_TRUE(ImageDecoder::hasSufficientDataToSniffImageType(*buffer));
    std::unique_ptr<DeferredImageDecoder> decoder =
        DeferredImageDecoder::create(buffer, false,
                                     ImageDecoder::AlphaPremultiplied,
                                     ColorBehavior::ignore());
    ASSERT_NE(decoder, nullptr);
    EXPECT_TRUE(String(testFiles[i]).endsWith(decoder->filenameExtension()));
  }
}

}  // namespace blink