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

Side by Side Diff: webkit/fileapi/media/picasa/pmp_column_reader_unittest.cc

Issue 13529028: PicasaAlbumTableReader for Media Galleries API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@0005-picasa-import-pmp-reader
Patch Set: Missing include. Created 7 years, 8 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/fileapi/media/picasa/pmp_column_reader.h" 10 #include "webkit/fileapi/media/picasa/pmp_column_reader.h"
(...skipping 21 matching lines...) Expand all
32 bool DoRead(const PmpColumnReader* reader, uint32 row, uint8* target) { 32 bool DoRead(const PmpColumnReader* reader, uint32 row, uint8* target) {
33 return reader->ReadUInt8(row, target); 33 return reader->ReadUInt8(row, target);
34 } 34 }
35 35
36 bool DoRead(const PmpColumnReader* reader, uint32 row, uint64* target) { 36 bool DoRead(const PmpColumnReader* reader, uint32 row, uint64* target) {
37 return reader->ReadUInt64(row, target); 37 return reader->ReadUInt64(row, target);
38 } 38 }
39 39
40 // TestValid 40 // TestValid
41 template<class T> 41 template<class T>
42 void TestValid(const uint16 field_type, const std::vector<T>& elems) { 42 void TestValid(const picasaimport::PmpFieldType field_type,
43 const std::vector<T>& elems) {
43 PmpTestHelper test_helper; 44 PmpTestHelper test_helper;
44 ASSERT_TRUE(test_helper.Init()); 45 ASSERT_TRUE(test_helper.Init());
45 46
46 PmpColumnReader reader; 47 PmpColumnReader reader("");
47 48
48 uint32 rows_read = 0xFF; 49 uint32 rows_read = 0xFF;
49 50
50 std::vector<uint8> data = 51 std::vector<uint8> data =
51 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 52 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
52 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(&reader, data, &rows_read)); 53 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(
54 &reader, data, field_type, &rows_read));
53 EXPECT_EQ(elems.size(), rows_read); 55 EXPECT_EQ(elems.size(), rows_read);
54 56
55 for(uint32 i = 0; i < elems.size() && i < rows_read; i++) { 57 for(uint32 i = 0; i < elems.size() && i < rows_read; i++) {
56 T target; 58 T target;
57 EXPECT_TRUE(DoRead(&reader, i, &target)); 59 EXPECT_TRUE(DoRead(&reader, i, &target));
58 EXPECT_EQ(elems[i], target); 60 EXPECT_EQ(elems[i], target);
59 } 61 }
60 } 62 }
61 63
62 template<class T> 64 template<class T>
63 void TestMalformed(const uint16 field_type, const std::vector<T>& elems) { 65 void TestMalformed(const picasaimport::PmpFieldType field_type,
66 const std::vector<T>& elems) {
64 PmpTestHelper test_helper; 67 PmpTestHelper test_helper;
65 ASSERT_TRUE(test_helper.Init()); 68 ASSERT_TRUE(test_helper.Init());
66 69
67 PmpColumnReader reader1, reader2, reader3, reader4; 70 PmpColumnReader reader1(""), reader2(""), reader3(""), reader4("");
vandebo (ex-Chrome) 2013/04/09 19:16:56 probably should put these on multiple lines now...
tommycli 2013/04/11 18:24:05 Done.
68 71
69 std::vector<uint8> data_too_few_declared_rows = 72 std::vector<uint8> data_too_few_declared_rows =
70 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems); 73 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems);
71 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 74 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
72 &reader1, data_too_few_declared_rows, NULL)); 75 &reader1, data_too_few_declared_rows, field_type, NULL));
73 76
74 std::vector<uint8> data_too_many_declared_rows = 77 std::vector<uint8> data_too_many_declared_rows =
75 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems); 78 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems);
76 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 79 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
77 &reader2, data_too_many_declared_rows, NULL)); 80 &reader2, data_too_many_declared_rows, field_type, NULL));
78 81
79 std::vector<uint8> data_truncated = 82 std::vector<uint8> data_truncated =
80 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 83 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
81 data_truncated.resize(data_truncated.size()-10); 84 data_truncated.resize(data_truncated.size()-10);
82 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 85 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
83 &reader3, data_truncated, NULL)); 86 &reader3, data_truncated, field_type, NULL));
84 87
85 std::vector<uint8> data_padded = 88 std::vector<uint8> data_padded =
86 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 89 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
87 data_padded.resize(data_padded.size()+10); 90 data_padded.resize(data_padded.size()+10);
88 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 91 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
89 &reader4, data_padded, NULL)); 92 &reader4, data_padded, field_type, NULL));
90 } 93 }
91 94
92 template<class T> 95 template<class T>
93 void TestPrimitive(const uint16 field_type) { 96 void TestPrimitive(const picasaimport::PmpFieldType field_type) {
94 // Make an ascending vector of the primitive. 97 // Make an ascending vector of the primitive.
95 uint32 n = 100; 98 uint32 n = 100;
96 std::vector<T> data(n, 0); 99 std::vector<T> data(n, 0);
97 for(uint32 i = 0; i < n; i++) { 100 for(uint32 i = 0; i < n; i++) {
98 data[i] = i*3; 101 data[i] = i*3;
99 } 102 }
100 103
101 TestValid<T>(field_type, data); 104 TestValid<T>(field_type, data);
102 TestMalformed<T>(field_type, data); 105 TestMalformed<T>(field_type, data);
103 } 106 }
104 107
105 108
106 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) { 109 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) {
107 PmpTestHelper test_helper; 110 PmpTestHelper test_helper;
108 ASSERT_TRUE(test_helper.Init()); 111 ASSERT_TRUE(test_helper.Init());
109 112
110 PmpColumnReader reader1, reader2, reader3, reader4, reader5; 113 PmpColumnReader reader1(""), reader2(""), reader3(""), reader4(""),
vandebo (ex-Chrome) 2013/04/09 19:16:56 same here
tommycli 2013/04/11 18:24:05 Done.
114 reader5("");
111 115
112 // Good header. 116 // Good header.
113 uint32 rows_read = 0xFF; 117 uint32 rows_read = 0xFF;
114 std::vector<uint8> good_header = PmpTestHelper::MakeHeader(0x00, 0); 118 std::vector<uint8> good_header =
119 PmpTestHelper::MakeHeader(picasaimport::PMP_STRING_TYPE, 0);
115 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes( 120 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(
116 &reader1, good_header, &rows_read)); 121 &reader1, good_header, picasaimport::PMP_STRING_TYPE, &rows_read));
117 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data."; 122 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data.";
118 123
119 // Botch up elements of the header. 124 // Botch up elements of the header.
120 std::vector<uint8> bad_magic_byte = PmpTestHelper::MakeHeader(0x00, 0); 125 std::vector<uint8> bad_magic_byte =
126 PmpTestHelper::MakeHeader(picasaimport::PMP_STRING_TYPE, 0);
121 bad_magic_byte[0] = 0xff; 127 bad_magic_byte[0] = 0xff;
122 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 128 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
123 &reader2, bad_magic_byte, NULL)); 129 &reader2, bad_magic_byte, picasaimport::PMP_STRING_TYPE, NULL));
124 130
125 // Corrupt means the type fields don't agree. 131 // Corrupt means the type fields don't agree.
126 std::vector<uint8> corrupt_type = PmpTestHelper::MakeHeader(0x00, 0); 132 std::vector<uint8> corrupt_type =
133 PmpTestHelper::MakeHeader(picasaimport::PMP_STRING_TYPE, 0);
127 corrupt_type[picasaimport::kPmpFieldType1Offset] = 0xff; 134 corrupt_type[picasaimport::kPmpFieldType1Offset] = 0xff;
128 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 135 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
129 &reader3, corrupt_type, NULL)); 136 &reader3, corrupt_type, picasaimport::PMP_STRING_TYPE, NULL));
130 137
131 std::vector<uint8> invalid_type = PmpTestHelper::MakeHeader(0x00, 0); 138 std::vector<uint8> invalid_type =
139 PmpTestHelper::MakeHeader(picasaimport::PMP_STRING_TYPE, 0);
132 invalid_type[picasaimport::kPmpFieldType1Offset] = 0xff; 140 invalid_type[picasaimport::kPmpFieldType1Offset] = 0xff;
133 invalid_type[picasaimport::kPmpFieldType2Offset] = 0xff; 141 invalid_type[picasaimport::kPmpFieldType2Offset] = 0xff;
134 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 142 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
135 &reader4, invalid_type, NULL)); 143 &reader4, invalid_type, picasaimport::PMP_STRING_TYPE, NULL));
136 144
137 std::vector<uint8> incomplete_header = PmpTestHelper::MakeHeader(0x00, 0); 145 std::vector<uint8> incomplete_header =
146 PmpTestHelper::MakeHeader(picasaimport::PMP_STRING_TYPE, 0);
138 incomplete_header.resize(10); 147 incomplete_header.resize(10);
139 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 148 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
140 &reader5, incomplete_header, NULL)); 149 &reader5, incomplete_header, picasaimport::PMP_STRING_TYPE, NULL));
141 } 150 }
142 151
143 TEST(PmpColumnReaderTest, StringParsing) { 152 TEST(PmpColumnReaderTest, StringParsing) {
144 std::vector<std::string> empty_strings(100, ""); 153 std::vector<std::string> empty_strings(100, "");
145 154
146 // Test empty strings read okay. 155 // Test empty strings read okay.
147 TestValid(picasaimport::PMP_STRING_TYPE, empty_strings); 156 TestValid(picasaimport::PMP_STRING_TYPE, empty_strings);
148 157
149 std::vector<std::string> mixed_strings; 158 std::vector<std::string> mixed_strings;
150 mixed_strings.push_back(""); 159 mixed_strings.push_back("");
(...skipping 12 matching lines...) Expand all
163 } 172 }
164 173
165 TEST(PmpColumnReaderTest, PrimitiveParsing) { 174 TEST(PmpColumnReaderTest, PrimitiveParsing) {
166 TestPrimitive<uint32>(picasaimport::PMP_UINT32_TYPE); 175 TestPrimitive<uint32>(picasaimport::PMP_UINT32_TYPE);
167 TestPrimitive<double>(picasaimport::PMP_DOUBLE64_TYPE); 176 TestPrimitive<double>(picasaimport::PMP_DOUBLE64_TYPE);
168 TestPrimitive<uint8>(picasaimport::PMP_UINT8_TYPE); 177 TestPrimitive<uint8>(picasaimport::PMP_UINT8_TYPE);
169 TestPrimitive<uint64>(picasaimport::PMP_UINT64_TYPE); 178 TestPrimitive<uint64>(picasaimport::PMP_UINT64_TYPE);
170 } 179 }
171 180
172 } // namespace 181 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698