| 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" | |
| 11 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
| 15 #include "components/strings/grit/components_strings.h" | 13 #include "components/strings/grit/components_strings.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | |
| 20 #include "ui/gfx/range/range.h" | 16 #include "ui/gfx/range/range.h" |
| 21 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 22 | 18 |
| 23 namespace { | 19 namespace { |
| 24 | 20 |
| 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 | |
| 65 const struct { | 21 const struct { |
| 66 const char* const user_visible_url; | 22 const char* const user_visible_url; |
| 67 const char* const form_origin_url; | 23 const char* const form_origin_url; |
| 68 bool is_smartlock_branding_enabled; | 24 bool is_smartlock_branding_enabled; |
| 69 PasswordTittleType bubble_type; | 25 PasswordTittleType bubble_type; |
| 70 const char* const expected_domain_placeholder; // "this site" or domain name | 26 const char* const expected_domain_placeholder; // "this site" or domain name |
| 71 size_t expected_link_range_start; | 27 size_t expected_link_range_start; |
| 72 size_t expected_link_range_end; | 28 size_t expected_link_range_end; |
| 73 } kDomainsTestCases[] = { | 29 } kDomainsTestCases[] = { |
| 74 // Same domains. | 30 // Same domains. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 PasswordTittleType::UPDATE_PASSWORD) { | 118 PasswordTittleType::UPDATE_PASSWORD) { |
| 163 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) != | 119 EXPECT_TRUE(title.find(base::ASCIIToUTF16("update")) != |
| 164 base::string16::npos); | 120 base::string16::npos); |
| 165 } else { | 121 } else { |
| 166 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) != | 122 EXPECT_TRUE(title.find(base::ASCIIToUTF16("save")) != |
| 167 base::string16::npos); | 123 base::string16::npos); |
| 168 } | 124 } |
| 169 } | 125 } |
| 170 } | 126 } |
| 171 | 127 |
| 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 | |
| 205 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) { | 128 TEST(ManagePasswordsViewUtilTest, GetManagePasswordsDialogTitleText) { |
| 206 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) { | 129 for (size_t i = 0; i < arraysize(kDomainsTestCases); ++i) { |
| 207 SCOPED_TRACE(testing::Message() << "user_visible_url = " | 130 SCOPED_TRACE(testing::Message() << "user_visible_url = " |
| 208 << kDomainsTestCases[i].user_visible_url | 131 << kDomainsTestCases[i].user_visible_url |
| 209 << ", password_origin_url = " | 132 << ", password_origin_url = " |
| 210 << kDomainsTestCases[i].form_origin_url); | 133 << kDomainsTestCases[i].form_origin_url); |
| 211 | 134 |
| 212 base::string16 title; | 135 base::string16 title; |
| 213 gfx::Range title_link_range; | 136 gfx::Range title_link_range; |
| 214 GetManagePasswordsDialogTitleText( | 137 GetManagePasswordsDialogTitleText( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 false /* is_smartlock_branding_enabled */, | 179 false /* is_smartlock_branding_enabled */, |
| 257 GetParam(), | 180 GetParam(), |
| 258 &title, &title_link_range); | 181 &title, &title_link_range); |
| 259 EXPECT_GE(title.find(branding, 0), title.size()); | 182 EXPECT_GE(title.find(branding, 0), title.size()); |
| 260 EXPECT_EQ(0U, title_link_range.start()); | 183 EXPECT_EQ(0U, title_link_range.start()); |
| 261 EXPECT_EQ(0U, title_link_range.end()); | 184 EXPECT_EQ(0U, title_link_range.end()); |
| 262 } | 185 } |
| 263 | 186 |
| 264 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest, | 187 INSTANTIATE_TEST_CASE_P(, AccountChooserDialogTitleTest, |
| 265 ::testing::Bool()); | 188 ::testing::Bool()); |
| OLD | NEW |