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/test/base/testing_profile.h"
14#include "components/password_manager/core/common/password_manager_pref_names.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17class PasswordManagerMetricsUtilTest : public testing::Test {
18 public:
19  PasswordManagerMetricsUtilTest() {}
20
21 protected:
22  bool IsMonitored(const char* url_host) {
23    size_t group_id = password_manager::metrics_util::MonitoredDomainGroupId(
24        url_host, profile_.GetPrefs());
25    return group_id > 0;
26  }
27
28  TestingProfile profile_;
29};
30
31TEST_F(PasswordManagerMetricsUtilTest, MonitoredDomainGroupAssigmentTest) {
32  const char* const kMonitoredWebsites[] = {
33      "https://www.google.com",
34      "https://www.yahoo.com",
35      "https://www.baidu.com",
36      "https://www.wikipedia.org",
37      "https://www.linkedin.com",
38      "https://www.twitter.com",
39      "https://www.facebook.com",
40      "https://www.amazon.com",
41      "https://www.ebay.com",
42      "https://www.tumblr.com",
43  };
44  const size_t kMonitoredWebsitesLength = arraysize(kMonitoredWebsites);
45
46  // The |groups| map contains the group id and the number of times
47  // it get assigned.
48  std::map<size_t, size_t> groups;
49
50  // Provide all possible values of the group id parameter for each monitored
51  // website.
52  for (size_t i = 0; i < kMonitoredWebsitesLength; ++i) {
53    for (size_t j = 0; j < password_manager::metrics_util::kGroupsPerDomain;
54         ++j) {
55      {  // Set the group index for domain |i| to |j|.
56        ListPrefUpdate group_indices(
57            profile_.GetPrefs(),
58            password_manager::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
75TEST_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}
82