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/common/mojo/keep_alive.mojom.h"
6 #include "extensions/renderer/api_test_base.h"
7 #include "grit/extensions_renderer_resources.h"
8 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
10 // A test launcher for tests for the stash client defined in
11 // extensions/test/data/keep_alive_client_unittest.js.
13 namespace extensions
{
16 // A KeepAlive implementation that calls provided callbacks on creation and
18 class TestKeepAlive
: public KeepAlive
{
20 TestKeepAlive(const base::Closure
& on_destruction
,
21 mojo::InterfaceRequest
<KeepAlive
> keep_alive
)
22 : on_destruction_(on_destruction
), binding_(this, keep_alive
.Pass()) {}
24 ~TestKeepAlive() override
{ on_destruction_
.Run(); }
26 static void Create(const base::Closure
& on_creation
,
27 const base::Closure
& on_destruction
,
28 mojo::InterfaceRequest
<KeepAlive
> keep_alive
) {
29 new TestKeepAlive(on_destruction
, keep_alive
.Pass());
34 const base::Closure on_destruction_
;
35 mojo::StrongBinding
<KeepAlive
> binding_
;
40 class KeepAliveClientTest
: public ApiTestBase
{
42 KeepAliveClientTest() {}
44 void SetUp() override
{
46 service_provider()->AddService(
47 base::Bind(&TestKeepAlive::Create
,
48 base::Bind(&KeepAliveClientTest::KeepAliveCreated
,
49 base::Unretained(this)),
50 base::Bind(&KeepAliveClientTest::KeepAliveDestroyed
,
51 base::Unretained(this))));
52 created_keep_alive_
= false;
53 destroyed_keep_alive_
= false;
56 void WaitForKeepAlive() {
57 // Wait for a keep-alive to be created and destroyed.
58 while (!created_keep_alive_
|| !destroyed_keep_alive_
) {
59 base::RunLoop run_loop
;
60 stop_run_loop_
= run_loop
.QuitClosure();
63 EXPECT_TRUE(created_keep_alive_
);
64 EXPECT_TRUE(destroyed_keep_alive_
);
68 void KeepAliveCreated() {
69 created_keep_alive_
= true;
70 if (!stop_run_loop_
.is_null())
73 void KeepAliveDestroyed() {
74 destroyed_keep_alive_
= true;
75 if (!stop_run_loop_
.is_null())
79 bool created_keep_alive_
;
80 bool destroyed_keep_alive_
;
81 base::Closure stop_run_loop_
;
83 DISALLOW_COPY_AND_ASSIGN(KeepAliveClientTest
);
86 TEST_F(KeepAliveClientTest
, KeepAliveWithSuccessfulCall
) {
87 RunTest("keep_alive_client_unittest.js", "testKeepAliveWithSuccessfulCall");
91 TEST_F(KeepAliveClientTest
, KeepAliveWithError
) {
92 RunTest("keep_alive_client_unittest.js", "testKeepAliveWithError");
96 } // namespace extensions