1 // Copyright (c) 2012 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 <string>
6
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/extensions/extension_action.h"
11 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/page_action_controller.h"
14 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/extensions/test_extension_system.h"
16 #include "chrome/browser/sessions/session_id.h"
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/common/value_builder.h"
22
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/login/users/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 extensions {
30 namespace {
31
32 class PageActionControllerTest : public ChromeRenderViewHostTestHarness {
33 protected:
SetUp()34 virtual void SetUp() OVERRIDE {
35 ChromeRenderViewHostTestHarness::SetUp();
36 #if defined OS_CHROMEOS
37 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
38 #endif
39 TabHelper::CreateForWebContents(web_contents());
40 // Create an ExtensionService so the PageActionController can find its
41 // extensions.
42 CommandLine command_line(CommandLine::NO_PROGRAM);
43 Profile* profile =
44 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
45 extension_service_ = static_cast<TestExtensionSystem*>(
46 ExtensionSystem::Get(profile))->CreateExtensionService(
47 &command_line, base::FilePath(), false);
48 }
49
TearDown()50 virtual void TearDown() OVERRIDE {
51 #if defined OS_CHROMEOS
52 test_user_manager_.reset();
53 #endif
54 ChromeRenderViewHostTestHarness::TearDown();
55 }
56
tab_id()57 int tab_id() {
58 return SessionID::IdForTab(web_contents());
59 }
60
61 ExtensionService* extension_service_;
62
63 private:
64 #if defined OS_CHROMEOS
65 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
66 chromeos::ScopedTestCrosSettings test_cros_settings_;
67 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
68 #endif
69 };
70
TEST_F(PageActionControllerTest,NavigationClearsState)71 TEST_F(PageActionControllerTest, NavigationClearsState) {
72 scoped_refptr<const Extension> extension =
73 ExtensionBuilder()
74 .SetManifest(DictionaryBuilder()
75 .Set("name", "Extension with page action")
76 .Set("version", "1.0.0")
77 .Set("manifest_version", 2)
78 .Set("permissions", ListBuilder()
79 .Append("tabs"))
80 .Set("page_action", DictionaryBuilder()
81 .Set("default_title", "Hello")))
82 .Build();
83 extension_service_->AddExtension(extension.get());
84
85 NavigateAndCommit(GURL("http://www.google.com"));
86
87 ExtensionAction& page_action =
88 *ExtensionActionManager::Get(profile())->GetPageAction(*extension.get());
89 page_action.SetTitle(tab_id(), "Goodbye");
90 page_action.SetPopupUrl(
91 tab_id(), extension->GetResourceURL("popup.html"));
92
93 EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id()));
94 EXPECT_EQ(extension->GetResourceURL("popup.html"),
95 page_action.GetPopupUrl(tab_id()));
96
97 // Within-page navigation should keep the settings.
98 NavigateAndCommit(GURL("http://www.google.com/#hash"));
99
100 EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id()));
101 EXPECT_EQ(extension->GetResourceURL("popup.html"),
102 page_action.GetPopupUrl(tab_id()));
103
104 // Should discard the settings, and go back to the defaults.
105 NavigateAndCommit(GURL("http://www.yahoo.com"));
106
107 EXPECT_EQ("Hello", page_action.GetTitle(tab_id()));
108 EXPECT_EQ(GURL(), page_action.GetPopupUrl(tab_id()));
109 };
110
111 } // namespace
112 } // namespace extensions
113