summaryrefslogtreecommitdiffstats
path: root/remoting/client/client_status_logger_unittest.cc
blob: b10301951013881021d253b0cf900ee56057beb9 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2014 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 "remoting/client/client_status_logger.h"

#include "base/message_loop/message_loop.h"
#include "remoting/protocol/performance_tracker.h"
#include "remoting/signaling/mock_signal_strategy.h"
#include "remoting/signaling/server_log_entry_unittest.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/libjingle/xmllite/xmlelement.h"

using buzz::XmlElement;
using buzz::QName;
using remoting::protocol::ConnectionToHost;
using testing::_;
using testing::DeleteArg;
using testing::InSequence;
using testing::Return;

namespace remoting {

namespace {

ACTION_P(QuitMainMessageLoop, message_loop) {
  message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
}

const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
const char kClientJid[] = "host@domain.com/1234";

MATCHER_P2(IsStateChange, new_state, error, "") {
  XmlElement* entry = GetSingleLogEntryFromStanza(arg);
  if (!entry) {
    return false;
  }

  bool is_state_change = (
      entry->Attr(QName(std::string(), "event-name")) == "session-state" &&
      entry->Attr(QName(std::string(), "session-state")) == new_state &&
      entry->Attr(QName(std::string(), "role")) == "client" &&
      entry->Attr(QName(std::string(), "mode")) == "me2me");
  if (!std::string(error).empty()) {
    is_state_change = is_state_change &&
        entry->Attr(QName(std::string(), "connection-error")) == error;
  }
  return is_state_change;
}

MATCHER(IsStatisticsLog, "") {
  XmlElement* entry = GetSingleLogEntryFromStanza(arg);
  if (!entry) {
    return false;
  }

  return entry->Attr(QName(std::string(), "event-name")) ==
      "connection-statistics";
}

}  // namespace

class ClientStatusLoggerTest : public testing::Test {
 public:
  ClientStatusLoggerTest() {}
  void SetUp() override {
    EXPECT_CALL(signal_strategy_, AddListener(_));
    EXPECT_CALL(signal_strategy_, RemoveListener(_));
    client_status_logger_.reset(
        new ClientStatusLogger(ServerLogEntry::ME2ME,
                               &signal_strategy_,
                               kTestBotJid));
  }

 protected:
  base::MessageLoop message_loop_;
  MockSignalStrategy signal_strategy_;
  scoped_ptr<ClientStatusLogger> client_status_logger_;
};

TEST_F(ClientStatusLoggerTest, LogStateChange) {
  {
    InSequence s;
    EXPECT_CALL(signal_strategy_, GetLocalJid())
        .WillRepeatedly(Return(kClientJid));
    EXPECT_CALL(signal_strategy_, AddListener(_));
    EXPECT_CALL(signal_strategy_, GetNextId());
    EXPECT_CALL(signal_strategy_, SendStanzaPtr(
        IsStateChange("connected", std::string())))
        .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
    EXPECT_CALL(signal_strategy_, RemoveListener(_))
        .WillOnce(QuitMainMessageLoop(&message_loop_))
        .RetiresOnSaturation();
  }
  client_status_logger_->LogSessionStateChange(ConnectionToHost::CONNECTED,
                                               protocol::OK);

  // Setting the state to CONNECTED causes the log to be sent. Setting the
  // state to DISCONNECTED causes |signal_strategy_| to be cleaned up,
  // which removes the listener and terminates the test.
  client_status_logger_->SetSignalingStateForTest(SignalStrategy::CONNECTED);
  client_status_logger_->SetSignalingStateForTest(SignalStrategy::DISCONNECTED);
  message_loop_.Run();
}

TEST_F(ClientStatusLoggerTest, LogStateChangeError) {
  {
    InSequence s;
    EXPECT_CALL(signal_strategy_, GetLocalJid())
        .WillRepeatedly(Return(kClientJid));
    EXPECT_CALL(signal_strategy_, AddListener(_));
    EXPECT_CALL(signal_strategy_, GetNextId());
    EXPECT_CALL(signal_strategy_, SendStanzaPtr(
        IsStateChange("connection-failed", "host-is-offline")))
        .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
    EXPECT_CALL(signal_strategy_, RemoveListener(_))
        .WillOnce(QuitMainMessageLoop(&message_loop_))
        .RetiresOnSaturation();
  }
  client_status_logger_->LogSessionStateChange(ConnectionToHost::FAILED,
                                               protocol::PEER_IS_OFFLINE);

  client_status_logger_->SetSignalingStateForTest(SignalStrategy::CONNECTED);
  client_status_logger_->SetSignalingStateForTest(SignalStrategy::DISCONNECTED);
  message_loop_.Run();
}

TEST_F(ClientStatusLoggerTest, LogStatistics) {
  {
    InSequence s;
    EXPECT_CALL(signal_strategy_, GetLocalJid())
        .WillRepeatedly(Return(kClientJid));
    EXPECT_CALL(signal_strategy_, AddListener(_));
    EXPECT_CALL(signal_strategy_, GetNextId());
    EXPECT_CALL(signal_strategy_, SendStanzaPtr(
        IsStatisticsLog()))
        .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
    EXPECT_CALL(signal_strategy_, RemoveListener(_))
        .WillOnce(QuitMainMessageLoop(&message_loop_))
        .RetiresOnSaturation();
  }

  protocol::PerformanceTracker perf_tracker;
  client_status_logger_->LogStatistics(&perf_tracker);

  client_status_logger_->SetSignalingStateForTest(SignalStrategy::CONNECTED);
  client_status_logger_->SetSignalingStateForTest(SignalStrategy::DISCONNECTED);
  message_loop_.Run();
}

}  // namespace remoting