1d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// found in the LICENSE file.
4d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_manager_metrics_util.h"
6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <iterator>
8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <map>
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/basictypes.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/values.h"
134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/common/password_manager_pref_names.h"
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class PasswordManagerMetricsUtilTest : public testing::Test {
184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) public:
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  PasswordManagerMetricsUtilTest() {}
20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) protected:
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool IsMonitored(const char* url_host) {
23c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    size_t group_id = password_manager::metrics_util::MonitoredDomainGroupId(
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        url_host, profile_.GetPrefs());
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return group_id > 0;
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TestingProfile profile_;
294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)};
30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(PasswordManagerMetricsUtilTest, MonitoredDomainGroupAssigmentTest) {
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const char* const kMonitoredWebsites[] = {
33d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.google.com",
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.yahoo.com",
35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.baidu.com",
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.wikipedia.org",
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.linkedin.com",
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.twitter.com",
39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.facebook.com",
40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.amazon.com",
41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.ebay.com",
42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "https://www.tumblr.com",
43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  };
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const size_t kMonitoredWebsitesLength = arraysize(kMonitoredWebsites);
45d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // The |groups| map contains the group id and the number of times
47d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // it get assigned.
48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  std::map<size_t, size_t> groups;
49d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Provide all possible values of the group id parameter for each monitored
51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // website.
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  for (size_t i = 0; i < kMonitoredWebsitesLength; ++i) {
53c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    for (size_t j = 0; j < password_manager::metrics_util::kGroupsPerDomain;
54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)         ++j) {
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      {  // Set the group index for domain |i| to |j|.
56c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        ListPrefUpdate group_indices(
57c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch            profile_.GetPrefs(),
58c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch            password_manager::prefs::kPasswordManagerGroupsForDomains);
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        group_indices->Set(i, new base::FundamentalValue(static_cast<int>(j)));
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      }  // At the end of the scope the prefs get updated.
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
62c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ++groups[password_manager::metrics_util::MonitoredDomainGroupId(
63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch          kMonitoredWebsites[i], profile_.GetPrefs())];
64d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    }
65d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Check if all groups get assigned the same number of times.
68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  size_t number_of_assigment = groups.begin()->second;
69d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  for (std::map<size_t, size_t>::iterator it = groups.begin();
70d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)       it != groups.end(); ++it) {
71d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    EXPECT_EQ(it->second, number_of_assigment) << " group id = " << it->first;
72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
73d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
74d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(PasswordManagerMetricsUtilTest, MonitoredDomainGroupTest) {
76d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_TRUE(IsMonitored("https://www.linkedin.com"));
77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_TRUE(IsMonitored("https://www.amazon.com"));
78d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_TRUE(IsMonitored("https://www.facebook.com"));
79d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_TRUE(IsMonitored("http://wikipedia.org"));
80d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_FALSE(IsMonitored("http://thisisnotwikipedia.org"));
81d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
82