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.
5 #include "chrome/browser/ui/toolbar/origin_chip_info.h"
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/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "extensions/common/extension_builder.h"
18 #include "grit/chromium_strings.h"
19 #include "grit/generated_resources.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/login/user_manager.h"
25 #include "chrome/browser/chromeos/settings/cros_settings.h"
26 #include "chrome/browser/chromeos/settings/device_settings_service.h"
31 const char kExampleUrl
[] = "http://www.example.com/";
32 const char kExampleUrlSecure
[] = "https://www.example.com/";
33 const char kOtherUrl
[] = "http://chrome.google.com/";
37 class OriginChipInfoTest
: public ChromeRenderViewHostTestHarness
,
38 public extensions::IconImage::Observer
{
40 OriginChipInfoTest() : icon_image_(NULL
) {}
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_
; }
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_
);
52 EXPECT_EQ(expect_update
, info_
->Update(web_contents(), toolbar_model()));
55 virtual void SetUp() OVERRIDE
{
56 ChromeRenderViewHostTestHarness::SetUp();
57 #if defined(OS_CHROMEOS)
58 test_user_manager_
.reset(new chromeos::ScopedTestUserManager());
60 toolbar_model_
.reset(new TestToolbarModel());
61 info_
.reset(new OriginChipInfo(this, profile()));
64 virtual void TearDown() OVERRIDE
{
66 toolbar_model_
.reset();
67 #if defined(OS_CHROMEOS)
68 test_user_manager_
.reset();
70 ChromeRenderViewHostTestHarness::TearDown();
73 virtual void OnExtensionIconImageChanged(
74 extensions::IconImage
* image
) OVERRIDE
{
75 // We keep the value of |image| to check if it's set, but the actual value
81 scoped_ptr
<OriginChipInfo
> info_
;
82 scoped_ptr
<TestToolbarModel
> toolbar_model_
;
84 extensions::IconImage
* icon_image_
;
86 #if defined(OS_CHROMEOS)
87 // OriginChipInfo sometimes calls into the extensions system, which, on CrOS,
88 // requires these services to be initialized.
89 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
90 chromeos::ScopedTestCrosSettings test_cros_settings_
;
91 scoped_ptr
<chromeos::ScopedTestUserManager
> test_user_manager_
;
94 DISALLOW_COPY_AND_ASSIGN(OriginChipInfoTest
);
97 TEST_F(OriginChipInfoTest
, NoChangeShouldNotUpdate
) {
98 SetURL(kExampleUrl
, true);
99 SetURL(kExampleUrl
, false);
102 TEST_F(OriginChipInfoTest
, ChangeShouldUpdate
) {
103 SetURL(kExampleUrl
, true);
104 SetURL(kOtherUrl
, true);
107 TEST_F(OriginChipInfoTest
, NormalOrigin
) {
108 SetURL(kExampleUrl
, true);
110 EXPECT_EQ(base::ASCIIToUTF16("example.com"), info()->label());
111 EXPECT_EQ(url(), info()->displayed_url());
112 EXPECT_EQ(ToolbarModel::NONE
, info()->security_level());
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);
120 EXPECT_EQ(base::ASCIIToUTF16("Example [US] example.com"), info()->label());
121 EXPECT_EQ(url(), info()->displayed_url());
122 EXPECT_EQ(ToolbarModel::EV_SECURE
, info()->security_level());
125 TEST_F(OriginChipInfoTest
, ChromeOrigin
) {
126 SetURL("chrome://version", true);
128 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_TITLE
),
130 EXPECT_EQ(url(), info()->displayed_url());
131 EXPECT_EQ(IDR_PRODUCT_LOGO_16
, info()->icon());
133 // chrome://flags has no title, so the title should be the product name.
134 SetURL("chrome://flags", true);
136 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME
), info()->label());
137 EXPECT_EQ(url(), info()->displayed_url());
138 EXPECT_EQ(IDR_PRODUCT_LOGO_16
, info()->icon());
140 SetURL(kExampleUrl
, true);
141 EXPECT_NE(IDR_PRODUCT_LOGO_16
, info()->icon());
144 TEST_F(OriginChipInfoTest
, ExtensionOrigin
) {
145 CommandLine
command_line(CommandLine::NO_PROGRAM
);
146 extensions::TestExtensionSystem
* test_extension_system
=
147 static_cast<extensions::TestExtensionSystem
*>(
148 extensions::ExtensionSystem::Get(profile()));
150 // |extension_service| is owned by |profile()|.
151 ExtensionService
* extension_service
=
152 test_extension_system
->CreateExtensionService(&command_line
,
156 // Create a dummy extension.
157 const char kFooId
[] = "hhgbjpmdppecanaaogonaigmmifgpaph";
158 const char kFooName
[] = "Foo";
159 extensions::ExtensionBuilder foo_extension
;
160 foo_extension
.SetManifest(extensions::DictionaryBuilder()
161 .Set("name", kFooName
)
162 .Set("version", "1.0.0")
163 .Set("manifest_version", 2));
164 foo_extension
.SetID(kFooId
);
165 extension_service
->AddExtension(foo_extension
.Build().get());
167 const extensions::IconImage
* null_image
= NULL
;
169 // Navigate to a URL from that extension.
170 SetURL(base::StringPrintf("chrome-extension://%s/index.html", kFooId
), true);
171 EXPECT_NE(null_image
, icon_image());
172 EXPECT_EQ(base::ASCIIToUTF16(kFooName
), info()->label());
174 SetURL(kExampleUrl
, true);
175 EXPECT_EQ(null_image
, icon_image());