Index: services/log/log_impl_unittest.cc |
diff --git a/services/log/log_impl_unittest.cc b/services/log/log_impl_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..879b24b001a30c2e015eba5b433f235eedd8519a |
--- /dev/null |
+++ b/services/log/log_impl_unittest.cc |
@@ -0,0 +1,98 @@ |
+// Copyright 2015 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 <stdio.h> |
+#include <unistd.h> |
+ |
+#include <utility> |
+ |
+#include "base/message_loop/message_loop.h" |
+#include "mojo/public/c/environment/logger.h" |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/application_test_base.h" |
+#include "mojo/public/cpp/system/functions.h" |
+#include "mojo/services/log/interfaces/entry.mojom.h" |
+#include "mojo/services/log/interfaces/log.mojom.h" |
+#include "services/log/log_impl.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace mojo { |
+namespace log { |
+namespace { |
+ |
+using LogImplTest = mojo::test::ApplicationTestBase; |
+ |
+// We need to supply a ApplicationConnection to LogImpl::Create(). |
+class TestApplicationConnection : public ApplicationConnection { |
+ public: |
+ const std::string& GetConnectionURL() override { |
+ static std::string kConnectionURL = "mojo:log"; |
+ return kConnectionURL; |
+ } |
+ |
+ const std::string& GetRemoteApplicationURL() override { |
+ static std::string kRemoteApplicationURL = "mojo:log_impl_unittest"; |
+ return kRemoteApplicationURL; |
+ } |
+ |
+ ServiceProvider* GetServiceProvider() override { return nullptr; } |
+ void SetServiceConnectorForName(ServiceConnector* service_connector, |
+ const std::string& name) override {} |
+}; |
+ |
+// Tests the Log service implementation by calling its AddEntry and verifying |
+// the log message it prints to the supplied FILE stream. |
+TEST_F(LogImplTest, AddEntryOutput) { |
+ // LogImpl will write to p[1] (which we wrap into a FILE*), we will EXPECT |
+ // what's written by reading in p[0]. |
+ int p[2]; |
+ pipe(p); |
+ |
+ FILE* output_file = fdopen(p[1], "w"); |
+ ASSERT_NE(nullptr, output_file); |
+ |
+ LogPtr log; |
+ auto log_request = GetProxy(&log); |
+ TestApplicationConnection app_connection; |
+ LogImpl::Create(&app_connection, std::move(log_request), output_file); |
+ |
+ Entry entry; |
+ entry.log_level = MOJO_LOG_LEVEL_INFO; |
+ entry.timestamp = GetTimeTicksNow(); |
+ entry.source_file = "file.ext"; |
+ entry.source_line = 0; |
+ entry.message = "1234567890"; |
+ log->AddEntry(entry.Clone()); |
+ |
+ entry.source_line = 1; |
+ log->AddEntry(entry.Clone()); |
+ |
+ entry.source_file.reset(); |
+ log->AddEntry(entry.Clone()); |
+ |
+ entry.message.reset(); |
+ log->AddEntry(entry.Clone()); |
+ |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ // We need to close p[1] it so a read(p[0],..) of a very large size will not |
+ // block. |
+ fclose(output_file); |
+ |
+ const char* expected_string = |
+ "<mojo:log_impl_unittest> [INFO] file.ext: 1234567890\n" |
+ "<mojo:log_impl_unittest> [INFO] file.ext:1: 1234567890\n" |
+ "<mojo:log_impl_unittest> [INFO] 1234567890\n" |
+ "<mojo:log_impl_unittest> [INFO] <no message>\n"; |
+ |
+ char actual_string[1024 * 10]; |
+ size_t nbytes = read(p[0], actual_string, sizeof(actual_string)); |
+ EXPECT_EQ(static_cast<size_t>(strlen(expected_string)), nbytes); |
+ |
+ actual_string[nbytes] = '\0'; |
+ EXPECT_STREQ(expected_string, actual_string); |
+} |
+ |
+} // namespace |
+} // namespace log |
+} // namespace mojo |