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
|
// Copyright (c) 2012 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 "content/browser/tracing/trace_subscriber_stdio.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/threading/sequenced_worker_pool.h"
#include "content/public/browser/browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
class TraceSubscriberStdioTest : public ::testing::Test {};
TEST_F(TraceSubscriberStdioTest, CanWriteArray) {
base::ScopedTempDir trace_dir;
ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
{
TraceSubscriberStdio subscriber(trace_file,
TraceSubscriberStdio::FILE_TYPE_ARRAY,
false);
std::string foo("foo");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&foo)));
std::string bar("bar");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&bar)));
subscriber.OnEndTracingComplete();
}
BrowserThread::GetBlockingPool()->FlushForTesting();
std::string result;
EXPECT_TRUE(base::ReadFileToString(trace_file, &result));
EXPECT_EQ("[foo,bar]", result);
}
TEST_F(TraceSubscriberStdioTest, CanWritePropertyList) {
base::ScopedTempDir trace_dir;
ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
{
TraceSubscriberStdio subscriber(
trace_file,
TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST,
false);
std::string foo("foo");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&foo)));
std::string bar("bar");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&bar)));
subscriber.OnEndTracingComplete();
}
BrowserThread::GetBlockingPool()->FlushForTesting();
std::string result;
EXPECT_TRUE(base::ReadFileToString(trace_file, &result));
EXPECT_EQ("{\"traceEvents\":[foo,bar]}", result);
}
TEST_F(TraceSubscriberStdioTest, CanWriteSystemDataFirst) {
base::ScopedTempDir trace_dir;
ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
{
TraceSubscriberStdio subscriber(
trace_file,
TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST,
true);
std::string foo("foo");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&foo)));
std::string bar("bar");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&bar)));
std::string systemTrace("event1\nev\"ent\"2\n");
subscriber.OnEndSystemTracing(
make_scoped_refptr(base::RefCountedString::TakeString(&systemTrace)));
subscriber.OnEndTracingComplete();
}
BrowserThread::GetBlockingPool()->FlushForTesting();
std::string result;
EXPECT_TRUE(base::ReadFileToString(trace_file, &result));
EXPECT_EQ(
"{\"traceEvents\":[foo,bar],\""
"systemTraceEvents\":\"event1\\nev\\\"ent\\\"2\\n\"}",
result);
}
TEST_F(TraceSubscriberStdioTest, CanWriteSystemDataLast) {
base::ScopedTempDir trace_dir;
ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
{
TraceSubscriberStdio subscriber(
trace_file,
TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST,
true);
std::string foo("foo");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&foo)));
std::string bar("bar");
subscriber.OnTraceDataCollected(
make_scoped_refptr(base::RefCountedString::TakeString(&bar)));
std::string systemTrace("event1\nev\"ent\"2\n");
subscriber.OnEndTracingComplete();
subscriber.OnEndSystemTracing(
make_scoped_refptr(base::RefCountedString::TakeString(&systemTrace)));
}
BrowserThread::GetBlockingPool()->FlushForTesting();
std::string result;
EXPECT_TRUE(base::ReadFileToString(trace_file, &result));
EXPECT_EQ(
"{\"traceEvents\":[foo,bar],\""
"systemTraceEvents\":\"event1\\nev\\\"ent\\\"2\\n\"}",
result);
}
} // namespace content
|