summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-01 01:38:35 +0000
committertommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-01 01:38:35 +0000
commitc25947b5440db4853091eefe1ef996225a9e92ec (patch)
treee9790f79b4360a87b6d89b6c359ff1e846c92f96
parent0acd9cecd100eff0a42c3bff62c68e1735adf0f7 (diff)
downloadchromium_src-c25947b5440db4853091eefe1ef996225a9e92ec.zip
chromium_src-c25947b5440db4853091eefe1ef996225a9e92ec.tar.gz
chromium_src-c25947b5440db4853091eefe1ef996225a9e92ec.tar.bz2
Media Galleries API Picasa: Move PMP test helper to chrome/common/.
Moving a PMP test helper to common because the PicasaDataProvider end-to-end test resides in browser and needs to use the PMP test helper. BUG=151701 Review URL: https://chromiumcodereview.appspot.com/21249004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@214911 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/chrome_tests_unit.gypi4
-rw-r--r--chrome/common/media_galleries/pmp_test_helper.cc (renamed from chrome/utility/media_galleries/pmp_test_helper.cc)78
-rw-r--r--chrome/common/media_galleries/pmp_test_helper.h (renamed from chrome/utility/media_galleries/pmp_test_helper.h)18
-rw-r--r--chrome/utility/media_galleries/picasa_album_table_reader_unittest.cc2
-rw-r--r--chrome/utility/media_galleries/pmp_column_reader_unittest.cc160
5 files changed, 125 insertions, 137 deletions
diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi
index 5d4910b..00ae883b 100644
--- a/chrome/chrome_tests_unit.gypi
+++ b/chrome/chrome_tests_unit.gypi
@@ -376,8 +376,8 @@
}],
['OS=="win" or OS=="mac"', {
'sources': [
- 'utility/media_galleries/pmp_test_helper.cc',
- 'utility/media_galleries/pmp_test_helper.h',
+ 'common/media_galleries/pmp_test_helper.cc',
+ 'common/media_galleries/pmp_test_helper.h',
],
}],
['OS=="mac"', {
diff --git a/chrome/utility/media_galleries/pmp_test_helper.cc b/chrome/common/media_galleries/pmp_test_helper.cc
index edda31f..1246493 100644
--- a/chrome/utility/media_galleries/pmp_test_helper.cc
+++ b/chrome/common/media_galleries/pmp_test_helper.cc
@@ -2,45 +2,35 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/utility/media_galleries/pmp_test_helper.h"
+#include "chrome/common/media_galleries/pmp_test_helper.h"
#include <algorithm>
#include <iterator>
#include "base/file_util.h"
#include "base/logging.h"
-#include "base/platform_file.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/media_galleries/picasa_types.h"
-#include "chrome/utility/media_galleries/pmp_column_reader.h"
namespace picasa {
namespace {
-bool WriteToFile(const base::FilePath& path, std::vector<uint8> data) {
- // Cast for usage in WriteFile function
- const char* data_char = reinterpret_cast<const char*>(&data[0]);
- size_t bytes_written = file_util::WriteFile(path, data_char, data.size());
- return (bytes_written == data.size());
-}
-
// Flatten a vector of elements into an array of bytes.
template<class T>
-std::vector<uint8> Flatten(const std::vector<T>& elems) {
+std::vector<char> Flatten(const std::vector<T>& elems) {
if (elems.empty())
- return std::vector<uint8>();
+ return std::vector<char>();
const uint8* elems0 = reinterpret_cast<const uint8*>(&elems[0]);
- std::vector<uint8> data_body(elems0, elems0 + sizeof(T) * elems.size());
-
+ std::vector<char> data_body(elems0, elems0 + sizeof(T) * elems.size());
return data_body;
}
// Custom specialization for std::string.
template<>
-std::vector<uint8> Flatten(const std::vector<std::string>& strings) {
- std::vector<uint8> totalchars;
+std::vector<char> Flatten(const std::vector<std::string>& strings) {
+ std::vector<char> totalchars;
for (std::vector<std::string>::const_iterator it = strings.begin();
it != strings.end(); ++it) {
@@ -52,9 +42,9 @@ std::vector<uint8> Flatten(const std::vector<std::string>& strings) {
}
// Returns a new vector with the concatenated contents of |a| and |b|.
-std::vector<uint8> CombinedVectors(const std::vector<uint8>& a,
- const std::vector<uint8>& b) {
- std::vector<uint8> total;
+std::vector<char> CombinedVectors(const std::vector<char>& a,
+ const std::vector<char>& b) {
+ std::vector<char> total;
std::copy(a.begin(), a.end(), std::back_inserter(total));
std::copy(b.begin(), b.end(), std::back_inserter(total));
@@ -94,10 +84,11 @@ bool PmpTestHelper::WriteColumnFileFromVector(
base::FilePath path = temp_dir_.path().Append(
base::FilePath::FromUTF8Unsafe(file_name));
- std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody(
+ std::vector<char> data = PmpTestHelper::MakeHeaderAndBody(
field_type, elements_vector.size(), elements_vector);
- return WriteToFile(path, data);
+ size_t bytes_written = file_util::WriteFile(path, &data[0], data.size());
+ return (bytes_written == data.size());
}
// Explicit Instantiation for all the valid types.
@@ -112,37 +103,10 @@ template bool PmpTestHelper::WriteColumnFileFromVector<uint8>(
template bool PmpTestHelper::WriteColumnFileFromVector<uint64>(
const std::string&, const PmpFieldType, const std::vector<uint64>&);
-bool PmpTestHelper::InitColumnReaderFromBytes(
- PmpColumnReader* const reader,
- const std::vector<uint8>& data,
- const PmpFieldType expected_type) {
- DCHECK(temp_dir_.IsValid());
-
- base::FilePath temp_path;
-
- if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path)
- || !WriteToFile(temp_path, data)) {
- return false;
- }
-
- int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
- base::PlatformFile platform_file =
- base::CreatePlatformFile(temp_path, flags, NULL, NULL);
- if (platform_file == base::kInvalidPlatformFileValue)
- return false;
-
- bool read_success = reader->ReadFile(platform_file, expected_type);
-
- base::ClosePlatformFile(platform_file);
- base::DeleteFile(temp_path, false /* recursive */);
-
- return read_success;
-}
-
// Return a vector so we don't have to worry about memory management.
-std::vector<uint8> PmpTestHelper::MakeHeader(const PmpFieldType field_type,
- const uint32 row_count) {
- std::vector<uint8> header(picasa::kPmpHeaderSize);
+std::vector<char> PmpTestHelper::MakeHeader(const PmpFieldType field_type,
+ const uint32 row_count) {
+ std::vector<char> header(picasa::kPmpHeaderSize);
// Copy in magic bytes.
memcpy(&header[picasa::kPmpMagic1Offset], &picasa::kPmpMagic1,
@@ -168,7 +132,7 @@ std::vector<uint8> PmpTestHelper::MakeHeader(const PmpFieldType field_type,
}
template<class T>
-std::vector<uint8> PmpTestHelper::MakeHeaderAndBody(
+std::vector<char> PmpTestHelper::MakeHeaderAndBody(
const PmpFieldType field_type, const uint32 row_count,
const std::vector<T>& elems) {
return CombinedVectors(PmpTestHelper::MakeHeader(field_type, row_count),
@@ -176,15 +140,15 @@ std::vector<uint8> PmpTestHelper::MakeHeaderAndBody(
}
// Explicit Instantiation for all the valid types.
-template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>(
+template std::vector<char> PmpTestHelper::MakeHeaderAndBody<std::string>(
const PmpFieldType, const uint32, const std::vector<std::string>&);
-template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>(
+template std::vector<char> PmpTestHelper::MakeHeaderAndBody<uint32>(
const PmpFieldType, const uint32, const std::vector<uint32>&);
-template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>(
+template std::vector<char> PmpTestHelper::MakeHeaderAndBody<double>(
const PmpFieldType, const uint32, const std::vector<double>&);
-template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>(
+template std::vector<char> PmpTestHelper::MakeHeaderAndBody<uint8>(
const PmpFieldType, const uint32, const std::vector<uint8>&);
-template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>(
+template std::vector<char> PmpTestHelper::MakeHeaderAndBody<uint64>(
const PmpFieldType, const uint32, const std::vector<uint64>&);
} // namespace picasa
diff --git a/chrome/utility/media_galleries/pmp_test_helper.h b/chrome/common/media_galleries/pmp_test_helper.h
index 2878efe..2de6452 100644
--- a/chrome/utility/media_galleries/pmp_test_helper.h
+++ b/chrome/common/media_galleries/pmp_test_helper.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_UTILITY_MEDIA_GALLERIES_PMP_TEST_HELPER_H_
-#define CHROME_UTILITY_MEDIA_GALLERIES_PMP_TEST_HELPER_H_
+#ifndef CHROME_COMMON_MEDIA_GALLERIES_PMP_TEST_HELPER_H_
+#define CHROME_COMMON_MEDIA_GALLERIES_PMP_TEST_HELPER_H_
#include <string>
#include <vector>
@@ -34,17 +34,13 @@ class PmpTestHelper {
const PmpFieldType field_type,
const std::vector<T>& elements_vector);
- bool InitColumnReaderFromBytes(PmpColumnReader* const reader,
- const std::vector<uint8>& data,
- const PmpFieldType expected_type);
-
- static std::vector<uint8> MakeHeader(const PmpFieldType field_type,
+ static std::vector<char> MakeHeader(const PmpFieldType field_type,
const uint32 row_count);
template<class T>
- static std::vector<uint8> MakeHeaderAndBody(const PmpFieldType field_type,
- const uint32 row_count,
- const std::vector<T>& elems);
+ static std::vector<char> MakeHeaderAndBody(const PmpFieldType field_type,
+ const uint32 row_count,
+ const std::vector<T>& elems);
private:
std::string table_name_;
@@ -53,4 +49,4 @@ class PmpTestHelper {
} // namespace picasa
-#endif // CHROME_UTILITY_MEDIA_GALLERIES_PMP_TEST_HELPER_H_
+#endif // CHROME_COMMON_MEDIA_GALLERIES_PMP_TEST_HELPER_H_
diff --git a/chrome/utility/media_galleries/picasa_album_table_reader_unittest.cc b/chrome/utility/media_galleries/picasa_album_table_reader_unittest.cc
index 3c5991c..fe9b828 100644
--- a/chrome/utility/media_galleries/picasa_album_table_reader_unittest.cc
+++ b/chrome/utility/media_galleries/picasa_album_table_reader_unittest.cc
@@ -3,8 +3,8 @@
// found in the LICENSE file.
#include "chrome/common/media_galleries/pmp_constants.h"
+#include "chrome/common/media_galleries/pmp_test_helper.h"
#include "chrome/utility/media_galleries/picasa_album_table_reader.h"
-#include "chrome/utility/media_galleries/pmp_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace picasa {
diff --git a/chrome/utility/media_galleries/pmp_column_reader_unittest.cc b/chrome/utility/media_galleries/pmp_column_reader_unittest.cc
index a9cfa5b..a1f44b7 100644
--- a/chrome/utility/media_galleries/pmp_column_reader_unittest.cc
+++ b/chrome/utility/media_galleries/pmp_column_reader_unittest.cc
@@ -5,15 +5,47 @@
#include <algorithm>
#include <vector>
+#include "base/file_util.h"
+#include "base/files/scoped_temp_dir.h"
+#include "base/platform_file.h"
#include "chrome/common/media_galleries/pmp_constants.h"
+#include "chrome/common/media_galleries/pmp_test_helper.h"
#include "chrome/utility/media_galleries/pmp_column_reader.h"
-#include "chrome/utility/media_galleries/pmp_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
+namespace picasa {
+
namespace {
-using picasa::PmpColumnReader;
-using picasa::PmpTestHelper;
+bool InitColumnReaderFromBytes(
+ PmpColumnReader* const reader,
+ const std::vector<char>& data,
+ const PmpFieldType expected_type) {
+ base::ScopedTempDir temp_dir;
+ if (!temp_dir.CreateUniqueTempDir())
+ return false;
+
+ base::FilePath temp_path;
+ if (!file_util::CreateTemporaryFileInDir(temp_dir.path(), &temp_path))
+ return false;
+
+ // Explicit conversion from signed to unsigned.
+ size_t bytes_written = file_util::WriteFile(temp_path, &data[0], data.size());
+ if (bytes_written != data.size())
+ return false;
+
+ int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
+ base::PlatformFile platform_file =
+ base::CreatePlatformFile(temp_path, flags, NULL, NULL);
+ if (platform_file == base::kInvalidPlatformFileValue)
+ return false;
+
+ bool read_success = reader->ReadFile(platform_file, expected_type);
+
+ base::ClosePlatformFile(platform_file);
+
+ return read_success;
+}
// Overridden version of Read method to make test code templatable.
bool DoRead(const PmpColumnReader* reader, uint32 row, std::string* target) {
@@ -38,16 +70,15 @@ bool DoRead(const PmpColumnReader* reader, uint32 row, uint64* target) {
// TestValid
template<class T>
-void TestValid(const picasa::PmpFieldType field_type,
+void TestValid(const PmpFieldType field_type,
const std::vector<T>& elems) {
PmpTestHelper test_helper("test");
ASSERT_TRUE(test_helper.Init());
PmpColumnReader reader;
- std::vector<uint8> data =
+ std::vector<char> data =
PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
- ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(
- &reader, data, field_type));
+ ASSERT_TRUE(InitColumnReaderFromBytes(&reader, data, field_type));
EXPECT_EQ(elems.size(), reader.rows_read());
for (uint32 i = 0; i < elems.size() && i < reader.rows_read(); i++) {
@@ -58,44 +89,44 @@ void TestValid(const picasa::PmpFieldType field_type,
}
template<class T>
-void TestMalformed(const picasa::PmpFieldType field_type,
+void TestMalformed(const PmpFieldType field_type,
const std::vector<T>& elems) {
PmpTestHelper test_helper("test");
ASSERT_TRUE(test_helper.Init());
PmpColumnReader reader_too_few_declared_rows;
- std::vector<uint8> data_too_few_declared_rows =
+ std::vector<char> data_too_few_declared_rows =
PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems);
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_too_few_declared_rows,
- data_too_few_declared_rows,
- field_type));
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_too_few_declared_rows,
+ data_too_few_declared_rows,
+ field_type));
PmpColumnReader reader_too_many_declared_rows;
- std::vector<uint8> data_too_many_declared_rows =
+ std::vector<char> data_too_many_declared_rows =
PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems);
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_too_many_declared_rows,
- data_too_many_declared_rows,
- field_type));
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_too_many_declared_rows,
+ data_too_many_declared_rows,
+ field_type));
PmpColumnReader reader_truncated;
- std::vector<uint8> data_truncated =
+ std::vector<char> data_truncated =
PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
data_truncated.resize(data_truncated.size()-10);
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_truncated, data_truncated, field_type));
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_truncated,
+ data_truncated,
+ field_type));
PmpColumnReader reader_padded;
- std::vector<uint8> data_padded =
+ std::vector<char> data_padded =
PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
data_padded.resize(data_padded.size()+10);
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_padded, data_padded, field_type));
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_padded,
+ data_padded,
+ field_type));
}
template<class T>
-void TestPrimitive(const picasa::PmpFieldType field_type) {
+void TestPrimitive(const PmpFieldType field_type) {
// Make an ascending vector of the primitive.
uint32 n = 100;
std::vector<T> data(n, 0);
@@ -113,58 +144,53 @@ TEST(PmpColumnReaderTest, HeaderParsingAndValidation) {
ASSERT_TRUE(test_helper.Init());
PmpColumnReader reader_good_header;
- std::vector<uint8> good_header =
- PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
- EXPECT_TRUE(test_helper.InitColumnReaderFromBytes(
- &reader_good_header,
- good_header,
- picasa::PMP_TYPE_STRING));
+ std::vector<char> good_header =
+ PmpTestHelper::MakeHeader(PMP_TYPE_STRING, 0);
+ EXPECT_TRUE(InitColumnReaderFromBytes(&reader_good_header,
+ good_header,
+ PMP_TYPE_STRING));
EXPECT_EQ(0U, reader_good_header.rows_read()) <<
"Read non-zero rows from header-only data.";
PmpColumnReader reader_bad_magic_bytes;
- std::vector<uint8> bad_magic_bytes =
- PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
- bad_magic_bytes[0] = 0xff;
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_bad_magic_bytes,
- bad_magic_bytes,
- picasa::PMP_TYPE_STRING));
+ std::vector<char> bad_magic_bytes =
+ PmpTestHelper::MakeHeader(PMP_TYPE_STRING, 0);
+ bad_magic_bytes[0] = (char)0xff;
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_bad_magic_bytes,
+ bad_magic_bytes,
+ PMP_TYPE_STRING));
PmpColumnReader reader_inconsistent_types;
- std::vector<uint8> inconsistent_type =
- PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
- inconsistent_type[picasa::kPmpFieldType1Offset] = 0xff;
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_inconsistent_types,
- inconsistent_type,
- picasa::PMP_TYPE_STRING));
+ std::vector<char> inconsistent_type =
+ PmpTestHelper::MakeHeader(PMP_TYPE_STRING, 0);
+ inconsistent_type[kPmpFieldType1Offset] = (char)0xff;
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_inconsistent_types,
+ inconsistent_type,
+ PMP_TYPE_STRING));
PmpColumnReader reader_invalid_type;
- std::vector<uint8> invalid_type =
- PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
- invalid_type[picasa::kPmpFieldType1Offset] = 0xff;
- invalid_type[picasa::kPmpFieldType2Offset] = 0xff;
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_invalid_type,
- invalid_type,
- picasa::PMP_TYPE_STRING));
+ std::vector<char> invalid_type =
+ PmpTestHelper::MakeHeader(PMP_TYPE_STRING, 0);
+ invalid_type[kPmpFieldType1Offset] = (char)0xff;
+ invalid_type[kPmpFieldType2Offset] = (char)0xff;
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_invalid_type,
+ invalid_type,
+ PMP_TYPE_STRING));
PmpColumnReader reader_incomplete_header;
- std::vector<uint8> incomplete_header =
- PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
+ std::vector<char> incomplete_header =
+ PmpTestHelper::MakeHeader(PMP_TYPE_STRING, 0);
incomplete_header.resize(10);
- EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
- &reader_incomplete_header,
- incomplete_header,
- picasa::PMP_TYPE_STRING));
+ EXPECT_FALSE(InitColumnReaderFromBytes(&reader_incomplete_header,
+ incomplete_header,
+ PMP_TYPE_STRING));
}
TEST(PmpColumnReaderTest, StringParsing) {
std::vector<std::string> empty_strings(100, "");
// Test empty strings read okay.
- TestValid(picasa::PMP_TYPE_STRING, empty_strings);
+ TestValid(PMP_TYPE_STRING, empty_strings);
std::vector<std::string> mixed_strings;
mixed_strings.push_back("");
@@ -176,17 +202,19 @@ TEST(PmpColumnReaderTest, StringParsing) {
mixed_strings.push_back("");
// Test that a mixed set of strings read correctly.
- TestValid(picasa::PMP_TYPE_STRING, mixed_strings);
+ TestValid(PMP_TYPE_STRING, mixed_strings);
// Test with the data messed up in a variety of ways.
- TestMalformed(picasa::PMP_TYPE_STRING, mixed_strings);
+ TestMalformed(PMP_TYPE_STRING, mixed_strings);
}
TEST(PmpColumnReaderTest, PrimitiveParsing) {
- TestPrimitive<uint32>(picasa::PMP_TYPE_UINT32);
- TestPrimitive<double>(picasa::PMP_TYPE_DOUBLE64);
- TestPrimitive<uint8>(picasa::PMP_TYPE_UINT8);
- TestPrimitive<uint64>(picasa::PMP_TYPE_UINT64);
+ TestPrimitive<uint32>(PMP_TYPE_UINT32);
+ TestPrimitive<double>(PMP_TYPE_DOUBLE64);
+ TestPrimitive<uint8>(PMP_TYPE_UINT8);
+ TestPrimitive<uint64>(PMP_TYPE_UINT64);
}
} // namespace
+
+} // namespace picasa