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/extension_registry.h"
11 #include "extensions/browser/extension_system.h"
12 #include "extensions/browser/extensions_test.h"
13 #include "extensions/browser/mock_extension_system.h"
14 #include "extensions/browser/process_manager.h"
15 #include "extensions/browser/test_extensions_browser_client.h"
16 #include "extensions/common/extension_builder.h"
18 namespace extensions
{
20 class KeepAliveTest
: public ExtensionsTest
{
23 : notification_service_(content::NotificationService::Create()) {}
24 ~KeepAliveTest() override
{}
26 void SetUp() override
{
27 ExtensionsTest::SetUp();
28 message_loop_
.reset(new base::MessageLoop
);
29 browser_client_
.reset(new TestExtensionsBrowserClient(browser_context()));
30 browser_client_
->set_extension_system_factory(&extension_system_factory_
);
31 ExtensionsBrowserClient::Set(browser_client_
.get());
38 .Set("manifest_version", 2)
40 DictionaryBuilder().Set(
42 DictionaryBuilder().Set(
44 ListBuilder().Append("background.js")))))
45 .SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
49 void TearDown() override
{
50 message_loop_
.reset();
51 ExtensionsTest::TearDown();
54 void WaitUntilLazyKeepAliveChanges() {
55 int initial_keep_alive_count
= GetKeepAliveCount();
56 while (GetKeepAliveCount() == initial_keep_alive_count
) {
57 base::RunLoop().RunUntilIdle();
61 void CreateKeepAlive(mojo::InterfaceRequest
<KeepAlive
> request
) {
62 KeepAliveImpl::Create(browser_context(), extension_
.get(), request
.Pass());
65 const Extension
* extension() { return extension_
.get(); }
67 int GetKeepAliveCount() {
68 return ProcessManager::Get(browser_context())
69 ->GetLazyKeepaliveCount(extension());
73 scoped_ptr
<base::MessageLoop
> message_loop_
;
74 MockExtensionSystemFactory
<MockExtensionSystem
> extension_system_factory_
;
75 scoped_ptr
<content::NotificationService
> notification_service_
;
76 scoped_refptr
<const Extension
> extension_
;
77 scoped_ptr
<TestExtensionsBrowserClient
> browser_client_
;
79 DISALLOW_COPY_AND_ASSIGN(KeepAliveTest
);
82 TEST_F(KeepAliveTest
, Basic
) {
83 mojo::InterfacePtr
<KeepAlive
> keep_alive
;
84 CreateKeepAlive(mojo::GetProxy(&keep_alive
));
85 EXPECT_EQ(1, GetKeepAliveCount());
88 WaitUntilLazyKeepAliveChanges();
89 EXPECT_EQ(0, GetKeepAliveCount());
92 TEST_F(KeepAliveTest
, TwoKeepAlives
) {
93 mojo::InterfacePtr
<KeepAlive
> keep_alive
;
94 CreateKeepAlive(mojo::GetProxy(&keep_alive
));
95 EXPECT_EQ(1, GetKeepAliveCount());
97 mojo::InterfacePtr
<KeepAlive
> other_keep_alive
;
98 CreateKeepAlive(mojo::GetProxy(&other_keep_alive
));
99 EXPECT_EQ(2, GetKeepAliveCount());
102 WaitUntilLazyKeepAliveChanges();
103 EXPECT_EQ(1, GetKeepAliveCount());
105 other_keep_alive
.reset();
106 WaitUntilLazyKeepAliveChanges();
107 EXPECT_EQ(0, GetKeepAliveCount());
110 } // namespace extensions