blob: 283398113dadaa0186c5520a5be25c8c4b57c0d6 [file] [log] [blame]
[email protected]a7789f22014-03-12 16:10:381// 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 "base/command_line.h"
6#include "base/message_loop/message_loop.h"
7#include "chrome/browser/extensions/extension_service.h"
8#include "chrome/browser/extensions/extension_web_ui.h"
9#include "chrome/browser/extensions/test_extension_system.h"
10#include "chrome/test/base/testing_profile.h"
11#include "content/public/test/test_browser_thread.h"
[email protected]03d25812014-06-22 19:41:5512#include "extensions/browser/extension_system.h"
[email protected]a7789f22014-03-12 16:10:3813#include "extensions/common/extension.h"
14#include "extensions/common/extension_builder.h"
15#include "extensions/common/manifest_constants.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:5819#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
[email protected]a7789f22014-03-12 16:10:3820#include "chrome/browser/chromeos/settings/cros_settings.h"
21#include "chrome/browser/chromeos/settings/device_settings_service.h"
22#endif
23
24namespace extensions {
25
26class ExtensionWebUITest : public testing::Test {
27 public:
28 ExtensionWebUITest()
29 : ui_thread_(content::BrowserThread::UI, &message_loop_) {}
30
31 protected:
dcheng72191812014-10-28 20:49:5632 void SetUp() override {
[email protected]a7789f22014-03-12 16:10:3833 profile_.reset(new TestingProfile());
34 TestExtensionSystem* system =
35 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get()));
36 extension_service_ = system->CreateExtensionService(
37 CommandLine::ForCurrentProcess(), base::FilePath(), false);
38 }
39
dcheng72191812014-10-28 20:49:5640 void TearDown() override {
[email protected]a7789f22014-03-12 16:10:3841 profile_.reset();
42 message_loop_.RunUntilIdle();
43 }
44
45 scoped_ptr<TestingProfile> profile_;
46 ExtensionService* extension_service_;
47 base::MessageLoop message_loop_;
48 content::TestBrowserThread ui_thread_;
49
50#if defined OS_CHROMEOS
51 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
52 chromeos::ScopedTestCrosSettings test_cros_settings_;
53 chromeos::ScopedTestUserManager test_user_manager_;
54#endif
55};
56
57// Test that component extension url overrides have lower priority than
58// non-component extension url overrides.
59TEST_F(ExtensionWebUITest, ExtensionURLOverride) {
[email protected]6584f472014-03-18 17:42:2460 // Register a non-component extension.
[email protected]03d25812014-06-22 19:41:5561 DictionaryBuilder manifest;
[email protected]a7789f22014-03-12 16:10:3862 manifest.Set(manifest_keys::kName, "ext1")
63 .Set(manifest_keys::kVersion, "0.1")
64 .Set(std::string(manifest_keys::kChromeURLOverrides),
[email protected]03d25812014-06-22 19:41:5565 DictionaryBuilder().Set("bookmarks", "1.html"));
[email protected]6584f472014-03-18 17:42:2466 scoped_refptr<Extension> ext_unpacked(
[email protected]03d25812014-06-22 19:41:5567 ExtensionBuilder()
[email protected]a7789f22014-03-12 16:10:3868 .SetManifest(manifest)
[email protected]6584f472014-03-18 17:42:2469 .SetLocation(Manifest::UNPACKED)
[email protected]a7789f22014-03-12 16:10:3870 .SetID("abcdefghijabcdefghijabcdefghijaa")
71 .Build());
[email protected]03d25812014-06-22 19:41:5572 extension_service_->AddExtension(ext_unpacked.get());
[email protected]a7789f22014-03-12 16:10:3873
[email protected]6584f472014-03-18 17:42:2474 GURL expected_unpacked_override_url(std::string(ext_unpacked->url().spec()) +
75 "1.html");
76 GURL url("chrome://bookmarks");
77 EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverride(&url, profile_.get()));
78 EXPECT_EQ(url, expected_unpacked_override_url);
79
80 // Register a component extension
[email protected]03d25812014-06-22 19:41:5581 DictionaryBuilder manifest2;
[email protected]a7789f22014-03-12 16:10:3882 manifest2.Set(manifest_keys::kName, "ext2")
83 .Set(manifest_keys::kVersion, "0.1")
84 .Set(std::string(manifest_keys::kChromeURLOverrides),
[email protected]03d25812014-06-22 19:41:5585 DictionaryBuilder().Set("bookmarks", "2.html"));
[email protected]6584f472014-03-18 17:42:2486 scoped_refptr<Extension> ext_component(
[email protected]03d25812014-06-22 19:41:5587 ExtensionBuilder()
[email protected]a7789f22014-03-12 16:10:3888 .SetManifest(manifest2)
[email protected]6584f472014-03-18 17:42:2489 .SetLocation(Manifest::COMPONENT)
[email protected]a7789f22014-03-12 16:10:3890 .SetID("bbabcdefghijabcdefghijabcdefghij")
91 .Build());
[email protected]03d25812014-06-22 19:41:5592 extension_service_->AddComponentExtension(ext_component.get());
[email protected]a7789f22014-03-12 16:10:3893
[email protected]6584f472014-03-18 17:42:2494 // Despite being registered more recently, the component extension should
[email protected]3b09cec52014-05-15 00:33:4195 // not take precedence over the non-component extension.
[email protected]a7789f22014-03-12 16:10:3896 url = GURL("chrome://bookmarks");
97 EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverride(&url, profile_.get()));
98 EXPECT_EQ(url, expected_unpacked_override_url);
99
[email protected]6584f472014-03-18 17:42:24100 GURL expected_component_override_url(
101 std::string(ext_component->url().spec()) + "2.html");
102
[email protected]a7789f22014-03-12 16:10:38103 // Unregister non-component extension. Only component extension remaining.
104 ExtensionWebUI::UnregisterChromeURLOverrides(
105 profile_.get(), URLOverrides::GetChromeURLOverrides(ext_unpacked.get()));
106 url = GURL("chrome://bookmarks");
107 EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverride(&url, profile_.get()));
108 EXPECT_EQ(url, expected_component_override_url);
109
[email protected]6584f472014-03-18 17:42:24110 // This time the non-component extension was registered more recently and
111 // should still take precedence.
[email protected]a7789f22014-03-12 16:10:38112 ExtensionWebUI::RegisterChromeURLOverrides(
113 profile_.get(), URLOverrides::GetChromeURLOverrides(ext_unpacked.get()));
114 url = GURL("chrome://bookmarks");
115 EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverride(&url, profile_.get()));
116 EXPECT_EQ(url, expected_unpacked_override_url);
117}
118
119} // namespace extensions