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 "chrome/browser/extensions/browser_event_router.h" 6 #include "chrome/browser/extensions/extension_action.h" 7 #include "chrome/browser/extensions/extension_action_manager.h" 8 #include "chrome/browser/extensions/extension_apitest.h" 9 #include "chrome/browser/extensions/extension_service.h" 10 #include "chrome/browser/extensions/extension_system.h" 11 #include "chrome/browser/extensions/location_bar_controller.h" 12 #include "chrome/browser/extensions/tab_helper.h" 13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" 16 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/extensions/extension.h" 18 #include "chrome/common/extensions/features/feature_channel.h" 19 #include "chrome/test/base/ui_test_utils.h" 20 #include "content/public/browser/web_contents.h" 21 #include "net/dns/mock_host_resolver.h" 22 #include "net/test/embedded_test_server/embedded_test_server.h" 23 #include "testing/gmock/include/gmock/gmock.h" 24 25 namespace extensions { 26 namespace { 27 28 class ScriptBadgeApiTest : public ExtensionApiTest { 29 public: 30 ScriptBadgeApiTest() : trunk_(chrome::VersionInfo::CHANNEL_UNKNOWN) {} 31 32 protected: 33 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 34 ExtensionApiTest::SetUpCommandLine(command_line); 35 command_line->AppendSwitchASCII(switches::kScriptBadges, "1"); 36 } 37 38 private: 39 extensions::ScopedCurrentChannel trunk_; 40 }; 41 42 IN_PROC_BROWSER_TEST_F(ScriptBadgeApiTest, Basics) { 43 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 44 ASSERT_TRUE(RunExtensionTest("script_badge/basics")) << message_; 45 const Extension* extension = GetSingleLoadedExtension(); 46 ASSERT_TRUE(extension) << message_; 47 ExtensionAction* script_badge = 48 ExtensionActionManager::Get(browser()->profile())-> 49 GetScriptBadge(*extension); 50 ASSERT_TRUE(script_badge); 51 const extensions::LocationBarController* location_bar_controller = 52 extensions::TabHelper::FromWebContents( 53 browser()->tab_strip_model()->GetActiveWebContents())-> 54 location_bar_controller(); 55 56 const int tab_id = SessionID::IdForTab( 57 browser()->tab_strip_model()->GetActiveWebContents()); 58 EXPECT_EQ(GURL(extension->GetResourceURL("default_popup.html")), 59 script_badge->GetPopupUrl(tab_id)); 60 61 EXPECT_FALSE(script_badge->GetIsVisible(tab_id)); 62 EXPECT_THAT(location_bar_controller->GetCurrentActions(), 63 testing::ElementsAre()); 64 65 { 66 ResultCatcher catcher; 67 // Tell the extension to update the script badge state. 68 ui_test_utils::NavigateToURL( 69 browser(), GURL(extension->GetResourceURL("update.html"))); 70 ASSERT_TRUE(catcher.GetNextResult()); 71 } 72 73 // Test that we received the changes. 74 EXPECT_EQ(GURL(extension->GetResourceURL("set_popup.html")), 75 script_badge->GetPopupUrl(tab_id)); 76 77 { 78 // Simulate the script badge being clicked. 79 ResultCatcher catcher; 80 ExtensionService* service = extensions::ExtensionSystem::Get( 81 browser()->profile())->extension_service(); 82 service->browser_event_router()->ScriptBadgeExecuted( 83 browser()->profile(), *script_badge, tab_id); 84 EXPECT_TRUE(catcher.GetNextResult()); 85 } 86 87 { 88 ResultCatcher catcher; 89 // Visit a non-extension page so the extension can run a content script and 90 // cause the script badge to be animated in. 91 ui_test_utils::NavigateToURL( 92 browser(), embedded_test_server()->GetURL("/title1.html")); 93 ASSERT_TRUE(catcher.GetNextResult()); 94 } 95 EXPECT_TRUE(script_badge->GetIsVisible(tab_id)); 96 EXPECT_THAT(location_bar_controller->GetCurrentActions(), 97 testing::ElementsAre(script_badge)); 98 } 99 100 } // namespace 101 } // namespace extensions 102