summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/testing_policy_url_fetcher_factory.h
blob: b2a9c6b241583b981435b3703b2bc6dac65e1f13 (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
// 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_TESTING_POLICY_URL_FETCHER_FACTORY_H_
#define CHROME_BROWSER_POLICY_TESTING_POLICY_URL_FETCHER_FACTORY_H_
#pragma once

#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "content/public/common/url_fetcher_delegate.h"
#include "content/test/test_url_fetcher_factory.h"
#include "testing/gmock/include/gmock/gmock.h"

class GURL;

namespace policy {

class EventLogger;
class LoggingWorkScheduler;
class TestingPolicyURLFetcher;

struct TestURLResponse {
  std::string response_data;
  int response_code;
};

// Creates mock URLFetchers whose behavior can be controlled in tests. To do so
// set mock expectations on the method |Intercept|.
class TestingPolicyURLFetcherFactory : public content::URLFetcherFactory,
                                       public ScopedURLFetcherFactory {
 public:
  explicit TestingPolicyURLFetcherFactory(EventLogger* logger);
  virtual ~TestingPolicyURLFetcherFactory();

  virtual content::URLFetcher* CreateURLFetcher(
      int id,
      const GURL& url,
      content::URLFetcher::RequestType request_type,
      content::URLFetcherDelegate* delegate) OVERRIDE;

  LoggingWorkScheduler* scheduler();

  // Called back by TestingPolicyURLFetcher objects. Uses |Intercept| to get
  // the response and notifies |logger_| of a network request event.
  void GetResponse(const std::string& auth_header,
                   const std::string& request_type,
                   TestURLResponse* response);

  // Place EXPECT_CALLs on this method to control the responses of the
  // produced URLFetchers. The response data should be copied into |response|.
  MOCK_METHOD3(Intercept, void(const std::string& auth_header,
                               const std::string& request_type,
                               TestURLResponse* response));

 private:
  EventLogger* logger_;
  scoped_ptr<LoggingWorkScheduler> scheduler_;

  base::WeakPtrFactory<TestingPolicyURLFetcherFactory> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(TestingPolicyURLFetcherFactory);
};

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_TESTING_POLICY_URL_FETCHER_FACTORY_H_