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 { | |
yzshen1
2013/12/14 01:07:01
Is there any reason that this namespace covers the
scheib
2013/12/14 05:43:50
Yes, the above class is given Friend access outsid
| |
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 const extensions::ProcessManager::ImpulseCallbackForTesting | |
yzshen1
2013/12/14 01:07:01
nit: this return value doesn't need to be const
scheib
2013/12/14 05:43:50
Done.
| |
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(base::Closure quit_callback, | |
yzshen1
2013/12/14 01:07:01
const &, and please move it to the next line.
scheib
2013/12/14 05:43:50
Done.
| |
180 const std::string& extension_id_from_test, | |
181 const std::string& extension_id_from_manager) { | |
182 if (extension_id_from_test == extension_id_from_manager) { | |
183 if (++observed_ >= goal_) { | |
184 quit_callback.Run(); | |
185 } | |
186 } | |
187 } | |
188 | |
189 int observed_; | |
190 int goal_; | |
191 const std::string extension_id_; | |
192 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
193 }; | |
194 | |
195 } // namespace | |
196 | |
111 // Disable on Mac only. http://crbug.com/95139 | 197 // Disable on Mac only. http://crbug.com/95139 |
112 #if defined(OS_MACOSX) | 198 #if defined(OS_MACOSX) |
113 #define MAYBE_Basic DISABLED_Basic | 199 #define MAYBE_Basic DISABLED_Basic |
114 #else | 200 #else |
115 #define MAYBE_Basic Basic | 201 #define MAYBE_Basic Basic |
116 #endif | 202 #endif |
117 | 203 |
118 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { | 204 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { |
119 host_resolver()->AddRule("a.com", "127.0.0.1"); | 205 host_resolver()->AddRule("a.com", "127.0.0.1"); |
120 ASSERT_TRUE(StartEmbeddedTestServer()); | 206 ASSERT_TRUE(StartEmbeddedTestServer()); |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 // Close all browsers - app should continue running. | 581 // Close all browsers - app should continue running. |
496 set_exit_when_last_browser_closes(false); | 582 set_exit_when_last_browser_closes(false); |
497 CloseBrowser(browser()); | 583 CloseBrowser(browser()); |
498 | 584 |
499 // Post a task to unload the extension - this should cause Chrome to exit | 585 // Post a task to unload the extension - this should cause Chrome to exit |
500 // cleanly (not crash). | 586 // cleanly (not crash). |
501 UnloadExtensionViaTask(extension->id()); | 587 UnloadExtensionViaTask(extension->id()); |
502 content::RunAllPendingInMessageLoop(); | 588 content::RunAllPendingInMessageLoop(); |
503 ASSERT_TRUE(WaitForBackgroundMode(false)); | 589 ASSERT_TRUE(WaitForBackgroundMode(false)); |
504 } | 590 } |
591 | |
592 // Verify active NaCl embeds cause many keepalive impulses to be sent. | |
593 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveActive) { | |
594 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
595 LaunchTestingApp(); | |
596 extensions::ProcessManager* manager = | |
597 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
598 ImpulseCallbackCounter active_impulse_counter(extension()->id()); | |
599 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
600 | |
601 // Target 1 second: 1 second / 50ms throttle * 2 embeds == 40 impulses. | |
602 manager->SetKeepaliveImpulseCallbackForTesting( | |
603 active_impulse_counter.SetGoalAndGetCallback(40)); | |
yzshen1
2013/12/14 01:07:01
Is 1 second too long for a single test? Maybe we c
scheib
2013/12/14 05:43:50
Done. Changed to 20 ~~ .5 seconds. This is fairly
| |
604 active_impulse_counter.Wait(); | |
605 } | |
606 | |
607 // Verify that nacl modules that go idle will not send keepalive impulses. | |
608 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveIdle) { | |
609 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true); | |
610 LaunchTestingApp(); | |
611 extensions::ProcessManager* manager = | |
612 extensions::ExtensionSystem::Get(browser()->profile())->process_manager(); | |
613 ImpulseCallbackCounter idle_impulse_counter(extension()->id()); | |
614 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied()); | |
615 | |
616 manager->SetKeepaliveImpulseDecrementCallbackForTesting( | |
617 idle_impulse_counter.SetGoalAndGetCallback(1)); | |
618 nacl_modules_loaded.Reply("be idle"); | |
619 idle_impulse_counter.Wait(); | |
620 } | |
621 | |
OLD | NEW |