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 extensions::Extension; | 36 using extensions::Extension; |
32 | 37 |
33 class AppBackgroundPageApiTest : public ExtensionApiTest { | 38 class AppBackgroundPageApiTest : public ExtensionApiTest { |
34 public: | 39 public: |
35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 40 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 void UnloadExtensionViaTask(const std::string& id) { | 106 void UnloadExtensionViaTask(const std::string& id) { |
102 base::MessageLoop::current()->PostTask( | 107 base::MessageLoop::current()->PostTask( |
103 FROM_HERE, | 108 FROM_HERE, |
104 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); | 109 base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id)); |
105 } | 110 } |
106 | 111 |
107 private: | 112 private: |
108 base::ScopedTempDir app_dir_; | 113 base::ScopedTempDir app_dir_; |
109 }; | 114 }; |
110 | 115 |
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 | |
111 // Disable on Mac only. http://crbug.com/95139 | 198 // Disable on Mac only. http://crbug.com/95139 |
112 #if defined(OS_MACOSX) | 199 #if defined(OS_MACOSX) |
113 #define MAYBE_Basic DISABLED_Basic | 200 #define MAYBE_Basic DISABLED_Basic |
114 #else | 201 #else |
115 #define MAYBE_Basic Basic | 202 #define MAYBE_Basic Basic |
116 #endif | 203 #endif |
117 | 204 |
118 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { | 205 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { |
119 host_resolver()->AddRule("a.com", "127.0.0.1"); | 206 host_resolver()->AddRule("a.com", "127.0.0.1"); |
120 ASSERT_TRUE(StartEmbeddedTestServer()); | 207 ASSERT_TRUE(StartEmbeddedTestServer()); |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 // Close all browsers - app should continue running. | 582 // Close all browsers - app should continue running. |
496 set_exit_when_last_browser_closes(false); | 583 set_exit_when_last_browser_closes(false); |
497 CloseBrowser(browser()); | 584 CloseBrowser(browser()); |
498 | 585 |
499 // Post a task to unload the extension - this should cause Chrome to exit | 586 // Post a task to unload the extension - this should cause Chrome to exit |
500 // cleanly (not crash). | 587 // cleanly (not crash). |
501 UnloadExtensionViaTask(extension->id()); | 588 UnloadExtensionViaTask(extension->id()); |
502 content::RunAllPendingInMessageLoop(); | 589 content::RunAllPendingInMessageLoop(); |
503 ASSERT_TRUE(WaitForBackgroundMode(false)); | 590 ASSERT_TRUE(WaitForBackgroundMode(false)); |
504 } | 591 } |
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. | |
Mark Seaborn
2013/12/18 21:07:57
I didn't follow the test closely enough to tell wh
scheib
2013/12/19 01:31:37
Worst case the test runs long, and it would have t
| |
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 |