OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/system_wrappers/include/logging.h" | 11 #include "webrtc/system_wrappers/include/logging.h" |
12 | 12 |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webrtc/base/arraysize.h" |
| 15 #include "webrtc/base/event.h" |
14 #include "webrtc/base/scoped_ptr.h" | 16 #include "webrtc/base/scoped_ptr.h" |
15 #include "webrtc/system_wrappers/include/condition_variable_wrapper.h" | |
16 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
17 #include "webrtc/system_wrappers/include/sleep.h" | |
18 #include "webrtc/system_wrappers/include/trace.h" | 17 #include "webrtc/system_wrappers/include/trace.h" |
19 | 18 |
20 namespace webrtc { | 19 namespace webrtc { |
21 namespace { | 20 namespace { |
| 21 const char kTestLogString[] = "Incredibly important test message!(?)"; |
| 22 const int kTestLevel = kTraceWarning; |
22 | 23 |
23 class LoggingTest : public ::testing::Test, public TraceCallback { | 24 class LoggingTestCallback : public TraceCallback { |
24 public: | 25 public: |
25 virtual void Print(TraceLevel level, const char* msg, int length) { | 26 LoggingTestCallback(rtc::Event* event) : event_(event) {} |
26 CriticalSectionScoped cs(crit_.get()); | 27 |
27 // We test the length here to ensure (with high likelihood) that only our | 28 private: |
28 // traces will be tested. | 29 void Print(TraceLevel level, const char* msg, int length) override { |
29 if (level_ != kTraceNone && static_cast<int>(expected_log_.str().size()) == | 30 if (static_cast<size_t>(length) < arraysize(kTestLogString) || |
30 length - Trace::kBoilerplateLength - 1) { | 31 level != kTestLevel) { |
31 EXPECT_EQ(level_, level); | 32 return; |
32 EXPECT_EQ(expected_log_.str(), &msg[Trace::kBoilerplateLength]); | |
33 level_ = kTraceNone; | |
34 cv_->Wake(); | |
35 } | 33 } |
| 34 |
| 35 std::string msg_str(msg, length); |
| 36 if (msg_str.find(kTestLogString) != std::string::npos) |
| 37 event_->Set(); |
36 } | 38 } |
37 | 39 |
38 protected: | 40 rtc::Event* const event_; |
39 LoggingTest() | |
40 : crit_(CriticalSectionWrapper::CreateCriticalSection()), | |
41 cv_(ConditionVariableWrapper::CreateConditionVariable()), | |
42 level_(kTraceNone), | |
43 expected_log_() { | |
44 } | |
45 | |
46 void SetUp() { | |
47 Trace::CreateTrace(); | |
48 Trace::SetTraceCallback(this); | |
49 } | |
50 | |
51 void TearDown() { | |
52 Trace::SetTraceCallback(NULL); | |
53 Trace::ReturnTrace(); | |
54 CriticalSectionScoped cs(crit_.get()); | |
55 ASSERT_EQ(kTraceNone, level_) << "Print() was not called"; | |
56 } | |
57 | |
58 rtc::scoped_ptr<CriticalSectionWrapper> crit_; | |
59 rtc::scoped_ptr<ConditionVariableWrapper> cv_; | |
60 TraceLevel level_ GUARDED_BY(crit_); | |
61 std::ostringstream expected_log_ GUARDED_BY(crit_); | |
62 }; | 41 }; |
63 | 42 |
64 TEST_F(LoggingTest, LogStream) { | 43 } // namespace |
65 { | 44 |
66 CriticalSectionScoped cs(crit_.get()); | 45 TEST(LoggingTest, LogStream) { |
67 level_ = kTraceWarning; | 46 Trace::CreateTrace(); |
68 std::string msg = "Important message"; | 47 |
69 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 1 << "): " << msg; | 48 rtc::Event event(false, false); |
70 LOG(LS_WARNING) << msg; | 49 LoggingTestCallback callback(&event); |
71 cv_->SleepCS(*crit_.get(), 2000); | 50 Trace::SetTraceCallback(&callback); |
72 } | 51 |
| 52 LOG(LS_WARNING) << kTestLogString; |
| 53 EXPECT_TRUE(event.Wait(2000)); |
| 54 |
| 55 Trace::SetTraceCallback(nullptr); |
| 56 Trace::ReturnTrace(); |
73 } | 57 } |
74 | |
75 } // namespace | |
76 } // namespace webrtc | 58 } // namespace webrtc |
OLD | NEW |