Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / environment / tests / logging_unittest.cc
index 2afad88..53a1f88 100644 (file)
 namespace mojo {
 namespace {
 
+class PtrToMemberHelper {
+ public:
+  int member;
+};
+
+bool DcheckTestHelper(bool* was_called) {
+  *was_called = true;
+  return false;
+}
+
 class LoggingTest : public testing::Test {
  public:
-  LoggingTest() : environment_(NULL, &kMockLogger) {
+  LoggingTest() : environment_(nullptr, &kMockLogger) {
     minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
     ResetMockLogger();
   }
-  virtual ~LoggingTest() {}
+  ~LoggingTest() override {}
 
  protected:
   // Note: Does not reset |minimum_log_level_|.
@@ -35,9 +45,18 @@ class LoggingTest : public testing::Test {
     last_message_.clear();
   }
 
+  // A function returning |bool| that shouldn't be called.
+  static bool NotCalledCondition() {
+    not_called_condition_was_called_ = true;
+    return false;
+  }
+
   static bool log_message_was_called() { return log_message_was_called_; }
   static MojoLogLevel last_log_level() { return last_log_level_; }
   static const std::string& last_message() { return last_message_; }
+  static bool not_called_condition_was_called() {
+    return not_called_condition_was_called_;
+  }
 
  private:
   // Note: We record calls even if |log_level| is below |minimum_log_level_|
@@ -49,9 +68,7 @@ class LoggingTest : public testing::Test {
     last_message_ = message;
   }
 
-  static MojoLogLevel MockGetMinimumLogLevel() {
-    return minimum_log_level_;
-  }
+  static MojoLogLevel MockGetMinimumLogLevel() { return minimum_log_level_; }
 
   static void MockSetMinimumLogLevel(MojoLogLevel minimum_log_level) {
     minimum_log_level_ = minimum_log_level;
@@ -64,16 +81,16 @@ class LoggingTest : public testing::Test {
   static bool log_message_was_called_;
   static MojoLogLevel last_log_level_;
   static std::string last_message_;
+  static bool not_called_condition_was_called_;
 
   MOJO_DISALLOW_COPY_AND_ASSIGN(LoggingTest);
 };
 
 // static
 const MojoLogger LoggingTest::kMockLogger = {
-  &LoggingTest::MockLogMessage,
-  &LoggingTest::MockGetMinimumLogLevel,
-  &LoggingTest::MockSetMinimumLogLevel
-};
+    &LoggingTest::MockLogMessage,
+    &LoggingTest::MockGetMinimumLogLevel,
+    &LoggingTest::MockSetMinimumLogLevel};
 
 // static
 MojoLogLevel LoggingTest::minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
@@ -87,23 +104,18 @@ MojoLogLevel LoggingTest::last_log_level_ = MOJO_LOG_LEVEL_INFO;
 // static
 std::string LoggingTest::last_message_;
 
+// static
+bool LoggingTest::not_called_condition_was_called_ = false;
+
 std::string ExpectedLogMessage(int line, const char* message) {
   std::ostringstream s;
   s << OUR_FILENAME "(" << line << "): " << message;
   return s.str();
 }
 
-// A function returning |bool| that shouldn't be called.
-bool NotCalled() {
-  abort();
-  // I think all compilers are smart enough to recognize that nothing is run
-  // after |abort()|. (Some definitely complain that things after it are
-  // unreachable.)
-}
-
 TEST_F(LoggingTest, InternalLogMessage) {
-  internal::LogMessage("foo.cc", 123, MOJO_LOG_LEVEL_INFO).stream()
-      << "hello " << "world";
+  internal::LogMessage("foo.cc", 123, MOJO_LOG_LEVEL_INFO).stream() << "hello "
+                                                                    << "world";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
   EXPECT_EQ("foo.cc(123): hello world", last_message());
@@ -111,7 +123,8 @@ TEST_F(LoggingTest, InternalLogMessage) {
   ResetMockLogger();
 
   internal::LogMessage("./path/to/foo.cc", 123, MOJO_LOG_LEVEL_WARNING).stream()
-      << "hello " << "world";
+      << "hello "
+      << "world";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level());
   EXPECT_EQ("foo.cc(123): hello world", last_message());
@@ -119,7 +132,8 @@ TEST_F(LoggingTest, InternalLogMessage) {
   ResetMockLogger();
 
   internal::LogMessage("/path/to/foo.cc", 123, MOJO_LOG_LEVEL_ERROR).stream()
-      << "hello " << "world";
+      << "hello "
+      << "world";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
   EXPECT_EQ("foo.cc(123): hello world", last_message());
@@ -127,7 +141,8 @@ TEST_F(LoggingTest, InternalLogMessage) {
   ResetMockLogger();
 
   internal::LogMessage("path/to/foo.cc", 123, MOJO_LOG_LEVEL_FATAL).stream()
-      << "hello " << "world";
+      << "hello "
+      << "world";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
   EXPECT_EQ("foo.cc(123): hello world", last_message());
@@ -135,31 +150,35 @@ TEST_F(LoggingTest, InternalLogMessage) {
   ResetMockLogger();
 
   internal::LogMessage(".\\xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE).stream()
-      << "hello " << "world";
+      << "hello "
+      << "world";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE, last_log_level());
   EXPECT_EQ("foo.cc(123): hello world", last_message());
 
   ResetMockLogger();
 
-  internal::LogMessage("xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE-1).stream()
-      << "hello " << "world";
+  internal::LogMessage("xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE - 1).stream()
+      << "hello "
+      << "world";
   EXPECT_TRUE(log_message_was_called());
-  EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE-1, last_log_level());
+  EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE - 1, last_log_level());
   EXPECT_EQ("foo.cc(123): hello world", last_message());
 
   ResetMockLogger();
 
-  internal::LogMessage("C:\\xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE-9).stream()
-      << "hello " << "world";
+  internal::LogMessage("C:\\xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE - 9)
+          .stream()
+      << "hello "
+      << "world";
   EXPECT_TRUE(log_message_was_called());
-  EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE-9, last_log_level());
+  EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE - 9, last_log_level());
   EXPECT_EQ("foo.cc(123): hello world", last_message());
 
   ResetMockLogger();
 
-  internal::LogMessage(__FILE__, 123, MOJO_LOG_LEVEL_INFO).stream()
-      << "hello " << "world";
+  internal::LogMessage(__FILE__, 123, MOJO_LOG_LEVEL_INFO).stream() << "hello "
+                                                                    << "world";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
   EXPECT_EQ(OUR_FILENAME "(123): hello world", last_message());
@@ -169,28 +188,28 @@ TEST_F(LoggingTest, LogStream) {
   MOJO_LOG_STREAM(INFO) << "hello";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message());
 
   ResetMockLogger();
 
   MOJO_LOG_STREAM(ERROR) << "hi " << 123;
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hi 123"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hi 123"), last_message());
 }
 
 TEST_F(LoggingTest, LazyLogStream) {
   MOJO_LAZY_LOG_STREAM(INFO, true) << "hello";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message());
 
   ResetMockLogger();
 
   MOJO_LAZY_LOG_STREAM(ERROR, true) << "hi " << 123;
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hi 123"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hi 123"), last_message());
 
   ResetMockLogger();
 
@@ -204,15 +223,19 @@ TEST_F(LoggingTest, LazyLogStream) {
 
   ResetMockLogger();
 
-  bool x = false;
+  PtrToMemberHelper helper;
+  helper.member = 1;
+  int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member;
+
   // This probably fails to compile if we forget to parenthesize the condition
-  // in the macro (= has low precedence, and needs an lvalue on the LHS).
-  MOJO_LAZY_LOG_STREAM(ERROR, x = true) << "hello";
+  // in the macro (.* has lower precedence than !, which can't apply to
+  // |helper|).
+  MOJO_LAZY_LOG_STREAM(ERROR, helper.*member_ptr == 1) << "hello";
   EXPECT_TRUE(log_message_was_called());
 
   ResetMockLogger();
 
-  MOJO_LAZY_LOG_STREAM(WARNING, x = false) << "hello";
+  MOJO_LAZY_LOG_STREAM(WARNING, helper.*member_ptr == 0) << "hello";
   EXPECT_FALSE(log_message_was_called());
 }
 
@@ -231,7 +254,8 @@ TEST_F(LoggingTest, ShouldLog) {
   EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
   EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
 
-  Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_VERBOSE-1);
+  Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_VERBOSE -
+                                                      1);
   EXPECT_TRUE(MOJO_SHOULD_LOG(VERBOSE));
   EXPECT_TRUE(MOJO_SHOULD_LOG(INFO));
   EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING));
