fakexmppclient.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // A fake XmppClient for use in unit tests.
  11. #ifndef THIRD_PARTY_LIBJINGLE_XMPP_XMPP_FAKEXMPPCLIENT_H_
  12. #define THIRD_PARTY_LIBJINGLE_XMPP_XMPP_FAKEXMPPCLIENT_H_
  13. #include <algorithm>
  14. #include <string>
  15. #include <vector>
  16. #include "third_party/libjingle_xmpp/xmpp/xmpptask.h"
  17. namespace jingle_xmpp {
  18. class XmlElement;
  19. class FakeXmppClient : public XmppTaskParentInterface,
  20. public XmppClientInterface {
  21. public:
  22. explicit FakeXmppClient(jingle_xmpp::TaskParent* parent)
  23. : XmppTaskParentInterface(parent) {
  24. }
  25. // As XmppTaskParentInterface
  26. virtual XmppClientInterface* GetClient() {
  27. return this;
  28. }
  29. virtual int ProcessStart() {
  30. return STATE_RESPONSE;
  31. }
  32. // As XmppClientInterface
  33. virtual XmppEngine::State GetState() const {
  34. return XmppEngine::STATE_OPEN;
  35. }
  36. virtual const Jid& jid() const {
  37. return jid_;
  38. }
  39. virtual std::string NextId() {
  40. // Implement if needed for tests.
  41. return "0";
  42. }
  43. virtual XmppReturnStatus SendStanza(const XmlElement* stanza) {
  44. sent_stanzas_.push_back(stanza);
  45. return XMPP_RETURN_OK;
  46. }
  47. const std::vector<const XmlElement*>& sent_stanzas() {
  48. return sent_stanzas_;
  49. }
  50. virtual XmppReturnStatus SendStanzaError(
  51. const XmlElement * pelOriginal,
  52. XmppStanzaError code,
  53. const std::string & text) {
  54. // Implement if needed for tests.
  55. return XMPP_RETURN_OK;
  56. }
  57. virtual void AddXmppTask(XmppTask* task,
  58. XmppEngine::HandlerLevel level) {
  59. tasks_.push_back(task);
  60. }
  61. virtual void RemoveXmppTask(XmppTask* task) {
  62. std::remove(tasks_.begin(), tasks_.end(), task);
  63. }
  64. // As FakeXmppClient
  65. void set_jid(const Jid& jid) {
  66. jid_ = jid;
  67. }
  68. // Takes ownership of stanza.
  69. void HandleStanza(XmlElement* stanza) {
  70. for (std::vector<XmppTask*>::iterator task = tasks_.begin();
  71. task != tasks_.end(); ++task) {
  72. if ((*task)->HandleStanza(stanza)) {
  73. delete stanza;
  74. return;
  75. }
  76. }
  77. delete stanza;
  78. }
  79. private:
  80. Jid jid_;
  81. std::vector<XmppTask*> tasks_;
  82. std::vector<const XmlElement*> sent_stanzas_;
  83. };
  84. } // namespace jingle_xmpp
  85. #endif // THIRD_PARTY_LIBJINGLE_XMPP_XMPP_FAKEXMPPCLIENT_H_