Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(733)

Side by Side Diff: chrome/browser/ui/passwords/manage_passwords_view_utils_unittest.cc

Issue 2526283002: Handle empty string in GetSavePasswordDialogTitleTextAndLinkRange gracefully (Closed)
Patch Set: Better test clean-up Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/passwords/manage_passwords_view_utils.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" 5 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
12 #include "chrome/grit/generated_resources.h" 14 #include "chrome/grit/generated_resources.h"
13 #include "components/strings/grit/components_strings.h" 15 #include "components/strings/grit/components_strings.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/range/range.h" 19 #include "ui/gfx/range/range.h"
17 #include "url/gurl.h" 20 #include "url/gurl.h"
18 21
19 namespace { 22 namespace {
20 23
24 // ScopedResourceOverride allows overriding localised strings in the shared
25 // instance of the resource bundle, while restoring the bundle state on
26 // destruction.
27 class ScopedResourceOverride {
28 public:
29 ScopedResourceOverride()
30 : had_shared_instance_(ui::ResourceBundle::HasSharedInstance()),
31 bundle_(GetOrCreateSharedInstance()),
32 app_locale_(g_browser_process->GetApplicationLocale()) {}
33
34 ~ScopedResourceOverride() {
35 if (had_shared_instance_) {
36 // Reloading the resources will discard all overrides.
37 bundle_.ReloadLocaleResources(app_locale_);
38 } else {
39 ui::ResourceBundle::CleanupSharedInstance();
40 }
41 }
42
43 void OverrideLocaleStringResource(int string_id, const base::string16& str) {
44 bundle_.OverrideLocaleStringResource(string_id, str);
45 }
46
47 private:
48 // Returns the shared resource bundle. Creates one if there was none.
49 static ui::ResourceBundle& GetOrCreateSharedInstance() {
50 if (!ui::ResourceBundle::HasSharedInstance()) {
51 ui::ResourceBundle::InitSharedInstanceWithLocale(
52 "en", nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
53 }
54 return ui::ResourceBundle::GetSharedInstance();
55 }
56
57 const bool had_shared_instance_; // Was there a shared bundle before?
58 ui::ResourceBundle& bundle_; // The shared bundle.
59 const std::string app_locale_;
60
61 DISALLOW_COPY_AND_ASSIGN(ScopedResourceOverride);
62 };
63
21 const struct { 64 const struct {
22 const char* const user_visible_url; 65 const char* const user_visible_url;
23 const char* const form_origin_url; 66 const char* const form_origin_url;
24 bool is_smartlock_branding_enabled; 67 bool is_smartlock_branding_enabled;
25 PasswordTitleType bubble_type; 68 PasswordTitleType bubble_type;
26 const char* const expected_domain_placeholder; // "this site" or domain name 69 const char* const expected_domain_placeholder; // "this site" or domain name
27 size_t expected_link_range_start; 70 size_t expected_link_range_start;
28 size_t expected_link_range_end; 71 size_t expected_link_range_end;
29 } kDomainsTestCases[] = { 72 } kDomainsTestCases[] = {
30 // Same domains. 73 // Same domains.
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 PasswordTitleType::UPDATE_PASSWORD) { 160 PasswordTitleType::UPDATE_PASSWORD) {
118 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) != 161 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) !=
119 base::string16::npos); 162 base::string16::npos);
120 } else { 163 } else {
121 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) != 164 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) !=
122 base::string16::npos); 165 base::string16::npos);
123 } 166 }
124 } 167 }
125 } 168 }
126 169
170 // Check that empty localised strings do not cause a crash.
171 TEST(ManagePasswordsViewUtilTest,
172 GetSavePasswordDialogTitleTextAndLinkRange_EmptyStrings) {
173 ScopedResourceOverride resource_override;
174
175 // Ensure that the resource bundle returns an empty string for the UI.
176 resource_override.OverrideLocaleStringResource(IDS_SAVE_PASSWORD,
177 base::string16());
178
179 base::string16 title;
180 gfx::Range title_link_range;
181 const GURL kExample("http://example.org");
182 const bool kBrandingEnabled = true;
183 // The arguments passed below have this importance for the codepath:
184 // * The first two URLs need to be the same, otherwise
185 // IDS_SAVE_PASSWORD_DIFFERENT_DOMAINS_TITLE will be used instead of
186 // IDS_SAVE_PASSWORD overridden above.
187 // * |kBrandingEnabled| needs to be true, otherwise the code won't try to
188 // dereference placeholder offsets from the localised string, which
189 // triggers the crash in http://crbug.com/658902.
190 // * SAVE_PASSWORD dialog type needs to be passed to match the
191 // IDS_SAVE_PASSWORD overridden above.
192 GetSavePasswordDialogTitleTextAndLinkRange(
193 kExample, kExample, kBrandingEnabled, PasswordTitleType::SAVE_PASSWORD,
194 &title, &title_link_range);
195 // Verify that the test did not pass just because
196 // GetSavePasswordDialogTitleTextAndLinkRange changed the resource IDs it uses
197 // (and hence did not get the overridden empty string). If the empty localised
198 // string was used, the title and the range will be empty as well.
199 EXPECT_TRUE(title_link_range.is_empty());
200 EXPECT_TRUE(title.empty()) << "|title| is non-empty: " << title;
vasilii 2016/11/25 10:47:24 You can say EXPECT_THAT(title, IsEmpty()); to get
vabr (Chromium) 2016/11/25 11:22:53 Done.
201 }
202
127 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) { 203 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) {
128 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) { 204 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) {
129 SCOPED_TRACE(testing::Message() << "user_visible_url = " 205 SCOPED_TRACE(testing::Message() << "user_visible_url = "
130 << kDomainsTestCases[i].user_visible_url 206 << kDomainsTestCases[i].user_visible_url
131 << ", password_origin_url = " 207 << ", password_origin_url = "
132 << kDomainsTestCases[i].form_origin_url); 208 << kDomainsTestCases[i].form_origin_url);
133 209
134 base::string16 title; 210 base::string16 title;
135 gfx::Range title_link_range; 211 gfx::Range title_link_range;
136 GetManagePasswordsDialogTitleText( 212 GetManagePasswordsDialogTitleText(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 false /* is_smartlock_branding_enabled */, 254 false /* is_smartlock_branding_enabled */,
179 GetParam(), 255 GetParam(),
180 &title, &title_link_range); 256 &title, &title_link_range);
181 EXPECT_GE(title.find(branding, 0), title.size()); 257 EXPECT_GE(title.find(branding, 0), title.size());
182 EXPECT_EQ(0U, title_link_range.start()); 258 EXPECT_EQ(0U, title_link_range.start());
183 EXPECT_EQ(0U, title_link_range.end()); 259 EXPECT_EQ(0U, title_link_range.end());
184 } 260 }
185 261
186 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest, 262 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest,
187 ::testing::Bool()); 263 ::testing::Bool());
OLDNEW
« no previous file with comments | « chrome/browser/ui/passwords/manage_passwords_view_utils.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698