Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / app_background_page_apitest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/path_service.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/background/background_contents_service.h"
9 #include "chrome/browser/background/background_contents_service_factory.h"
10 #include "chrome/browser/background/background_mode_manager.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_dialogs.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"
22 #include "chrome/common/chrome_switches.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/test/test_notification_tracker.h"
25 #include "content/public/test/test_utils.h"
26 #include "extensions/common/extension.h"
27 #include "extensions/common/switches.h"
28 #include "net/dns/mock_host_resolver.h"
29 #include "net/test/embedded_test_server/embedded_test_server.h"
30
31 #if !defined(DISABLE_NACL)
32 #include "components/nacl/browser/nacl_process_host.h"
33 #endif
34
35 #if defined(OS_MACOSX)
36 #include "base/mac/scoped_nsautorelease_pool.h"
37 #endif
38
39 using base::ASCIIToUTF16;
40 using extensions::Extension;
41
42 class AppBackgroundPageApiTest : public ExtensionApiTest {
43  public:
44   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
45     ExtensionApiTest::SetUpCommandLine(command_line);
46     command_line->AppendSwitch(switches::kDisablePopupBlocking);
47     command_line->AppendSwitch(extensions::switches::kAllowHTTPBackgroundPage);
48   }
49
50   bool CreateApp(const std::string& app_manifest,
51                  base::FilePath* app_dir) {
52     if (!app_dir_.CreateUniqueTempDir()) {
53       LOG(ERROR) << "Unable to create a temporary directory.";
54       return false;
55     }
56     base::FilePath manifest_path = app_dir_.path().AppendASCII("manifest.json");
57     int bytes_written = base::WriteFile(manifest_path,
58                                         app_manifest.data(),
59                                         app_manifest.size());
60     if (bytes_written != static_cast<int>(app_manifest.size())) {
61       LOG(ERROR) << "Unable to write complete manifest to file. Return code="
62                  << bytes_written;
63       return false;
64     }
65     *app_dir = app_dir_.path();
66     return true;
67   }
68
69   bool WaitForBackgroundMode(bool expected_background_mode) {
70 #if defined(OS_CHROMEOS)
71     // BackgroundMode is not supported on chromeos, so we should test the
72     // behavior of BackgroundContents, but not the background mode state itself.
73     return true;
74 #else
75     BackgroundModeManager* manager =
76         g_browser_process->background_mode_manager();
77     // If background mode is disabled on this platform (e.g. cros), then skip
78     // this check.
79     if (!manager || !manager->IsBackgroundModePrefEnabled()) {
80       DLOG(WARNING) << "Skipping check - background mode disabled";
81       return true;
82     }
83     if (manager->IsBackgroundModeActive() == expected_background_mode)
84       return true;
85
86     // We are not currently in the expected state - wait for the state to
87     // change.
88     content::WindowedNotificationObserver watcher(
89         chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED,
90         content::NotificationService::AllSources());
91     watcher.Wait();
92     return manager->IsBackgroundModeActive() == expected_background_mode;
93 #endif
94   }
95
96   void CloseBrowser(Browser* browser) {
97     content::WindowedNotificationObserver observer(
98         chrome::NOTIFICATION_BROWSER_CLOSED,
99         content::NotificationService::AllSources());
100     browser->window()->Close();
101 #if defined(OS_MACOSX)
102     // BrowserWindowController depends on the auto release pool being recycled
103     // in the message loop to delete itself, which frees the Browser object
104     // which fires this event.
105     AutoreleasePool()->Recycle();
106 #endif
107     observer.Wait();
108   }
109
110   void UnloadExtensionViaTask(const std::string& id) {
111     base::MessageLoop::current()->PostTask(
112         FROM_HERE,
113         base::Bind(&AppBackgroundPageApiTest::UnloadExtension, this, id));
114   }
115
116  private:
117   base::ScopedTempDir app_dir_;
118 };
119
120 namespace {
121
122 // Fixture to assist in testing v2 app background pages containing
123 // Native Client embeds.
124 class AppBackgroundPageNaClTest : public AppBackgroundPageApiTest {
125  public:
126   AppBackgroundPageNaClTest()
127       : extension_(NULL) {}
128   virtual ~AppBackgroundPageNaClTest() {
129   }
130
131   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
132     AppBackgroundPageApiTest::SetUpCommandLine(command_line);
133 #if !defined(DISABLE_NACL)
134     nacl::NaClProcessHost::SetPpapiKeepAliveThrottleForTesting(50);
135 #endif
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     base::FilePath app_dir;
147     PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir);
148     app_dir = app_dir.AppendASCII(
149         "ppapi/tests/extensions/background_keepalive/newlib");    
150     extension_ = LoadExtension(app_dir);
151     ASSERT_TRUE(extension_);
152   }
153
154  private:
155   const Extension* extension_;
156 };
157
158 // Produces an extensions::ProcessManager::ImpulseCallbackForTesting callback
159 // that will match a specified goal and can be waited on.
160 class ImpulseCallbackCounter {
161  public:
162   explicit ImpulseCallbackCounter(extensions::ProcessManager* manager,
163                                   const std::string& extension_id)
164       : observed_(0),
165         goal_(0),
166         manager_(manager),
167         extension_id_(extension_id) {
168   }
169
170   extensions::ProcessManager::ImpulseCallbackForTesting
171       SetGoalAndGetCallback(int goal) {
172     observed_ = 0;
173     goal_ = goal;
174     message_loop_runner_ = new content::MessageLoopRunner();
175     return base::Bind(&ImpulseCallbackCounter::ImpulseCallback,
176                       base::Unretained(this),
177                       message_loop_runner_->QuitClosure(),
178                       extension_id_);
179   }
180
181   void Wait() {
182     message_loop_runner_->Run();
183   }
184  private:
185   void 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         // Clear callback to free reference to message loop.
192         manager_->SetKeepaliveImpulseCallbackForTesting(
193             extensions::ProcessManager::ImpulseCallbackForTesting());
194         manager_->SetKeepaliveImpulseDecrementCallbackForTesting(
195             extensions::ProcessManager::ImpulseCallbackForTesting());
196         quit_callback.Run();
197       }
198     }
199   }
200
201   int observed_;
202   int goal_;
203   extensions::ProcessManager* manager_;
204   const std::string extension_id_;
205   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
206 };
207
208 }  // namespace
209
210 // Disable on Mac only.  http://crbug.com/95139
211 #if defined(OS_MACOSX)
212 #define MAYBE_Basic DISABLED_Basic
213 #else
214 #define MAYBE_Basic Basic
215 #endif
216
217 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_Basic) {
218   host_resolver()->AddRule("a.com", "127.0.0.1");
219   ASSERT_TRUE(StartEmbeddedTestServer());
220
221   std::string app_manifest = base::StringPrintf(
222       "{"
223       "  \"name\": \"App\","
224       "  \"version\": \"0.1\","
225       "  \"manifest_version\": 2,"
226       "  \"app\": {"
227       "    \"urls\": ["
228       "      \"http://a.com/\""
229       "    ],"
230       "    \"launch\": {"
231       "      \"web_url\": \"http://a.com:%d/\""
232       "    }"
233       "  },"
234       "  \"permissions\": [\"background\"]"
235       "}",
236       embedded_test_server()->port());
237
238   base::FilePath app_dir;
239   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
240   ASSERT_TRUE(LoadExtension(app_dir));
241   // Background mode should not be active until a background page is created.
242   ASSERT_TRUE(WaitForBackgroundMode(false));
243   ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
244   // The test closes the background contents, so we should fall back to no
245   // background mode at the end.
246   ASSERT_TRUE(WaitForBackgroundMode(false));
247 }
248
249 // Crashy, http://crbug.com/69215.
250 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_LacksPermission) {
251   host_resolver()->AddRule("a.com", "127.0.0.1");
252   ASSERT_TRUE(StartEmbeddedTestServer());
253
254   std::string app_manifest = base::StringPrintf(
255       "{"
256       "  \"name\": \"App\","
257       "  \"version\": \"0.1\","
258       "  \"manifest_version\": 2,"
259       "  \"app\": {"
260       "    \"urls\": ["
261       "      \"http://a.com/\""
262       "    ],"
263       "    \"launch\": {"
264       "      \"web_url\": \"http://a.com:%d/\""
265       "    }"
266       "  }"
267       "}",
268       embedded_test_server()->port());
269
270   base::FilePath app_dir;
271   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
272   ASSERT_TRUE(LoadExtension(app_dir));
273   ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
274       << message_;
275   ASSERT_TRUE(WaitForBackgroundMode(false));
276 }
277
278 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
279   host_resolver()->AddRule("a.com", "127.0.0.1");
280   ASSERT_TRUE(StartEmbeddedTestServer());
281
282   std::string app_manifest = base::StringPrintf(
283       "{"
284       "  \"name\": \"App\","
285       "  \"version\": \"0.1\","
286       "  \"manifest_version\": 2,"
287       "  \"app\": {"
288       "    \"urls\": ["
289       "      \"http://a.com/\""
290       "    ],"
291       "    \"launch\": {"
292       "      \"web_url\": \"http://a.com:%d/\""
293       "    }"
294       "  },"
295       "  \"permissions\": [\"background\"],"
296       "  \"background\": {"
297       "    \"page\": \"http://a.com:%d/test.html\""
298       "  }"
299       "}",
300       embedded_test_server()->port(),
301       embedded_test_server()->port());
302
303   base::FilePath app_dir;
304   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
305   // Background mode should not be active now because no background app was
306   // loaded.
307   ASSERT_TRUE(LoadExtension(app_dir));
308   // Background mode be active now because a background page was created when
309   // the app was loaded.
310   ASSERT_TRUE(WaitForBackgroundMode(true));
311
312   const Extension* extension = GetSingleLoadedExtension();
313   ASSERT_TRUE(
314       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
315           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
316   UnloadExtension(extension->id());
317 }
318
319 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) {
320   // Keep the task manager up through this test to verify that a crash doesn't
321   // happen when window.open creates a background page that switches
322   // RenderViewHosts. See http://crbug.com/165138.
323   chrome::ShowTaskManager(browser());
324
325   // Make sure that no BackgroundContentses get deleted (a signal that repeated
326   // window.open calls recreate instances, instead of being no-ops).
327   content::TestNotificationTracker background_deleted_tracker;
328   background_deleted_tracker.ListenFor(
329       chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
330       content::Source<Profile>(browser()->profile()));
331
332   host_resolver()->AddRule("a.com", "127.0.0.1");
333   ASSERT_TRUE(StartEmbeddedTestServer());
334
335   std::string app_manifest = base::StringPrintf(
336       "{"
337       "  \"name\": \"App\","
338       "  \"version\": \"0.1\","
339       "  \"manifest_version\": 2,"
340       "  \"app\": {"
341       "    \"urls\": ["
342       "      \"http://a.com/\""
343       "    ],"
344       "    \"launch\": {"
345       "      \"web_url\": \"http://a.com:%d/test.html\""
346       "    }"
347       "  },"
348       "  \"permissions\": [\"background\"],"
349       "  \"background\": {"
350       "    \"allow_js_access\": false"
351       "  }"
352       "}",
353       embedded_test_server()->port());
354
355   base::FilePath app_dir;
356   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
357   ASSERT_TRUE(LoadExtension(app_dir));
358
359   // There isn't a background page loaded initially.
360   const Extension* extension = GetSingleLoadedExtension();
361   ASSERT_FALSE(
362       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
363           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
364   // The test makes sure that window.open returns null.
365   ASSERT_TRUE(RunExtensionTest("app_background_page/no_js")) << message_;
366   // And after it runs there should be a background page.
367   ASSERT_TRUE(
368       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
369           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
370
371   EXPECT_EQ(0u, background_deleted_tracker.size());
372   UnloadExtension(extension->id());
373 }
374
375 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsManifestBackgroundPage) {
376   host_resolver()->AddRule("a.com", "127.0.0.1");
377   ASSERT_TRUE(StartEmbeddedTestServer());
378
379   std::string app_manifest = base::StringPrintf(
380       "{"
381       "  \"name\": \"App\","
382       "  \"version\": \"0.1\","
383       "  \"manifest_version\": 2,"
384       "  \"app\": {"
385       "    \"urls\": ["
386       "      \"http://a.com/\""
387       "    ],"
388       "    \"launch\": {"
389       "      \"web_url\": \"http://a.com:%d/\""
390       "    }"
391       "  },"
392       "  \"permissions\": [\"background\"],"
393       "  \"background\": {"
394       "    \"page\": \"http://a.com:%d/bg.html\","
395       "    \"allow_js_access\": false"
396       "  }"
397       "}",
398       embedded_test_server()->port(),
399       embedded_test_server()->port());
400
401   base::FilePath app_dir;
402   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
403   ASSERT_TRUE(LoadExtension(app_dir));
404
405   // The background page should load, but window.open should return null.
406   const Extension* extension = GetSingleLoadedExtension();
407   ASSERT_TRUE(
408       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
409           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
410   ASSERT_TRUE(RunExtensionTest("app_background_page/no_js_manifest")) <<
411       message_;
412   UnloadExtension(extension->id());
413 }
414
415 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) {
416   host_resolver()->AddRule("a.com", "127.0.0.1");
417   ASSERT_TRUE(StartEmbeddedTestServer());
418
419   std::string app_manifest = base::StringPrintf(
420       "{"
421       "  \"name\": \"App\","
422       "  \"version\": \"0.1\","
423       "  \"manifest_version\": 2,"
424       "  \"app\": {"
425       "    \"urls\": ["
426       "      \"http://a.com/\""
427       "    ],"
428       "    \"launch\": {"
429       "      \"web_url\": \"http://a.com:%d/\""
430       "    }"
431       "  },"
432       "  \"permissions\": [\"background\"]"
433       "}",
434       embedded_test_server()->port());
435
436   base::FilePath app_dir;
437   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
438   ASSERT_TRUE(LoadExtension(app_dir));
439   const Extension* extension = GetSingleLoadedExtension();
440   ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_;
441   UnloadExtension(extension->id());
442 }
443
444 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoPagesWithManifest) {
445   host_resolver()->AddRule("a.com", "127.0.0.1");
446   ASSERT_TRUE(StartEmbeddedTestServer());
447
448   std::string app_manifest = base::StringPrintf(
449       "{"
450       "  \"name\": \"App\","
451       "  \"version\": \"0.1\","
452       "  \"manifest_version\": 2,"
453       "  \"app\": {"
454       "    \"urls\": ["
455       "      \"http://a.com/\""
456       "    ],"
457       "    \"launch\": {"
458       "      \"web_url\": \"http://a.com:%d/\""
459       "    }"
460       "  },"
461       "  \"background\": {"
462       "    \"page\": \"http://a.com:%d/bg.html\""
463       "  },"
464       "  \"permissions\": [\"background\"]"
465       "}",
466       embedded_test_server()->port(),
467       embedded_test_server()->port());
468
469   base::FilePath app_dir;
470   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
471   ASSERT_TRUE(LoadExtension(app_dir));
472   const Extension* extension = GetSingleLoadedExtension();
473   ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) <<
474       message_;
475   UnloadExtension(extension->id());
476 }
477
478 // Times out occasionally -- see crbug.com/108493
479 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenPopupFromBGPage) {
480   host_resolver()->AddRule("a.com", "127.0.0.1");
481   ASSERT_TRUE(StartEmbeddedTestServer());
482
483   std::string app_manifest = base::StringPrintf(
484       "{"
485       "  \"name\": \"App\","
486       "  \"version\": \"0.1\","
487       "  \"manifest_version\": 2,"
488       "  \"app\": {"
489       "    \"urls\": ["
490       "      \"http://a.com/\""
491       "    ],"
492       "    \"launch\": {"
493       "      \"web_url\": \"http://a.com:%d/\""
494       "    }"
495       "  },"
496       "  \"background\": { \"page\": \"http://a.com:%d/extensions/api_test/"
497       "app_background_page/bg_open/bg_open_bg.html\" },"
498       "  \"permissions\": [\"background\"]"
499       "}",
500       embedded_test_server()->port(),
501       embedded_test_server()->port());
502
503   base::FilePath app_dir;
504   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
505   ASSERT_TRUE(LoadExtension(app_dir));
506   ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_;
507 }
508
509 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenThenClose) {
510   host_resolver()->AddRule("a.com", "127.0.0.1");
511   ASSERT_TRUE(StartEmbeddedTestServer());
512
513   std::string app_manifest = base::StringPrintf(
514       "{"
515       "  \"name\": \"App\","
516       "  \"version\": \"0.1\","
517       "  \"manifest_version\": 2,"
518       "  \"app\": {"
519       "    \"urls\": ["
520       "      \"http://a.com/\""
521       "    ],"
522       "    \"launch\": {"
523       "      \"web_url\": \"http://a.com:%d/\""
524       "    }"
525       "  },"
526       "  \"permissions\": [\"background\"]"
527       "}",
528       embedded_test_server()->port());
529
530   base::FilePath app_dir;
531   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
532   ASSERT_TRUE(LoadExtension(app_dir));
533   // There isn't a background page loaded initially.
534   const Extension* extension = GetSingleLoadedExtension();
535   ASSERT_FALSE(
536       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
537           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
538   // Background mode should not be active until a background page is created.
539   ASSERT_TRUE(WaitForBackgroundMode(false));
540   ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_;
541   // Background mode should be active now because a background page was created.
542   ASSERT_TRUE(WaitForBackgroundMode(true));
543   ASSERT_TRUE(
544       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
545           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
546   // Now close the BackgroundContents.
547   ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_;
548   // Background mode should no longer be active.
549   ASSERT_TRUE(WaitForBackgroundMode(false));
550   ASSERT_FALSE(
551       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
552           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
553 }
554
555 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, UnloadExtensionWhileHidden) {
556   host_resolver()->AddRule("a.com", "127.0.0.1");
557   ASSERT_TRUE(StartEmbeddedTestServer());
558
559   std::string app_manifest = base::StringPrintf(
560       "{"
561       "  \"name\": \"App\","
562       "  \"version\": \"0.1\","
563       "  \"manifest_version\": 2,"
564       "  \"app\": {"
565       "    \"urls\": ["
566       "      \"http://a.com/\""
567       "    ],"
568       "    \"launch\": {"
569       "      \"web_url\": \"http://a.com:%d/\""
570       "    }"
571       "  },"
572       "  \"permissions\": [\"background\"],"
573       "  \"background\": {"
574       "    \"page\": \"http://a.com:%d/test.html\""
575       "  }"
576       "}",
577       embedded_test_server()->port(),
578       embedded_test_server()->port());
579
580   base::FilePath app_dir;
581   ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
582   // Background mode should not be active now because no background app was
583   // loaded.
584   ASSERT_TRUE(LoadExtension(app_dir));
585   // Background mode be active now because a background page was created when
586   // the app was loaded.
587   ASSERT_TRUE(WaitForBackgroundMode(true));
588
589   const Extension* extension = GetSingleLoadedExtension();
590   ASSERT_TRUE(
591       BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
592           GetAppBackgroundContents(ASCIIToUTF16(extension->id())));
593
594   // Close all browsers - app should continue running.
595   set_exit_when_last_browser_closes(false);
596   CloseBrowser(browser());
597
598   // Post a task to unload the extension - this should cause Chrome to exit
599   // cleanly (not crash).
600   UnloadExtensionViaTask(extension->id());
601   content::RunAllPendingInMessageLoop();
602   ASSERT_TRUE(WaitForBackgroundMode(false));
603 }
604
605 // Verify active NaCl embeds cause many keepalive impulses to be sent.
606 // Disabled on Windows due to flakiness: http://crbug.com/346278
607 #if defined(OS_WIN)
608 #define MAYBE_BackgroundKeepaliveActive DISABLED_BackgroundKeepaliveActive
609 #else
610 #define MAYBE_BackgroundKeepaliveActive BackgroundKeepaliveActive
611 #endif
612 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest,
613                        MAYBE_BackgroundKeepaliveActive) {
614 #if !defined(DISABLE_NACL)
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 active_impulse_counter(manager, extension()->id());
620   EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied());
621
622   // Target .5 seconds: .5 seconds / 50ms throttle * 2 embeds == 20 impulses.
623   manager->SetKeepaliveImpulseCallbackForTesting(
624       active_impulse_counter.SetGoalAndGetCallback(20));
625   active_impulse_counter.Wait();
626 #endif
627 }
628
629 // Verify that nacl modules that go idle will not send keepalive impulses.
630 // Disabled on windows due to Win XP failures:
631 // DesktopWindowTreeHostWin::HandleCreate not implemented. crbug.com/331954
632 #if defined(OS_WIN)
633 #define MAYBE_BackgroundKeepaliveIdle DISABLED_BackgroundKeepaliveIdle
634 #else
635 // ASAN errors appearing: https://crbug.com/332440
636 #define MAYBE_BackgroundKeepaliveIdle DISABLED_BackgroundKeepaliveIdle
637 #endif
638 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest,
639                        MAYBE_BackgroundKeepaliveIdle) {
640 #if !defined(DISABLE_NACL)
641   ExtensionTestMessageListener nacl_modules_loaded("nacl_modules_loaded", true);
642   LaunchTestingApp();
643   extensions::ProcessManager* manager =
644     extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
645   ImpulseCallbackCounter idle_impulse_counter(manager, extension()->id());
646   EXPECT_TRUE(nacl_modules_loaded.WaitUntilSatisfied());
647
648   manager->SetKeepaliveImpulseDecrementCallbackForTesting(
649       idle_impulse_counter.SetGoalAndGetCallback(1));
650   nacl_modules_loaded.Reply("be idle");
651   idle_impulse_counter.Wait();
652 #endif
653 }
654