1 // Copyright 2015 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 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/gfx/range/range.h"
18 const char* const user_visible_url
;
19 const char* const form_origin_url
;
20 bool is_smartlock_branding_enabled
;
21 bool is_update_password_bubble
;
22 const char* const expected_domain_placeholder
; // "this site" or domain name
23 size_t expected_link_range_start
;
24 size_t expected_link_range_end
;
25 } kDomainsTestCases
[] = {
27 {"http://example.com/landing", "http://example.com/login#form?value=3",
28 false, false, "this site", 0, 0},
29 {"http://example.com/landing", "http://example.com/login#form?value=3",
30 true, false, "this site", 12, 29},
32 // Different subdomains.
33 {"https://a.example.com/landing",
34 "https://b.example.com/login#form?value=3", false, false, "this site", 0,
36 {"https://a.example.com/landing",
37 "https://b.example.com/login#form?value=3", true, false, "this site", 12,
41 {"https://another.org", "https://example.com:/login#form?value=3", false,
42 false, "https://example.com", 0, 0},
43 {"https://another.org", "https://example.com/login#form?value=3", true,
44 false, "https://example.com", 12, 29},
46 // Different domains and password form origin url with
47 // default port for the scheme.
48 {"https://another.org", "https://example.com:443/login#form?value=3", false,
49 false, "https://example.com", 0, 0},
50 {"https://another.org", "http://example.com:80/login#form?value=3", true,
51 false, "http://example.com", 12, 29},
53 // Different domains and password form origin url with
54 // non-default port for the scheme.
55 {"https://another.org", "https://example.com:8001/login#form?value=3",
56 false, false, "https://example.com:8001", 0, 0},
57 {"https://another.org", "https://example.com:8001/login#form?value=3", true,
58 false, "https://example.com:8001", 12, 29},
61 {"http://example.com/landing", "http://example.com/login#form?value=3",
62 false, true, "this site", 0, 0},
63 {"http://example.com/landing", "http://example.com/login#form?value=3",
64 true, true, "this site", 12, 29}};
68 // Test for GetSavePasswordDialogTitleTextAndLinkRange().
69 TEST(ManagePasswordsViewUtilTest
, GetSavePasswordDialogTitleTextAndLinkRange
) {
70 for (size_t i
= 0; i
< arraysize(kDomainsTestCases
); ++i
) {
71 SCOPED_TRACE(testing::Message() << "user_visible_url = "
72 << kDomainsTestCases
[i
].user_visible_url
73 << ", form_origin_url = "
74 << kDomainsTestCases
[i
].form_origin_url
);
77 gfx::Range title_link_range
;
78 GetSavePasswordDialogTitleTextAndLinkRange(
79 GURL(kDomainsTestCases
[i
].user_visible_url
),
80 GURL(kDomainsTestCases
[i
].form_origin_url
),
81 kDomainsTestCases
[i
].is_smartlock_branding_enabled
,
82 kDomainsTestCases
[i
].is_update_password_bubble
, &title
,
85 // Verify against expectations.
86 base::string16 domain
=
87 base::ASCIIToUTF16(kDomainsTestCases
[i
].expected_domain_placeholder
);
88 EXPECT_TRUE(title
.find(domain
) != base::string16::npos
);
89 EXPECT_EQ(kDomainsTestCases
[i
].expected_link_range_start
,
90 title_link_range
.start());
91 EXPECT_EQ(kDomainsTestCases
[i
].expected_link_range_end
,
92 title_link_range
.end());
93 if (kDomainsTestCases
[i
].is_update_password_bubble
) {
94 EXPECT_TRUE(title
.find(base::ASCIIToUTF16("update")) !=
95 base::string16::npos
);
97 EXPECT_TRUE(title
.find(base::ASCIIToUTF16("save")) !=
98 base::string16::npos
);
103 TEST(ManagePasswordsViewUtilTest
, GetManagePasswordsDialogTitleText
) {
104 for (size_t i
= 0; i
< arraysize(kDomainsTestCases
); ++i
) {
105 SCOPED_TRACE(testing::Message() << "user_visible_url = "
106 << kDomainsTestCases
[i
].user_visible_url
107 << ", password_origin_url = "
108 << kDomainsTestCases
[i
].form_origin_url
);
110 base::string16 title
;
111 gfx::Range title_link_range
;
112 GetManagePasswordsDialogTitleText(
113 GURL(kDomainsTestCases
[i
].user_visible_url
),
114 GURL(kDomainsTestCases
[i
].form_origin_url
), &title
);
116 // Verify against expectations.
117 base::string16 domain
=
118 base::ASCIIToUTF16(kDomainsTestCases
[i
].expected_domain_placeholder
);
119 EXPECT_TRUE(title
.find(domain
) != base::string16::npos
);
123 TEST(ManagePasswordsViewUtilTest
,
124 GetAccountChooserDialogTitleTextAndLinkRangeSmartLockUsers
) {
125 base::string16 branding
=
126 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK
);
127 base::string16 title
;
128 gfx::Range title_link_range
;
129 GetAccountChooserDialogTitleTextAndLinkRange(
130 true /* is_smartlock_branding_enabled */, &title
, &title_link_range
);
132 // Check that branding string is a part of a title.
133 EXPECT_LT(title
.find(branding
, 0), title
.size());
134 EXPECT_GT(title
.find(branding
, 0), 0U);
135 // Check that link range is not empty.
136 EXPECT_NE(0U, title_link_range
.start());
137 EXPECT_NE(0U, title_link_range
.end());
140 TEST(ManagePasswordsViewUtilTest
,
141 GetAccountChooserDialogTitleTextAndLinkRangeNonSmartLockUsers
) {
142 base::string16 branding
=
143 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK
);
144 base::string16 title
;
145 gfx::Range title_link_range
;
146 GetAccountChooserDialogTitleTextAndLinkRange(
147 false /* is_smartlock_branding_enabled */, &title
, &title_link_range
);
148 EXPECT_GE(title
.find(branding
, 0), title
.size());
149 EXPECT_EQ(0U, title_link_range
.start());
150 EXPECT_EQ(0U, title_link_range
.end());