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"
15 // Test for GetSavePasswordDialogTitleTextAndLinkRange().
16 TEST(ManagePasswordsViewUtilTest
, GetSavePasswordDialogTitleTextAndLinkRange
) {
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_title_text_ends_with
;
23 size_t expected_link_range_start
;
24 size_t expected_link_range_end
;
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?",
36 {"https://a.example.com/landing",
37 "https://b.example.com/login#form?value=3", true, false, "this site?",
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",
49 false, 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",
58 true, 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}};
66 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
67 SCOPED_TRACE(testing::Message()
68 << "user_visible_url = " << test_cases
[i
].user_visible_url
69 << ", form_origin_url = " << test_cases
[i
].form_origin_url
);
72 gfx::Range title_link_range
;
73 GetSavePasswordDialogTitleTextAndLinkRange(
74 GURL(test_cases
[i
].user_visible_url
),
75 GURL(test_cases
[i
].form_origin_url
),
76 test_cases
[i
].is_smartlock_branding_enabled
,
77 test_cases
[i
].is_update_password_bubble
, &title
, &title_link_range
);
79 // Verify against expectations.
80 EXPECT_TRUE(base::EndsWith(
81 title
, base::ASCIIToUTF16(test_cases
[i
].expected_title_text_ends_with
),
82 base::CompareCase::INSENSITIVE_ASCII
));
83 EXPECT_EQ(test_cases
[i
].expected_link_range_start
,
84 title_link_range
.start());
85 EXPECT_EQ(test_cases
[i
].expected_link_range_end
, title_link_range
.end());
86 if (test_cases
[i
].is_update_password_bubble
) {
87 EXPECT_TRUE(title
.find(base::ASCIIToUTF16("update")) !=
88 base::string16::npos
);
90 EXPECT_TRUE(title
.find(base::ASCIIToUTF16("save")) !=
91 base::string16::npos
);
96 TEST(ManagePasswordsViewUtilTest
,
97 GetAccountChooserDialogTitleTextAndLinkRangeSmartLockUsers
) {
98 base::string16 branding
=
99 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK
);
100 base::string16 title
;
101 gfx::Range title_link_range
;
102 GetAccountChooserDialogTitleTextAndLinkRange(
103 true /* is_smartlock_branding_enabled */, &title
, &title_link_range
);
105 // Check that branding string is a part of a title.
106 EXPECT_LT(title
.find(branding
, 0), title
.size());
107 EXPECT_GT(title
.find(branding
, 0), 0U);
108 // Check that link range is not empty.
109 EXPECT_NE(0U, title_link_range
.start());
110 EXPECT_NE(0U, title_link_range
.end());
113 TEST(ManagePasswordsViewUtilTest
,
114 GetAccountChooserDialogTitleTextAndLinkRangeNonSmartLockUsers
) {
115 base::string16 branding
=
116 l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SMART_LOCK
);
117 base::string16 title
;
118 gfx::Range title_link_range
;
119 GetAccountChooserDialogTitleTextAndLinkRange(
120 false /* is_smartlock_branding_enabled */, &title
, &title_link_range
);
121 EXPECT_GE(title
.find(branding
, 0), title
.size());
122 EXPECT_EQ(0U, title_link_range
.start());
123 EXPECT_EQ(0U, title_link_range
.end());