Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / jingle / notifier / base / weak_xmpp_client_unittest.cc
bloba9bfd35b65702503c1902c8138610b2b667cad11
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "jingle/notifier/base/weak_xmpp_client.h"
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "jingle/glue/task_pump.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/base/sigslot.h"
16 namespace notifier {
18 namespace {
20 class MockXmppDelegate : public sigslot::has_slots<> {
21 public:
22 virtual ~MockXmppDelegate() {}
24 MOCK_METHOD1(OnStateChange, void(buzz::XmppEngine::State));
25 MOCK_METHOD2(OnInputLog, void(const char*, int));
26 MOCK_METHOD2(OnOutputLog, void(const char*, int));
29 const buzz::XmppEngine::State kState = buzz::XmppEngine::STATE_OPEN;
30 const char kInputLog[] = "input log";
31 const char kOutputLog[] = "output log";
33 class WeakXmppClientTest : public testing::Test {
34 protected:
35 WeakXmppClientTest() : task_pump_(new jingle_glue::TaskPump()) {}
37 ~WeakXmppClientTest() override {}
39 void ConnectSignals(buzz::XmppClient* xmpp_client) {
40 xmpp_client->SignalStateChange.connect(
41 &mock_xmpp_delegate_, &MockXmppDelegate::OnStateChange);
42 xmpp_client->SignalLogInput.connect(
43 &mock_xmpp_delegate_, &MockXmppDelegate::OnInputLog);
44 xmpp_client->SignalLogOutput.connect(
45 &mock_xmpp_delegate_, &MockXmppDelegate::OnOutputLog);
48 void ExpectSignalCalls() {
49 EXPECT_CALL(mock_xmpp_delegate_, OnStateChange(kState));
50 EXPECT_CALL(mock_xmpp_delegate_,
51 OnInputLog(kInputLog, arraysize(kInputLog)));
52 EXPECT_CALL(mock_xmpp_delegate_,
53 OnOutputLog(kOutputLog, arraysize(kOutputLog)));
56 void RaiseSignals(buzz::XmppClient* xmpp_client) {
57 xmpp_client->SignalStateChange(kState);
58 xmpp_client->SignalLogInput(kInputLog, arraysize(kInputLog));
59 xmpp_client->SignalLogOutput(kOutputLog, arraysize(kOutputLog));
62 // Needed by TaskPump.
63 base::MessageLoop message_loop_;
65 scoped_ptr<jingle_glue::TaskPump> task_pump_;
66 MockXmppDelegate mock_xmpp_delegate_;
69 TEST_F(WeakXmppClientTest, InvalidationViaInvalidate) {
70 ExpectSignalCalls();
72 WeakXmppClient* weak_xmpp_client = new WeakXmppClient(task_pump_.get());
73 ConnectSignals(weak_xmpp_client);
75 weak_xmpp_client->Start();
76 base::WeakPtr<WeakXmppClient> weak_ptr = weak_xmpp_client->AsWeakPtr();
77 EXPECT_TRUE(weak_ptr.get());
78 RaiseSignals(weak_ptr.get());
80 weak_xmpp_client->Invalidate();
81 EXPECT_FALSE(weak_ptr.get());
82 // We know that |weak_xmpp_client| is still valid at this point,
83 // although it should be entirely disconnected.
84 RaiseSignals(weak_xmpp_client);
87 TEST_F(WeakXmppClientTest, InvalidationViaStop) {
88 ExpectSignalCalls();
90 WeakXmppClient* weak_xmpp_client = new WeakXmppClient(task_pump_.get());
91 ConnectSignals(weak_xmpp_client);
93 weak_xmpp_client->Start();
94 base::WeakPtr<WeakXmppClient> weak_ptr = weak_xmpp_client->AsWeakPtr();
95 EXPECT_TRUE(weak_ptr.get());
96 RaiseSignals(weak_ptr.get());
98 weak_xmpp_client->Abort();
99 EXPECT_FALSE(weak_ptr.get());
100 // We know that |weak_xmpp_client| is still valid at this point,
101 // although it should be entirely disconnected.
102 RaiseSignals(weak_xmpp_client);
105 TEST_F(WeakXmppClientTest, InvalidationViaDestructor) {
106 ExpectSignalCalls();
108 WeakXmppClient* weak_xmpp_client = new WeakXmppClient(task_pump_.get());
109 ConnectSignals(weak_xmpp_client);
111 weak_xmpp_client->Start();
112 base::WeakPtr<WeakXmppClient> weak_ptr = weak_xmpp_client->AsWeakPtr();
113 EXPECT_TRUE(weak_ptr.get());
114 RaiseSignals(weak_ptr.get());
116 task_pump_.reset();
117 EXPECT_FALSE(weak_ptr.get());
118 // |weak_xmpp_client| is truly invalid at this point so we can't
119 // RaiseSignals() with it.
122 } // namespace
124 } // namespace notifier