OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/trace_subscriber_stdio.h" | 5 #include "content/browser/trace_subscriber_stdio.h" |
6 | 6 |
7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
| 8 #include "base/threading/thread_restrictions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
| 13 class ScopedDisallowIO { |
| 14 public: |
| 15 ScopedDisallowIO() { |
| 16 previous_value_ = base::ThreadRestrictions::SetIOAllowed(false); |
| 17 } |
| 18 |
| 19 ~ScopedDisallowIO() { |
| 20 base::ThreadRestrictions::SetIOAllowed(previous_value_); |
| 21 } |
| 22 |
| 23 private: |
| 24 bool previous_value_; |
| 25 |
| 26 DISALLOW_COPY_AND_ASSIGN(ScopedDisallowIO); |
| 27 }; |
| 28 |
12 class TraceSubscriberStdioTest : public testing::Test { | 29 class TraceSubscriberStdioTest : public testing::Test { |
13 public: | 30 public: |
14 virtual void SetUp() { | 31 virtual void SetUp() { |
15 ASSERT_TRUE(trace_dir_.CreateUniqueTempDir()); | 32 ASSERT_TRUE(trace_dir_.CreateUniqueTempDir()); |
16 trace_file_ = trace_dir_.path().AppendASCII("trace.txt"); | 33 trace_file_ = trace_dir_.path().AppendASCII("trace.txt"); |
17 } | 34 } |
18 | 35 |
19 std::string ReadTraceFile() { | 36 std::string ReadTraceFile() { |
20 std::string result; | 37 std::string result; |
21 EXPECT_TRUE(file_util::ReadFileToString(trace_file_, &result)); | 38 EXPECT_TRUE(file_util::ReadFileToString(trace_file_, &result)); |
22 return result; | 39 return result; |
23 } | 40 } |
24 | 41 |
25 ScopedTempDir trace_dir_; | 42 ScopedTempDir trace_dir_; |
26 FilePath trace_file_; | 43 FilePath trace_file_; |
27 }; | 44 }; |
28 | 45 |
29 } // namespace | 46 } // namespace |
30 | 47 |
31 TEST_F(TraceSubscriberStdioTest, CanWriteDataToFile) { | 48 TEST_F(TraceSubscriberStdioTest, CanWriteDataToFile) { |
| 49 // TraceSubscriber callbacks are documented to occur on the UI thread, |
| 50 // so this class must work with ThreadRestictions::SetIOAllowed(false); |
| 51 ScopedDisallowIO scoped_disallow_io; |
| 52 |
32 TraceSubscriberStdio subscriber(trace_file_); | 53 TraceSubscriberStdio subscriber(trace_file_); |
33 subscriber.OnTraceDataCollected("[foo]"); | 54 subscriber.OnTraceDataCollected("[foo]"); |
34 subscriber.OnTraceDataCollected("[bar]"); | 55 subscriber.OnTraceDataCollected("[bar]"); |
35 EXPECT_TRUE(subscriber.IsValid()); | 56 EXPECT_TRUE(subscriber.IsValid()); |
36 | 57 |
37 subscriber.OnEndTracingComplete(); | 58 subscriber.OnEndTracingComplete(); |
38 EXPECT_FALSE(subscriber.IsValid()); | 59 EXPECT_FALSE(subscriber.IsValid()); |
39 } | 60 } |
40 | |
OLD | NEW |