From 11c9a55afd95078d14ab8cd7c1c5c0032af2a498 Mon Sep 17 00:00:00 2001 From: "bungeman@google.com" Date: Mon, 3 Jun 2013 17:10:35 +0000 Subject: Add SkData::NewFromFD. Chromium needs a SkStream backed by a file descriptor. Skia already has the code and can do the work, this change exposes the functionality in Skia in a clean way. https://codereview.chromium.org/15941025/ git-svn-id: http://skia.googlecode.com/svn/trunk@9408 2bbb7eff-a529-9590-31e7-b0007b416f81 --- tests/DataRefTest.cpp | 52 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) (limited to 'tests/DataRefTest.cpp') diff --git a/tests/DataRefTest.cpp b/tests/DataRefTest.cpp index 0cbce3fcd1..d8bd24b2ba 100644 --- a/tests/DataRefTest.cpp +++ b/tests/DataRefTest.cpp @@ -9,21 +9,10 @@ #include "SkData.h" #include "SkDataSet.h" #include "SkDataTable.h" -#include "SkStream.h" #include "SkOrderedReadBuffer.h" #include "SkOrderedWriteBuffer.h" - -template class SkTUnref { -public: - SkTUnref(T* ref) : fRef(ref) {} - ~SkTUnref() { fRef->unref(); } - - operator T*() { return fRef; } - operator const T*() { return fRef; } - -private: - T* fRef; -}; +#include "SkOSFile.h" +#include "SkStream.h" static void test_is_equal(skiatest::Reporter* reporter, const SkDataTable* a, const SkDataTable* b) { @@ -223,7 +212,7 @@ static void test_dataset(skiatest::Reporter* reporter, const SkDataSet& ds, static void test_dataset(skiatest::Reporter* reporter) { SkDataSet set0(NULL, 0); - SkDataSet set1("hello", SkTUnref(SkData::NewWithCString("world"))); + SkDataSet set1("hello", SkAutoTUnref(SkData::NewWithCString("world"))); const SkDataSet::Pair pairs[] = { { "one", SkData::NewWithCString("1") }, @@ -270,6 +259,40 @@ static void test_cstring(skiatest::Reporter* reporter) { REPORTER_ASSERT(reporter, 0 == *r2->bytes()); } +static void test_files(skiatest::Reporter* reporter) { + if (skiatest::Test::GetTmpDir().isEmpty()) { + return; + } + + const char* tmpDir = skiatest::Test::GetTmpDir().c_str(); + SkString path; + path.printf("%s%s", tmpDir, "data_test"); + + const char s[] = "abcdefghijklmnopqrstuvwxyz"; + { + SkFILEWStream writer(path.c_str()); + if (!writer.isValid()) { + SkString msg; + msg.printf("Failed to create tmp file %s\n", path.c_str()); + reporter->reportFailed(msg.c_str()); + return; + } + writer.write(s, 26); + } + + SkFILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag); + SkAutoTUnref r1(SkData::NewFromFILE(file)); + REPORTER_ASSERT(reporter, r1.get() != NULL); + REPORTER_ASSERT(reporter, r1->size() == 26); + REPORTER_ASSERT(reporter, strncmp(static_cast(r1->data()), s, 26) == 0); + + int fd = sk_fileno(file); + SkAutoTUnref r2(SkData::NewFromFD(fd)); + REPORTER_ASSERT(reporter, r2.get() != NULL); + REPORTER_ASSERT(reporter, r2->size() == 26); + REPORTER_ASSERT(reporter, strncmp(static_cast(r2->data()), s, 26) == 0); +} + static void TestData(skiatest::Reporter* reporter) { const char* str = "We the people, in order to form a more perfect union."; const int N = 10; @@ -297,6 +320,7 @@ static void TestData(skiatest::Reporter* reporter) { test_cstring(reporter); test_dataset(reporter); + test_files(reporter); } #include "TestClassDef.h" -- cgit v1.2.3