Chromium Code Reviews| Index: base/metrics/metrics_utils_unittest_chromeos.cc |
| diff --git a/base/metrics/metrics_utils_unittest_chromeos.cc b/base/metrics/metrics_utils_unittest_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f5620542ce7bc8148ef75b44825c94a04a5f976b |
| --- /dev/null |
| +++ b/base/metrics/metrics_utils_unittest_chromeos.cc |
| @@ -0,0 +1,172 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
|
Alexei Svitkine (slow)
2014/04/17 16:33:21
Nit: Remove (c) from copyright strings for new fil
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Test of crash class |
|
Alexei Svitkine (slow)
2014/04/17 16:33:21
Remove this comment. Make the metrics_utils_chrome
|
| + |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/metrics/crash_sample_chromeos.h" |
| +#include "base/metrics/histogram_sample_chromeos.h" |
| +#include "base/metrics/linearhistogram_sample_chromeos.h" |
| +#include "base/metrics/metric_sample_chromeos.h" |
| +#include "base/metrics/metrics_utils_chromeos.h" |
| +#include "base/metrics/sparsehistogram_sample_chromeos.h" |
| +#include "base/metrics/useraction_sample_chromeos.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace base { |
| +namespace { |
| +using MetricsUtils::ReadSample; |
| + |
| +void AreEqHistogram(HistogramSample* a, HistogramSample* b) { |
| + ASSERT_EQ(a->name(), b->name()); |
| + ASSERT_EQ(a->sample(), b->sample()); |
| + ASSERT_EQ(a->min(), b->min()); |
| + ASSERT_EQ(a->max(), b->max()); |
| + ASSERT_EQ(a->nbucket(), b->nbucket()); |
| +} |
| + |
| +void AreEqCrash(CrashSample* a, CrashSample* b) { |
| + ASSERT_EQ(a->name(), b->name()); |
| +} |
| + |
| +void AreEqLinear(LinearHistogramSample* a, LinearHistogramSample* b) { |
| + ASSERT_EQ(a->name(), b->name()); |
| + ASSERT_EQ(a->sample(), b->sample()); |
| + ASSERT_EQ(a->max(), b->max()); |
| +} |
| + |
| +void AreEqSparse(SparseHistogramSample* a, SparseHistogramSample* b) { |
| + ASSERT_EQ(a->name(), b->name()); |
| + ASSERT_EQ(a->sample(), b->sample()); |
| +} |
| + |
| +void AreEqAction(UserActionSample* a, UserActionSample* b) { |
| + ASSERT_EQ(a->name(), b->name()); |
| +} |
| + |
| +void AreEq(MetricSample* a, MetricSample* b) { |
| + ASSERT_TRUE(a); |
| + ASSERT_TRUE(b); |
| + MetricSample::SampleType type = a->type(); |
| + ASSERT_EQ(a->type(), b->type()); |
| + if (type == MetricSample::CRASH) { |
| + AreEqCrash(reinterpret_cast<CrashSample*>(a), |
| + reinterpret_cast<CrashSample*>(b)); |
| + } else if (type == MetricSample::HISTOGRAM) { |
| + AreEqHistogram(reinterpret_cast<HistogramSample*>(a), |
| + reinterpret_cast<HistogramSample*>(b)); |
| + } else if (type == MetricSample::SPARSE_HISTOGRAM) { |
| + AreEqSparse(reinterpret_cast<SparseHistogramSample*>(a), |
| + reinterpret_cast<SparseHistogramSample*>(b)); |
| + } else if (type == MetricSample::LINEAR_HISTOGRAM) { |
| + AreEqLinear(reinterpret_cast<LinearHistogramSample*>(a), |
| + reinterpret_cast<LinearHistogramSample*>(b)); |
| + } else if (type == MetricSample::USER_ACTION) { |
| + AreEqAction(reinterpret_cast<UserActionSample*>(a), |
| + reinterpret_cast<UserActionSample*>(b)); |
| + } else { |
| + DLOG(ERROR) << "could not recognize type"; |
| + FAIL(); |
| + } |
| +} |
| + |
| +class MetricsUtilsChromeOSTest : public testing::Test { |
| + protected: |
| + static const int buff_size = 1024; |
| + char* buffer; |
| + std::string filename; |
| + FilePath filepath; |
| + MetricsUtilsChromeOSTest() { |
| + filename = "/tmp/chromeossampletest"; |
| + filepath = FilePath(filename); |
| + } |
| + virtual void SetUp() { |
| + DeleteFile(filepath, false); |
| + buffer = new char[buff_size]; |
| + } |
| + |
| + void TestSerialization(MetricSample* sample) { |
| + string serialized(sample->ToString()); |
| + ASSERT_EQ('\0', serialized[serialized.length() - 1]); |
| + AreEq(sample, MetricsUtils::ReadSample(serialized)); |
| + } |
| +}; |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, CrashSerializeTest) { |
| + CrashSample crash("test"); |
| + TestSerialization(&crash); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, HistogramSerializeTest) { |
| + HistogramSample hist("myhist", 13, 1, 100, 10); |
| + TestSerialization(&hist); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, LinearSerializeTest) { |
| + LinearHistogramSample lhist("linearhist", 12, 30); |
| + TestSerialization(&lhist); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, SparseSerializeTest) { |
| + SparseHistogramSample shist("mysparse", 30); |
| + TestSerialization(&shist); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, UserActionSerializeTest) { |
| + TestSerialization(new UserActionSample("myaction")); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, IllegalNameAreFilteredTest) { |
| + SparseHistogramSample sample1("no space", 10); |
| + LinearHistogramSample sample2(StringPrintf("here%cbhe", '\0'), 1, 3); |
| + EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample1, filename)); |
| + EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample2, filename)); |
| + int64 size = GetFileSize(filepath, &size); |
| + |
| + EXPECT_EQ(0, size); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, BadInputIsCaughtTest) { |
| + string input(StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); |
| + EXPECT_EQ(NULL, SparseHistogramSample::ReadSparseHistogram(input)); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, MessageSeparatedByZero) { |
| + CrashSample crash("crash"); |
| + |
| + MetricsUtils::WriteMetricToFile(crash, filename); |
| + int64 size; |
| + GetFileSize(filepath, &size); |
| + ASSERT_EQ(size, 16); |
| +} |
| + |
| +TEST_F(MetricsUtilsChromeOSTest, WriteReadTest) { |
| + HistogramSample hist("myhist", 1, 2, 3, 4); |
| + CrashSample crash("crash"); |
| + LinearHistogramSample lhist("linear", 1, 10); |
| + SparseHistogramSample shist("mysparse", 30); |
| + UserActionSample action("myaction"); |
| + |
| + MetricsUtils::WriteMetricToFile(hist, filename); |
| + MetricsUtils::WriteMetricToFile(crash, filename); |
| + MetricsUtils::WriteMetricToFile(lhist, filename); |
| + MetricsUtils::WriteMetricToFile(shist, filename); |
| + MetricsUtils::WriteMetricToFile(action, filename); |
| + ScopedVector<MetricSample> vect; |
| + MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &vect); |
| + ASSERT_EQ(vect.size(), size_t(5)); |
| + AreEq(&hist, vect[0]); |
| + AreEq(&crash, vect[1]); |
| + AreEq(&lhist, vect[2]); |
| + AreEq(&shist, vect[3]); |
| + AreEq(&action, vect[4]); |
| + |
| + int64 size; |
| + GetFileSize(filepath, &size); |
| + ASSERT_EQ(0, size); |
| +} |
| +} // namespace |
| +} // namespace base |