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.
5 #import <Cocoa/Cocoa.h>
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/command_updater.h"
10 #include "chrome/browser/command_updater_delegate.h"
11 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12 #include "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
13 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/password_manager/core/common/password_manager_ui.h"
16 #include "grit/theme_resources.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/gtest_mac.h"
19 #include "ui/base/l10n/l10n_util_mac.h"
20 #include "ui/gfx/image/image.h"
24 // A simple CommandUpdaterDelegate for testing whether the correct command
26 class TestCommandUpdaterDelegate : public CommandUpdaterDelegate {
28 TestCommandUpdaterDelegate() : id_(0) {}
30 virtual void ExecuteCommandWithDisposition(
32 WindowOpenDisposition disposition) OVERRIDE {
36 int id() { return id_; }
42 bool ImagesEqual(NSImage* left, NSImage* right) {
45 gfx::Image leftImage([left copy]);
46 gfx::Image rightImage([right copy]);
47 return leftImage.As1xPNGBytes()->Equals(rightImage.As1xPNGBytes());
52 // Tests isolated functionality of the ManagedPasswordsDecoration.
53 class ManagePasswordsDecorationTest : public CocoaTest {
55 ManagePasswordsDecorationTest()
56 : commandUpdater_(&commandDelegate_),
57 decoration_(&commandUpdater_, NULL) {
58 commandUpdater_.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE, true);
62 TestCommandUpdaterDelegate* commandDelegate() { return &commandDelegate_; }
63 ManagePasswordsDecoration* decoration() { return &decoration_; }
66 TestCommandUpdaterDelegate commandDelegate_;
67 CommandUpdater commandUpdater_;
68 ManagePasswordsDecoration decoration_;
71 TEST_F(ManagePasswordsDecorationTest, ExecutesManagePasswordsCommandOnClick) {
72 EXPECT_TRUE(decoration()->AcceptsMousePress());
73 EXPECT_FALSE(decoration()->OnMousePressed(NSRect(), NSPoint()));
74 EXPECT_EQ(IDC_MANAGE_PASSWORDS_FOR_PAGE, commandDelegate()->id());
77 // Parameter object for ManagePasswordsDecorationStateTests.
78 struct ManagePasswordsTestCase {
80 password_manager::ui::State state;
89 // Tests that setting different combinations of password_manager::ui::State
90 // and the Active property of the decoration result in the correct visibility,
91 // decoration icon, and tooltip.
92 class ManagePasswordsDecorationStateTest
93 : public ManagePasswordsDecorationTest,
94 public ::testing::WithParamInterface<ManagePasswordsTestCase> {};
96 TEST_P(ManagePasswordsDecorationStateTest, TestState) {
97 decoration()->icon()->SetState(GetParam().state);
98 decoration()->icon()->SetActive(GetParam().active);
99 EXPECT_EQ(GetParam().visible, decoration()->IsVisible());
100 EXPECT_TRUE(ImagesEqual(
101 GetParam().image ? OmniboxViewMac::ImageForResource(GetParam().image)
103 decoration()->GetImage()));
104 EXPECT_NSEQ(GetParam().toolTip
105 ? l10n_util::GetNSStringWithFixup(GetParam().toolTip)
107 decoration()->GetToolTip());
110 ManagePasswordsTestCase managerInactiveOnPageAndEnabledTests[] = {
111 {.state = password_manager::ui::INACTIVE_STATE,
116 {.state = password_manager::ui::INACTIVE_STATE,
122 INSTANTIATE_TEST_CASE_P(
123 ManagerInactiveOnPage,
124 ManagePasswordsDecorationStateTest,
125 ::testing::ValuesIn(managerInactiveOnPageAndEnabledTests));
127 ManagePasswordsTestCase managerActiveOnPageAndEnabledTests[] = {
128 {.state = password_manager::ui::MANAGE_STATE,
131 .image = IDR_SAVE_PASSWORD_ACTIVE,
132 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE},
133 {.state = password_manager::ui::MANAGE_STATE,
136 .image = IDR_SAVE_PASSWORD_INACTIVE,
137 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE}};
139 INSTANTIATE_TEST_CASE_P(
140 ManagerActiveOnPageAndEnabled,
141 ManagePasswordsDecorationStateTest,
142 ::testing::ValuesIn(managerActiveOnPageAndEnabledTests));
144 ManagePasswordsTestCase managerActiveOnPageAndBlacklistedTests[] = {
145 {.state = password_manager::ui::BLACKLIST_STATE,
148 .image = IDR_SAVE_PASSWORD_DISABLED_ACTIVE,
149 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE},
150 {.state = password_manager::ui::BLACKLIST_STATE,
153 .image = IDR_SAVE_PASSWORD_DISABLED_INACTIVE,
154 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE}};
156 INSTANTIATE_TEST_CASE_P(
157 ManagerActiveOnPageAndBlacklisted,
158 ManagePasswordsDecorationStateTest,
159 ::testing::ValuesIn(managerActiveOnPageAndBlacklistedTests));
161 ManagePasswordsTestCase managerActiveOnPageAndPendingTests[] = {
162 {.state = password_manager::ui::PENDING_PASSWORD_AND_BUBBLE_STATE,
165 .image = IDR_SAVE_PASSWORD_ACTIVE,
166 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE},
167 {.state = password_manager::ui::PENDING_PASSWORD_AND_BUBBLE_STATE,
170 .image = IDR_SAVE_PASSWORD_INACTIVE,
171 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE},
172 {.state = password_manager::ui::PENDING_PASSWORD_STATE,
175 .image = IDR_SAVE_PASSWORD_ACTIVE,
176 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE},
177 {.state = password_manager::ui::PENDING_PASSWORD_STATE,
180 .image = IDR_SAVE_PASSWORD_INACTIVE,
181 .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE}};
183 INSTANTIATE_TEST_CASE_P(
184 ManagerActiveOnPageAndPending,
185 ManagePasswordsDecorationStateTest,
186 ::testing::ValuesIn(managerActiveOnPageAndPendingTests));