1 // Copyright 2014 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.
5 #include "extensions/browser/mojo/keep_alive_impl.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "content/public/browser/notification_service.h"
10 #include "extensions/browser/extensions_test.h"
11 #include "extensions/browser/process_manager.h"
12 #include "extensions/common/extension_builder.h"
14 namespace extensions
{
16 class KeepAliveTest
: public ExtensionsTest
{
19 : notification_service_(content::NotificationService::Create()) {}
20 ~KeepAliveTest() override
{}
22 void SetUp() override
{
23 ExtensionsTest::SetUp();
24 message_loop_
.reset(new base::MessageLoop
);
31 .Set("manifest_version", 2)
33 DictionaryBuilder().Set(
35 DictionaryBuilder().Set(
37 ListBuilder().Append("background.js")))))
38 .SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
42 void TearDown() override
{
43 message_loop_
.reset();
44 ExtensionsTest::TearDown();
47 void WaitUntilLazyKeepAliveChanges() {
48 int initial_keep_alive_count
= GetKeepAliveCount();
49 while (GetKeepAliveCount() == initial_keep_alive_count
) {
50 base::RunLoop().RunUntilIdle();
54 void CreateKeepAlive(mojo::InterfaceRequest
<KeepAlive
> request
) {
55 KeepAliveImpl::Create(browser_context(), extension_
.get(), request
.Pass());
58 const Extension
* extension() { return extension_
.get(); }
60 int GetKeepAliveCount() {
61 return ProcessManager::Get(browser_context())
62 ->GetLazyKeepaliveCount(extension());
66 scoped_ptr
<base::MessageLoop
> message_loop_
;
67 scoped_ptr
<content::NotificationService
> notification_service_
;
68 scoped_refptr
<const Extension
> extension_
;
70 DISALLOW_COPY_AND_ASSIGN(KeepAliveTest
);
73 TEST_F(KeepAliveTest
, Basic
) {
74 mojo::InterfacePtr
<KeepAlive
> keep_alive
;
75 CreateKeepAlive(mojo::GetProxy(&keep_alive
));
76 EXPECT_EQ(1, GetKeepAliveCount());
79 WaitUntilLazyKeepAliveChanges();
80 EXPECT_EQ(0, GetKeepAliveCount());
83 TEST_F(KeepAliveTest
, TwoKeepAlives
) {
84 mojo::InterfacePtr
<KeepAlive
> keep_alive
;
85 CreateKeepAlive(mojo::GetProxy(&keep_alive
));
86 EXPECT_EQ(1, GetKeepAliveCount());
88 mojo::InterfacePtr
<KeepAlive
> other_keep_alive
;
89 CreateKeepAlive(mojo::GetProxy(&other_keep_alive
));
90 EXPECT_EQ(2, GetKeepAliveCount());
93 WaitUntilLazyKeepAliveChanges();
94 EXPECT_EQ(1, GetKeepAliveCount());
96 other_keep_alive
.reset();
97 WaitUntilLazyKeepAliveChanges();
98 EXPECT_EQ(0, GetKeepAliveCount());
101 } // namespace extensions