37a09360af5c5cbbcfdc930f8f4aaa90a3bd40ac
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / location_bar / manage_passwords_decoration_unittest.mm
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 #import <Cocoa/Cocoa.h>
6
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"
21
22 namespace {
23
24 // A simple CommandUpdaterDelegate for testing whether the correct command
25 // was sent.
26 class TestCommandUpdaterDelegate : public CommandUpdaterDelegate {
27  public:
28   TestCommandUpdaterDelegate() : id_(0) {}
29
30   virtual void ExecuteCommandWithDisposition(
31       int id,
32       WindowOpenDisposition disposition) OVERRIDE {
33     id_ = id;
34   }
35
36   int id() { return id_; }
37
38  private:
39   int id_;
40 };
41
42 bool ImagesEqual(NSImage* left, NSImage* right) {
43   if (!left || !right)
44     return left == right;
45   gfx::Image leftImage([left copy]);
46   gfx::Image rightImage([right copy]);
47   return leftImage.As1xPNGBytes()->Equals(rightImage.As1xPNGBytes());
48 }
49
50 }  // namespace
51
52 // Tests isolated functionality of the ManagedPasswordsDecoration.
53 class ManagePasswordsDecorationTest : public CocoaTest {
54  public:
55   ManagePasswordsDecorationTest()
56       : commandUpdater_(&commandDelegate_),
57         decoration_(&commandUpdater_, NULL) {
58     commandUpdater_.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE, true);
59   }
60
61  protected:
62   TestCommandUpdaterDelegate* commandDelegate() { return &commandDelegate_; }
63   ManagePasswordsDecoration* decoration() { return &decoration_; }
64
65  private:
66   TestCommandUpdaterDelegate commandDelegate_;
67   CommandUpdater commandUpdater_;
68   ManagePasswordsDecoration decoration_;
69 };
70
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());
75 }
76
77 // Parameter object for ManagePasswordsDecorationStateTests.
78 struct ManagePasswordsTestCase {
79   // Inputs
80   password_manager::ui::State state;
81   bool active;
82
83   // Outputs
84   bool visible;
85   int image;
86   int toolTip;
87 };
88
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> {};
95
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)
102                        : nil,
103       decoration()->GetImage()));
104   EXPECT_NSEQ(GetParam().toolTip
105                   ? l10n_util::GetNSStringWithFixup(GetParam().toolTip)
106                   : nil,
107               decoration()->GetToolTip());
108 }
109
110 ManagePasswordsTestCase managerInactiveOnPageAndEnabledTests[] = {
111     {.state = password_manager::ui::INACTIVE_STATE,
112      .active = true,
113      .visible = false,
114      .image = 0,
115      .toolTip = 0},
116     {.state = password_manager::ui::INACTIVE_STATE,
117      .active = false,
118      .visible = false,
119      .image = 0,
120      .toolTip = 0}};
121
122 INSTANTIATE_TEST_CASE_P(
123     ManagerInactiveOnPage,
124     ManagePasswordsDecorationStateTest,
125     ::testing::ValuesIn(managerInactiveOnPageAndEnabledTests));
126
127 ManagePasswordsTestCase managerActiveOnPageAndEnabledTests[] = {
128     {.state = password_manager::ui::MANAGE_STATE,
129      .active = true,
130      .visible = true,
131      .image = IDR_SAVE_PASSWORD_ACTIVE,
132      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE},
133     {.state = password_manager::ui::MANAGE_STATE,
134      .active = false,
135      .visible = true,
136      .image = IDR_SAVE_PASSWORD_INACTIVE,
137      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE}};
138
139 INSTANTIATE_TEST_CASE_P(
140     ManagerActiveOnPageAndEnabled,
141     ManagePasswordsDecorationStateTest,
142     ::testing::ValuesIn(managerActiveOnPageAndEnabledTests));
143
144 ManagePasswordsTestCase managerActiveOnPageAndBlacklistedTests[] = {
145     {.state = password_manager::ui::BLACKLIST_STATE,
146      .active = true,
147      .visible = true,
148      .image = IDR_SAVE_PASSWORD_DISABLED_ACTIVE,
149      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE},
150     {.state = password_manager::ui::BLACKLIST_STATE,
151      .active = false,
152      .visible = true,
153      .image = IDR_SAVE_PASSWORD_DISABLED_INACTIVE,
154      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE}};
155
156 INSTANTIATE_TEST_CASE_P(
157     ManagerActiveOnPageAndBlacklisted,
158     ManagePasswordsDecorationStateTest,
159     ::testing::ValuesIn(managerActiveOnPageAndBlacklistedTests));
160
161 ManagePasswordsTestCase managerActiveOnPageAndPendingTests[] = {
162     {.state = password_manager::ui::PENDING_PASSWORD_AND_BUBBLE_STATE,
163      .active = true,
164      .visible = true,
165      .image = IDR_SAVE_PASSWORD_ACTIVE,
166      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE},
167     {.state = password_manager::ui::PENDING_PASSWORD_AND_BUBBLE_STATE,
168      .active = false,
169      .visible = true,
170      .image = IDR_SAVE_PASSWORD_INACTIVE,
171      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE},
172     {.state = password_manager::ui::PENDING_PASSWORD_STATE,
173      .active = true,
174      .visible = true,
175      .image = IDR_SAVE_PASSWORD_ACTIVE,
176      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE},
177     {.state = password_manager::ui::PENDING_PASSWORD_STATE,
178      .active = false,
179      .visible = true,
180      .image = IDR_SAVE_PASSWORD_INACTIVE,
181      .toolTip = IDS_PASSWORD_MANAGER_TOOLTIP_SAVE}};
182
183 INSTANTIATE_TEST_CASE_P(
184     ManagerActiveOnPageAndPending,
185     ManagePasswordsDecorationStateTest,
186     ::testing::ValuesIn(managerActiveOnPageAndPendingTests));