Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / mojo / keep_alive_client_unittest.cc
blobae170b6c70df7a1ecdc237f2844f56f164d65282
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 {
14 namespace {
16 // A KeepAlive implementation that calls provided callbacks on creation and
17 // destruction.
18 class TestKeepAlive : public KeepAlive {
19 public:
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());
30 on_creation.Run();
33 private:
34 const base::Closure on_destruction_;
35 mojo::StrongBinding<KeepAlive> binding_;
38 } // namespace
40 class KeepAliveClientTest : public ApiTestBase {
41 public:
42 KeepAliveClientTest() {}
44 void SetUp() override {
45 ApiTestBase::SetUp();
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();
61 run_loop.Run();
63 EXPECT_TRUE(created_keep_alive_);
64 EXPECT_TRUE(destroyed_keep_alive_);
67 private:
68 void KeepAliveCreated() {
69 created_keep_alive_ = true;
70 if (!stop_run_loop_.is_null())
71 stop_run_loop_.Run();
73 void KeepAliveDestroyed() {
74 destroyed_keep_alive_ = true;
75 if (!stop_run_loop_.is_null())
76 stop_run_loop_.Run();
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");
88 WaitForKeepAlive();
91 TEST_F(KeepAliveClientTest, KeepAliveWithError) {
92 RunTest("keep_alive_client_unittest.js", "testKeepAliveWithError");
93 WaitForKeepAlive();
96 } // namespace extensions