Files
palemoon27/image/test/gtest/TestStreamingLexer.cpp
T
roytam1 b75b514a74 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1194059 (Part 1) - Ensure that metadata decode progress is always delivered atomically. r=tn (07f0441600)
- Bug 1191090 - Use the normal PNG decoder for PNG metadata decodes. r=tn (ce3fe1be5f)
- Bug 1191114 (Part 1) - Always detect HAS_TRANSPARENCY during the metadata decode. r=tn (3841132932)
- Bug 1191114 (Part 2) - Add support for creating an anonymous metadata decoder, for use in tests. r=tn (2cdcc0c278)
- Bug 1191114 (Part 3) - Add flags to image test cases. r=tn (4a6f5a5230)
- Bug 1191114 (Part 4) - Add tests for metadata decoding, including that we always deliver HAS_TRANSPARENCY during the metadata decode. r=tn (b9c5d1cd4a)
- Bug 1126330 - Remove the check for non-looping animations. r=seth (828dabba24)
- Bug 1194059 (Part 2) - Always detect IS_ANIMATED during the metadatadecode. r=tn (0ba5bf38f1)
- Bug 1194059 (Part 3) - Ensure the nsIInputStream LoadImage() returns is always buffered. r=tn (ed2b02205b)
- Bug 1194059 (Part 4) - Add tests that we detect IS_ANIMATED during the metadata decode. r=tn (298f14a7c9)
- Bug 1188705 (part 1) - Remove gfxASurface::GetMemoryLocation(). r=seth. (1f0da73a08)
- Bug 1188705 (part 2) - Remove unused SizeOfDecodedWithComputedFallbackIfHeap declaration. r=seth. (3356dbed06)
- Bug 1188705 (part 3) - Simplify imgFrame::SizeOfExcludingThis(). r=seth. (563262a834)
- Bug 1155252 - Don't allocate X11TextureClients bigger than xlib's maximum surface size. r=jrmuizel (3f11590667)
- Bug 1143994 - Fix some -Wunreachable-code and -Wswitch warnings in imagelib. r=seth (008becc7e2)
- Bug 1060609 (Part 1) - Disable downscale-during-decode when HQ scaling is disabled. r=tn (6da77e3cad)
- Bug 1187569 - PNGs getting stuck in a pixelated state. r=seth (da305ef99c)
- Bug 1194900 - Stop deciding when to send invalidations in nsPNGDecoder and let Decoder handle it. r=tn (50fa14a984)
- Bug 1151694 - Part 1 - Move CommonAnimationManager::sLayerAnimationInfo into LayerAnimationInfo.(cpp|h). r=bbirtles (9f93e0d569)
-  Bug 1151694 - Part 2 - imgTools should be inside mozilla::image namespace. r=bbirtles (8dfc3f2e4b)
- Bug 1196066 (Part 1) - Fix bad directory entries in two of our ICO reftests. r=tn (9e4c70d2b4)
- Bug 1196065 - Add sanity tests for image decoders. r=tn (557b9131cb)
- Bug 1194912 (Part 1) - Add CopyOnWrite<T> to support automatic copy-on-write for recursive writes to data structures. r=tn (b081a50716)
- Bug 1196066 (Part 2) - Add a streaming lexing framework to ImageLib. r=tn (59eb634ea5)
- Bug 1196476 - Replace ProgressTracker::FirstObserverIs() with a simpler mechanism on imgRequest. r=tn (db9ecc65ef)
- missing part of Bug 1139225 (Part 2) - Dispatch OnImageAvailable to the main thread manually in imgRequest. r=tn (e7b22db614)
- Bug 1194912 (Part 2) - Store ProgressTracker observers in a copy-on-write hash table, and dispatch notifications to them using a template. r=tn (5efd7b38b3)
- Bug 1180225. Make convolver more like upstream. r=seth (18e3c168fc)
- Bug 1149318 - Fix the calling convention on SkGetUserDefaultLocaleNameProc. r=eihrul (7b750d4e4e)
- Bug 1210493 - enlarge stroke bounds by line width when doing a quick-reject in SkDraw::drawRect. r=jmuizelaar (e8b5d0fe2d)
- Bug 1188206 - Fix more constructors in gfx; r=jrmuizel (944ea9938c)
2022-05-16 21:07:53 +08:00

267 lines
7.7 KiB
C++

