| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/location_bar/origin_chip_info.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 12 #include "chrome/browser/extensions/test_extension_system.h" | |
| 13 #include "chrome/browser/ui/toolbar/test_toolbar_model.h" | |
| 14 #include "chrome/grit/chromium_strings.h" | |
| 15 #include "chrome/grit/generated_resources.h" | |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 17 #include "chrome/test/base/testing_profile.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "extensions/common/extension_builder.h" | |
| 20 #include "grit/theme_resources.h" | |
| 21 #include "ui/base/l10n/l10n_util.h" | |
| 22 | |
| 23 #if defined(OS_CHROMEOS) | |
| 24 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" | |
| 25 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 26 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 27 #endif | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 const char kExampleUrl[] = "http://www.example.com/"; | |
| 32 const char kExampleUrlSecure[] = "https://www.example.com/"; | |
| 33 const char kOtherUrl[] = "http://chrome.google.com/"; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 class OriginChipInfoTest : public ChromeRenderViewHostTestHarness, | |
| 38 public extensions::IconImage::Observer { | |
| 39 public: | |
| 40 OriginChipInfoTest() : icon_image_(NULL) {} | |
| 41 | |
| 42 TestToolbarModel* toolbar_model() { return toolbar_model_.get(); } | |
| 43 OriginChipInfo* info() { return info_.get(); } | |
| 44 const GURL& url() const { return url_; } | |
| 45 const extensions::IconImage* icon_image() const { return icon_image_; } | |
| 46 | |
| 47 void SetURL(const std::string& dest_url, bool expect_update) { | |
| 48 url_ = GURL(dest_url); | |
| 49 NavigateAndCommit(url_); | |
| 50 toolbar_model_->set_url(url_); | |
| 51 | |
| 52 EXPECT_EQ(expect_update, info_->Update(web_contents(), toolbar_model())); | |
| 53 } | |
| 54 | |
| 55 void SetUp() override { | |
| 56 ChromeRenderViewHostTestHarness::SetUp(); | |
| 57 #if defined(OS_CHROMEOS) | |
| 58 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); | |
| 59 #endif | |
| 60 toolbar_model_.reset(new TestToolbarModel()); | |
| 61 info_.reset(new OriginChipInfo(this, profile())); | |
| 62 } | |
| 63 | |
| 64 void TearDown() override { | |
| 65 info_.reset(); | |
| 66 toolbar_model_.reset(); | |
| 67 #if defined(OS_CHROMEOS) | |
| 68 test_user_manager_.reset(); | |
| 69 #endif | |
| 70 ChromeRenderViewHostTestHarness::TearDown(); | |
| 71 } | |
| 72 | |
| 73 void OnExtensionIconImageChanged(extensions::IconImage* image) override { | |
| 74 // We keep the value of |image| to check if it's set, but the actual value | |
| 75 // is never used. | |
| 76 icon_image_ = image; | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 scoped_ptr<OriginChipInfo> info_; | |
| 81 scoped_ptr<TestToolbarModel> toolbar_model_; | |
| 82 GURL url_; | |
| 83 extensions::IconImage* icon_image_; | |
| 84 | |
| 85 #if defined(OS_CHROMEOS) | |
| 86 // OriginChipInfo sometimes calls into the extensions system, which, on CrOS, | |
| 87 // requires these services to be initialized. | |
| 88 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
| 89 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
| 90 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; | |
| 91 #endif | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(OriginChipInfoTest); | |
| 94 }; | |
| 95 | |
| 96 TEST_F(OriginChipInfoTest, NoChangeShouldNotUpdate) { | |
| 97 SetURL(kExampleUrl, true); | |
| 98 SetURL(kExampleUrl, false); | |
| 99 } | |
| 100 | |
| 101 TEST_F(OriginChipInfoTest, ChangeShouldUpdate) { | |
| 102 SetURL(kExampleUrl, true); | |
| 103 SetURL(kOtherUrl, true); | |
| 104 } | |
| 105 | |
| 106 TEST_F(OriginChipInfoTest, NormalOrigin) { | |
| 107 SetURL(kExampleUrl, true); | |
| 108 | |
| 109 EXPECT_EQ(base::ASCIIToUTF16("example.com"), info()->label()); | |
| 110 EXPECT_EQ(base::ASCIIToUTF16(kExampleUrl), info()->Tooltip()); | |
| 111 EXPECT_EQ(url(), info()->displayed_url()); | |
| 112 EXPECT_EQ(ToolbarModel::NONE, info()->security_level()); | |
| 113 } | |
| 114 | |
| 115 TEST_F(OriginChipInfoTest, EVSecureOrigin) { | |
| 116 toolbar_model()->set_security_level(ToolbarModel::EV_SECURE); | |
| 117 toolbar_model()->set_ev_cert_name(base::ASCIIToUTF16("Example [US]")); | |
| 118 SetURL(kExampleUrlSecure, true); | |
| 119 | |
| 120 EXPECT_EQ(base::ASCIIToUTF16("Example [US] example.com"), info()->label()); | |
| 121 EXPECT_EQ(base::ASCIIToUTF16(kExampleUrlSecure), info()->Tooltip()); | |
| 122 EXPECT_EQ(url(), info()->displayed_url()); | |
| 123 EXPECT_EQ(ToolbarModel::EV_SECURE, info()->security_level()); | |
| 124 } | |
| 125 | |
| 126 TEST_F(OriginChipInfoTest, ChromeOrigin) { | |
| 127 const char kChromeOrigin1[] = "chrome://version/"; | |
| 128 SetURL(kChromeOrigin1, true); | |
| 129 | |
| 130 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_TITLE), | |
| 131 info()->label()); | |
| 132 EXPECT_EQ(base::ASCIIToUTF16(kChromeOrigin1), info()->Tooltip()); | |
| 133 EXPECT_EQ(url(), info()->displayed_url()); | |
| 134 EXPECT_EQ(IDR_PRODUCT_LOGO_16, info()->icon()); | |
| 135 | |
| 136 // chrome://flags has no title, so the title should be the product name. | |
| 137 const char kChromeOrigin2[] = "chrome://flags/"; | |
| 138 SetURL(kChromeOrigin2, true); | |
| 139 | |
| 140 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME), info()->label()); | |
| 141 EXPECT_EQ(base::ASCIIToUTF16(kChromeOrigin2), info()->Tooltip()); | |
| 142 EXPECT_EQ(url(), info()->displayed_url()); | |
| 143 EXPECT_EQ(IDR_PRODUCT_LOGO_16, info()->icon()); | |
| 144 | |
| 145 SetURL(kExampleUrl, true); | |
| 146 EXPECT_NE(IDR_PRODUCT_LOGO_16, info()->icon()); | |
| 147 } | |
| 148 | |
| 149 TEST_F(OriginChipInfoTest, ExtensionOrigin) { | |
| 150 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 151 extensions::TestExtensionSystem* test_extension_system = | |
| 152 static_cast<extensions::TestExtensionSystem*>( | |
| 153 extensions::ExtensionSystem::Get(profile())); | |
| 154 | |
| 155 // |extension_service| is owned by |profile()|. | |
| 156 ExtensionService* extension_service = | |
| 157 test_extension_system->CreateExtensionService(&command_line, | |
| 158 base::FilePath(), | |
| 159 false); | |
| 160 | |
| 161 // Create a dummy extension. | |
| 162 const char kFooId[] = "hhgbjpmdppecanaaogonaigmmifgpaph"; | |
| 163 const char kFooName[] = "Foo"; | |
| 164 extensions::ExtensionBuilder foo_extension; | |
| 165 foo_extension.SetManifest(extensions::DictionaryBuilder() | |
| 166 .Set("name", kFooName) | |
| 167 .Set("version", "1.0.0") | |
| 168 .Set("manifest_version", 2)); | |
| 169 foo_extension.SetID(kFooId); | |
| 170 extension_service->AddExtension(foo_extension.Build().get()); | |
| 171 | |
| 172 const extensions::IconImage* null_image = NULL; | |
| 173 | |
| 174 // Navigate to a URL from that extension. | |
| 175 const std::string extension_origin = | |
| 176 base::StringPrintf("chrome-extension://%s/index.html", kFooId); | |
| 177 SetURL(extension_origin, true); | |
| 178 EXPECT_NE(null_image, icon_image()); | |
| 179 EXPECT_EQ(base::ASCIIToUTF16(kFooName), info()->label()); | |
| 180 EXPECT_EQ(base::ASCIIToUTF16(extension_origin), info()->Tooltip()); | |
| 181 | |
| 182 SetURL(kExampleUrl, true); | |
| 183 EXPECT_EQ(null_image, icon_image()); | |
| 184 } | |
| OLD | NEW |