| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/path_service.h" | |
| 6 #include "base/strings/stringprintf.h" | 5 #include "base/strings/stringprintf.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/background/background_contents_service.h" | 7 #include "chrome/browser/background/background_contents_service.h" |
| 9 #include "chrome/browser/background/background_contents_service_factory.h" | 8 #include "chrome/browser/background/background_contents_service_factory.h" |
| 10 #include "chrome/browser/background/background_mode_manager.h" | 9 #include "chrome/browser/background/background_mode_manager.h" |
| 11 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/extensions/extension_apitest.h" | 12 #include "chrome/browser/extensions/extension_apitest.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_dialogs.h" | 16 #include "chrome/browser/ui/browser_dialogs.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 17 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/browser/ui/extensions/application_launch.h" | |
| 21 #include "chrome/common/chrome_paths.h" | |
| 22 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 23 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/test/test_notification_tracker.h" | 20 #include "content/public/test/test_notification_tracker.h" |
| 25 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
| 26 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 27 #include "extensions/common/switches.h" | 23 #include "extensions/common/switches.h" |
| 28 #include "net/dns/mock_host_resolver.h" | 24 #include "net/dns/mock_host_resolver.h" |
| 29 #include "net/test/embedded_test_server/embedded_test_server.h" | 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 30 #include "ppapi/shared_impl/ppapi_switches.h" | |
| 31 | 26 |
| 32 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
| 33 #include "base/mac/scoped_nsautorelease_pool.h" | 28 #include "base/mac/scoped_nsautorelease_pool.h" |
| 34 #endif | 29 #endif |
| 35 | 30 |
| 36 using extensions::Extension; | 31 using extensions::Extension; |
| 37 | 32 |
| 38 class AppBackgroundPageApiTest : public ExtensionApiTest { | 33 class AppBackgroundPageApiTest : public ExtensionApiTest { |
| 39 public: | 34 public: |
| 40 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 void UnloadExtensionViaTask(const std::string& id) { | 101 void UnloadExtensionViaTask(const std::string& id) { |
| 107 base::MessageLoop::current()->PostTask( | 102 base::MessageLoop::current()->PostTask( |
| 108 FROM_HERE, | 103 FROM_HERE, |
| 109 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); | 104 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); |
| 110 } | 105 } |
| 111 | 106 |
| 112 private: | 107 private: |
| 113 base::ScopedTempDir app_dir_; | 108 base::ScopedTempDir app_dir_; |
| 114 }; | 109 }; |
| 115 | 110 |
| 116 namespace { | |
| 117 | |
| 118 // Fixture to assist in testing v2 app background pages containing | |
| 119 // Native Client embeds. | |
| 120 class AppBackgroundPageNaClTest : public AppBackgroundPageApiTest { | |
| 121 public: | |
| 122 AppBackgroundPageNaClTest() | |
| 123 : extension_(NULL) { | |
| 124 PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir_); | |
| 125 app_dir_ = app_dir_.AppendASCII( | |
| 126 "ppapi/tests/extensions/background_keepalive/newlib"); | |
| 127 } | |
| 128 virtual ~AppBackgroundPageNaClTest() { | |
| 129 } | |
| 130 | |
| 131 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 132 AppBackgroundPageApiTest::SetUpCommandLine(command_line); | |
| 133 command_line->AppendSwitchASCII( | |
| 134 switches::kPpapiKeepAliveThrottle, "50"); | |
| 135 command_line->AppendSwitchASCII( | |
| 136 extensions::switches::kEventPageIdleTime, "1000"); | |
| 137 command_line->AppendSwitchASCII( | |
| 138 extensions::switches::kEventPageSuspendingTime, "1000"); | |
| 139 } | |
| 140 | |
| 141 const Extension* extension() { return extension_; } | |
| 142 | |
| 143 protected: | |
| 144 void LaunchTestingApp() { | |
| 145 extension_ = LoadExtension(app_dir_); | |
| 146 ASSERT_TRUE(extension_); | |
| 147 } | |
| 148 | |
| 149 private: | |
| 150 base::FilePath app_dir_; | |
| 151 const Extension* extension_; | |
| 152 }; | |
| 153 | |
| 154 // Produces an extensions::ProcessManager::ImpulseCallbackForTesting callback | |
| 155 // that will match a specified goal and can be waited on. | |
| 156 class ImpulseCallbackCounter { | |
| 157 public: | |
| 158 explicit ImpulseCallbackCounter(const std::string& extension_id) | |
| 159 : observed_(0), | |
| 160 goal_(0), | |
| 161 extension_id_(extension_id) { | |
| 162 } | |
| 163 | |
| 164 extensions::ProcessManager::ImpulseCallbackForTesting | |
| 165 SetGoalAndGetCallback(int goal) { | |
| 166 observed_ = 0; | |
| 167 goal_ = goal; | |
| 168 message_loop_runner_ = new content::MessageLoopRunner(); | |
| 169 return base::Bind(&ImpulseCallbackCounter::ImpulseCallback, | |
| 170 base::Unretained(this), | |
| 171 message_loop_runner_->QuitClosure(), | |
| 172 extension_id_); | |
| 173 } | |
| 174 | |
| 175 void Wait() { | |
| 176 message_loop_runner_->Run(); | |
| 177 } | |
| 178 private: | |
| 179 void ImpulseCallback( | |
| 180 const base::Closure& quit_callback, | |
| 181 const std::string& extension_id_from_test, | |
| 182 const std::string& extension_id_from_manager) { | |
| 183 if (extension_id_from_test == extension_id_from_manager) { | |
| 184 if (++observed_ >= goal_) { | |
| 185 quit_callback.Run(); | |
| 186 } | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 int observed_; | |
| 191 int goal_; | |
| 192 const std::string extension_id_; | |
| 193 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
| 194 }; | |
| 195 | |
| 196 } // namespace | |
| 197 | |
| 198 // Disable on Mac only. http://crbug.com/95139 | 111 // Disable on Mac only. http://crbug.com/95139 |
| 199 #if defined(OS_MACOSX) | 112 #if defined(OS_MACOSX) |
| 200 #define MAYBE_Basic DISABLED_Basic | 113 #define MAYBE_Basic DISABLED_Basic |
| 201 #else | 114 #else |
| 202 #define MAYBE_Basic Basic | 115 #define MAYBE_Basic Basic |
| 203 #endif | 116 #endif |
| 204 | 117 |
| 205 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { | 118 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { |
| 206 host_resolver()->AddRule("a.com", "127.0.0.1"); | 119 host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 207 ASSERT_TRUE(StartEmbeddedTestServer()); | 120 ASSERT_TRUE(StartEmbeddedTestServer()); |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 // Close all browsers - app should continue running. | 495 // Close all browsers - app should continue running. |
| 583 set_exit_when_last_browser_closes(false); | 496 set_exit_when_last_browser_closes(false); |
| 584 CloseBrowser(browser()); | 497 CloseBrowser(browser()); |
| 585 | 498 |
| 586 // Post a task to unload the extension - this should cause Chrome to exit | 499 // Post a task to unload the extension - this should cause Chrome to exit |
| 587 // cleanly (not crash). | 500 // cleanly (not crash). |
| 588 UnloadExtensionViaTask(extension->id()); | 501 UnloadExtensionViaTask(extension->id()); |
| 589 content::RunAllPendingInMessageLoop(); | 502 content::RunAllPendingInMessageLoop(); |
| 590 ASSERT_TRUE(WaitForBackgroundMode(false)); | 503 ASSERT_TRUE(WaitForBackgroundMode(false)); |
| 591 } | 504 } |
| 592 | |
| 593 // Verify active NaCl embeds cause many keepalive impulses to be sent. | |
| 594 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveActive) { | |
| 595 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
| 596 LaunchTestingApp(); | |
| 597 extensions::ProcessManager* manager = | |
| 598 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
| 599 ImpulseCallbackCounter active_impulse_counter(extension()->id()); | |
| 600 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
| 601 | |
| 602 // Target .5 seconds: .5 seconds / 50ms throttle * 2 embeds == 20 impulses. | |
| 603 manager->SetKeepaliveImpulseCallbackForTesting( | |
| 604 active_impulse_counter.SetGoalAndGetCallback(20)); | |
| 605 active_impulse_counter.Wait(); | |
| 606 } | |
| 607 | |
| 608 // Verify that nacl modules that go idle will not send keepalive impulses. | |
| 609 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveIdle) { | |
| 610 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
| 611 LaunchTestingApp(); | |
| 612 extensions::ProcessManager* manager = | |
| 613 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
| 614 ImpulseCallbackCounter idle_impulse_counter(extension()->id()); | |
| 615 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
| 616 | |
| 617 manager->SetKeepaliveImpulseDecrementCallbackForTesting( | |
| 618 idle_impulse_counter.SetGoalAndGetCallback(1)); | |
| 619 nacl_modules_loaded.Reply("be idle"); | |
| 620 idle_impulse_counter.Wait(); | |
| 621 } | |
| 622 | |
| OLD | NEW |