OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <map> |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "base/metrics/statistics_recorder.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "content/public/browser/notification_registrar.h" |
| 11 #include "content/public/browser/notification_service.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // The tests that are run by this extension are expected to record the following |
| 16 // user actions, with the specified counts. If the tests in test.js are |
| 17 // modified, this array may need to be updated. |
| 18 struct RecordedUserAction { |
| 19 const char* name; |
| 20 int count; // number of times the metric was recorded. |
| 21 } g_user_actions[] = { |
| 22 {"test.ua.1", 1}, |
| 23 {"test.ua.2", 2}, |
| 24 }; |
| 25 |
| 26 // The tests that are run by this extension are expected to record the following |
| 27 // histograms. If the tests in test.js are modified, this array may need to be |
| 28 // updated. |
| 29 struct RecordedHistogram { |
| 30 const char* name; |
| 31 base::HistogramType type; |
| 32 int min; |
| 33 int max; |
| 34 size_t buckets; |
| 35 } g_histograms[] = { |
| 36 {"test.h.1", base::HISTOGRAM, 1, 100, 50}, // custom |
| 37 {"test.h.2", base::LINEAR_HISTOGRAM, 1, 200, 50}, // custom |
| 38 {"test.h.3", base::LINEAR_HISTOGRAM, 1, 101, 102}, // percentage |
| 39 {"test.time", base::HISTOGRAM, 1, 10000, 50}, |
| 40 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50}, |
| 41 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50}, |
| 42 {"test.count", base::HISTOGRAM, 1, 1000000, 50}, |
| 43 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50}, |
| 44 {"test.small.count", base::HISTOGRAM, 1, 100, 50}, |
| 45 }; |
| 46 |
| 47 // This class observes and collects user action notifications that are sent |
| 48 // by the tests, so that they can be examined afterwards for correctness. |
| 49 class UserActionObserver : public content::NotificationObserver { |
| 50 public: |
| 51 UserActionObserver(); |
| 52 |
| 53 void ValidateUserActions(const RecordedUserAction* recorded, int count); |
| 54 |
| 55 virtual void Observe(int type, |
| 56 const content::NotificationSource& source, |
| 57 const content::NotificationDetails& details); |
| 58 |
| 59 private: |
| 60 typedef std::map<std::string, int> UserActionCountMap; |
| 61 |
| 62 int num_metrics() const { |
| 63 return count_map_.size(); |
| 64 } |
| 65 |
| 66 int GetMetricCount(const std::string& name) const { |
| 67 UserActionCountMap::const_iterator i = count_map_.find(name); |
| 68 return i == count_map_.end() ? -1 : i->second; |
| 69 } |
| 70 |
| 71 content::NotificationRegistrar registrar_; |
| 72 UserActionCountMap count_map_; |
| 73 }; |
| 74 |
| 75 UserActionObserver::UserActionObserver() { |
| 76 registrar_.Add(this, content::NOTIFICATION_USER_ACTION, |
| 77 content::NotificationService::AllSources()); |
| 78 } |
| 79 |
| 80 void UserActionObserver::Observe(int type, |
| 81 const content::NotificationSource& source, |
| 82 const content::NotificationDetails& details) { |
| 83 const char* name = *content::Details<const char*>(details).ptr(); |
| 84 ++(count_map_[name]); |
| 85 } |
| 86 |
| 87 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded, |
| 88 int count) { |
| 89 EXPECT_EQ(count, num_metrics()); |
| 90 |
| 91 for (int i = 0; i < count; ++i) { |
| 92 const RecordedUserAction& ua = recorded[i]; |
| 93 EXPECT_EQ(ua.count, GetMetricCount(ua.name)); |
| 94 } |
| 95 } |
| 96 |
| 97 void ValidateHistograms(const RecordedHistogram* recorded, |
| 98 int count) { |
| 99 base::StatisticsRecorder::Histograms histograms; |
| 100 base::StatisticsRecorder::GetHistograms(&histograms); |
| 101 |
| 102 // Code other than the tests tun here will record some histogram values, but |
| 103 // we will ignore those. This function validates that all the histogram we |
| 104 // expect to see are present in the list, and that their basic info is |
| 105 // correct. |
| 106 for (int i = 0; i < count; ++i) { |
| 107 const RecordedHistogram& r = recorded[i]; |
| 108 size_t j = 0; |
| 109 for (j = 0; j < histograms.size(); ++j) { |
| 110 base::Histogram* histogram(histograms[j]); |
| 111 |
| 112 if (r.name == histogram->histogram_name()) { |
| 113 EXPECT_EQ(r.type, histogram->GetHistogramType()); |
| 114 EXPECT_EQ(r.min, histogram->declared_min()); |
| 115 EXPECT_EQ(r.max, histogram->declared_max()); |
| 116 EXPECT_EQ(r.buckets, histogram->bucket_count()); |
| 117 break; |
| 118 } |
| 119 } |
| 120 EXPECT_LT(j, histograms.size()); |
| 121 } |
| 122 } |
| 123 |
| 124 } // anonymous namespace |
| 125 |
| 126 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { |
| 127 UserActionObserver observer; |
| 128 |
| 129 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_; |
| 130 |
| 131 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions)); |
| 132 ValidateHistograms(g_histograms, arraysize(g_histograms)); |
| 133 } |
OLD | NEW |