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