1e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
2e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// found in the LICENSE file.
4e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
5e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include <string>
6e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
7e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/bind.h"
8e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/message_loop/message_loop.h"
9e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/renderer/media/chrome_webrtc_log_message_delegate.h"
10e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/renderer/media/mock_webrtc_logging_message_filter.h"
11e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "testing/gtest/include/gtest/gtest.h"
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochTEST(ChromeWebRtcLogMessageDelegateTest, Basic) {
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  const char kTestString[] = "abcdefghijklmnopqrstuvwxyz";
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  base::MessageLoopForIO message_loop;
16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  scoped_refptr<MockWebRtcLoggingMessageFilter> log_message_filter(
17e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      new MockWebRtcLoggingMessageFilter(message_loop.message_loop_proxy()));
18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Run message loop to initialize delegate.
19e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // TODO(vrk): Fix this so that we can construct a delegate without needing to
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // construct a message filter.
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  message_loop.RunUntilIdle();
22e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
23e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  ChromeWebRtcLogMessageDelegate* log_message_delegate =
24e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      log_message_filter->log_message_delegate();
25e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Start logging on the IO loop.
27e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  message_loop.message_loop_proxy()->PostTask(
28e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      FROM_HERE, base::Bind(
29e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          &ChromeWebRtcLogMessageDelegate::OnStartLogging,
30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          base::Unretained(log_message_delegate)));
31e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
32e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // These log messages should be added to the log buffer outside of the IO
33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // loop.
34e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  log_message_delegate->LogMessage(kTestString);
35e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  log_message_delegate->LogMessage(kTestString);
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Stop logging on IO loop.
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  message_loop.message_loop_proxy()->PostTask(
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      FROM_HERE, base::Bind(
40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          &ChromeWebRtcLogMessageDelegate::OnStopLogging,
41e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          base::Unretained(log_message_delegate)));
42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
43e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // This log message should not be added to the log buffer.
44e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  log_message_delegate->LogMessage(kTestString);
45e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
46e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  message_loop.RunUntilIdle();
47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
48e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Size is calculated as (sizeof(kTestString) - 1 for terminating null
49e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // + 1 for eol added for each log message in LogMessage) * 2.
50e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  const uint32 kExpectedSize = sizeof(kTestString) * 2;
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  EXPECT_EQ(kExpectedSize, log_message_filter->log_buffer_.size());
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
53  std::string ref_output = kTestString;
54  ref_output.append("\n");
55  ref_output.append(kTestString);
56  ref_output.append("\n");
57  EXPECT_STREQ(ref_output.c_str(), log_message_filter->log_buffer_.c_str());
58}
59