@@ -249,14 +273,14 @@ TEST_F(LoggingTest, Log) {
   MOJO_LOG(INFO) << "hello";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message());
 
   ResetMockLogger();
 
   MOJO_LOG(ERROR) << "hello";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message());
 
   ResetMockLogger();
 
@@ -275,7 +299,7 @@ TEST_F(LoggingTest, Log) {
   MOJO_LOG(ERROR) << "hello";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message());
 }
 
 TEST_F(LoggingTest, LogIf) {
@@ -289,50 +313,41 @@ TEST_F(LoggingTest, LogIf) {
   EXPECT_FALSE(log_message_was_called());
 
   ResetMockLogger();
+  Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
 
-  bool x = false;
+  bool x = true;
   // Also try to make sure that we parenthesize the condition properly.
-  MOJO_LOG_IF(INFO, x = true) << "hello";
-  EXPECT_TRUE(log_message_was_called());
-  EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message());
-
-  ResetMockLogger();
-
-  MOJO_LOG_IF(INFO, x = false) << "hello";
+  MOJO_LOG_IF(INFO, false || x) << "hello";
   EXPECT_FALSE(log_message_was_called());
 
   ResetMockLogger();
 
-  Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
-
-  ResetMockLogger();
-
   MOJO_LOG_IF(INFO, 0 != 1) << "hello";
   EXPECT_FALSE(log_message_was_called());
 
   ResetMockLogger();
 
-  MOJO_LOG_IF(WARNING, 1+1 == 2) << "hello";
+  MOJO_LOG_IF(WARNING, 1 + 1 == 2) << "hello";
   EXPECT_FALSE(log_message_was_called());
 
   ResetMockLogger();
 
-  MOJO_LOG_IF(ERROR, 1*2 == 2) << "hello";
+  MOJO_LOG_IF(ERROR, 1 * 2 == 2) << "hello";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message());
 
   ResetMockLogger();
 
