OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stdio.h> | 5 #include <string> |
6 #include <string.h> | 6 #include <utility> |
7 #include <unistd.h> | 7 #include <vector> |
8 | 8 |
9 #include <utility> | |
10 | |
11 #include "base/files/scoped_file.h" | |
12 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
13 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
14 #include "mojo/public/c/environment/logger.h" | 11 #include "mojo/public/c/environment/logger.h" |
15 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
16 #include "mojo/public/cpp/application/application_test_base.h" | 13 #include "mojo/public/cpp/application/application_test_base.h" |
17 #include "mojo/public/cpp/system/time.h" | 14 #include "mojo/public/cpp/system/time.h" |
18 #include "mojo/services/log/interfaces/entry.mojom.h" | 15 #include "mojo/services/log/interfaces/entry.mojom.h" |
19 #include "mojo/services/log/interfaces/log.mojom.h" | 16 #include "mojo/services/log/interfaces/log.mojom.h" |
20 #include "services/log/log_impl.h" | 17 #include "services/log/log_impl.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 17 matching lines...) Expand all Loading... |
39 static std::string kRemoteApplicationURL = "mojo:log_impl_unittest"; | 36 static std::string kRemoteApplicationURL = "mojo:log_impl_unittest"; |
40 return kRemoteApplicationURL; | 37 return kRemoteApplicationURL; |
41 } | 38 } |
42 | 39 |
43 ServiceProvider* GetServiceProvider() override { return nullptr; } | 40 ServiceProvider* GetServiceProvider() override { return nullptr; } |
44 void SetServiceConnectorForName(ServiceConnector* service_connector, | 41 void SetServiceConnectorForName(ServiceConnector* service_connector, |
45 const std::string& name) override {} | 42 const std::string& name) override {} |
46 }; | 43 }; |
47 | 44 |
48 // Tests the Log service implementation by calling its AddEntry and verifying | 45 // Tests the Log service implementation by calling its AddEntry and verifying |
49 // the log message it prints to the supplied FILE stream. | 46 // the log message that it "prints". |
50 TEST_F(LogImplTest, AddEntryOutput) { | 47 TEST_F(LogImplTest, AddEntryOutput) { |
51 // LogImpl will write to p[1] (which we wrap into a FILE*), we will EXPECT | 48 std::vector<std::string> messages; |
52 // what's written by reading in p[0]. | |
53 int p[2]; | |
54 ASSERT_EQ(0, pipe(p)); | |
55 | |
56 base::ScopedFILE output_file(fdopen(p[1], "w")); | |
57 ASSERT_NE(nullptr, output_file.get()); | |
58 | 49 |
59 LogPtr log; | 50 LogPtr log; |
60 TestApplicationConnection app_connection; | 51 TestApplicationConnection app_connection; |
61 LogImpl::Create(&app_connection, GetProxy(&log), output_file.get()); | 52 LogImpl::Create( |
| 53 &app_connection, GetProxy(&log), |
| 54 [&messages](const std::string& message) { messages.push_back(message); }); |
62 | 55 |
63 Entry entry; | 56 Entry entry; |
64 entry.log_level = MOJO_LOG_LEVEL_INFO; | 57 entry.log_level = MOJO_LOG_LEVEL_INFO; |
65 entry.timestamp = GetTimeTicksNow(); | 58 entry.timestamp = GetTimeTicksNow(); |
66 entry.source_file = "file.ext"; | 59 entry.source_file = "file.ext"; |
67 entry.source_line = 0; | 60 entry.source_line = 0; |
68 entry.message = "1234567890"; | 61 entry.message = "1234567890"; |
69 log->AddEntry(entry.Clone()); | 62 log->AddEntry(entry.Clone()); |
70 | 63 |
71 entry.source_line = 1; | 64 entry.source_line = 1; |
72 log->AddEntry(entry.Clone()); | 65 log->AddEntry(entry.Clone()); |
73 | 66 |
74 entry.source_file.reset(); | 67 entry.source_file.reset(); |
75 log->AddEntry(entry.Clone()); | 68 log->AddEntry(entry.Clone()); |
76 | 69 |
77 entry.message.reset(); | 70 entry.message.reset(); |
78 log->AddEntry(entry.Clone()); | 71 log->AddEntry(entry.Clone()); |
79 | 72 |
80 log.reset(); | 73 log.reset(); |
81 | 74 |
82 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 75 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
83 MessageLoop::QuitWhenIdleClosure(), | 76 MessageLoop::QuitWhenIdleClosure(), |
84 TestTimeouts::tiny_timeout()); | 77 TestTimeouts::tiny_timeout()); |
85 | 78 |
86 MessageLoop::current()->Run(); | 79 MessageLoop::current()->Run(); |
87 output_file.reset(); // This closes p[1]. | |
88 | 80 |
89 const char expected_string[] = | 81 const std::vector<std::string> kExpectedMessages = { |
90 "<mojo:log_impl_unittest> [INFO] file.ext: 1234567890\n" | 82 "<mojo:log_impl_unittest> [INFO] file.ext: 1234567890", |
91 "<mojo:log_impl_unittest> [INFO] file.ext:1: 1234567890\n" | 83 "<mojo:log_impl_unittest> [INFO] file.ext:1: 1234567890", |
92 "<mojo:log_impl_unittest> [INFO] 1234567890\n" | 84 "<mojo:log_impl_unittest> [INFO] 1234567890", |
93 "<mojo:log_impl_unittest> [INFO] <no message>\n"; | 85 "<mojo:log_impl_unittest> [INFO] <no message>", |
94 char actual_string[1024 * 10]; | 86 }; |
95 ssize_t nbytes = read(p[0], actual_string, strlen(expected_string)); | 87 EXPECT_EQ(kExpectedMessages, messages); |
96 ASSERT_EQ(static_cast<ssize_t>(strlen(expected_string)), nbytes); | |
97 | |
98 close(p[0]); | |
99 | |
100 actual_string[nbytes] = '\0'; | |
101 EXPECT_STREQ(expected_string, actual_string); | |
102 } | 88 } |
103 | 89 |
104 } // namespace | 90 } // namespace |
105 } // namespace log | 91 } // namespace log |
106 } // namespace mojo | 92 } // namespace mojo |
OLD | NEW |