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" | |
5 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
6 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/browser/background/background_contents_service.h" | 8 #include "chrome/browser/background/background_contents_service.h" |
8 #include "chrome/browser/background/background_contents_service_factory.h" | 9 #include "chrome/browser/background/background_contents_service_factory.h" |
9 #include "chrome/browser/background/background_mode_manager.h" | 10 #include "chrome/browser/background/background_mode_manager.h" |
10 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/extensions/extension_apitest.h" | 13 #include "chrome/browser/extensions/extension_apitest.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_dialogs.h" | 18 #include "chrome/browser/ui/browser_dialogs.h" |
17 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
20 #include "chrome/browser/ui/extensions/application_launch.h" | |
21 #include "chrome/common/chrome_paths.h" | |
18 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
19 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
20 #include "content/public/test/test_notification_tracker.h" | 24 #include "content/public/test/test_notification_tracker.h" |
21 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
22 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
23 #include "extensions/common/switches.h" | 27 #include "extensions/common/switches.h" |
24 #include "net/dns/mock_host_resolver.h" | 28 #include "net/dns/mock_host_resolver.h" |
25 #include "net/test/embedded_test_server/embedded_test_server.h" | 29 #include "net/test/embedded_test_server/embedded_test_server.h" |
30 #include "ppapi/shared_impl/ppapi_switches.h" | |
26 | 31 |
27 #if defined(OS_MACOSX) | 32 #if defined(OS_MACOSX) |
28 #include "base/mac/scoped_nsautorelease_pool.h" | 33 #include "base/mac/scoped_nsautorelease_pool.h" |
29 #endif | 34 #endif |
30 | 35 |
31 using base::ASCIIToUTF16; | 36 using base::ASCIIToUTF16; |
32 using extensions::Extension; | 37 using extensions::Extension; |
33 | 38 |
34 class AppBackgroundPageApiTest : public ExtensionApiTest { | 39 class AppBackgroundPageApiTest : public ExtensionApiTest { |
35 public: | 40 public: |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 void UnloadExtensionViaTask(const std::string& id) { | 107 void UnloadExtensionViaTask(const std::string& id) { |
103 base::MessageLoop::current()->PostTask( | 108 base::MessageLoop::current()->PostTask( |
104 FROM_HERE, | 109 FROM_HERE, |
105 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); | 110 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); |
106 } | 111 } |
107 | 112 |
108 private: | 113 private: |
109 base::ScopedTempDir app_dir_; | 114 base::ScopedTempDir app_dir_; |
110 }; | 115 }; |
111 | 116 |
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. The callback | |
157 // must be released when it returns true, as it holds a reference to the | |
158 // internal message loop. | |
159 class ImpulseCallbackCounter { | |
160 public: | |
161 explicit ImpulseCallbackCounter(const std::string& extension_id) | |
162 : observed_(0), | |
163 goal_(0), | |
164 extension_id_(extension_id) { | |
165 } | |
166 | |
167 extensions::ProcessManager::ImpulseCallbackForTesting | |
168 SetGoalAndGetCallback(int goal) { | |
169 observed_ = 0; | |
170 goal_ = goal; | |
171 message_loop_runner_ = new content::MessageLoopRunner(); | |
172 return base::Bind(&ImpulseCallbackCounter::ImpulseCallback, | |
173 base::Unretained(this), | |
174 message_loop_runner_->QuitClosure(), | |
175 extension_id_); | |
176 } | |
177 | |
178 void Wait() { | |
179 message_loop_runner_->Run(); | |
180 // Assert that the callback produced in SetGoalAndGetCallback | |
181 // has been destroyed and released its reference to the message loop. | |
182 ASSERT_TRUE(message_loop_runner_->HasOneRef()); | |
183 } | |
184 private: | |
185 bool ImpulseCallback( | |
186 const base::Closure& quit_callback, | |
187 const std::string& extension_id_from_test, | |
188 const std::string& extension_id_from_manager) { | |
189 if (extension_id_from_test == extension_id_from_manager) { | |
190 if (++observed_ >= goal_) { | |
191 quit_callback.Run(); | |
192 return true; // Indicate the callback must be released. | |
193 } | |
194 } | |
195 return false; | |
196 } | |
197 | |
198 int observed_; | |
199 int goal_; | |
200 const std::string extension_id_; | |
201 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
202 }; | |
203 | |
204 } // namespace | |
205 | |
112 // Disable on Mac only. http://crbug.com/95139 | 206 // Disable on Mac only. http://crbug.com/95139 |
113 #if defined(OS_MACOSX) | 207 #if defined(OS_MACOSX) |
114 #define MAYBE_Basic DISABLED_Basic | 208 #define MAYBE_Basic DISABLED_Basic |
115 #else | 209 #else |
116 #define MAYBE_Basic Basic | 210 #define MAYBE_Basic Basic |
117 #endif | 211 #endif |
118 | 212 |
119 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { | 213 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { |
120 host_resolver()->AddRule("a.com", "127.0.0.1"); | 214 host_resolver()->AddRule("a.com", "127.0.0.1"); |
121 ASSERT_TRUE(StartEmbeddedTestServer()); | 215 ASSERT_TRUE(StartEmbeddedTestServer()); |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 // Close all browsers - app should continue running. | 590 // Close all browsers - app should continue running. |
497 set_exit_when_last_browser_closes(false); | 591 set_exit_when_last_browser_closes(false); |
498 CloseBrowser(browser()); | 592 CloseBrowser(browser()); |
499 | 593 |
500 // Post a task to unload the extension - this should cause Chrome to exit | 594 // Post a task to unload the extension - this should cause Chrome to exit |
501 // cleanly (not crash). | 595 // cleanly (not crash). |
502 UnloadExtensionViaTask(extension->id()); | 596 UnloadExtensionViaTask(extension->id()); |
503 content::RunAllPendingInMessageLoop(); | 597 content::RunAllPendingInMessageLoop(); |
504 ASSERT_TRUE(WaitForBackgroundMode(false)); | 598 ASSERT_TRUE(WaitForBackgroundMode(false)); |
505 } | 599 } |
600 | |
601 // Verify active NaCl embeds cause many keepalive impulses to be sent. | |
602 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveActive) { | |
603 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
604 LaunchTestingApp(); | |
605 extensions::ProcessManager* manager = | |
606 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
607 ImpulseCallbackCounter active_impulse_counter(extension()->id()); | |
608 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
609 | |
610 // Target .5 seconds: .5 seconds / 50ms throttle * 2 embeds == 20 impulses. | |
611 manager->SetKeepaliveImpulseCallbackForTesting( | |
612 active_impulse_counter.SetGoalAndGetCallback(20)); | |
613 ASSERT_NO_FATAL_FAILURE(active_impulse_counter.Wait()); | |
yzshen1
2014/01/03 22:10:10
Why do we need this? ASSERT_TRUE() in Wait() is su
scheib
2014/01/03 23:11:29
Correct gtest form is to wrap any subroutine call
yzshen1
2014/01/03 23:23:28
Thanks, I didn't know that!
On 2014/01/03 23:11:29
| |
614 } | |
615 | |
616 // Verify that nacl modules that go idle will not send keepalive impulses. | |
617 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveIdle) { | |
618 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
619 LaunchTestingApp(); | |
620 extensions::ProcessManager* manager = | |
621 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
622 ImpulseCallbackCounter idle_impulse_counter(extension()->id()); | |
623 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
624 | |
625 manager->SetKeepaliveImpulseDecrementCallbackForTesting( | |
626 idle_impulse_counter.SetGoalAndGetCallback(1)); | |
627 nacl_modules_loaded.Reply("be idle"); | |
628 ASSERT_NO_FATAL_FAILURE(idle_impulse_counter.Wait()); | |
yzshen1
2014/01/03 22:10:10
ditto
scheib
2014/01/03 23:11:29
ditto
| |
629 } | |
630 | |
OLD | NEW |