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