| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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. | |
| 4 | |
| 5 #include "net/base/forwarding_net_log.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "net/base/capturing_net_log.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace net { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Test forwarding a call to AddEntry() to another implementation, operating | |
| 16 // on this same message loop. | |
| 17 TEST(ForwardingNetLogTest, Basic) { | |
| 18 // Create a forwarding NetLog that sends messages to this same thread. | |
| 19 CapturingNetLog log(CapturingNetLog::kUnbounded); | |
| 20 ForwardingNetLog forwarding(&log, MessageLoop::current()); | |
| 21 | |
| 22 EXPECT_EQ(0u, log.entries().size()); | |
| 23 | |
| 24 NetLogStringParameter* params = new NetLogStringParameter("xxx", "yyy"); | |
| 25 | |
| 26 forwarding.AddEntry( | |
| 27 NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
| 28 base::TimeTicks(), | |
| 29 NetLog::Source(), | |
| 30 NetLog::PHASE_NONE, | |
| 31 params); | |
| 32 | |
| 33 // Should still be empty, since we posted an async task. | |
| 34 EXPECT_EQ(0u, log.entries().size()); | |
| 35 | |
| 36 MessageLoop::current()->RunAllPending(); | |
| 37 | |
| 38 // After draining the message loop, we should now have executed the task | |
| 39 // and hence emitted the log entry. | |
| 40 ASSERT_EQ(1u, log.entries().size()); | |
| 41 | |
| 42 // Check that the forwarded call contained received all the right inputs. | |
| 43 EXPECT_EQ(NetLog::TYPE_PAC_JAVASCRIPT_ALERT, log.entries()[0].type); | |
| 44 EXPECT_EQ(NetLog::SOURCE_NONE, log.entries()[0].source.type); | |
| 45 EXPECT_EQ(NetLog::PHASE_NONE, log.entries()[0].phase); | |
| 46 EXPECT_EQ(params, log.entries()[0].extra_parameters.get()); | |
| 47 | |
| 48 // Check that the parameters is still referenced. (if the reference was | |
| 49 // lost then this will be a memory error and probaby crash). | |
| 50 EXPECT_EQ("yyy", params->value()); | |
| 51 } | |
| 52 | |
| 53 // Test forwarding a call to AddEntry() to another implementation that runs | |
| 54 // on the same message loop. However destroy the forwarder before the posted | |
| 55 // task has a chance to run. | |
| 56 TEST(ForwardingNetLogTest, Orphan) { | |
| 57 // Create a forwarding NetLog that sends messages to this same thread. | |
| 58 CapturingNetLog log(CapturingNetLog::kUnbounded); | |
| 59 { | |
| 60 ForwardingNetLog forwarding(&log, MessageLoop::current()); | |
| 61 EXPECT_EQ(0u, log.entries().size()); | |
| 62 | |
| 63 forwarding.AddEntry( | |
| 64 NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
| 65 base::TimeTicks(), | |
| 66 NetLog::Source(), | |
| 67 NetLog::PHASE_NONE, | |
| 68 NULL); | |
| 69 | |
| 70 // Should still be empty, since we posted an async task. | |
| 71 EXPECT_EQ(0u, log.entries().size()); | |
| 72 } | |
| 73 | |
| 74 // At this point the ForwardingNetLog is deleted. However it had already | |
| 75 // posted a task to the message loop. Once we drain the message loop, we | |
| 76 // verify that the task didn't actually try to emit to the NetLog. | |
| 77 MessageLoop::current()->RunAllPending(); | |
| 78 EXPECT_EQ(0u, log.entries().size()); | |
| 79 } | |
| 80 | |
| 81 } // namespace | |
| 82 | |
| 83 } // namespace net | |
| 84 | |
| OLD | NEW |