OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/histogram.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/notification_registrar.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; // base name of metric without extension id. |
| 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; // base name of metric without extension id. |
| 31 Histogram::ClassType type; |
| 32 int min; |
| 33 int max; |
| 34 size_t buckets; |
| 35 } g_histograms[] = { |
| 36 {"test.h.1", Histogram::HISTOGRAM, 1, 100, 50}, // custom |
| 37 {"test.h.2", Histogram::LINEAR_HISTOGRAM, 1, 200, 50}, // custom |
| 38 {"test.h.3", Histogram::LINEAR_HISTOGRAM, 1, 101, 102}, // percentage |
| 39 {"test.time", Histogram::HISTOGRAM, 1, 10000, 50}, |
| 40 {"test.medium.time", Histogram::HISTOGRAM, 1, 180000, 50}, |
| 41 {"test.long.time", Histogram::HISTOGRAM, 1, 3600000, 50}, |
| 42 {"test.count", Histogram::HISTOGRAM, 1, 1000000, 50}, |
| 43 {"test.medium.count", Histogram::HISTOGRAM, 1, 10000, 50}, |
| 44 {"test.small.count", Histogram::HISTOGRAM, 1, 100, 50}, |
| 45 }; |
| 46 |
| 47 // Build the full name of a metrics for the given extension. Each metric |
| 48 // is made up of the unique name within the extension followed by the |
| 49 // extension's id. |
| 50 std::string BuildFullName(const std::string& name, const Extension* extension) { |
| 51 std::string full_name(name); |
| 52 full_name += extension->id(); |
| 53 return full_name; |
| 54 } |
| 55 |
| 56 // This class observes and collects user action notifications that are sent |
| 57 // by the tests, so that they can be examined afterwards for correctness. |
| 58 class UserActionObserver : public NotificationObserver { |
| 59 public: |
| 60 UserActionObserver(); |
| 61 |
| 62 void ValidateUserActions(const Extension* extension, |
| 63 const RecordedUserAction* recorded, |
| 64 int count); |
| 65 |
| 66 virtual void Observe(NotificationType type, |
| 67 const NotificationSource& source, |
| 68 const NotificationDetails& details); |
| 69 |
| 70 private: |
| 71 typedef std::map<std::string, int> UserActionCountMap; |
| 72 |
| 73 int num_metrics() const { |
| 74 return count_map_.size(); |
| 75 } |
| 76 |
| 77 int GetMetricCount(const std::string& name) const { |
| 78 UserActionCountMap::const_iterator i = count_map_.find(name); |
| 79 return i == count_map_.end() ? -1 : i->second; |
| 80 } |
| 81 |
| 82 NotificationRegistrar registrar_; |
| 83 UserActionCountMap count_map_; |
| 84 }; |
| 85 |
| 86 UserActionObserver::UserActionObserver() { |
| 87 registrar_.Add(this, NotificationType::USER_ACTION, |
| 88 NotificationService::AllSources()); |
| 89 } |
| 90 |
| 91 void UserActionObserver::Observe(NotificationType type, |
| 92 const NotificationSource& source, |
| 93 const NotificationDetails& details) { |
| 94 const char* name = *Details<const char*>(details).ptr(); |
| 95 ++(count_map_[name]); |
| 96 } |
| 97 |
| 98 void UserActionObserver::ValidateUserActions(const Extension* extension, |
| 99 const RecordedUserAction* recorded, |
| 100 int count) { |
| 101 EXPECT_EQ(count, num_metrics()); |
| 102 |
| 103 for (int i = 0; i < count; ++i) { |
| 104 const RecordedUserAction& ua = recorded[i]; |
| 105 EXPECT_EQ(ua.count, GetMetricCount(BuildFullName(ua.name, extension))); |
| 106 } |
| 107 } |
| 108 |
| 109 void ValidateHistograms(const Extension* extension, |
| 110 const RecordedHistogram* recorded, |
| 111 int count) { |
| 112 StatisticsRecorder::Histograms histograms; |
| 113 StatisticsRecorder::GetHistograms(&histograms); |
| 114 |
| 115 // Code other than the tests tun here will record some histogram values, but |
| 116 // we will ignore those. This function validates that all the histogram we |
| 117 // expect to see are present in the list, and that their basic info is |
| 118 // correct. |
| 119 for (int i = 0; i < count; ++i) { |
| 120 const RecordedHistogram& r = recorded[i]; |
| 121 std::string name(BuildFullName(r.name, extension)); |
| 122 |
| 123 size_t j = 0; |
| 124 for (j = 0; j < histograms.size(); ++j) { |
| 125 scoped_refptr<Histogram> histogram(histograms[j]); |
| 126 |
| 127 if (name == histogram->histogram_name()) { |
| 128 EXPECT_EQ(r.type, histogram->histogram_type()); |
| 129 EXPECT_EQ(r.min, histogram->declared_min()); |
| 130 EXPECT_EQ(r.max, histogram->declared_max()); |
| 131 EXPECT_EQ(r.buckets, histogram->bucket_count()); |
| 132 break; |
| 133 } |
| 134 } |
| 135 EXPECT_LT(j, histograms.size()); |
| 136 } |
| 137 } |
| 138 |
| 139 } // anonymous namespace |
| 140 |
| 141 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { |
| 142 UserActionObserver observer; |
| 143 |
| 144 ASSERT_TRUE(RunExtensionTest("metrics")) << message_; |
| 145 Extension* extension = GetSingleLoadedExtension(); |
| 146 ASSERT_TRUE(extension); |
| 147 |
| 148 observer.ValidateUserActions(extension, |
| 149 g_user_actions, |
| 150 arraysize(g_user_actions)); |
| 151 ValidateHistograms(extension, g_histograms, arraysize(g_histograms)); |
| 152 } |
OLD | NEW |