File: cros_dbus_service_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (115 lines) | stat: -rw-r--r-- 3,757 bytes parent folder | download | duplicates (9)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/services/cros_dbus_service.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_exported_object.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/object_path.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

using ::testing::_;
using ::testing::Eq;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Mock;
using ::testing::Return;

namespace ash {

namespace {

class MockProxyResolutionService
    : public CrosDBusService::ServiceProviderInterface {
 public:
  MOCK_METHOD1(Start, void(scoped_refptr<dbus::ExportedObject>
                           exported_object));
};

}  // namespace

class CrosDBusServiceTest : public testing::Test {
 public:
  CrosDBusServiceTest() = default;

  // Creates an instance of CrosDBusService with mocks injected.
  void SetUp() override {
    // Create a mock bus.
    dbus::Bus::Options options;
    options.bus_type = dbus::Bus::SYSTEM;
    mock_bus_ = new dbus::MockBus(options);

    // ShutdownAndBlock() will be called in TearDown().
    EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());

    // Create a mock exported object.
    const dbus::ObjectPath kObjectPath("/org/example/TestService");
    mock_exported_object_ =
        new dbus::MockExportedObject(mock_bus_.get(), kObjectPath);

    // |mock_bus_|'s GetExportedObject() will return mock_exported_object_|
    // for the given service name and the object path.
    EXPECT_CALL(*mock_bus_.get(), GetExportedObject(kObjectPath))
        .WillRepeatedly(Return(mock_exported_object_.get()));

    // Create a mock proxy resolution service.
    auto mock_proxy_resolution_service_provider =
        std::make_unique<MockProxyResolutionService>();

    const char kServiceName[] = "org.example.TestService";

    {
      // |mock_exported_object_|'s Start method should be called before
      // RequestOwnership: https://crbug.com/874978
      InSequence export_methods_before_taking_ownership;
      EXPECT_CALL(*mock_proxy_resolution_service_provider,
                  Start(Eq(mock_exported_object_)))
          .WillOnce(Return());
      EXPECT_CALL(
          *mock_bus_.get(),
          RequestOwnership(kServiceName,
                           dbus::Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT, _))
          .Times(1);
    }

    // Initialize the cros service with the mocks injected.
    CrosDBusService::ServiceProviderList service_providers;
    service_providers.push_back(
        std::move(mock_proxy_resolution_service_provider));
    cros_dbus_service_ = CrosDBusService::CreateRealImpl(
        mock_bus_.get(), kServiceName, kObjectPath,
        std::move(service_providers));
  }

  void TearDown() override {
    cros_dbus_service_.reset();
    mock_bus_->ShutdownAndBlock();

    // Clear expectations to allow |mock_bus_| to be destroyed:
    // http://go/soverflow/10286514
    EXPECT_TRUE(Mock::VerifyAndClearExpectations(mock_bus_.get()));
  }

 protected:
  scoped_refptr<dbus::MockBus> mock_bus_;
  scoped_refptr<dbus::MockExportedObject> mock_exported_object_;
  std::unique_ptr<CrosDBusService> cros_dbus_service_;
};

TEST_F(CrosDBusServiceTest, Start) {
  // Simply start the service and see if mock expectations are met:
  // - The service object is exported by GetExportedObject()
  // - The proxy resolution service is started.
}

}  // namespace ash