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/extension_apitest.h" 6 7 #include "chrome/browser/extensions/lazy_background_page_test_util.h" 8 #include "chrome/browser/notifications/desktop_notification_service.h" 9 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/ui/browser.h" 12 #include "extensions/browser/process_manager.h" 13 #include "extensions/common/extension.h" 14 #include "extensions/common/switches.h" 15 16 class NotificationIdleTest : public ExtensionApiTest { 17 protected: SetUpCommandLine(CommandLine * command_line)18 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 19 ExtensionApiTest::SetUpCommandLine(command_line); 20 21 command_line->AppendSwitchASCII( 22 extensions::switches::kEventPageIdleTime, "1000"); 23 command_line->AppendSwitchASCII( 24 extensions::switches::kEventPageSuspendingTime, "1000"); 25 } 26 LoadExtensionAndWait(const std::string & test_name)27 const extensions::Extension* LoadExtensionAndWait( 28 const std::string& test_name) { 29 LazyBackgroundObserver page_complete; 30 base::FilePath extdir = test_data_dir_.AppendASCII(test_name); 31 const extensions::Extension* extension = LoadExtension(extdir); 32 if (extension) 33 page_complete.Wait(); 34 return extension; 35 } 36 }; 37 IN_PROC_BROWSER_TEST_F(ExtensionApiTest,NotificationsNoPermission)38IN_PROC_BROWSER_TEST_F(ExtensionApiTest, NotificationsNoPermission) { 39 ASSERT_TRUE(RunExtensionTest("notifications/has_not_permission")) << message_; 40 } 41 IN_PROC_BROWSER_TEST_F(ExtensionApiTest,NotificationsHasPermission)42IN_PROC_BROWSER_TEST_F(ExtensionApiTest, NotificationsHasPermission) { 43 DesktopNotificationServiceFactory::GetForProfile(browser()->profile()) 44 ->GrantPermission(GURL( 45 "chrome-extension://peoadpeiejnhkmpaakpnompolbglelel")); 46 ASSERT_TRUE(RunExtensionTest("notifications/has_permission_prefs")) 47 << message_; 48 } 49 50 // MessaceCenter-specific test. 51 #if defined(RUN_MESSAGE_CENTER_TESTS) 52 #define MAYBE_NotificationsAllowUnload NotificationsAllowUnload 53 #else 54 #define MAYBE_NotificationsAllowUnload DISABLED_NotificationsAllowUnload 55 #endif 56 IN_PROC_BROWSER_TEST_F(NotificationIdleTest,MAYBE_NotificationsAllowUnload)57IN_PROC_BROWSER_TEST_F(NotificationIdleTest, MAYBE_NotificationsAllowUnload) { 58 const extensions::Extension* extension = 59 LoadExtensionAndWait("notifications/api/unload"); 60 ASSERT_TRUE(extension) << message_; 61 62 // Lazy Background Page has been shut down. 63 extensions::ProcessManager* pm = 64 extensions::ExtensionSystem::Get(profile())->process_manager(); 65 EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id())); 66 } 67