| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "content/public/browser/notification_registrar.h" | 10 #include "content/public/browser/user_metrics.h" |
| 11 #include "content/public/browser/notification_service.h" | |
| 12 | 11 |
| 13 namespace { | 12 namespace { |
| 14 | 13 |
| 15 // The tests that are run by this extension are expected to record the following | 14 // 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 | 15 // user actions, with the specified counts. If the tests in test.js are |
| 17 // modified, this array may need to be updated. | 16 // modified, this array may need to be updated. |
| 18 struct RecordedUserAction { | 17 struct RecordedUserAction { |
| 19 const char* name; | 18 const char* name; |
| 20 int count; // number of times the metric was recorded. | 19 int count; // number of times the metric was recorded. |
| 21 } g_user_actions[] = { | 20 } g_user_actions[] = { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 39 {"test.time", base::HISTOGRAM, 1, 10000, 50}, | 38 {"test.time", base::HISTOGRAM, 1, 10000, 50}, |
| 40 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50}, | 39 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50}, |
| 41 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50}, | 40 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50}, |
| 42 {"test.count", base::HISTOGRAM, 1, 1000000, 50}, | 41 {"test.count", base::HISTOGRAM, 1, 1000000, 50}, |
| 43 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50}, | 42 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50}, |
| 44 {"test.small.count", base::HISTOGRAM, 1, 100, 50}, | 43 {"test.small.count", base::HISTOGRAM, 1, 100, 50}, |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 // This class observes and collects user action notifications that are sent | 46 // This class observes and collects user action notifications that are sent |
| 48 // by the tests, so that they can be examined afterwards for correctness. | 47 // by the tests, so that they can be examined afterwards for correctness. |
| 49 class UserActionObserver : public content::NotificationObserver { | 48 class UserActionObserver { |
| 50 public: | 49 public: |
| 51 UserActionObserver(); | 50 UserActionObserver(); |
| 51 ~UserActionObserver(); |
| 52 | 52 |
| 53 void ValidateUserActions(const RecordedUserAction* recorded, int count); | 53 void ValidateUserActions(const RecordedUserAction* recorded, int count); |
| 54 | 54 |
| 55 virtual void Observe(int type, | |
| 56 const content::NotificationSource& source, | |
| 57 const content::NotificationDetails& details); | |
| 58 | |
| 59 private: | 55 private: |
| 60 typedef std::map<std::string, int> UserActionCountMap; | 56 typedef std::map<std::string, int> UserActionCountMap; |
| 61 | 57 |
| 58 void OnUserAction(const std::string& action); |
| 59 |
| 62 int num_metrics() const { | 60 int num_metrics() const { |
| 63 return count_map_.size(); | 61 return count_map_.size(); |
| 64 } | 62 } |
| 65 | 63 |
| 66 int GetMetricCount(const std::string& name) const { | 64 int GetMetricCount(const std::string& name) const { |
| 67 UserActionCountMap::const_iterator i = count_map_.find(name); | 65 UserActionCountMap::const_iterator i = count_map_.find(name); |
| 68 return i == count_map_.end() ? -1 : i->second; | 66 return i == count_map_.end() ? -1 : i->second; |
| 69 } | 67 } |
| 70 | 68 |
| 71 content::NotificationRegistrar registrar_; | |
| 72 UserActionCountMap count_map_; | 69 UserActionCountMap count_map_; |
| 70 |
| 71 content::ActionCallback action_callback_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(UserActionObserver); |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 UserActionObserver::UserActionObserver() { | 76 UserActionObserver::UserActionObserver() |
| 76 registrar_.Add(this, content::NOTIFICATION_USER_ACTION, | 77 : action_callback_(base::Bind(&UserActionObserver::OnUserAction, |
| 77 content::NotificationService::AllSources()); | 78 base::Unretained(this))) { |
| 79 content::AddActionCallback(action_callback_); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void UserActionObserver::Observe(int type, | 82 UserActionObserver::~UserActionObserver() { |
| 81 const content::NotificationSource& source, | 83 content::RemoveActionCallback(action_callback_); |
| 82 const content::NotificationDetails& details) { | 84 } |
| 83 const char* name = *content::Details<const char*>(details).ptr(); | 85 |
| 84 ++(count_map_[name]); | 86 void UserActionObserver::OnUserAction(const std::string& action) { |
| 87 ++(count_map_[action]); |
| 85 } | 88 } |
| 86 | 89 |
| 87 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded, | 90 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded, |
| 88 int count) { | 91 int count) { |
| 89 EXPECT_EQ(count, num_metrics()); | 92 EXPECT_EQ(count, num_metrics()); |
| 90 | 93 |
| 91 for (int i = 0; i < count; ++i) { | 94 for (int i = 0; i < count; ++i) { |
| 92 const RecordedUserAction& ua = recorded[i]; | 95 const RecordedUserAction& ua = recorded[i]; |
| 93 EXPECT_EQ(ua.count, GetMetricCount(ua.name)); | 96 EXPECT_EQ(ua.count, GetMetricCount(ua.name)); |
| 94 } | 97 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 124 } // anonymous namespace | 127 } // anonymous namespace |
| 125 | 128 |
| 126 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { | 129 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { |
| 127 UserActionObserver observer; | 130 UserActionObserver observer; |
| 128 | 131 |
| 129 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_; | 132 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_; |
| 130 | 133 |
| 131 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions)); | 134 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions)); |
| 132 ValidateHistograms(g_histograms, arraysize(g_histograms)); | 135 ValidateHistograms(g_histograms, arraysize(g_histograms)); |
| 133 } | 136 } |
| OLD | NEW |