[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "content/browser/tracing/trace_subscriber_stdio.h" |
| 6 | |
[email protected] | e6fec47 | 2013-05-14 05:29:02 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
| 9 | #include "base/threading/sequenced_worker_pool.h" |
| 10 | #include "content/public/browser/browser_thread.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace content { |
| 14 | |
| 15 | class TraceSubscriberStdioTest : public ::testing::Test {}; |
| 16 | |
[email protected] | 28a015d8 | 2013-08-20 15:42:46 | [diff] [blame^] | 17 | TEST_F(TraceSubscriberStdioTest, CanWriteArray) { |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 18 | base::ScopedTempDir trace_dir; |
| 19 | ASSERT_TRUE(trace_dir.CreateUniqueTempDir()); |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 20 | base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt")); |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 21 | { |
[email protected] | 28a015d8 | 2013-08-20 15:42:46 | [diff] [blame^] | 22 | TraceSubscriberStdio subscriber(trace_file, |
| 23 | TraceSubscriberStdio::FILE_TYPE_ARRAY, |
| 24 | false); |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 25 | |
| 26 | std::string foo("foo"); |
| 27 | subscriber.OnTraceDataCollected( |
| 28 | make_scoped_refptr(base::RefCountedString::TakeString(&foo))); |
| 29 | |
| 30 | std::string bar("bar"); |
| 31 | subscriber.OnTraceDataCollected( |
| 32 | make_scoped_refptr(base::RefCountedString::TakeString(&bar))); |
| 33 | |
| 34 | subscriber.OnEndTracingComplete(); |
| 35 | } |
| 36 | BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 37 | std::string result; |
| 38 | EXPECT_TRUE(file_util::ReadFileToString(trace_file, &result)); |
| 39 | EXPECT_EQ("[foo,bar]", result); |
| 40 | } |
| 41 | |
[email protected] | 28a015d8 | 2013-08-20 15:42:46 | [diff] [blame^] | 42 | TEST_F(TraceSubscriberStdioTest, CanWritePropertyList) { |
| 43 | base::ScopedTempDir trace_dir; |
| 44 | ASSERT_TRUE(trace_dir.CreateUniqueTempDir()); |
| 45 | base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt")); |
| 46 | { |
| 47 | TraceSubscriberStdio subscriber( |
| 48 | trace_file, |
| 49 | TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST, |
| 50 | false); |
| 51 | |
| 52 | std::string foo("foo"); |
| 53 | subscriber.OnTraceDataCollected( |
| 54 | make_scoped_refptr(base::RefCountedString::TakeString(&foo))); |
| 55 | |
| 56 | std::string bar("bar"); |
| 57 | subscriber.OnTraceDataCollected( |
| 58 | make_scoped_refptr(base::RefCountedString::TakeString(&bar))); |
| 59 | |
| 60 | subscriber.OnEndTracingComplete(); |
| 61 | } |
| 62 | BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 63 | std::string result; |
| 64 | EXPECT_TRUE(file_util::ReadFileToString(trace_file, &result)); |
| 65 | EXPECT_EQ("{\"traceEvents\":[foo,bar]}", result); |
| 66 | } |
| 67 | |
| 68 | TEST_F(TraceSubscriberStdioTest, CanWriteSystemDataFirst) { |
| 69 | base::ScopedTempDir trace_dir; |
| 70 | ASSERT_TRUE(trace_dir.CreateUniqueTempDir()); |
| 71 | base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt")); |
| 72 | { |
| 73 | TraceSubscriberStdio subscriber( |
| 74 | trace_file, |
| 75 | TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST, |
| 76 | true); |
| 77 | |
| 78 | std::string foo("foo"); |
| 79 | subscriber.OnTraceDataCollected( |
| 80 | make_scoped_refptr(base::RefCountedString::TakeString(&foo))); |
| 81 | |
| 82 | std::string bar("bar"); |
| 83 | subscriber.OnTraceDataCollected( |
| 84 | make_scoped_refptr(base::RefCountedString::TakeString(&bar))); |
| 85 | |
| 86 | std::string systemTrace("event1\nev\"ent\"2\n"); |
| 87 | subscriber.OnEndSystemTracing( |
| 88 | make_scoped_refptr(base::RefCountedString::TakeString(&systemTrace))); |
| 89 | subscriber.OnEndTracingComplete(); |
| 90 | } |
| 91 | BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 92 | std::string result; |
| 93 | EXPECT_TRUE(file_util::ReadFileToString(trace_file, &result)); |
| 94 | EXPECT_EQ( |
| 95 | "{\"traceEvents\":[foo,bar],\"" |
| 96 | "systemTraceEvents\":\"event1\\nev\\\"ent\\\"2\\n\"}", |
| 97 | result); |
| 98 | } |
| 99 | |
| 100 | TEST_F(TraceSubscriberStdioTest, CanWriteSystemDataLast) { |
| 101 | base::ScopedTempDir trace_dir; |
| 102 | ASSERT_TRUE(trace_dir.CreateUniqueTempDir()); |
| 103 | base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt")); |
| 104 | { |
| 105 | TraceSubscriberStdio subscriber( |
| 106 | trace_file, |
| 107 | TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST, |
| 108 | true); |
| 109 | |
| 110 | std::string foo("foo"); |
| 111 | subscriber.OnTraceDataCollected( |
| 112 | make_scoped_refptr(base::RefCountedString::TakeString(&foo))); |
| 113 | |
| 114 | std::string bar("bar"); |
| 115 | subscriber.OnTraceDataCollected( |
| 116 | make_scoped_refptr(base::RefCountedString::TakeString(&bar))); |
| 117 | |
| 118 | std::string systemTrace("event1\nev\"ent\"2\n"); |
| 119 | subscriber.OnEndTracingComplete(); |
| 120 | subscriber.OnEndSystemTracing( |
| 121 | make_scoped_refptr(base::RefCountedString::TakeString(&systemTrace))); |
| 122 | } |
| 123 | BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 124 | std::string result; |
| 125 | EXPECT_TRUE(file_util::ReadFileToString(trace_file, &result)); |
| 126 | EXPECT_EQ( |
| 127 | "{\"traceEvents\":[foo,bar],\"" |
| 128 | "systemTraceEvents\":\"event1\\nev\\\"ent\\\"2\\n\"}", |
| 129 | result); |
| 130 | } |
| 131 | |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 132 | } // namespace content |