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 base::ASCIIToUTF16; | 31 using base::ASCIIToUTF16; |
37 using extensions::Extension; | 32 using extensions::Extension; |
38 | 33 |
39 class AppBackgroundPageApiTest : public ExtensionApiTest { | 34 class AppBackgroundPageApiTest : public ExtensionApiTest { |
40 public: | 35 public: |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 void UnloadExtensionViaTask(const std::string& id) { | 102 void UnloadExtensionViaTask(const std::string& id) { |
108 base::MessageLoop::current()->PostTask( | 103 base::MessageLoop::current()->PostTask( |
109 FROM_HERE, | 104 FROM_HERE, |
110 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); | 105 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); |
111 } | 106 } |
112 | 107 |
113 private: | 108 private: |
114 base::ScopedTempDir app_dir_; | 109 base::ScopedTempDir app_dir_; |
115 }; | 110 }; |
116 | 111 |
117 namespace { | |
118 | |
119 // Fixture to assist in testing v2 app background pages containing | |
120 // Native Client embeds. | |
121 class AppBackgroundPageNaClTest : public AppBackgroundPageApiTest { | |
122 public: | |
123 AppBackgroundPageNaClTest() | |
124 : extension_(NULL) { | |
125 PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir_); | |
126 app_dir_ = app_dir_.AppendASCII( | |
127 "ppapi/tests/extensions/background_keepalive/newlib"); | |
128 } | |
129 virtual ~AppBackgroundPageNaClTest() { | |
130 } | |
131 | |
132 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
133 AppBackgroundPageApiTest::SetUpCommandLine(command_line); | |
134 command_line->AppendSwitchASCII( | |
135 switches::kPpapiKeepAliveThrottle, "50"); | |
136 command_line->AppendSwitchASCII( | |
137 extensions::switches::kEventPageIdleTime, "1000"); | |
138 command_line->AppendSwitchASCII( | |
139 extensions::switches::kEventPageSuspendingTime, "1000"); | |
140 } | |
141 | |
142 const Extension* extension() { return extension_; } | |
143 | |
144 protected: | |
145 void LaunchTestingApp() { | |
146 extension_ = LoadExtension(app_dir_); | |
147 ASSERT_TRUE(extension_); | |
148 } | |
149 | |
150 private: | |
151 base::FilePath app_dir_; | |
152 const Extension* extension_; | |
153 }; | |
154 | |
155 // Produces an extensions::ProcessManager::ImpulseCallbackForTesting callback | |
156 // that will match a specified goal and can be waited on. | |
157 class ImpulseCallbackCounter { | |
158 public: | |
159 explicit ImpulseCallbackCounter(const std::string& extension_id) | |
160 : observed_(0), | |
161 goal_(0), | |
162 extension_id_(extension_id) { | |
163 } | |
164 | |
165 extensions::ProcessManager::ImpulseCallbackForTesting | |
166 SetGoalAndGetCallback(int goal) { | |
167 observed_ = 0; | |
168 goal_ = goal; | |
169 message_loop_runner_ = new content::MessageLoopRunner(); | |
170 return base::Bind(&ImpulseCallbackCounter::ImpulseCallback, | |
171 base::Unretained(this), | |
172 message_loop_runner_->QuitClosure(), | |
173 extension_id_); | |
174 } | |
175 | |
176 void Wait() { | |
177 message_loop_runner_->Run(); | |
178 } | |
179 private: | |
180 void ImpulseCallback( | |
181 const base::Closure& quit_callback, | |
182 const std::string& extension_id_from_test, | |
183 const std::string& extension_id_from_manager) { | |
184 if (extension_id_from_test == extension_id_from_manager) { | |
185 if (++observed_ >= goal_) { | |
186 quit_callback.Run(); | |
187 } | |
188 } | |
189 } | |
190 | |
191 int observed_; | |
192 int goal_; | |
193 const std::string extension_id_; | |
194 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
195 }; | |
196 | |
197 } // namespace | |
198 | |
199 // Disable on Mac only. http://crbug.com/95139 | 112 // Disable on Mac only. http://crbug.com/95139 |
200 #if defined(OS_MACOSX) | 113 #if defined(OS_MACOSX) |
201 #define MAYBE_Basic DISABLED_Basic | 114 #define MAYBE_Basic DISABLED_Basic |
202 #else | 115 #else |
203 #define MAYBE_Basic Basic | 116 #define MAYBE_Basic Basic |
204 #endif | 117 #endif |
205 | 118 |
206 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { | 119 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { |
207 host_resolver()->AddRule("a.com", "127.0.0.1"); | 120 host_resolver()->AddRule("a.com", "127.0.0.1"); |
208 ASSERT_TRUE(StartEmbeddedTestServer()); | 121 ASSERT_TRUE(StartEmbeddedTestServer()); |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 // Close all browsers - app should continue running. | 496 // Close all browsers - app should continue running. |
584 set_exit_when_last_browser_closes(false); | 497 set_exit_when_last_browser_closes(false); |
585 CloseBrowser(browser()); | 498 CloseBrowser(browser()); |
586 | 499 |
587 // Post a task to unload the extension - this should cause Chrome to exit | 500 // Post a task to unload the extension - this should cause Chrome to exit |
588 // cleanly (not crash). | 501 // cleanly (not crash). |
589 UnloadExtensionViaTask(extension->id()); | 502 UnloadExtensionViaTask(extension->id()); |
590 content::RunAllPendingInMessageLoop(); | 503 content::RunAllPendingInMessageLoop(); |
591 ASSERT_TRUE(WaitForBackgroundMode(false)); | 504 ASSERT_TRUE(WaitForBackgroundMode(false)); |
592 } | 505 } |
593 | |
594 // Verify active NaCl embeds cause many keepalive impulses to be sent. | |
595 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveActive) { | |
596 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
597 LaunchTestingApp(); | |
598 extensions::ProcessManager* manager = | |
599 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
600 ImpulseCallbackCounter active_impulse_counter(extension()->id()); | |
601 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
602 | |
603 // Target .5 seconds: .5 seconds / 50ms throttle * 2 embeds == 20 impulses. | |
604 manager->SetKeepaliveImpulseCallbackForTesting( | |
605 active_impulse_counter.SetGoalAndGetCallback(20)); | |
606 active_impulse_counter.Wait(); | |
607 | |
608 // Clear callback to free reference to message loop in ImpulseCallbackCounter. | |
609 manager->SetKeepaliveImpulseCallbackForTesting( | |
610 extensions::ProcessManager::ImpulseCallbackForTesting()); | |
611 } | |
612 | |
613 // Verify that nacl modules that go idle will not send keepalive impulses. | |
614 // Disabled on windows due to Win XP failures: | |
615 // DesktopRootWindowHostWin::HandleCreate not implemented. crbug.com/331954 | |
616 #if defined(OS_WIN) | |
617 #define MAYBE_BackgroundKeepaliveIdle DISABLED_BackgroundKeepaliveIdle | |
618 #else | |
619 #define MAYBE_BackgroundKeepaliveIdle BackgroundKeepaliveIdle | |
620 #endif | |
621 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, | |
622 MAYBE_BackgroundKeepaliveIdle) { | |
623 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
624 LaunchTestingApp(); | |
625 extensions::ProcessManager* manager = | |
626 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
627 ImpulseCallbackCounter idle_impulse_counter(extension()->id()); | |
628 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
629 | |
630 manager->SetKeepaliveImpulseDecrementCallbackForTesting( | |
631 idle_impulse_counter.SetGoalAndGetCallback(1)); | |
632 nacl_modules_loaded.Reply("be idle"); | |
633 idle_impulse_counter.Wait(); | |
634 | |
635 // Clear callback to free reference to message loop in ImpulseCallbackCounter. | |
636 manager->SetKeepaliveImpulseDecrementCallbackForTesting( | |
637 extensions::ProcessManager::ImpulseCallbackForTesting()); | |
638 } | |
639 | |
OLD | NEW |