summaryrefslogtreecommitdiffstats
path: root/dbus/test_service.h
blob: 362ecb7cc6b02d3f56a2f7d0cca9f9961e226b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DBUS_TEST_SERVICE_H_
#define DBUS_TEST_SERVICE_H_
#pragma once

#include "base/memory/ref_counted.h"
#include "base/threading/thread.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"

namespace dbus {

class Bus;
class ExportedObject;
class MethodCall;
class Response;

// The test service is used for end-to-end tests.  The service runs in a
// separate thread, so it does not interfere the test code that runs in
// the main thread. Methods such as Echo() and SlowEcho() are exported.
class TestService : public base::Thread {
 public:
  // SlowEcho() sleeps for this period of time before returns.
  static const int kSlowEchoSleepMs;

  TestService();
  virtual ~TestService();

  // Starts the service in a separate thread.
  void StartService();

  // Waits until the service is started (i.e. methods are exported).
  void WaitUntilServiceIsStarted();

 private:
  // Called when the service is started (i.e. the task is run from the
  // message loop).
  void OnServiceStarted();

  // base::Thread override.
  virtual void Run(MessageLoop* message_loop);

  // base::Thread override.
  virtual void CleanUp();

  //
  // Exported methods.
  //

  // Echos the text message received from the method call.
  Response* Echo(MethodCall* method_call);

  // Echos the text message received from the method call, but sleeps for
  // kSlowEchoSleepMs before returning the response.
  Response* SlowEcho(MethodCall* method_call);

  // Returns NULL, instead of a valid Response.
  Response* BrokenMethod(MethodCall* method_call);

  bool service_started_;
  base::Lock service_started_lock_;
  base::ConditionVariable on_service_started_;

  scoped_refptr<Bus> bus_;
  ExportedObject* exported_object_;
};

}  // namespace dbus

#endif  // DBUS_TEST_SERVICE_H_