This source file includes following definitions.
- TEST
- TEST
#include "chrome/common/metrics/metrics_log_base.h"
#include <string>
#include "base/base64.h"
#include "base/metrics/bucket_ranges.h"
#include "base/metrics/sample_vector.h"
#include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class TestMetricsLogBase : public MetricsLogBase {
public:
TestMetricsLogBase() : MetricsLogBase("client_id", 1, "1.2.3.4") {}
virtual ~TestMetricsLogBase() {}
using MetricsLogBase::uma_proto;
private:
DISALLOW_COPY_AND_ASSIGN(TestMetricsLogBase);
};
}
TEST(MetricsLogBaseTest, EmptyRecord) {
MetricsLogBase log("totally bogus client ID", 137, "bogus version");
log.set_hardware_class("sample-class");
log.CloseLog();
std::string encoded;
log.GetEncodedLog(&encoded);
metrics::ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
metrics::ChromeUserMetricsExtension expected;
expected.set_client_id(5217101509553811875);
expected.set_session_id(137);
expected.mutable_system_profile()->set_build_timestamp(
parsed.system_profile().build_timestamp());
expected.mutable_system_profile()->set_app_version("bogus version");
expected.mutable_system_profile()->set_channel(
parsed.system_profile().channel());
expected.mutable_system_profile()->mutable_hardware()->set_hardware_class(
"sample-class");
EXPECT_EQ(expected.SerializeAsString(), encoded);
}
TEST(MetricsLogBaseTest, HistogramBucketFields) {
base::BucketRanges ranges(8);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
ranges.set_range(2, 7);
ranges.set_range(3, 8);
ranges.set_range(4, 9);
ranges.set_range(5, 10);
ranges.set_range(6, 11);
ranges.set_range(7, 12);
base::SampleVector samples(&ranges);
samples.Accumulate(3, 1);
samples.Accumulate(6, 1);
samples.Accumulate(8, 1);
samples.Accumulate(10, 1);
samples.Accumulate(11, 1);
TestMetricsLogBase log;
log.RecordHistogramDelta("Test", samples);
const metrics::ChromeUserMetricsExtension* uma_proto = log.uma_proto();
const metrics::HistogramEventProto& histogram_proto =
uma_proto->histogram_event(uma_proto->histogram_event_size() - 1);
ASSERT_EQ(5, histogram_proto.bucket_size());
EXPECT_TRUE(histogram_proto.bucket(0).has_min());
EXPECT_FALSE(histogram_proto.bucket(0).has_max());
EXPECT_EQ(1, histogram_proto.bucket(0).min());
EXPECT_TRUE(histogram_proto.bucket(1).has_min());
EXPECT_TRUE(histogram_proto.bucket(1).has_max());
EXPECT_EQ(5, histogram_proto.bucket(1).min());
EXPECT_EQ(7, histogram_proto.bucket(1).max());
EXPECT_FALSE(histogram_proto.bucket(2).has_min());
EXPECT_TRUE(histogram_proto.bucket(2).has_max());
EXPECT_EQ(9, histogram_proto.bucket(2).max());
EXPECT_TRUE(histogram_proto.bucket(3).has_min());
EXPECT_FALSE(histogram_proto.bucket(3).has_max());
EXPECT_EQ(10, histogram_proto.bucket(3).min());
EXPECT_FALSE(histogram_proto.bucket(4).has_min());
EXPECT_TRUE(histogram_proto.bucket(4).has_max());
EXPECT_EQ(12, histogram_proto.bucket(4).max());
}