manage_passwords_bubble_view_browsertest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2014 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 "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h"
6
7#include "base/metrics/histogram_samples.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/tabs/tab_strip_model.h"
10#include "chrome/browser/ui/views/passwords/manage_passwords_view_test.h"
11#include "components/password_manager/core/browser/password_manager_metrics_util.h"
12#include "components/password_manager/core/browser/stub_password_manager_client.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace {
16
17const char kDisplayDispositionMetric[] = "PasswordBubble.DisplayDisposition";
18
19}  // namespace
20
21typedef ManagePasswordsViewTest ManagePasswordsBubbleViewTest;
22namespace metrics_util = password_manager::metrics_util;
23
24IN_PROC_BROWSER_TEST_F(ManagePasswordsBubbleViewTest, BasicOpenAndClose) {
25  EXPECT_FALSE(ManagePasswordsBubbleView::IsShowing());
26  ManagePasswordsBubbleView::ShowBubble(
27      browser()->tab_strip_model()->GetActiveWebContents(),
28      ManagePasswordsBubble::USER_ACTION);
29  EXPECT_TRUE(ManagePasswordsBubbleView::IsShowing());
30  const ManagePasswordsBubbleView* bubble =
31      ManagePasswordsBubbleView::manage_password_bubble();
32  EXPECT_TRUE(bubble->initially_focused_view());
33  EXPECT_EQ(bubble->initially_focused_view(),
34            bubble->GetFocusManager()->GetFocusedView());
35  EXPECT_FALSE(bubble->IsTimerRunning());
36  ManagePasswordsBubbleView::CloseBubble();
37  EXPECT_FALSE(ManagePasswordsBubbleView::IsShowing());
38
39  // And, just for grins, ensure that we can re-open the bubble.
40  ManagePasswordsBubbleView::ShowBubble(
41      browser()->tab_strip_model()->GetActiveWebContents(),
42      ManagePasswordsBubble::USER_ACTION);
43  EXPECT_TRUE(ManagePasswordsBubbleView::manage_password_bubble()->
44      GetFocusManager()->GetFocusedView());
45  EXPECT_TRUE(ManagePasswordsBubbleView::IsShowing());
46  ManagePasswordsBubbleView::CloseBubble();
47  EXPECT_FALSE(ManagePasswordsBubbleView::IsShowing());
48}
49
50// Same as 'BasicOpenAndClose', but use the command rather than the static
51// method directly.
52IN_PROC_BROWSER_TEST_F(ManagePasswordsBubbleViewTest, CommandControlsBubble) {
53  // The command only works if the icon is visible, so get into management mode.
54  SetupManagingPasswords();
55  EXPECT_FALSE(ManagePasswordsBubbleView::IsShowing());
56  ExecuteManagePasswordsCommand();
57  EXPECT_TRUE(ManagePasswordsBubbleView::IsShowing());
58  const ManagePasswordsBubbleView* bubble =
59      ManagePasswordsBubbleView::manage_password_bubble();
60  EXPECT_TRUE(bubble->initially_focused_view());
61  EXPECT_EQ(bubble->initially_focused_view(),
62            bubble->GetFocusManager()->GetFocusedView());
63  ManagePasswordsBubbleView::CloseBubble();
64  EXPECT_FALSE(ManagePasswordsBubbleView::IsShowing());
65
66  // And, just for grins, ensure that we can re-open the bubble.
67  ExecuteManagePasswordsCommand();
68  EXPECT_TRUE(ManagePasswordsBubbleView::IsShowing());
69  ManagePasswordsBubbleView::CloseBubble();
70  EXPECT_FALSE(ManagePasswordsBubbleView::IsShowing());
71}
72
73IN_PROC_BROWSER_TEST_F(ManagePasswordsBubbleViewTest,
74                       CommandExecutionInManagingState) {
75  SetupManagingPasswords();
76  ExecuteManagePasswordsCommand();
77
78  scoped_ptr<base::HistogramSamples> samples(
79      GetSamples(kDisplayDispositionMetric));
80  EXPECT_EQ(
81      0,
82      samples->GetCount(
83          metrics_util::AUTOMATIC_WITH_PASSWORD_PENDING));
84  EXPECT_EQ(0,
85            samples->GetCount(
86                metrics_util::MANUAL_WITH_PASSWORD_PENDING));
87  EXPECT_EQ(1,
88            samples->GetCount(
89                metrics_util::MANUAL_MANAGE_PASSWORDS));
90}
91
92IN_PROC_BROWSER_TEST_F(ManagePasswordsBubbleViewTest,
93                       CommandExecutionInAutomaticState) {
94  // Open with pending password: automagical!
95  SetupPendingPassword();
96
97  // Bubble should not be focused by default.
98  EXPECT_FALSE(ManagePasswordsBubbleView::manage_password_bubble()->
99      GetFocusManager()->GetFocusedView());
100  // Bubble can be active if user clicks it.
101  EXPECT_TRUE(ManagePasswordsBubbleView::manage_password_bubble()->
102      CanActivate());
103  EXPECT_TRUE(ManagePasswordsBubbleView::manage_password_bubble()->
104      IsTimerRunning());
105
106  scoped_ptr<base::HistogramSamples> samples(
107      GetSamples(kDisplayDispositionMetric));
108  EXPECT_EQ(
109      1,
110      samples->GetCount(
111          metrics_util::AUTOMATIC_WITH_PASSWORD_PENDING));
112  EXPECT_EQ(0,
113            samples->GetCount(
114                metrics_util::MANUAL_WITH_PASSWORD_PENDING));
115  EXPECT_EQ(0,
116            samples->GetCount(
117                metrics_util::MANUAL_MANAGE_PASSWORDS));
118}
119
120IN_PROC_BROWSER_TEST_F(ManagePasswordsBubbleViewTest,
121                       CommandExecutionInPendingState) {
122  // Open once with pending password: automagical!
123  SetupPendingPassword();
124  ManagePasswordsBubbleView::CloseBubble();
125  // This opening should be measured as manual.
126  ExecuteManagePasswordsCommand();
127
128  scoped_ptr<base::HistogramSamples> samples(
129      GetSamples(kDisplayDispositionMetric));
130  EXPECT_EQ(
131      1,
132      samples->GetCount(
133          metrics_util::AUTOMATIC_WITH_PASSWORD_PENDING));
134  EXPECT_EQ(1,
135            samples->GetCount(
136                metrics_util::MANUAL_WITH_PASSWORD_PENDING));
137  EXPECT_EQ(0,
138            samples->GetCount(
139                metrics_util::MANUAL_MANAGE_PASSWORDS));
140}
141
142IN_PROC_BROWSER_TEST_F(ManagePasswordsBubbleViewTest,
143                       CommandExecutionInAutomaticSaveState) {
144  SetupAutomaticPassword();
145  ManagePasswordsBubbleView::CloseBubble();
146  // Re-opening should count as manual.
147  ExecuteManagePasswordsCommand();
148
149 scoped_ptr<base::HistogramSamples> samples(
150      GetSamples(kDisplayDispositionMetric));
151  EXPECT_EQ(
152      1,
153      samples->GetCount(
154          metrics_util::AUTOMATIC_GENERATED_PASSWORD_CONFIRMATION));
155  EXPECT_EQ(0,
156            samples->GetCount(
157                metrics_util::MANUAL_WITH_PASSWORD_PENDING));
158  EXPECT_EQ(1,
159            samples->GetCount(
160                metrics_util::MANUAL_MANAGE_PASSWORDS));
161}
162