summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/external_metrics_unittest.cc
blob: 9a7a545077e7e235ae7deb47171e692980ed0591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/statistics_recorder.h"
#include "chrome/browser/chromeos/external_metrics.h"
#include "chrome/test/base/uma_histogram_helper.h"
#include "components/metrics/chromeos/metric_sample.h"
#include "components/metrics/chromeos/serialization_utils.h"
#include "content/public/test/test_browser_thread_bundle.h"

namespace chromeos {  // Need this because of the FRIEND_TEST

class ExternalMetricsTest : public testing::Test {
 public:
  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(dir_.CreateUniqueTempDir());
    external_metrics_ = ExternalMetrics::CreateForTesting(
        dir_.path().Append("testfile").value());

    base::StatisticsRecorder::Initialize();
  }

  base::ScopedTempDir dir_;
  scoped_refptr<ExternalMetrics> external_metrics_;
  content::TestBrowserThreadBundle thread_bundle_;
};

TEST_F(ExternalMetricsTest, HandleMissingFile) {
  ASSERT_TRUE(base::DeleteFile(
      base::FilePath(external_metrics_->uma_events_file_), false));

  EXPECT_EQ(0, external_metrics_->CollectEvents());
}

TEST_F(ExternalMetricsTest, CanReceiveHistogram) {
  scoped_ptr<metrics::MetricSample> hist =
      metrics::MetricSample::HistogramSample("foo", 2, 1, 100, 10);

  EXPECT_TRUE(metrics::SerializationUtils::WriteMetricToFile(
      *hist.get(), external_metrics_->uma_events_file_));

  EXPECT_EQ(1, external_metrics_->CollectEvents());

  UMAHistogramHelper helper;
  helper.Fetch();
  helper.ExpectTotalCount("foo", 1);
}

TEST_F(ExternalMetricsTest, IncorrectHistogramsAreDiscarded) {
  // Malformed histogram (min > max).
  scoped_ptr<metrics::MetricSample> hist =
      metrics::MetricSample::HistogramSample("bar", 30, 200, 20, 10);

  EXPECT_TRUE(metrics::SerializationUtils::WriteMetricToFile(
      *hist.get(), external_metrics_->uma_events_file_));

  external_metrics_->CollectEvents();

  UMAHistogramHelper helper;
  helper.Fetch();
  helper.ExpectTotalCount("bar", 0);
}

}  // namespace chromeos