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

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

Issue 2534113002: Handle empty string in GetSavePasswordDialogTitleTextAndLinkRange gracefully (Closed)
Patch Set: 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"
16 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/range/range.h" 20 #include "ui/gfx/range/range.h"
17 #include "url/gurl.h" 21 #include "url/gurl.h"
18 22
19 namespace { 23 namespace {
20 24
25 // ScopedResourceOverride allows overriding localised strings in the shared
26 // instance of the resource bundle, while restoring the bundle state on
27 // destruction.
28 class ScopedResourceOverride {
29 public:
30 ScopedResourceOverride()
31 : had_shared_instance_(ui::ResourceBundle::HasSharedInstance()),
32 bundle_(GetOrCreateSharedInstance()),
33 app_locale_(g_browser_process->GetApplicationLocale()) {}
34
35 ~ScopedResourceOverride() {
36 if (had_shared_instance_) {
37 // Reloading the resources will discard all overrides.
38 bundle_.ReloadLocaleResources(app_locale_);
39 } else {
40 ui::ResourceBundle::CleanupSharedInstance();
41 }
42 }
43
44 void OverrideLocaleStringResource(int string_id, const base::string16& str) {
45 bundle_.OverrideLocaleStringResource(string_id, str);
46 }
47
48 private:
49 // Returns the shared resource bundle. Creates one if there was none.
50 static ui::ResourceBundle& GetOrCreateSharedInstance() {
51 if (!ui::ResourceBundle::HasSharedInstance()) {
52 ui::ResourceBundle::InitSharedInstanceWithLocale(
53 "en", nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
54 }
55 return ui::ResourceBundle::GetSharedInstance();
56 }
57
58 const bool had_shared_instance_; // Was there a shared bundle before?
59 ui::ResourceBundle& bundle_; // The shared bundle.
60 const std::string app_locale_;
61
62 DISALLOW_COPY_AND_ASSIGN(ScopedResourceOverride);
63 };
64
21 const struct { 65 const struct {
22 const char* const user_visible_url; 66 const char* const user_visible_url;
23 const char* const form_origin_url; 67 const char* const form_origin_url;
24 bool is_smartlock_branding_enabled; 68 bool is_smartlock_branding_enabled;
25 PasswordTittleType bubble_type; 69 PasswordTittleType bubble_type;
26 const char* const expected_domain_placeholder; // "this site" or domain name 70 const char* const expected_domain_placeholder; // "this site" or domain name
27 size_t expected_link_range_start; 71 size_t expected_link_range_start;
28 size_t expected_link_range_end; 72 size_t expected_link_range_end;
29 } kDomainsTestCases[] = { 73 } kDomainsTestCases[] = {
30 // Same domains. 74 // Same domains.
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 PasswordTittleType::UPDATE_PASSWORD) { 162 PasswordTittleType::UPDATE_PASSWORD) {
119 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) != 163 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) !=
120 base::string16::npos); 164 base::string16::npos);
121 } else { 165 } else {
122 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) != 166 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) !=
123 base::string16::npos); 167 base::string16::npos);
124 } 168 }
125 } 169 }
126 } 170 }
127 171
172 // Check that empty localised strings do not cause a crash.
173 TEST(ManagePasswordsViewUtilTest,
174 GetSavePasswordDialogTitleTextAndLinkRange_EmptyStrings) {
175 ScopedResourceOverride resource_override;
176
177 // Ensure that the resource bundle returns an empty string for the UI.
178 resource_override.OverrideLocaleStringResource(IDS_SAVE_PASSWORD,
179 base::string16());
180
181 base::string16 title;
182 gfx::Range title_link_range;
183 const GURL kExample("http://example.org");
184 const bool kBrandingEnabled = true;
185 // The arguments passed below have this importance for the codepath:
186 // * The first two URLs need to be the same, otherwise
187 // IDS_SAVE_PASSWORD_DIFFERENT_DOMAINS_TITLE will be used instead of
188 // IDS_SAVE_PASSWORD overridden above.
189 // * |kBrandingEnabled| needs to be true, otherwise the code won't try to
190 // dereference placeholder offsets from the localised string, which
191 // triggers the crash in http://crbug.com/658902.
192 // * SAVE_PASSWORD dialog type needs to be passed to match the
193 // IDS_SAVE_PASSWORD overridden above.
194 GetSavePasswordDialogTitleTextAndLinkRange(
195 kExample, kExample, kBrandingEnabled, PasswordTitleType::SAVE_PASSWORD,
196 &title, &title_link_range);
197 // Verify that the test did not pass just because
198 // GetSavePasswordDialogTitleTextAndLinkRange changed the resource IDs it uses
199 // (and hence did not get the overridden empty string). If the empty localised
200 // string was used, the title and the range will be empty as well.
201 EXPECT_TRUE(title_link_range.is_empty());
202 EXPECT_THAT(title, testing::IsEmpty());
203 }
204
128 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) { 205 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) {
129 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) { 206 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) {
130 SCOPED_TRACE(testing::Message() << "user_visible_url = " 207 SCOPED_TRACE(testing::Message() << "user_visible_url = "
131 << kDomainsTestCases[i].user_visible_url 208 << kDomainsTestCases[i].user_visible_url
132 << ", password_origin_url = " 209 << ", password_origin_url = "
133 << kDomainsTestCases[i].form_origin_url); 210 << kDomainsTestCases[i].form_origin_url);
134 211
135 base::string16 title; 212 base::string16 title;
136 gfx::Range title_link_range; 213 gfx::Range title_link_range;
137 GetManagePasswordsDialogTitleText( 214 GetManagePasswordsDialogTitleText(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 false /* is_smartlock_branding_enabled */, 256 false /* is_smartlock_branding_enabled */,
180 GetParam(), 257 GetParam(),
181 &title, &title_link_range); 258 &title, &title_link_range);
182 EXPECT_GE(title.find(branding, 0), title.size()); 259 EXPECT_GE(title.find(branding, 0), title.size());
183 EXPECT_EQ(0U, title_link_range.start()); 260 EXPECT_EQ(0U, title_link_range.start());
184 EXPECT_EQ(0U, title_link_range.end()); 261 EXPECT_EQ(0U, title_link_range.end());
185 } 262 }
186 263
187 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest, 264 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest,
188 ::testing::Bool()); 265 ::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