summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/mock_device_management_service.h
blob: 1217d2731ee59b95ceb132f5c81402c3ed93fb9e (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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 CHROME_BROWSER_POLICY_MOCK_DEVICE_MANAGEMENT_SERVICE_H_
#define CHROME_BROWSER_POLICY_MOCK_DEVICE_MANAGEMENT_SERVICE_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "chrome/browser/policy/device_management_backend.h"
#include "chrome/browser/policy/device_management_service.h"
#include "chrome/browser/policy/proto/device_management_backend.pb.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace policy {

// This proxy class is used so that expectations can be defined for a single
// persistent instance of DMBackend while the DeviceTokenFetcher under test
// merrily creates and destroys proxies.
class ProxyDeviceManagementBackend : public DeviceManagementBackend {
 public:
  explicit ProxyDeviceManagementBackend(DeviceManagementBackend* backend);
  virtual ~ProxyDeviceManagementBackend();

  // DeviceRegisterRequest implementation:
  virtual void ProcessRegisterRequest(
      const std::string& gaia_auth_token,
      const std::string& oauth_token,
      const std::string& device_id,
      const em::DeviceRegisterRequest& request,
      DeviceRegisterResponseDelegate* delegate) OVERRIDE;
  virtual void ProcessUnregisterRequest(
      const std::string& device_management_token,
      const std::string& device_id,
      const em::DeviceUnregisterRequest& request,
      DeviceUnregisterResponseDelegate* delegate) OVERRIDE;
  virtual void ProcessPolicyRequest(
      const std::string& device_management_token,
      const std::string& device_id,
      const em::DevicePolicyRequest& request,
      DevicePolicyResponseDelegate* delegate) OVERRIDE;

 private:
  DeviceManagementBackend* backend_;  // weak
  DISALLOW_COPY_AND_ASSIGN(ProxyDeviceManagementBackend);
};

class MockDeviceManagementService : public DeviceManagementService {
 public:
  MockDeviceManagementService();
  virtual ~MockDeviceManagementService();

  MOCK_METHOD0(CreateBackend, DeviceManagementBackend*());
  MOCK_METHOD1(StartJob, void(DeviceManagementJob*));

  // This method bypasses the mocked version and calls the superclass'
  // CreateBackend(), returning a "real" backend.
  DeviceManagementBackend* CreateBackendNotMocked() {
    return DeviceManagementService::CreateBackend();
  }

  // This method bypasses the mocked version and calls the superclass'
  // StartJob.
  void StartJobNotMocked(DeviceManagementJob* job) {
    DeviceManagementService::StartJob(job);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(MockDeviceManagementService);
};

// Use this to make CreateBackend return a ProxyDeviceManagementBackend for
// the given |backend|.
ACTION_P(MockDeviceManagementServiceProxyBackend, backend) {
  return new ProxyDeviceManagementBackend(backend);
}

// Use this to make StartJob send a faked response to the job immediately.
ACTION_P4(MockDeviceManagementServiceRespondToJob, status, response_code,
          cookies, data) {
  arg0->HandleResponse(status, response_code, cookies, data);
}

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_MOCK_DEVICE_MANAGEMENT_SERVICE_H_