Commit 2d05d148 authored by jhawkins@chromium.org's avatar jhawkins@chromium.org

Sync/Valgrind: Add gmock printers for SyncChange, SyncData, SyncError.

Fixes a valgrind error in AutofillProfileSyncableServiceTest.ActOnChange
which uses gmock to mock a method that uses these classes. Due to
alignment issues, SyncData.is_valid_ has 3 bytes of uninitalized
padding. The default gmock printer prints all of these bytes, which
causes the uninit access.

BUG=none
TEST=Green memory waterfall

R=akalin@chromium.org


Review URL: http://codereview.chromium.org/7978044

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@102407 0039d316-1c4b-4281-b951-d872f2087c98
parent b0539755
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/sync/api/sync_change.h" #include "chrome/browser/sync/api/sync_change.h"
#include <ostream>
SyncChange::SyncChange() : change_type_(ACTION_INVALID) { SyncChange::SyncChange() : change_type_(ACTION_INVALID) {
} }
...@@ -59,3 +61,12 @@ std::string SyncChange::ChangeTypeToString(SyncChangeType change_type) { ...@@ -59,3 +61,12 @@ std::string SyncChange::ChangeTypeToString(SyncChangeType change_type) {
} }
return std::string(); return std::string();
} }
std::string SyncChange::ToString() const {
return "{ changeType: " + ChangeTypeToString(change_type_) +
", syncData: " + sync_data_.ToString() + "}";
}
void PrintTo(const SyncChange& sync_change, std::ostream* os) {
*os << sync_change.ToString();
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_SYNC_API_SYNC_CHANGE_H_ #define CHROME_BROWSER_SYNC_API_SYNC_CHANGE_H_
#pragma once #pragma once
#include <iosfwd>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -48,6 +49,10 @@ class SyncChange { ...@@ -48,6 +49,10 @@ class SyncChange {
// Returns a string representation of |change_type|. // Returns a string representation of |change_type|.
static std::string ChangeTypeToString(SyncChangeType change_type); static std::string ChangeTypeToString(SyncChangeType change_type);
// Returns a string representation of the entire object. Used for gmock
// printing method, PrintTo.
std::string ToString() const;
private: private:
SyncChangeType change_type_; SyncChangeType change_type_;
...@@ -56,4 +61,7 @@ class SyncChange { ...@@ -56,4 +61,7 @@ class SyncChange {
SyncData sync_data_; SyncData sync_data_;
}; };
// gmock printer helper.
void PrintTo(const SyncChange& sync_change, std::ostream* os);
#endif // CHROME_BROWSER_SYNC_API_SYNC_CHANGE_H_ #endif // CHROME_BROWSER_SYNC_API_SYNC_CHANGE_H_
...@@ -4,8 +4,16 @@ ...@@ -4,8 +4,16 @@
#include "chrome/browser/sync/api/sync_data.h" #include "chrome/browser/sync/api/sync_data.h"
#include <ostream>
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/sync/internal_api/base_node.h" #include "chrome/browser/sync/internal_api/base_node.h"
#include "chrome/browser/sync/protocol/proto_value_conversions.h"
#include "chrome/browser/sync/protocol/sync.pb.h" #include "chrome/browser/sync/protocol/sync.pb.h"
#include "chrome/browser/sync/syncable/model_type.h"
void SyncData::ImmutableSyncEntityTraits::InitializeWrapper( void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(
Wrapper* wrapper) { Wrapper* wrapper) {
...@@ -102,3 +110,27 @@ int64 SyncData::GetRemoteId() const { ...@@ -102,3 +110,27 @@ int64 SyncData::GetRemoteId() const {
bool SyncData::IsLocal() const { bool SyncData::IsLocal() const {
return id_ == sync_api::kInvalidId; return id_ == sync_api::kInvalidId;
} }
std::string SyncData::ToString() const {
if (!IsValid())
return "<Invalid SyncData>";
std::string type = syncable::ModelTypeToString(GetDataType());
std::string specifics;
scoped_ptr<DictionaryValue> value(
browser_sync::EntitySpecificsToValue(GetSpecifics()));
base::JSONWriter::Write(value.get(), true, &specifics);
if (IsLocal()) {
return "{ isLocal: true, type: " + type + ", tag: " + GetTag() +
", title: " + GetTitle() + ", specifics: " + specifics + "}";
}
std::string id = base::Int64ToString(GetRemoteId());
return "{ isLocal: false, type: " + type + ", specifics: " + specifics +
", id: " + id + "}";
}
void PrintTo(const SyncData& sync_data, std::ostream* os) {
*os << sync_data.ToString();
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_SYNC_API_SYNC_DATA_H_ #define CHROME_BROWSER_SYNC_API_SYNC_DATA_H_
#pragma once #pragma once
#include <iosfwd>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -75,6 +76,8 @@ class SyncData { ...@@ -75,6 +76,8 @@ class SyncData {
// Whether this sync data is for local data or data coming from the syncer. // Whether this sync data is for local data or data coming from the syncer.
bool IsLocal() const; bool IsLocal() const;
std::string ToString() const;
// TODO(zea): Query methods for other sync properties: parent, successor, etc. // TODO(zea): Query methods for other sync properties: parent, successor, etc.
private: private:
...@@ -111,4 +114,7 @@ class SyncData { ...@@ -111,4 +114,7 @@ class SyncData {
ImmutableSyncEntity immutable_entity_; ImmutableSyncEntity immutable_entity_;
}; };
// gmock printer helper.
void PrintTo(const SyncData& sync_data, std::ostream* os);
#endif // CHROME_BROWSER_SYNC_API_SYNC_DATA_H_ #endif // CHROME_BROWSER_SYNC_API_SYNC_DATA_H_
...@@ -4,8 +4,11 @@ ...@@ -4,8 +4,11 @@
#include "chrome/browser/sync/api/sync_error.h" #include "chrome/browser/sync/api/sync_error.h"
#include <ostream>
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/sync/syncable/model_type.h"
SyncError::SyncError() { SyncError::SyncError() {
Clear(); Clear();
...@@ -83,6 +86,15 @@ syncable::ModelType SyncError::type() const { ...@@ -83,6 +86,15 @@ syncable::ModelType SyncError::type() const {
return type_; return type_;
} }
std::string SyncError::ToString() const {
if (IsSet()) {
return "{ location: " + location_->ToString() + ", type: " +
syncable::ModelTypeToString(type()) + ", message: " + message() + "}";
}
return "<Unset SyncError>";
}
void SyncError::PrintLogError() const { void SyncError::PrintLogError() const {
LAZY_STREAM(logging::LogMessage(location_->file_name(), LAZY_STREAM(logging::LogMessage(location_->file_name(),
location_->line_number(), location_->line_number(),
...@@ -90,3 +102,7 @@ void SyncError::PrintLogError() const { ...@@ -90,3 +102,7 @@ void SyncError::PrintLogError() const {
LOG_IS_ON(ERROR)) LOG_IS_ON(ERROR))
<< syncable::ModelTypeToString(type_) << " Sync Error: " << message_; << syncable::ModelTypeToString(type_) << " Sync Error: " << message_;
} }
void PrintTo(const SyncError& sync_error, std::ostream* os) {
*os << sync_error.ToString();
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_SYNC_API_SYNC_ERROR_H_ #define CHROME_BROWSER_SYNC_API_SYNC_ERROR_H_
#pragma once #pragma once
#include <iosfwd>
#include <string> #include <string>
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
...@@ -53,6 +54,8 @@ class SyncError { ...@@ -53,6 +54,8 @@ class SyncError {
const std::string& message() const; const std::string& message() const;
syncable::ModelType type() const; syncable::ModelType type() const;
std::string ToString() const;
private: private:
// Print error information to log. // Print error information to log.
void PrintLogError() const; void PrintLogError() const;
...@@ -76,4 +79,7 @@ class SyncError { ...@@ -76,4 +79,7 @@ class SyncError {
syncable::ModelType type_; syncable::ModelType type_;
}; };
// gmock printer helper.
void PrintTo(const SyncError& sync_error, std::ostream* os);
#endif // CHROME_BROWSER_SYNC_API_SYNC_ERROR_H_ #endif // CHROME_BROWSER_SYNC_API_SYNC_ERROR_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment