Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_manager_metrics_util_unittest.cc
1 // Copyright 2013 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 "components/password_manager/core/browser/password_manager_metrics_util.h"
6
7 #include <iterator>
8 #include <map>
9
10 #include "base/basictypes.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "base/values.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/password_manager/core/common/password_manager_pref_names.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 class PasswordManagerMetricsUtilTest : public testing::Test {
19  public:
20   PasswordManagerMetricsUtilTest() {}
21
22  protected:
23   bool IsMonitored(const char* url_host) {
24     size_t group_id = password_manager_metrics_util::MonitoredDomainGroupId(
25         url_host, profile_.GetPrefs());
26     return group_id > 0;
27   }
28
29   TestingProfile profile_;
30 };
31
32 TEST_F(PasswordManagerMetricsUtilTest, MonitoredDomainGroupAssigmentTest) {
33   const char* const kMonitoredWebsites[] = {
34       "https://www.google.com",
35       "https://www.yahoo.com",
36       "https://www.baidu.com",
37       "https://www.wikipedia.org",
38       "https://www.linkedin.com",
39       "https://www.twitter.com",
40       "https://www.facebook.com",
41       "https://www.amazon.com",
42       "https://www.ebay.com",
43       "https://www.tumblr.com",
44   };
45   const size_t kMonitoredWebsitesLength = arraysize(kMonitoredWebsites);
46
47   // The |groups| map contains the group id and the number of times
48   // it get assigned.
49   std::map<size_t, size_t> groups;
50
51   // Provide all possible values of the group id parameter for each monitored
52   // website.
53   for (size_t i = 0; i < kMonitoredWebsitesLength; ++i) {
54     for (size_t j = 0; j < password_manager_metrics_util::kGroupsPerDomain;
55          ++j) {
56       {  // Set the group index for domain |i| to |j|.
57         ListPrefUpdate group_indices(profile_.GetPrefs(),
58                                      prefs::kPasswordManagerGroupsForDomains);
59         group_indices->Set(i, new base::FundamentalValue(static_cast<int>(j)));
60       }  // At the end of the scope the prefs get updated.
61
62       ++groups[password_manager_metrics_util::MonitoredDomainGroupId(
63             kMonitoredWebsites[i], profile_.GetPrefs())];
64     }
65   }
66
67   // Check if all groups get assigned the same number of times.
68   size_t number_of_assigment = groups.begin()->second;
69   for (std::map<size_t, size_t>::iterator it = groups.begin();
70        it != groups.end(); ++it) {
71     EXPECT_EQ(it->second, number_of_assigment) << " group id = " << it->first;
72   }
73 }
74
75 TEST_F(PasswordManagerMetricsUtilTest, MonitoredDomainGroupTest) {
76   EXPECT_TRUE(IsMonitored("https://www.linkedin.com"));
77   EXPECT_TRUE(IsMonitored("https://www.amazon.com"));
78   EXPECT_TRUE(IsMonitored("https://www.facebook.com"));
79   EXPECT_TRUE(IsMonitored("http://wikipedia.org"));
80   EXPECT_FALSE(IsMonitored("http://thisisnotwikipedia.org"));
81 }