diff options
author | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-06-03 17:10:35 +0000 |
---|---|---|
committer | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-06-03 17:10:35 +0000 |
commit | 11c9a55afd95078d14ab8cd7c1c5c0032af2a498 (patch) | |
tree | 3479a58fe5e8e8dfc687f6a76412b049773e3c8f /tests/DataRefTest.cpp | |
parent | 135ece137b471219eea06a652069b86a3b6ec349 (diff) |
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
Diffstat (limited to 'tests/DataRefTest.cpp')
-rw-r--r-- | tests/DataRefTest.cpp | 52 |
1 files changed, 38 insertions, 14 deletions
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 <typename T> 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>(SkData::NewWithCString("world"))); + SkDataSet set1("hello", SkAutoTUnref<SkData>(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<SkData> r1(SkData::NewFromFILE(file)); + REPORTER_ASSERT(reporter, r1.get() != NULL); + REPORTER_ASSERT(reporter, r1->size() == 26); + REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0); + + int fd = sk_fileno(file); + SkAutoTUnref<SkData> r2(SkData::NewFromFD(fd)); + REPORTER_ASSERT(reporter, r2.get() != NULL); + REPORTER_ASSERT(reporter, r2->size() == 26); + REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(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" |