1/*
2 *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11// A fake XmppClient for use in unit tests.
12
13#ifndef WEBRTC_LIBJINGLE_XMPP_FAKEXMPPCLIENT_H_
14#define WEBRTC_LIBJINGLE_XMPP_FAKEXMPPCLIENT_H_
15
16#include <string>
17#include <vector>
18
19#include "webrtc/libjingle/xmpp/xmpptask.h"
20
21namespace buzz {
22
23class XmlElement;
24
25class FakeXmppClient : public XmppTaskParentInterface,
26                       public XmppClientInterface {
27 public:
28  explicit FakeXmppClient(rtc::TaskParent* parent)
29      : XmppTaskParentInterface(parent) {
30  }
31
32  // As XmppTaskParentInterface
33  virtual XmppClientInterface* GetClient() {
34    return this;
35  }
36
37  virtual int ProcessStart() {
38    return STATE_RESPONSE;
39  }
40
41  // As XmppClientInterface
42  virtual XmppEngine::State GetState() const {
43    return XmppEngine::STATE_OPEN;
44  }
45
46  virtual const Jid& jid() const {
47    return jid_;
48  }
49
50  virtual std::string NextId() {
51    // Implement if needed for tests.
52    return "0";
53  }
54
55  virtual XmppReturnStatus SendStanza(const XmlElement* stanza) {
56    sent_stanzas_.push_back(stanza);
57    return XMPP_RETURN_OK;
58  }
59
60  const std::vector<const XmlElement*>& sent_stanzas() {
61    return sent_stanzas_;
62  }
63
64  virtual XmppReturnStatus SendStanzaError(
65      const XmlElement * pelOriginal,
66      XmppStanzaError code,
67      const std::string & text) {
68    // Implement if needed for tests.
69    return XMPP_RETURN_OK;
70  }
71
72  virtual void AddXmppTask(XmppTask* task,
73                           XmppEngine::HandlerLevel level) {
74    tasks_.push_back(task);
75  }
76
77  virtual void RemoveXmppTask(XmppTask* task) {
78    std::remove(tasks_.begin(), tasks_.end(), task);
79  }
80
81  // As FakeXmppClient
82  void set_jid(const Jid& jid) {
83    jid_ = jid;
84  }
85
86  // Takes ownership of stanza.
87  void HandleStanza(XmlElement* stanza) {
88    for (std::vector<XmppTask*>::iterator task = tasks_.begin();
89         task != tasks_.end(); ++task) {
90      if ((*task)->HandleStanza(stanza)) {
91        delete stanza;
92        return;
93      }
94    }
95    delete stanza;
96  }
97
98 private:
99  Jid jid_;
100  std::vector<XmppTask*> tasks_;
101  std::vector<const XmlElement*> sent_stanzas_;
102};
103
104}  // namespace buzz
105
106#endif  // WEBRTC_LIBJINGLE_XMPP_FAKEXMPPCLIENT_H_
107