summaryrefslogtreecommitdiffstats
path: root/remoting/jingle_glue/jingle_client_unittest.cc
blob: 807f061a319edd37007afa3e7d0c5e4498f3458b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2010 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.

#include "base/synchronization/waitable_event.h"
#include "remoting/jingle_glue/jingle_client.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;

namespace remoting {

class MockJingleClientCallback : public JingleClient::Callback {
 public:
  ~MockJingleClientCallback() { }

  MOCK_METHOD2(OnStateChange, void(JingleClient*, JingleClient::State));
};

class JingleClientTest : public testing::Test {
 public:
  virtual ~JingleClientTest() { }

  static void OnClosed(bool* called) {
    *called = true;
  }

  // A helper that calls OnConnectionStateChanged(). Need this because we want
  // to call it on the jingle thread.
  static void ChangeState(JingleClient* client, buzz::XmppEngine::State state,
                          base::WaitableEvent* done_event) {
    client->OnConnectionStateChanged(state);
    if (done_event)
      done_event->Signal();
  }

 protected:
  virtual void SetUp() {
    client_ = new JingleClient(&thread_);
    // Fake initialization
    client_->initialized_ = true;
    client_->callback_ = &callback_;
  }

  JingleThread thread_;
  scoped_refptr<JingleClient> client_;
  MockJingleClientCallback callback_;
};

TEST_F(JingleClientTest, OnStateChanged) {
  EXPECT_CALL(callback_, OnStateChange(_, JingleClient::CONNECTING))
      .Times(1);

  thread_.Start();

  base::WaitableEvent state_changed_event(true, false);
  thread_.message_loop()->PostTask(FROM_HERE, NewRunnableFunction(
      &JingleClientTest::ChangeState, client_,
      buzz::XmppEngine::STATE_OPENING, &state_changed_event));
  state_changed_event.Wait();

  client_->Close();

  thread_.Stop();
}

TEST_F(JingleClientTest, Close) {
  EXPECT_CALL(callback_, OnStateChange(_, _))
      .Times(0);
  thread_.Start();
  client_->Close();
  // Verify that the channel doesn't call callback anymore.
  thread_.message_loop()->PostTask(FROM_HERE, NewRunnableFunction(
      &JingleClientTest::ChangeState, client_,
      buzz::XmppEngine::STATE_OPENING,
      static_cast<base::WaitableEvent*>(NULL)));
  thread_.Stop();
}

TEST_F(JingleClientTest, ClosedTask) {
  thread_.Start();
  bool closed = false;
  client_->Close(NewRunnableFunction(&JingleClientTest::OnClosed,
                                     &closed));
  thread_.Stop();
  EXPECT_TRUE(closed);
}

TEST_F(JingleClientTest, DoubleClose) {
  thread_.Start();
  bool closed1 = false;
  client_->Close(NewRunnableFunction(&JingleClientTest::OnClosed,
                                     &closed1));
  bool closed2 = false;
  client_->Close(NewRunnableFunction(&JingleClientTest::OnClosed,
                                     &closed2));
  thread_.Stop();
  EXPECT_TRUE(closed1 && closed2);
}

}  // namespace remoting