Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: tests/SerializationTest.cpp

Issue 138803005: Set write buffer flags only in SkWriteBuffer and SkFlatController constructors. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: INHERITED Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pipe/SkGPipeWrite.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { 103 static void Write(SkWriteBuffer& writer, SkScalar* data, uint32_t arraySize) {
104 writer.writeScalarArray(data, arraySize); 104 writer.writeScalarArray(data, arraySize);
105 } 105 }
106 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t ar raySize) { 106 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t ar raySize) {
107 return reader.readScalarArray(data, arraySize); 107 return reader.readScalarArray(data, arraySize);
108 } 108 }
109 }; 109 };
110 110
111 template<typename T> 111 template<typename T>
112 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { 112 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) {
113 SkWriteBuffer writer; 113 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
114 writer.setFlags(SkWriteBuffer::kValidation_Flag);
115 SerializationUtils<T>::Write(writer, testObj); 114 SerializationUtils<T>::Write(writer, testObj);
116 size_t bytesWritten = writer.bytesWritten(); 115 size_t bytesWritten = writer.bytesWritten();
117 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); 116 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
118 117
119 unsigned char dataWritten[1024]; 118 unsigned char dataWritten[1024];
120 writer.writeToMemory(dataWritten); 119 writer.writeToMemory(dataWritten);
121 120
122 // Make sure this fails when it should (test with smaller size, but still mu ltiple of 4) 121 // Make sure this fails when it should (test with smaller size, but still mu ltiple of 4)
123 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); 122 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
124 T obj; 123 T obj;
125 SerializationUtils<T>::Read(buffer, &obj); 124 SerializationUtils<T>::Read(buffer, &obj);
126 REPORTER_ASSERT(reporter, !buffer.isValid()); 125 REPORTER_ASSERT(reporter, !buffer.isValid());
127 126
128 // Make sure this succeeds when it should 127 // Make sure this succeeds when it should
129 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); 128 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
130 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2. skip(0)); 129 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2. skip(0));
131 T obj2; 130 T obj2;
132 SerializationUtils<T>::Read(buffer2, &obj2); 131 SerializationUtils<T>::Read(buffer2, &obj2);
133 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s kip(0)); 132 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 133 // This should have succeeded, since there are enough bytes to read this
135 REPORTER_ASSERT(reporter, buffer2.isValid()); 134 REPORTER_ASSERT(reporter, buffer2.isValid());
136 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt esWritten); 135 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt esWritten);
137 136
138 TestAlignment(testObj, reporter); 137 TestAlignment(testObj, reporter);
139 } 138 }
140 139
141 template<typename T> 140 template<typename T>
142 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, 141 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed,
143 skiatest::Reporter* reporter) { 142 skiatest::Reporter* reporter) {
144 SkWriteBuffer writer; 143 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
145 writer.setFlags(SkWriteBuffer::kValidation_Flag);
146 SerializationUtils<T>::Write(writer, testObj); 144 SerializationUtils<T>::Write(writer, testObj);
147 size_t bytesWritten = writer.bytesWritten(); 145 size_t bytesWritten = writer.bytesWritten();
148 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); 146 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
149 147
150 unsigned char dataWritten[1024]; 148 unsigned char dataWritten[1024];
151 SkASSERT(bytesWritten <= sizeof(dataWritten)); 149 SkASSERT(bytesWritten <= sizeof(dataWritten));
152 writer.writeToMemory(dataWritten); 150 writer.writeToMemory(dataWritten);
153 151
154 // Make sure this fails when it should (test with smaller size, but still mu ltiple of 4) 152 // Make sure this fails when it should (test with smaller size, but still mu ltiple of 4)
155 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); 153 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
(...skipping 17 matching lines...) Expand all
173 // If the deserialization was supposed to fail, make sure it did 171 // If the deserialization was supposed to fail, make sure it did
174 REPORTER_ASSERT(reporter, !buffer.isValid()); 172 REPORTER_ASSERT(reporter, !buffer.isValid());
175 REPORTER_ASSERT(reporter, NULL == obj2); 173 REPORTER_ASSERT(reporter, NULL == obj2);
176 } 174 }
177 175
178 return obj2; // Return object to perform further validity tests on it 176 return obj2; // Return object to perform further validity tests on it
179 } 177 }
180 178
181 template<typename T> 179 template<typename T>
182 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { 180 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
183 SkWriteBuffer writer; 181 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
184 writer.setFlags(SkWriteBuffer::kValidation_Flag);
185 SerializationUtils<T>::Write(writer, data, kArraySize); 182 SerializationUtils<T>::Write(writer, data, kArraySize);
186 size_t bytesWritten = writer.bytesWritten(); 183 size_t bytesWritten = writer.bytesWritten();
187 // This should write the length (in 4 bytes) and the array 184 // This should write the length (in 4 bytes) and the array
188 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); 185 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
189 186
190 unsigned char dataWritten[1024]; 187 unsigned char dataWritten[1024];
191 writer.writeToMemory(dataWritten); 188 writer.writeToMemory(dataWritten);
192 189
193 // Make sure this fails when it should 190 // Make sure this fails when it should
194 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); 191 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 info.fHeight = 1; 323 info.fHeight = 1;
327 324
328 invalidBitmap2.setPixelRef(SkMallocPixelRef::NewAllocate( 325 invalidBitmap2.setPixelRef(SkMallocPixelRef::NewAllocate(
329 info, invalidBitmap2.rowBytes(), NULL))->unref(); 326 info, invalidBitmap2.rowBytes(), NULL))->unref();
330 327
331 // The deserialization should detect the pixel ref being too small and f ail 328 // The deserialization should detect the pixel ref being too small and f ail
332 TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter); 329 TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter);
333 #endif 330 #endif
334 } 331 }
335 } 332 }
OLDNEW
« no previous file with comments | « src/pipe/SkGPipeWrite.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698