| OLD | NEW |
| 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 "webkit/fileapi/media/picasa/pmp_test_helper.h" | 5 #include "webkit/fileapi/media/picasa/pmp_test_helper.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "webkit/fileapi/media/picasa/pmp_column_reader.h" | 12 #include "webkit/fileapi/media/picasa/pmp_column_reader.h" |
| 13 #include "webkit/fileapi/media/picasa/pmp_constants.h" | |
| 14 | 13 |
| 15 namespace picasaimport { | 14 namespace picasaimport { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 bool WriteToFile(const base::FilePath& path, std::vector<uint8> data) { | 18 bool WriteToFile(const base::FilePath& path, std::vector<uint8> data) { |
| 20 // Cast for usage in WriteFile function | 19 // Cast for usage in WriteFile function |
| 21 const char* data_char = reinterpret_cast<const char*>(&data[0]); | 20 const char* data_char = reinterpret_cast<const char*>(&data[0]); |
| 22 size_t bytes_written = file_util::WriteFile(path, data_char, data.size()); | 21 size_t bytes_written = file_util::WriteFile(path, data_char, data.size()); |
| 23 return (bytes_written == data.size()); | 22 return (bytes_written == data.size()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 return temp_dir_.CreateUniqueTempDir(); | 64 return temp_dir_.CreateUniqueTempDir(); |
| 66 } | 65 } |
| 67 | 66 |
| 68 base::FilePath PmpTestHelper::GetTempDirPath() { | 67 base::FilePath PmpTestHelper::GetTempDirPath() { |
| 69 DCHECK(temp_dir_.IsValid()); | 68 DCHECK(temp_dir_.IsValid()); |
| 70 return temp_dir_.path(); | 69 return temp_dir_.path(); |
| 71 } | 70 } |
| 72 | 71 |
| 73 template<class T> | 72 template<class T> |
| 74 bool PmpTestHelper::WriteColumnFileFromVector( | 73 bool PmpTestHelper::WriteColumnFileFromVector( |
| 75 std::string table_name, std::string column_name, const uint16 field_type, | 74 std::string table_name, std::string column_name, |
| 76 std::vector<T> elements_vector) { | 75 const PmpFieldType field_type, std::vector<T> elements_vector) { |
| 77 DCHECK(temp_dir_.IsValid()); | 76 DCHECK(temp_dir_.IsValid()); |
| 78 | 77 |
| 79 std::string file_name = table_name + "_" + column_name + "." + kPmpExtension; | 78 std::string file_name = table_name + "_" + column_name + "." + kPmpExtension; |
| 80 | 79 |
| 81 #if defined(OS_WIN) | 80 #if defined(OS_WIN) |
| 82 base::FilePath path = temp_dir_.path().Append(UTF8ToUTF16(file_name)); | 81 base::FilePath path = temp_dir_.path().Append(UTF8ToUTF16(file_name)); |
| 83 #else | 82 #else |
| 84 base::FilePath path = temp_dir_.path().Append(file_name); | 83 base::FilePath path = temp_dir_.path().Append(file_name); |
| 85 #endif | 84 #endif |
| 86 | 85 |
| 87 std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody( | 86 std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody( |
| 88 field_type, elements_vector.size(), elements_vector); | 87 field_type, elements_vector.size(), elements_vector); |
| 89 | 88 |
| 90 return WriteToFile(path, data); | 89 return WriteToFile(path, data); |
| 91 } | 90 } |
| 92 | 91 |
| 93 // Explicit Instantiation for all the valid types. | 92 // Explicit Instantiation for all the valid types. |
| 94 template bool PmpTestHelper::WriteColumnFileFromVector<std::string>( | 93 template bool PmpTestHelper::WriteColumnFileFromVector<std::string>( |
| 95 std::string, std::string, const uint16, std::vector<std::string>); | 94 std::string, std::string, const PmpFieldType, std::vector<std::string>); |
| 96 template bool PmpTestHelper::WriteColumnFileFromVector<uint32>( | 95 template bool PmpTestHelper::WriteColumnFileFromVector<uint32>( |
| 97 std::string, std::string, const uint16, std::vector<uint32>); | 96 std::string, std::string, const PmpFieldType, std::vector<uint32>); |
| 98 template bool PmpTestHelper::WriteColumnFileFromVector<double>( | 97 template bool PmpTestHelper::WriteColumnFileFromVector<double>( |
| 99 std::string, std::string, const uint16, std::vector<double>); | 98 std::string, std::string, const PmpFieldType, std::vector<double>); |
| 100 template bool PmpTestHelper::WriteColumnFileFromVector<uint8>( | 99 template bool PmpTestHelper::WriteColumnFileFromVector<uint8>( |
| 101 std::string, std::string, const uint16, std::vector<uint8>); | 100 std::string, std::string, const PmpFieldType, std::vector<uint8>); |
| 102 template bool PmpTestHelper::WriteColumnFileFromVector<uint64>( | 101 template bool PmpTestHelper::WriteColumnFileFromVector<uint64>( |
| 103 std::string, std::string, const uint16, std::vector<uint64>); | 102 std::string, std::string, const PmpFieldType, std::vector<uint64>); |
| 104 | 103 |
| 105 bool PmpTestHelper::InitColumnReaderFromBytes( | 104 bool PmpTestHelper::InitColumnReaderFromBytes( |
| 106 PmpColumnReader* const reader, std::vector<uint8> data, uint32* rows_read) { | 105 PmpColumnReader* const reader, std::vector<uint8> data, |
| 106 const PmpFieldType expected_type, uint32* rows_read) { |
| 107 DCHECK(temp_dir_.IsValid()); | 107 DCHECK(temp_dir_.IsValid()); |
| 108 | 108 |
| 109 base::FilePath temp_path; | 109 base::FilePath temp_path; |
| 110 | 110 |
| 111 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path) || | 111 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path) || |
| 112 !WriteToFile(temp_path, data)) { | 112 !WriteToFile(temp_path, data)) { |
| 113 return false; | 113 return false; |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool success = reader->Init(temp_path, rows_read); | 116 bool success = reader->Init(temp_path, expected_type, rows_read); |
| 117 | 117 |
| 118 file_util::Delete(temp_path, true); | 118 file_util::Delete(temp_path, true); |
| 119 | 119 |
| 120 return success; | 120 return success; |
| 121 | 121 |
| 122 } | 122 } |
| 123 | 123 |
| 124 // Return a vector so we don't have to worry about memory management. | 124 // Return a vector so we don't have to worry about memory management. |
| 125 std::vector<uint8> PmpTestHelper::MakeHeader(const uint16 field_type, | 125 std::vector<uint8> PmpTestHelper::MakeHeader(const PmpFieldType field_type, |
| 126 const uint32 row_count) { | 126 const uint32 row_count) { |
| 127 std::vector<uint8> header(picasaimport::kPmpHeaderSize); | 127 std::vector<uint8> header(picasaimport::kPmpHeaderSize); |
| 128 | 128 |
| 129 // Copy in magic bytes. | 129 // Copy in magic bytes. |
| 130 memcpy(&header[picasaimport::kPmpMagic1Offset], &picasaimport::kPmpMagic1, | 130 memcpy(&header[picasaimport::kPmpMagic1Offset], &picasaimport::kPmpMagic1, |
| 131 sizeof(picasaimport::kPmpMagic1)); | 131 sizeof(picasaimport::kPmpMagic1)); |
| 132 memcpy(&header[picasaimport::kPmpMagic2Offset], &picasaimport::kPmpMagic2, | 132 memcpy(&header[picasaimport::kPmpMagic2Offset], &picasaimport::kPmpMagic2, |
| 133 sizeof(picasaimport::kPmpMagic2)); | 133 sizeof(picasaimport::kPmpMagic2)); |
| 134 memcpy(&header[picasaimport::kPmpMagic3Offset], &picasaimport::kPmpMagic3, | 134 memcpy(&header[picasaimport::kPmpMagic3Offset], &picasaimport::kPmpMagic3, |
| 135 sizeof(picasaimport::kPmpMagic3)); | 135 sizeof(picasaimport::kPmpMagic3)); |
| 136 memcpy(&header[picasaimport::kPmpMagic4Offset], &picasaimport::kPmpMagic4, | 136 memcpy(&header[picasaimport::kPmpMagic4Offset], &picasaimport::kPmpMagic4, |
| 137 sizeof(picasaimport::kPmpMagic4)); | 137 sizeof(picasaimport::kPmpMagic4)); |
| 138 | 138 |
| 139 // Copy in field type. | 139 // Copy in field type. |
| 140 memcpy(&header[picasaimport::kPmpFieldType1Offset], &field_type, 2); | 140 uint16 field_type_short = static_cast<uint16>(field_type); |
| 141 memcpy(&header[picasaimport::kPmpFieldType2Offset], &field_type, 2); | 141 memcpy(&header[picasaimport::kPmpFieldType1Offset], &field_type_short, |
| 142 sizeof(uint16)); |
| 143 memcpy(&header[picasaimport::kPmpFieldType2Offset], &field_type_short, |
| 144 sizeof(uint16)); |
| 142 | 145 |
| 143 // Copy in row count. | 146 // Copy in row count. |
| 144 memcpy(&header[picasaimport::kPmpRowCountOffset], &row_count, 4); | 147 memcpy(&header[picasaimport::kPmpRowCountOffset], &row_count, sizeof(uint32)); |
| 145 | 148 |
| 146 return header; | 149 return header; |
| 147 } | 150 } |
| 148 | 151 |
| 149 template<class T> | 152 template<class T> |
| 150 std::vector<uint8> PmpTestHelper::MakeHeaderAndBody( | 153 std::vector<uint8> PmpTestHelper::MakeHeaderAndBody( |
| 151 const uint16 field_type, const uint32 row_count, | 154 const PmpFieldType field_type, const uint32 row_count, |
| 152 const std::vector<T>& elems) { | 155 const std::vector<T>& elems) { |
| 153 return CombinedVectors(PmpTestHelper::MakeHeader(field_type, row_count), | 156 return CombinedVectors(PmpTestHelper::MakeHeader(field_type, row_count), |
| 154 Flatten(elems)); | 157 Flatten(elems)); |
| 155 } | 158 } |
| 156 | 159 |
| 157 // Explicit Instantiation for all the valid types. | 160 // Explicit Instantiation for all the valid types. |
| 158 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>( | 161 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>( |
| 159 const uint16, const uint32, const std::vector<std::string>&); | 162 const PmpFieldType, const uint32, const std::vector<std::string>&); |
| 160 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>( | 163 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>( |
| 161 const uint16, const uint32, const std::vector<uint32>&); | 164 const PmpFieldType, const uint32, const std::vector<uint32>&); |
| 162 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>( | 165 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>( |
| 163 const uint16, const uint32, const std::vector<double>&); | 166 const PmpFieldType, const uint32, const std::vector<double>&); |
| 164 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>( | 167 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>( |
| 165 const uint16, const uint32, const std::vector<uint8>&); | 168 const PmpFieldType, const uint32, const std::vector<uint8>&); |
| 166 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>( | 169 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>( |
| 167 const uint16, const uint32, const std::vector<uint64>&); | 170 const PmpFieldType, const uint32, const std::vector<uint64>&); |
| 168 | 171 |
| 169 } // namespace picasaimport | 172 } // namespace picasaimport |
| OLD | NEW |