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

Side by Side Diff: chrome/browser/media_galleries/fileapi/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: Update gypi file to exclude test on android. Created 7 years, 7 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 "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" 9 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h"
10 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" 10 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h"
(...skipping 23 matching lines...) Expand all
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 picasaimport::PmpFieldType field_type, 42 void TestValid(const picasaimport::PmpFieldType field_type,
43 const std::vector<T>& elems) { 43 const std::vector<T>& elems) {
44 PmpTestHelper test_helper; 44 PmpTestHelper test_helper("test");
45 ASSERT_TRUE(test_helper.Init()); 45 ASSERT_TRUE(test_helper.Init());
46 46
47 PmpColumnReader reader; 47 PmpColumnReader reader;
48 48
49 uint32 rows_read = 0xFF; 49 uint32 rows_read = 0xFF;
50 50
51 std::vector<uint8> data = 51 std::vector<uint8> data =
52 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 52 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
53 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(&reader, data, &rows_read)); 53 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(
54 &reader, data, field_type, &rows_read));
54 EXPECT_EQ(elems.size(), rows_read); 55 EXPECT_EQ(elems.size(), rows_read);
55 56
56 for (uint32 i = 0; i < elems.size() && i < rows_read; i++) { 57 for (uint32 i = 0; i < elems.size() && i < rows_read; i++) {
57 T target; 58 T target;
58 EXPECT_TRUE(DoRead(&reader, i, &target)); 59 EXPECT_TRUE(DoRead(&reader, i, &target));
59 EXPECT_EQ(elems[i], target); 60 EXPECT_EQ(elems[i], target);
60 } 61 }
61 } 62 }
62 63
63 template<class T> 64 template<class T>
64 void TestMalformed(const picasaimport::PmpFieldType field_type, 65 void TestMalformed(const picasaimport::PmpFieldType field_type,
65 const std::vector<T>& elems) { 66 const std::vector<T>& elems) {
66 PmpTestHelper test_helper; 67 PmpTestHelper test_helper("test");
67 ASSERT_TRUE(test_helper.Init()); 68 ASSERT_TRUE(test_helper.Init());
68 69
69 PmpColumnReader reader1, reader2, reader3, reader4; 70 PmpColumnReader reader_too_few_declared_rows;
70
71 std::vector<uint8> data_too_few_declared_rows = 71 std::vector<uint8> data_too_few_declared_rows =
72 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems); 72 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems);
73 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 73 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
74 &reader1, data_too_few_declared_rows, NULL)); 74 &reader_too_few_declared_rows,
75 data_too_few_declared_rows,
76 field_type,
77 NULL));
75 78
79 PmpColumnReader reader_too_many_declared_rows;
76 std::vector<uint8> data_too_many_declared_rows = 80 std::vector<uint8> data_too_many_declared_rows =
77 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems); 81 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems);
78 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 82 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
79 &reader2, data_too_many_declared_rows, NULL)); 83 &reader_too_many_declared_rows,
84 data_too_many_declared_rows,
85 field_type,
86 NULL));
80 87
88 PmpColumnReader reader_truncated;
81 std::vector<uint8> data_truncated = 89 std::vector<uint8> data_truncated =
82 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 90 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
83 data_truncated.resize(data_truncated.size()-10); 91 data_truncated.resize(data_truncated.size()-10);
84 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 92 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
85 &reader3, data_truncated, NULL)); 93 &reader_truncated, data_truncated, field_type, NULL));
86 94
95 PmpColumnReader reader_padded;
87 std::vector<uint8> data_padded = 96 std::vector<uint8> data_padded =
88 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 97 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
89 data_padded.resize(data_padded.size()+10); 98 data_padded.resize(data_padded.size()+10);
90 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 99 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
91 &reader4, data_padded, NULL)); 100 &reader_padded, data_padded, field_type, NULL));
92 } 101 }
93 102
94 template<class T> 103 template<class T>
95 void TestPrimitive(const picasaimport::PmpFieldType field_type) { 104 void TestPrimitive(const picasaimport::PmpFieldType field_type) {
96 // Make an ascending vector of the primitive. 105 // Make an ascending vector of the primitive.
97 uint32 n = 100; 106 uint32 n = 100;
98 std::vector<T> data(n, 0); 107 std::vector<T> data(n, 0);
99 for (uint32 i = 0; i < n; i++) { 108 for (uint32 i = 0; i < n; i++) {
100 data[i] = i*3; 109 data[i] = i*3;
101 } 110 }
102 111
103 TestValid<T>(field_type, data); 112 TestValid<T>(field_type, data);
104 TestMalformed<T>(field_type, data); 113 TestMalformed<T>(field_type, data);
105 } 114 }
106 115
107 116
108 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) { 117 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) {
109 PmpTestHelper test_helper; 118 PmpTestHelper test_helper("test");
110 ASSERT_TRUE(test_helper.Init()); 119 ASSERT_TRUE(test_helper.Init());
111 120
112 PmpColumnReader reader1, reader2, reader3, reader4, reader5; 121 PmpColumnReader reader_good_header;
113
114 // Good header.
115 uint32 rows_read = 0xFF; 122 uint32 rows_read = 0xFF;
116 std::vector<uint8> good_header = PmpTestHelper::MakeHeader( 123 std::vector<uint8> good_header =
117 picasaimport::PMP_TYPE_STRING, 0); 124 PmpTestHelper::MakeHeader(picasaimport::PMP_TYPE_STRING, 0);
118 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes( 125 EXPECT_TRUE(test_helper.InitColumnReaderFromBytes(
119 &reader1, good_header, &rows_read)); 126 &reader_good_header,
127 good_header,
128 picasaimport::PMP_TYPE_STRING,
129 &rows_read));
120 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data."; 130 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data.";
121 131
122 // Botch up elements of the header. 132 PmpColumnReader reader_bad_magic_bytes;
123 std::vector<uint8> bad_magic_byte = PmpTestHelper::MakeHeader( 133 std::vector<uint8> bad_magic_bytes =
124 picasaimport::PMP_TYPE_STRING, 0); 134 PmpTestHelper::MakeHeader(picasaimport::PMP_TYPE_STRING, 0);
125 bad_magic_byte[0] = 0xff; 135 bad_magic_bytes[0] = 0xff;
126 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 136 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
127 &reader2, bad_magic_byte, NULL)); 137 &reader_bad_magic_bytes,
138 bad_magic_bytes,
139 picasaimport::PMP_TYPE_STRING,
140 NULL));
128 141
129 // Corrupt means the type fields don't agree. 142 PmpColumnReader reader_inconsistent_types;
130 std::vector<uint8> corrupt_type = PmpTestHelper::MakeHeader( 143 std::vector<uint8> inconsistent_type =
131 picasaimport::PMP_TYPE_STRING, 0); 144 PmpTestHelper::MakeHeader(picasaimport::PMP_TYPE_STRING, 0);
132 corrupt_type[picasaimport::kPmpFieldType1Offset] = 0xff; 145 inconsistent_type[picasaimport::kPmpFieldType1Offset] = 0xff;
133 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 146 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
134 &reader3, corrupt_type, NULL)); 147 &reader_inconsistent_types,
148 inconsistent_type,
149 picasaimport::PMP_TYPE_STRING,
150 NULL));
135 151
136 std::vector<uint8> invalid_type = PmpTestHelper::MakeHeader( 152 PmpColumnReader reader_invalid_type;
137 picasaimport::PMP_TYPE_STRING, 0); 153 std::vector<uint8> invalid_type =
154 PmpTestHelper::MakeHeader(picasaimport::PMP_TYPE_STRING, 0);
138 invalid_type[picasaimport::kPmpFieldType1Offset] = 0xff; 155 invalid_type[picasaimport::kPmpFieldType1Offset] = 0xff;
139 invalid_type[picasaimport::kPmpFieldType2Offset] = 0xff; 156 invalid_type[picasaimport::kPmpFieldType2Offset] = 0xff;
140 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 157 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
141 &reader4, invalid_type, NULL)); 158 &reader_invalid_type,
159 invalid_type,
160 picasaimport::PMP_TYPE_STRING,
161 NULL));
142 162
143 std::vector<uint8> incomplete_header = PmpTestHelper::MakeHeader( 163 PmpColumnReader reader_incomplete_header;
144 picasaimport::PMP_TYPE_STRING, 0); 164 std::vector<uint8> incomplete_header =
165 PmpTestHelper::MakeHeader(picasaimport::PMP_TYPE_STRING, 0);
145 incomplete_header.resize(10); 166 incomplete_header.resize(10);
146 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 167 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
147 &reader5, incomplete_header, NULL)); 168 &reader_incomplete_header,
169 incomplete_header,
170 picasaimport::PMP_TYPE_STRING,
171 NULL));
148 } 172 }
149 173
150 TEST(PmpColumnReaderTest, StringParsing) { 174 TEST(PmpColumnReaderTest, StringParsing) {
151 std::vector<std::string> empty_strings(100, ""); 175 std::vector<std::string> empty_strings(100, "");
152 176
153 // Test empty strings read okay. 177 // Test empty strings read okay.
154 TestValid(picasaimport::PMP_TYPE_STRING, empty_strings); 178 TestValid(picasaimport::PMP_TYPE_STRING, empty_strings);
155 179
156 std::vector<std::string> mixed_strings; 180 std::vector<std::string> mixed_strings;
157 mixed_strings.push_back(""); 181 mixed_strings.push_back("");
(...skipping 12 matching lines...) Expand all
170 } 194 }
171 195
172 TEST(PmpColumnReaderTest, PrimitiveParsing) { 196 TEST(PmpColumnReaderTest, PrimitiveParsing) {
173 TestPrimitive<uint32>(picasaimport::PMP_TYPE_UINT32); 197 TestPrimitive<uint32>(picasaimport::PMP_TYPE_UINT32);
174 TestPrimitive<double>(picasaimport::PMP_TYPE_DOUBLE64); 198 TestPrimitive<double>(picasaimport::PMP_TYPE_DOUBLE64);
175 TestPrimitive<uint8>(picasaimport::PMP_TYPE_UINT8); 199 TestPrimitive<uint8>(picasaimport::PMP_TYPE_UINT8);
176 TestPrimitive<uint64>(picasaimport::PMP_TYPE_UINT64); 200 TestPrimitive<uint64>(picasaimport::PMP_TYPE_UINT64);
177 } 201 }
178 202
179 } // namespace 203 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698