-  MOJO_LOG_IF(FATAL, 1*2 == 3) << "hello";
+  MOJO_LOG_IF(FATAL, 1 * 2 == 3) << "hello";
   EXPECT_FALSE(log_message_was_called());
 
   ResetMockLogger();
 
   // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the
   // minimum.
-  MOJO_LOG_IF(INFO, NotCalled()) << "hello";
+  MOJO_LOG_IF(INFO, NotCalledCondition()) << "hello";
+  EXPECT_FALSE(not_called_condition_was_called());
   EXPECT_FALSE(log_message_was_called());
 }
 
@@ -342,21 +357,25 @@ TEST_F(LoggingTest, Check) {
 
   ResetMockLogger();
 
-  bool x = true;
+  PtrToMemberHelper helper;
+  helper.member = 0;
+  int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member;
+
   // Also try to make sure that we parenthesize the condition properly.
-  MOJO_CHECK(x = false) << "hello";
+  MOJO_CHECK(helper.*member_ptr == 1) << "hello";
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
   // Different compilers have different ideas about the line number of a split
   // line.
   int line = __LINE__;
-  EXPECT_EQ(ExpectedLogMessage(line-5, "Check failed: x = false. hello"),
+  EXPECT_EQ(ExpectedLogMessage(line - 5,
+                               "Check failed: helper.*member_ptr == 1. hello"),
             last_message());
 
   ResetMockLogger();
 
   // Also test a "naked" |MOJO_CHECK()|s.
-  MOJO_CHECK(1+2 == 3);
+  MOJO_CHECK(1 + 2 == 3);
   EXPECT_FALSE(log_message_was_called());
 }
 
@@ -373,14 +392,15 @@ TEST_F(LoggingTest, Dlog) {
 #else
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message());
 #endif
 }
 
 TEST_F(LoggingTest, DlogIf) {
   // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in
   // this case.
-  MOJO_DLOG_IF(VERBOSE, NotCalled()) << "hello";
+  MOJO_DLOG_IF(VERBOSE, NotCalledCondition()) << "hello";
+  EXPECT_FALSE(not_called_condition_was_called());
   EXPECT_FALSE(log_message_was_called());
 
   ResetMockLogger();
@@ -396,12 +416,12 @@ TEST_F(LoggingTest, DlogIf) {
 #else
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message());
 #endif
 
   ResetMockLogger();
 
-  // |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds.
+// |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds.
 #ifndef NDEBUG
   bool debug_only = true;
 #endif
@@ -411,7 +431,7 @@ TEST_F(LoggingTest, DlogIf) {
 #else
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level());
-  EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message());
+  EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message());
 #endif
 }
 
@@ -429,19 +449,30 @@ TEST_F(LoggingTest, Dcheck) {
   // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for
   // non-debug builds. (Hopefully, we'll get an unused variable error if it
   // fails to compile the condition.)
-  bool x = true;
-  MOJO_DCHECK(x = false) << "hello";
+  bool was_called = false;
+  MOJO_DCHECK(DcheckTestHelper(&was_called)) << "hello";
 #ifdef NDEBUG
+  EXPECT_FALSE(was_called);
   EXPECT_FALSE(log_message_was_called());
 #else
+  EXPECT_TRUE(was_called);
   EXPECT_TRUE(log_message_was_called());
   EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
   // Different compilers have different ideas about the line number of a split
   // line.
   int line = __LINE__;
-  EXPECT_EQ(ExpectedLogMessage(line-8, "Check failed: x = false. hello"),
-            last_message());
+  EXPECT_EQ(
+      ExpectedLogMessage(line - 10,
+                         "Check failed: DcheckTestHelper(&was_called). hello"),
+      last_message());
 #endif
+
+  ResetMockLogger();
+
+  // Also try to make sure that we parenthesize the condition properly.
+  bool x = true;
+  MOJO_DCHECK(false || x) << "hello";
+  EXPECT_FALSE(log_message_was_called());
 }
 
 }  // namespace