Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chrome/browser/extensions/app_background_page_apitest.cc

Issue 112663007: Test Keeping NaCl plugins used in app background pages alive when active. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaner fix by simply clearing callbacks in individual tests. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/extensions/DEPS ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.
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
112 // Disable on Mac only. http://crbug.com/95139 199 // Disable on Mac only. http://crbug.com/95139
113 #if defined(OS_MACOSX) 200 #if defined(OS_MACOSX)
114 #define MAYBE_Basic DISABLED_Basic 201 #define MAYBE_Basic DISABLED_Basic
115 #else 202 #else
116 #define MAYBE_Basic Basic 203 #define MAYBE_Basic Basic
117 #endif 204 #endif
118 205
119 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) { 206 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) {
120 host_resolver()->AddRule("a.com", "127.0.0.1"); 207 host_resolver()->AddRule("a.com", "127.0.0.1");
121 ASSERT_TRUE(StartEmbeddedTestServer()); 208 ASSERT_TRUE(StartEmbeddedTestServer());
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 // Close all browsers - app should continue running. 583 // Close all browsers - app should continue running.
497 set_exit_when_last_browser_closes(false); 584 set_exit_when_last_browser_closes(false);
498 CloseBrowser(browser()); 585 CloseBrowser(browser());
499 586
500 // Post a task to unload the extension - this should cause Chrome to exit 587 // Post a task to unload the extension - this should cause Chrome to exit
501 // cleanly (not crash). 588 // cleanly (not crash).
502 UnloadExtensionViaTask(extension->id()); 589 UnloadExtensionViaTask(extension->id());
503 content::RunAllPendingInMessageLoop(); 590 content::RunAllPendingInMessageLoop();
504 ASSERT_TRUE(WaitForBackgroundMode(false)); 591 ASSERT_TRUE(WaitForBackgroundMode(false));
505 } 592 }
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 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveIdle) {
615 ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true);
616 LaunchTestingApp();
617 extensions::ProcessManager* manager =
618 extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
619 ImpulseCallbackCounter idle_impulse_counter(extension()->id());
620 EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied());
621
622 manager->SetKeepaliveImpulseDecrementCallbackForTesting(
623 idle_impulse_counter.SetGoalAndGetCallback(1));
624 nacl_modules_loaded.Reply("be idle");
625 idle_impulse_counter.Wait();
626
627 // Clear callback to free reference to message loop in ImpulseCallbackCounter.
628 manager->SetKeepaliveImpulseDecrementCallbackForTesting(
629 extensions::ProcessManager::ImpulseCallbackForTesting());
630 }
631
OLDNEW
« no previous file with comments | « chrome/browser/extensions/DEPS ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698