OLD | NEW |
(Empty) | |
| 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.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/extensions/extension_tab_helper.h" |
| 12 #include "chrome/browser/extensions/script_badge_controller.h" |
| 13 #include "chrome/browser/extensions/test_extension_system.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 15 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" |
| 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_builder.h" |
| 19 #include "chrome/common/extensions/value_builder.h" |
| 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/navigation_entry.h" |
| 23 #include "content/public/browser/notification_registrar.h" |
| 24 #include "content/public/browser/notification_source.h" |
| 25 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/test/test_browser_thread.h" |
| 27 #include "testing/gmock/include/gmock/gmock.h" |
| 28 |
| 29 using content::BrowserThread; |
| 30 |
| 31 namespace extensions { |
| 32 namespace { |
| 33 |
| 34 class ScriptBadgeControllerTest : public TabContentsTestHarness { |
| 35 public: |
| 36 ScriptBadgeControllerTest() |
| 37 : ui_thread_(BrowserThread::UI, MessageLoop::current()) { |
| 38 } |
| 39 |
| 40 virtual void SetUp() { |
| 41 // Note that this sets a PageActionController into the |
| 42 // extension_tab_helper()->location_bar_controller() field. Do |
| 43 // not use that for testing. |
| 44 TabContentsTestHarness::SetUp(); |
| 45 |
| 46 TestExtensionSystem* extension_system = |
| 47 static_cast<TestExtensionSystem*>(ExtensionSystem::Get( |
| 48 tab_contents()->profile())); |
| 49 |
| 50 // Create an ExtensionService so the ScriptBadgeController can find its |
| 51 // extensions. |
| 52 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 53 extension_service_ = extension_system->CreateExtensionService( |
| 54 &command_line, FilePath(), false); |
| 55 |
| 56 script_executor_.reset(new ScriptExecutor(web_contents())); |
| 57 script_badge_controller_.reset(new ScriptBadgeController( |
| 58 tab_contents(), script_executor_.get())); |
| 59 } |
| 60 |
| 61 protected: |
| 62 ExtensionService* extension_service_; |
| 63 scoped_ptr<ScriptExecutor> script_executor_; |
| 64 scoped_ptr<ScriptBadgeController> script_badge_controller_; |
| 65 |
| 66 private: |
| 67 content::TestBrowserThread ui_thread_; |
| 68 }; |
| 69 |
| 70 struct CountingNotificationObserver : public content::NotificationObserver { |
| 71 CountingNotificationObserver() : events(0) {} |
| 72 |
| 73 virtual void Observe(int type, |
| 74 const content::NotificationSource& source, |
| 75 const content::NotificationDetails& details) OVERRIDE { |
| 76 events++; |
| 77 } |
| 78 |
| 79 int events; |
| 80 }; |
| 81 |
| 82 TEST_F(ScriptBadgeControllerTest, ExecutionMakesBadgeVisible) { |
| 83 content::NotificationRegistrar notification_registrar; |
| 84 |
| 85 EXPECT_THAT(script_badge_controller_->GetCurrentActions(), |
| 86 testing::ElementsAre()); |
| 87 |
| 88 scoped_refptr<const Extension> extension = |
| 89 ExtensionBuilder() |
| 90 .SetManifest(DictionaryBuilder() |
| 91 .Set("name", "Extension with page action") |
| 92 .Set("version", "1.0.0") |
| 93 .Set("manifest_version", 2) |
| 94 .Set("permissions", ListBuilder() |
| 95 .Append("tabs")) |
| 96 .Set("page_action", DictionaryBuilder() |
| 97 .Set("default_title", "Hello"))) |
| 98 .Build(); |
| 99 extension_service_->AddExtension(extension); |
| 100 |
| 101 // Establish a page id. |
| 102 NavigateAndCommit(GURL("http://www.google.com")); |
| 103 |
| 104 CountingNotificationObserver location_bar_updated; |
| 105 notification_registrar.Add( |
| 106 &location_bar_updated, |
| 107 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, |
| 108 content::Source<Profile>(tab_contents()->profile())); |
| 109 |
| 110 // Initially, no script badges. |
| 111 EXPECT_THAT(script_badge_controller_->GetCurrentActions(), |
| 112 testing::ElementsAre()); |
| 113 |
| 114 script_badge_controller_->OnExecuteScriptFinished( |
| 115 extension->id(), true, |
| 116 tab_contents()->web_contents()->GetController().GetActiveEntry()-> |
| 117 GetPageID(), |
| 118 ""); |
| 119 EXPECT_THAT(script_badge_controller_->GetCurrentActions(), |
| 120 testing::ElementsAre(extension->script_badge())); |
| 121 EXPECT_THAT(location_bar_updated.events, testing::Gt(0)); |
| 122 }; |
| 123 |
| 124 } // namespace |
| 125 } // namespace extensions |
OLD | NEW |