/* 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 "mozilla/Vector.h"
#include "StreamingLexer.h"
using namespace mozilla;
using namespace mozilla::image;
enum class TestState
{
ONE,
TWO,
THREE,
UNBUFFERED,
SUCCESS,
FAILURE
};
void
CheckData(const char* aData, size_t aLength)
{
EXPECT_TRUE(aLength == 3);
EXPECT_EQ(1, aData[0]);
EXPECT_EQ(2, aData[1]);
EXPECT_EQ(3, aData[2]);
}
LexerTransition<TestState>
DoLex(TestState aState, const char* aData, size_t aLength)
{
switch (aState) {
case TestState::ONE:
CheckData(aData, aLength);
return Transition::To(TestState::TWO, 3);
case TestState::TWO:
CheckData(aData, aLength);
return Transition::To(TestState::THREE, 3);
case TestState::THREE:
CheckData(aData, aLength);
return Transition::Terminate(TestState::SUCCESS);
default:
EXPECT_TRUE(false); // Shouldn't get here.
return Transition::Terminate(TestState::FAILURE);
}
}
LexerTransition<TestState>
DoLexWithUnbuffered(TestState aState, const char* aData, size_t aLength,
Vector<char>& aUnbufferedVector)
{
switch (aState) {
case TestState::ONE:
CheckData(aData, aLength);
return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 3);
case TestState::UNBUFFERED:
EXPECT_TRUE(aLength <= 3);
aUnbufferedVector.append(aData, aLength);
return Transition::ContinueUnbuffered(TestState::UNBUFFERED);
case TestState::TWO:
CheckData(aUnbufferedVector.begin(), aUnbufferedVector.length());
return Transition::To(TestState::THREE, 3);
case TestState::THREE:
CheckData(aData, aLength);
return Transition::Terminate(TestState::SUCCESS);
default:
EXPECT_TRUE(false);
return Transition::Terminate(TestState::FAILURE);
}
}
LexerTransition<TestState>
DoLexWithUnbufferedTerminate(TestState aState, const char* aData, size_t aLength)
{
switch (aState) {
case TestState::ONE:
CheckData(aData, aLength);
return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 3);
case TestState::UNBUFFERED:
return Transition::Terminate(TestState::SUCCESS);
default:
EXPECT_TRUE(false);
return Transition::Terminate(TestState::FAILURE);
}
}
TEST(ImageStreamingLexer, SingleChunk)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
// Test delivering all the data at once.
Maybe<TestState> result = lexer.Lex(data, sizeof(data), DoLex);
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
}
TEST(ImageStreamingLexer, SingleChunkWithUnbuffered)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
Vector<char> unbufferedVector;
// Test delivering all the data at once.
Maybe<TestState> result =
lexer.Lex(data, sizeof(data),
[&](TestState aState, const char* aData, size_t aLength) {
return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
});
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
}
TEST(ImageStreamingLexer, ChunkPerState)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
// Test delivering in perfectly-sized chunks, one per state.
for (unsigned i = 0 ; i < 3 ; ++i) {
Maybe<TestState> result = lexer.Lex(data + 3 * i, 3, DoLex);
if (i == 2) {
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
} else {
EXPECT_TRUE(result.isNothing());
}
}
}
TEST(ImageStreamingLexer, ChunkPerStateWithUnbuffered)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
Vector<char> unbufferedVector;
// Test delivering in perfectly-sized chunks, one per state.
for (unsigned i = 0 ; i < 3 ; ++i) {
Maybe<TestState> result =
lexer.Lex(data + 3 * i, 3,
[&](TestState aState, const char* aData, size_t aLength) {
return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
});
if (i == 2) {
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
} else {
EXPECT_TRUE(result.isNothing());
}
}
}
TEST(ImageStreamingLexer, OneByteChunks)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
// Test delivering in one byte chunks.
for (unsigned i = 0 ; i < 9 ; ++i) {
Maybe<TestState> result = lexer.Lex(data + i, 1, DoLex);
if (i == 8) {
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
} else {
EXPECT_TRUE(result.isNothing());
}
}
}
TEST(ImageStreamingLexer, OneByteChunksWithUnbuffered)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
Vector<char> unbufferedVector;
// Test delivering in one byte chunks.
for (unsigned i = 0 ; i < 9 ; ++i) {
Maybe<TestState> result =
lexer.Lex(data + i, 1,
[&](TestState aState, const char* aData, size_t aLength) {
return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
});
if (i == 8) {
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
} else {
EXPECT_TRUE(result.isNothing());
}
}
}
TEST(ImageStreamingLexer, TerminateSuccess)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
// Test that Terminate is "sticky".
Maybe<TestState> result =
lexer.Lex(data, sizeof(data),
[&](TestState aState, const char* aData, size_t aLength) {
EXPECT_TRUE(aState == TestState::ONE);
return Transition::Terminate(TestState::SUCCESS);
});
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
result =
lexer.Lex(data, sizeof(data),
[&](TestState aState, const char* aData, size_t aLength) {
EXPECT_TRUE(false); // Shouldn't get here.
return Transition::Terminate(TestState::FAILURE);
});
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
}
TEST(ImageStreamingLexer, TerminateFailure)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
// Test that Terminate is "sticky".
Maybe<TestState> result =
lexer.Lex(data, sizeof(data),
[&](TestState aState, const char* aData, size_t aLength) {
EXPECT_TRUE(aState == TestState::ONE);
return Transition::Terminate(TestState::FAILURE);
});
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::FAILURE, *result);
result =
lexer.Lex(data, sizeof(data),
[&](TestState aState, const char* aData, size_t aLength) {
EXPECT_TRUE(false); // Shouldn't get here.
return Transition::Terminate(TestState::FAILURE);
});
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::FAILURE, *result);
}
TEST(ImageStreamingLexer, TerminateUnbuffered)
{
StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
// Test that Terminate works during an unbuffered read.
for (unsigned i = 0 ; i < 9 ; ++i) {
Maybe<TestState> result =
lexer.Lex(data + i, 1, DoLexWithUnbufferedTerminate);
if (i > 2) {
EXPECT_TRUE(result.isSome());
EXPECT_EQ(TestState::SUCCESS, *result);
} else {
EXPECT_TRUE(result.isNothing());
}
}
}