Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/public/common/content_debug_logging.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Makes sure that the handlers are cleared during teardown. | |
| 16 class ContentDebugLoggingTest : public testing::Test { | |
| 17 public: | |
| 18 ContentDebugLoggingTest() {} | |
| 19 virtual ~ContentDebugLoggingTest() {} | |
| 20 | |
| 21 void SetUp() { | |
| 22 messages.clear(); | |
| 23 content::debug::RegisterMessageHandlers( | |
| 24 ContentDebugLoggingTest::RecordMsg, | |
| 25 ContentDebugLoggingTest::GetMessages); | |
| 26 } | |
| 27 | |
| 28 void TearDown() { | |
| 29 // Clean up side effects. | |
| 30 messages.clear(); | |
| 31 content::debug::RegisterMessageHandlers(NULL, NULL); | |
| 32 } | |
| 33 | |
| 34 // Map bug ids to messages for that bug. | |
| 35 static std::map<int, std::vector<std::string> > messages; | |
|
Bernhard Bauer
2012/09/05 15:04:55
Could you make this a pointer to avoid the static
Scott Hess - ex-Googler
2012/09/05 17:17:05
Hmm, and then the handler functions can just watch
| |
| 36 | |
| 37 static void RecordMsg(int bug_id, const std::string& msg) { | |
| 38 messages[bug_id].push_back(msg); | |
| 39 } | |
| 40 | |
| 41 static bool GetMessages(int bug_id, std::vector<std::string>* msgs) { | |
| 42 std::vector<std::string>& bug_messages = messages[bug_id]; | |
| 43 msgs->assign(bug_messages.begin(), bug_messages.end()); | |
| 44 return !msgs->empty(); | |
| 45 } | |
| 46 }; | |
| 47 | |
| 48 // static | |
| 49 std::map<int, std::vector<std::string> > ContentDebugLoggingTest::messages; | |
| 50 | |
| 51 TEST_F(ContentDebugLoggingTest, NullLogging) { | |
| 52 content::debug::RegisterMessageHandlers(NULL, NULL); | |
| 53 | |
| 54 const std::string kMessage("This is a test"); | |
| 55 const int kBug = 1; | |
| 56 content::debug::RecordMsg(kBug, kMessage); | |
| 57 | |
| 58 // Cannot get the message back. | |
| 59 std::vector<std::string> bug_messages; | |
| 60 EXPECT_FALSE(content::debug::GetMessages(kBug, &bug_messages)); | |
| 61 EXPECT_EQ(bug_messages.size(), 0u); | |
| 62 } | |
| 63 | |
| 64 TEST_F(ContentDebugLoggingTest, BasicLogging) { | |
| 65 const std::string kMessage("This is a test"); | |
| 66 const int kBug = 1; | |
| 67 content::debug::RecordMsg(kBug, kMessage); | |
| 68 | |
| 69 // The message was stored and can be retrieved. | |
| 70 std::vector<std::string> bug_messages; | |
| 71 EXPECT_TRUE(content::debug::GetMessages(kBug, &bug_messages)); | |
| 72 EXPECT_EQ(bug_messages.size(), 1u); | |
| 73 EXPECT_EQ(bug_messages[0], kMessage); | |
| 74 } | |
| 75 | |
| 76 TEST_F(ContentDebugLoggingTest, SeparateBugs) { | |
| 77 const std::string kMessage("This is a test"); | |
| 78 const int kBug = 1; | |
| 79 content::debug::RecordMsg(kBug, kMessage); | |
| 80 | |
| 81 content::debug::RecordMsg(17, std::string("Some other message")); | |
| 82 | |
| 83 // Only get the message for this bug back. | |
| 84 std::vector<std::string> bug_messages; | |
| 85 EXPECT_TRUE(content::debug::GetMessages(kBug, &bug_messages)); | |
| 86 EXPECT_EQ(bug_messages.size(), 1u); | |
| 87 EXPECT_EQ(bug_messages[0], kMessage); | |
| 88 | |
| 89 // There are messages for multiple bugs, though. | |
| 90 EXPECT_EQ(messages.size(), 2u); | |
| 91 } | |
| 92 | |
| 93 } // namespace | |
| OLD | NEW |