OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdio.h> | |
6 #include <unistd.h> | |
7 | |
8 #include <utility> | |
9 | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "mojo/public/c/environment/logger.h" | |
12 #include "mojo/public/cpp/application/application_impl.h" | |
13 #include "mojo/public/cpp/application/application_test_base.h" | |
14 #include "mojo/public/cpp/system/functions.h" | |
15 #include "mojo/services/log/interfaces/entry.mojom.h" | |
16 #include "mojo/services/log/interfaces/log.mojom.h" | |
17 #include "services/log/log_impl.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace mojo { | |
21 namespace log { | |
22 namespace { | |
23 | |
24 using base::MessageLoop; | |
25 using LogImplTest = mojo::test::ApplicationTestBase; | |
26 | |
27 // We need to supply a ApplicationConnection to LogImpl::Create(). | |
28 class TestApplicationConnection : public ApplicationConnection { | |
29 public: | |
30 const std::string& GetConnectionURL() override { | |
31 static std::string kConnectionURL = "mojo:log"; | |
32 return kConnectionURL; | |
33 } | |
34 | |
35 const std::string& GetRemoteApplicationURL() override { | |
36 static std::string kRemoteApplicationURL = "mojo:log_impl_unittest"; | |
37 return kRemoteApplicationURL; | |
38 } | |
39 | |
40 ServiceProvider* GetServiceProvider() override { return nullptr; } | |
41 void SetServiceConnectorForName(ServiceConnector* service_connector, | |
42 const std::string& name) override {} | |
43 }; | |
44 | |
45 // Tests the Log service implementation by calling its AddEntry and verifying | |
46 // the log message it prints to the supplied FILE stream. | |
47 TEST_F(LogImplTest, AddEntryOutput) { | |
48 // LogImpl will write to p[1] (which we wrap into a FILE*), we will EXPECT | |
49 // what's written by reading in p[0]. | |
50 int p[2]; | |
51 pipe(p); | |
viettrungluu
2015/12/15 18:33:08
You should, say, ASSERT_EQ(0, pipe(p));.
vardhan
2015/12/16 18:29:05
Done.
| |
52 | |
53 FILE* output_file = fdopen(p[1], "w"); | |
viettrungluu
2015/12/15 18:33:08
Probably should use ScopedFILE (base/files/scoped_
vardhan
2015/12/16 18:29:05
I figured it wasn't necessary since i'm closing it
| |
54 ASSERT_NE(nullptr, output_file); | |
55 | |
56 LogPtr log; | |
57 auto log_request = GetProxy(&log); | |
58 TestApplicationConnection app_connection; | |
59 LogImpl::Create(&app_connection, std::move(log_request), output_file); | |
viettrungluu
2015/12/15 18:33:08
Why don't you just directly put GetProxy(&log) in
vardhan
2015/12/16 18:29:05
Done.
| |
60 | |
61 Entry entry; | |
62 entry.log_level = MOJO_LOG_LEVEL_INFO; | |
63 entry.timestamp = GetTimeTicksNow(); | |
64 entry.source_file = "file.ext"; | |
65 entry.source_line = 0; | |
66 entry.message = "1234567890"; | |
67 log->AddEntry(entry.Clone()); | |
68 | |
69 entry.source_line = 1; | |
70 log->AddEntry(entry.Clone()); | |
71 | |
72 entry.source_file.reset(); | |
73 log->AddEntry(entry.Clone()); | |
74 | |
75 entry.message.reset(); | |
76 log->AddEntry(entry.Clone()); | |
77 | |
78 log.reset(); | |
79 | |
80 MessageLoop::current()->PostDelayedTask( | |
81 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | |
82 base::TimeDelta::FromMilliseconds(10)); | |
viettrungluu
2015/12/15 18:33:08
Probably should use TestTimeouts::tiny_timeout(),
vardhan
2015/12/16 18:29:05
Done.
| |
83 | |
84 MessageLoop::current()->Run(); | |
85 fclose(output_file); // This closes p[1]. | |
86 | |
87 const char* expected_string = | |
viettrungluu
2015/12/15 18:33:08
const char expected_string[] = ...
vardhan
2015/12/16 18:29:05
Done.
| |
88 "<mojo:log_impl_unittest> [INFO] file.ext: 1234567890\n" | |
89 "<mojo:log_impl_unittest> [INFO] file.ext:1: 1234567890\n" | |
90 "<mojo:log_impl_unittest> [INFO] 1234567890\n" | |
91 "<mojo:log_impl_unittest> [INFO] <no message>\n"; | |
92 char actual_string[1024 * 10]; | |
93 size_t nbytes = read(p[0], actual_string, strlen(expected_string)); | |
viettrungluu
2015/12/15 18:33:08
Theoretically, this should be wrapped in HANDLE_EI
vardhan
2015/12/16 18:29:05
Done. (also, im thinking the EXPECT_EQ() is suffi
| |
94 EXPECT_EQ(static_cast<size_t>(strlen(expected_string)), nbytes); | |
95 | |
96 close(p[0]); | |
97 | |
98 actual_string[nbytes] = '\0'; | |
99 EXPECT_STREQ(expected_string, actual_string); | |
100 } | |
101 | |
102 } // namespace | |
103 } // namespace log | |
104 } // namespace mojo | |
OLD | NEW |