| OLD | NEW |
| 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 PasswordTitleType bubble_type; | 69 PasswordTitleType 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 PasswordTitleType::UPDATE_PASSWORD) { | 161 PasswordTitleType::UPDATE_PASSWORD) { |
| 118 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) != | 162 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) != |
| 119 base::string16::npos); | 163 base::string16::npos); |
| 120 } else { | 164 } else { |
| 121 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) != | 165 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) != |
| 122 base::string16::npos); | 166 base::string16::npos); |
| 123 } | 167 } |
| 124 } | 168 } |
| 125 } | 169 } |
| 126 | 170 |
| 171 // Check that empty localised strings do not cause a crash. |
| 172 TEST(ManagePasswordsViewUtilTest, |
| 173 GetSavePasswordDialogTitleTextAndLinkRange_EmptyStrings) { |
| 174 ScopedResourceOverride resource_override; |
| 175 |
| 176 // Ensure that the resource bundle returns an empty string for the UI. |
| 177 resource_override.OverrideLocaleStringResource(IDS_SAVE_PASSWORD, |
| 178 base::string16()); |
| 179 |
| 180 base::string16 title; |
| 181 gfx::Range title_link_range; |
| 182 const GURL kExample("http://example.org"); |
| 183 const bool kBrandingEnabled = true; |
| 184 // The arguments passed below have this importance for the codepath: |
| 185 // * The first two URLs need to be the same, otherwise |
| 186 // IDS_SAVE_PASSWORD_DIFFERENT_DOMAINS_TITLE will be used instead of |
| 187 // IDS_SAVE_PASSWORD overridden above. |
| 188 // * |kBrandingEnabled| needs to be true, otherwise the code won't try to |
| 189 // dereference placeholder offsets from the localised string, which |
| 190 // triggers the crash in http://crbug.com/658902. |
| 191 // * SAVE_PASSWORD dialog type needs to be passed to match the |
| 192 // IDS_SAVE_PASSWORD overridden above. |
| 193 GetSavePasswordDialogTitleTextAndLinkRange( |
| 194 kExample, kExample, kBrandingEnabled, PasswordTitleType::SAVE_PASSWORD, |
| 195 &title, &title_link_range); |
| 196 // Verify that the test did not pass just because |
| 197 // GetSavePasswordDialogTitleTextAndLinkRange changed the resource IDs it uses |
| 198 // (and hence did not get the overridden empty string). If the empty localised |
| 199 // string was used, the title and the range will be empty as well. |
| 200 EXPECT_TRUE(title_link_range.is_empty()); |
| 201 EXPECT_THAT(title, testing::IsEmpty()); |
| 202 } |
| 203 |
| 127 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) { | 204 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) { |
| 128 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) { | 205 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) { |
| 129 SCOPED_TRACE(testing::Message() << "user_visible_url = " | 206 SCOPED_TRACE(testing::Message() << "user_visible_url = " |
| 130 << kDomainsTestCases[i].user_visible_url | 207 << kDomainsTestCases[i].user_visible_url |
| 131 << ", password_origin_url = " | 208 << ", password_origin_url = " |
| 132 << kDomainsTestCases[i].form_origin_url); | 209 << kDomainsTestCases[i].form_origin_url); |
| 133 | 210 |
| 134 base::string16 title; | 211 base::string16 title; |
| 135 gfx::Range title_link_range; | 212 gfx::Range title_link_range; |
| 136 GetManagePasswordsDialogTitleText( | 213 GetManagePasswordsDialogTitleText( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 false /* is_smartlock_branding_enabled */, | 255 false /* is_smartlock_branding_enabled */, |
| 179 GetParam(), | 256 GetParam(), |
| 180 &title, &title_link_range); | 257 &title, &title_link_range); |
| 181 EXPECT_GE(title.find(branding, 0), title.size()); | 258 EXPECT_GE(title.find(branding, 0), title.size()); |
| 182 EXPECT_EQ(0U, title_link_range.start()); | 259 EXPECT_EQ(0U, title_link_range.start()); |
| 183 EXPECT_EQ(0U, title_link_range.end()); | 260 EXPECT_EQ(0U, title_link_range.end()); |
| 184 } | 261 } |
| 185 | 262 |
| 186 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest, | 263 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest, |
| 187 ::testing::Bool()); | 264 ::testing::Bool()); |
| OLD | NEW |