| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
| 9 #include "SkBitmapSource.h" | 9 #include "SkBitmapSource.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 size_t bytesWritten = writer.bytesWritten(); | 116 size_t bytesWritten = writer.bytesWritten(); |
| 117 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 117 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 118 | 118 |
| 119 unsigned char dataWritten[1024]; | 119 unsigned char dataWritten[1024]; |
| 120 writer.writeToMemory(dataWritten); | 120 writer.writeToMemory(dataWritten); |
| 121 | 121 |
| 122 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 122 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
| 123 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | 123 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| 124 T obj; | 124 T obj; |
| 125 SerializationUtils<T>::Read(buffer, &obj); | 125 SerializationUtils<T>::Read(buffer, &obj); |
| 126 REPORTER_ASSERT(reporter, !buffer.validate(true)); | 126 REPORTER_ASSERT(reporter, !buffer.isValid()); |
| 127 | 127 |
| 128 // Make sure this succeeds when it should | 128 // Make sure this succeeds when it should |
| 129 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 129 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 130 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); | 130 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); |
| 131 T obj2; | 131 T obj2; |
| 132 SerializationUtils<T>::Read(buffer2, &obj2); | 132 SerializationUtils<T>::Read(buffer2, &obj2); |
| 133 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); | 133 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); |
| 134 // This should have succeeded, since there are enough bytes to read this | 134 // This should have succeeded, since there are enough bytes to read this |
| 135 REPORTER_ASSERT(reporter, buffer2.validate(true)); | 135 REPORTER_ASSERT(reporter, buffer2.isValid()); |
| 136 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt
esWritten); | 136 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt
esWritten); |
| 137 | 137 |
| 138 TestAlignment(testObj, reporter); | 138 TestAlignment(testObj, reporter); |
| 139 } | 139 } |
| 140 | 140 |
| 141 template<typename T> | 141 template<typename T> |
| 142 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, | 142 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
| 143 skiatest::Reporter* reporter) { | 143 skiatest::Reporter* reporter) { |
| 144 SkOrderedWriteBuffer writer(1024); | 144 SkOrderedWriteBuffer writer(1024); |
| 145 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 145 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
| 146 SerializationUtils<T>::Write(writer, testObj); | 146 SerializationUtils<T>::Write(writer, testObj); |
| 147 size_t bytesWritten = writer.bytesWritten(); | 147 size_t bytesWritten = writer.bytesWritten(); |
| 148 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 148 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
| 149 | 149 |
| 150 unsigned char dataWritten[1024]; | 150 unsigned char dataWritten[1024]; |
| 151 writer.writeToMemory(dataWritten); | 151 writer.writeToMemory(dataWritten); |
| 152 | 152 |
| 153 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 153 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
| 154 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | 154 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| 155 T* obj = NULL; | 155 T* obj = NULL; |
| 156 SerializationUtils<T>::Read(buffer, &obj); | 156 SerializationUtils<T>::Read(buffer, &obj); |
| 157 REPORTER_ASSERT(reporter, !buffer.validate(true)); | 157 REPORTER_ASSERT(reporter, !buffer.isValid()); |
| 158 REPORTER_ASSERT(reporter, NULL == obj); | 158 REPORTER_ASSERT(reporter, NULL == obj); |
| 159 | 159 |
| 160 // Make sure this succeeds when it should | 160 // Make sure this succeeds when it should |
| 161 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 161 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 162 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); | 162 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2.
skip(0)); |
| 163 T* obj2 = NULL; | 163 T* obj2 = NULL; |
| 164 SerializationUtils<T>::Read(buffer2, &obj2); | 164 SerializationUtils<T>::Read(buffer2, &obj2); |
| 165 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); | 165 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s
kip(0)); |
| 166 if (shouldSucceed) { | 166 if (shouldSucceed) { |
| 167 // This should have succeeded, since there are enough bytes to read this | 167 // This should have succeeded, since there are enough bytes to read this |
| 168 REPORTER_ASSERT(reporter, buffer2.validate(true)); | 168 REPORTER_ASSERT(reporter, buffer2.isValid()); |
| 169 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) ==
bytesWritten); | 169 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) ==
bytesWritten); |
| 170 REPORTER_ASSERT(reporter, NULL != obj2); | 170 REPORTER_ASSERT(reporter, NULL != obj2); |
| 171 } else { | 171 } else { |
| 172 // If the deserialization was supposed to fail, make sure it did | 172 // If the deserialization was supposed to fail, make sure it did |
| 173 REPORTER_ASSERT(reporter, !buffer.validate(true)); | 173 REPORTER_ASSERT(reporter, !buffer.isValid()); |
| 174 REPORTER_ASSERT(reporter, NULL == obj2); | 174 REPORTER_ASSERT(reporter, NULL == obj2); |
| 175 } | 175 } |
| 176 | 176 |
| 177 return obj2; // Return object to perform further validity tests on it | 177 return obj2; // Return object to perform further validity tests on it |
| 178 } | 178 } |
| 179 | 179 |
| 180 template<typename T> | 180 template<typename T> |
| 181 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { | 181 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { |
| 182 SkOrderedWriteBuffer writer(1024); | 182 SkOrderedWriteBuffer writer(1024); |
| 183 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 183 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 invalidBitmap2.setPixelRef(SkNEW_ARGS(SkMallocPixelRef, | 312 invalidBitmap2.setPixelRef(SkNEW_ARGS(SkMallocPixelRef, |
| 313 (NULL, 256, NULL)))->unref(); | 313 (NULL, 256, NULL)))->unref(); |
| 314 | 314 |
| 315 // The deserialization should detect the pixel ref being too small and f
ail | 315 // The deserialization should detect the pixel ref being too small and f
ail |
| 316 TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter); | 316 TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter); |
| 317 } | 317 } |
| 318 } | 318 } |
| 319 | 319 |
| 320 #include "TestClassDef.h" | 320 #include "TestClassDef.h" |
| 321 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) | 321 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) |
| OLD | NEW |