Chromium Code Reviews| Index: content/common/content_debug_logging_unittest.cc |
| diff --git a/content/common/content_debug_logging_unittest.cc b/content/common/content_debug_logging_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2c3264c3d776414745fecab87d3ff9e346b59099 |
| --- /dev/null |
| +++ b/content/common/content_debug_logging_unittest.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright (c) 2011 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 "content/public/common/content_debug_logging.h" |
| + |
| +#include <map> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +// Makes sure that the handlers are cleared during teardown. |
| +class ContentDebugLoggingTest : public testing::Test { |
| + public: |
| + ContentDebugLoggingTest() {} |
| + virtual ~ContentDebugLoggingTest() {} |
| + |
| + void SetUp() { |
| + messages.clear(); |
| + content::debug::RegisterMessageHandlers( |
| + ContentDebugLoggingTest::RecordMsg, |
| + ContentDebugLoggingTest::GetMessages); |
| + } |
| + |
| + void TearDown() { |
| + // Clean up side effects. |
| + messages.clear(); |
| + content::debug::RegisterMessageHandlers(NULL, NULL); |
| + } |
| + |
| + // Map bug ids to messages for that bug. |
| + 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
|
| + |
| + static void RecordMsg(int bug_id, const std::string& msg) { |
| + messages[bug_id].push_back(msg); |
| + } |
| + |
| + static bool GetMessages(int bug_id, std::vector<std::string>* msgs) { |
| + std::vector<std::string>& bug_messages = messages[bug_id]; |
| + msgs->assign(bug_messages.begin(), bug_messages.end()); |
| + return !msgs->empty(); |
| + } |
| +}; |
| + |
| +// static |
| +std::map<int, std::vector<std::string> > ContentDebugLoggingTest::messages; |
| + |
| +TEST_F(ContentDebugLoggingTest, NullLogging) { |
| + content::debug::RegisterMessageHandlers(NULL, NULL); |
| + |
| + const std::string kMessage("This is a test"); |
| + const int kBug = 1; |
| + content::debug::RecordMsg(kBug, kMessage); |
| + |
| + // Cannot get the message back. |
| + std::vector<std::string> bug_messages; |
| + EXPECT_FALSE(content::debug::GetMessages(kBug, &bug_messages)); |
| + EXPECT_EQ(bug_messages.size(), 0u); |
| +} |
| + |
| +TEST_F(ContentDebugLoggingTest, BasicLogging) { |
| + const std::string kMessage("This is a test"); |
| + const int kBug = 1; |
| + content::debug::RecordMsg(kBug, kMessage); |
| + |
| + // The message was stored and can be retrieved. |
| + std::vector<std::string> bug_messages; |
| + EXPECT_TRUE(content::debug::GetMessages(kBug, &bug_messages)); |
| + EXPECT_EQ(bug_messages.size(), 1u); |
| + EXPECT_EQ(bug_messages[0], kMessage); |
| +} |
| + |
| +TEST_F(ContentDebugLoggingTest, SeparateBugs) { |
| + const std::string kMessage("This is a test"); |
| + const int kBug = 1; |
| + content::debug::RecordMsg(kBug, kMessage); |
| + |
| + content::debug::RecordMsg(17, std::string("Some other message")); |
| + |
| + // Only get the message for this bug back. |
| + std::vector<std::string> bug_messages; |
| + EXPECT_TRUE(content::debug::GetMessages(kBug, &bug_messages)); |
| + EXPECT_EQ(bug_messages.size(), 1u); |
| + EXPECT_EQ(bug_messages[0], kMessage); |
| + |
| + // There are messages for multiple bugs, though. |
| + EXPECT_EQ(messages.size(), 2u); |
| +} |
| + |
| +} // namespace |