17a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// Copyright 2014 The Android Open Source Project
27a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner//
37a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// This software is licensed under the terms of the GNU General Public
47a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// License version 2, as published by the Free Software Foundation, and
57a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// may be copied, distributed, and modified under those terms.
67a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner//
77a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// This program is distributed in the hope that it will be useful,
87a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// but WITHOUT ANY WARRANTY; without even the implied warranty of
97a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
107a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// GNU General Public License for more details.
117a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
127a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#include "android/base/Log.h"
137a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
147a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#include <errno.h>
157a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#include <stdio.h>
167a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#include <string.h>
177a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
187a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#include <gtest/gtest.h>
197a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
207a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnernamespace android {
217a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnernamespace base {
227a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
237a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// Create a severity level which is guaranteed to never generate a log
247a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// message. See LogOnlyEvaluatesArgumentsIfNeeded for usage.
25c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turnerconst LogSeverity LOG_INVISIBLE = -10000;
26c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turner
277a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerclass LogTest : public ::testing::Test, android::base::testing::LogOutput {
287a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerpublic:
297a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LogTest() : mFatal(false) {
307a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mSavedOutput = ::android::base::testing::LogOutput::setNewOutput(this);
317a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mExpected[0] = '\0';
327a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mBuffer[0] = '\x7f';
337a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mBuffer[1] = '\0';
347a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
35c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turner
367a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    ~LogTest() {
377a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        ::android::base::testing::LogOutput::setNewOutput(mSavedOutput);
387a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
397a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
407a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    void setExpected(LogSeverity severity, int line, const char* suffix) {
417a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mExpectedParams.file = __FILE__;
427a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mExpectedParams.lineno = line;
437a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mExpectedParams.severity = severity;
447a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        snprintf(mExpected, sizeof(mExpected), "%s", suffix);
457a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
467a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
477a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    // LogOutput override
487a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    void logMessage(const LogParams& params,
497a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                    const char* message,
507a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                    size_t messageLen) {
517a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mParams = params;
527a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        if (messageLen > sizeof(mBuffer) - 1)
537a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner            messageLen = sizeof(mBuffer) - 1;
547a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        ::memcpy(mBuffer, message, messageLen);
557a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mBuffer[messageLen] = '\0';
567a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mFatal = (params.severity >= LOG_FATAL);
577a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
587a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
597a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerprotected:
607a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    ::android::base::testing::LogOutput* mSavedOutput;
617a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LogParams mParams;
627a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LogParams mExpectedParams;
637a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    char mExpected[1024];
647a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    char mBuffer[1024];
657a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool mFatal;
667a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner};
677a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
687a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerclass CheckTest : public LogTest {
697a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner};
707a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
717a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#if ENABLE_DCHECK != 0
727a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerclass DCheckEnabledTest : public LogTest {
73c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turnerpublic:
747a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    DCheckEnabledTest() : LogTest() {
757a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        // Ensure DCHECKS() always run.
767a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mSavedLevel = setDcheckLevel(true);
777a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
78c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turner
797a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    ~DCheckEnabledTest() {
807a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        setDcheckLevel(mSavedLevel);
817a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
827a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerprivate:
837a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool mSavedLevel;
847a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner};
857a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#endif  // ENABLE_DCHECK == 0
867a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
877a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#if ENABLE_DCHECK != 2
887a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerclass DCheckDisabledTest : public LogTest {
897a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerpublic:
907a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    DCheckDisabledTest() : LogTest() {
917a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mSavedLevel = setDcheckLevel(false);
927a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
93c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turner
947a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    ~DCheckDisabledTest() {
957a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        setDcheckLevel(mSavedLevel);
967a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
977a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerprivate:
987a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool mSavedLevel;
997a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner};
1007a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#endif  // ENABLE_DCHECK != 2
1017a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1027a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerclass PLogTest : public LogTest {
1037a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerpublic:
1047a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    PLogTest() : LogTest(), mForcedErrno(-1000) {}
1057a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1067a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    void setForcedErrno(int errnoCode) {
1077a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mForcedErrno = errnoCode;
1087a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
1097a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1107a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    void setExpectedErrno(LogSeverity severity,
1117a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                          int line,
1127a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                          int errnoCode,
1137a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                          const char* suffix) {
1147a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mExpectedParams.file = __FILE__;
1157a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mExpectedParams.lineno = line;
1167a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        mExpectedParams.severity = severity;
1177a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        snprintf(mExpected,
1187a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                 sizeof(mExpected),
1197a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                 "%sError message: %s",
120c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turner                 suffix,
1217a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                 strerror(errnoCode));
1227a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
1237a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1247a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    void logMessage(const LogParams& params,
1257a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                    const char* message,
1267a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner                    size_t messageLen) {
1277a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        LogTest::logMessage(params, message, messageLen);
128c005246ed03de874fdc432073ba8e5e8ebfed922David 'Digit' Turner
1297a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner        if (mForcedErrno != -1000)
1307a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner            errno = mForcedErrno;
1317a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    }
1327a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1337a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerprotected:
1347a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    int mForcedErrno;
1357a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner};
1367a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1377a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#define STRINGIFY(x) STRINGIFY_(x)
1387a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#define STRINGIFY_(x) #x
1397a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1407a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#define EXPECTED_STRING_PREFIX(prefix, line) \
1417a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner  prefix ":" __FILE__ ":" STRINGIFY(line) ": "
1427a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1437a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#define CHECK_EXPECTATIONS() \
1447a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_STREQ(mExpectedParams.file, mParams.file); \
1457a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_EQ(mExpectedParams.lineno, mParams.lineno); \
1467a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_EQ(mExpectedParams.severity, mParams.severity); \
1477a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_STREQ(mExpected, mBuffer)
1487a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1497a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// Helper function used to set a boolean |flag|, then return |string|.
1507a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turnerstatic const char* setFlag(bool* flag, const char* string) {
1517a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    *flag = true;
1527a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    return string;
1537a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1547a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1557a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST(LogString, EmptyString) {
1567a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LogString ls("");
1577a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_STREQ("", ls.string());
1587a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1597a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1607a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST(LogString, SimpleString) {
1617a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LogString ls("Hello");
1627a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_STREQ("Hello", ls.string());
1637a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1647a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1657a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST(LogString, FormattedString) {
1667a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LogString ls("%d plus %d equals %d", 12, 23, 35);
1677a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_STREQ("12 plus 23 equals 35", ls.string());
1687a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1697a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1707a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogInfoEmpty) {
1717a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_INFO, __LINE__ + 1, "");
1727a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(INFO);
1737a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
1747a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1757a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1767a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogInfoWithString) {
1777a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    static const char kString[] = "Hello World!";
1787a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_INFO, __LINE__ + 1, kString);
1797a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(INFO) << kString;
1807a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
1817a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1827a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1837a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogInfoWithTwoStrings) {
1847a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_INFO, __LINE__ + 1, "Hello Globe!");
1857a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(INFO) << "Hello " << "Globe!";
1867a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
1877a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1887a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1897a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogInfoWithLogString) {
1907a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LogString ls("Hello You!");
1917a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_INFO, __LINE__ + 1, ls.string());
1927a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(INFO) << ls;
1937a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
1947a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
1957a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
1967a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogWarning) {
1977a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    static const char kWarning[] = "Elvis has left the building!";
1987a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_WARNING, __LINE__ + 1, kWarning);
1997a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(WARNING) << kWarning;
2007a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
2017a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2027a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2037a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogError) {
2047a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    static const char kError[] = "Bad Bad Robot!";
2057a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_ERROR, __LINE__ + 1, kError);
2067a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(ERROR) << kError;
2077a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2087a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
2097a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2107a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2117a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogFatal) {
2127a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    static const char kFatalMessage[] = "I'm dying";
2137a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_FATAL, __LINE__ + 1, kFatalMessage);
2147a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(FATAL) << kFatalMessage;
2157a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
2167a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_TRUE(mFatal);
2177a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2187a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2197a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogEvaluatesArgumentsIfNeeded) {
2207a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    // Use LOG_FATAL since it is always active.
2217a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool flag = false;
2227a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_FATAL, __LINE__ + 1, "PANIC: Flag was set!");
2237a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(FATAL) << "PANIC: " << setFlag(&flag, "Flag was set!");
2247a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
2257a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_TRUE(mFatal);
2267a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_TRUE(flag);
2277a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2287a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2297a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(LogTest, LogOnlyEvaluatesArgumentsIfNeeded) {
2307a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool flag = false;
2317a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    LOG(INVISIBLE) << setFlag(&flag, "Flag was set!");
2327a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(flag);
2337a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2347a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2357a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2367a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// TODO(digit): Convert this to a real death test when this is supported
2377a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner// by our version of GTest.
2387a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(CheckTest, CheckFalse) {
2397a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_FATAL, __LINE__ + 1, "Check failed: false. ");
2407a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK(false);
2417a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
2427a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2437a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2447a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(CheckTest, CheckFalseEvaluatesArguments) {
2457a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool flag = false;
2467a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_FATAL, __LINE__ + 1, "Check failed: false. Flag was set!");
2477a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK(false) << setFlag(&flag, "Flag was set!");
2487a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_TRUE(flag);
2497a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
2507a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2517a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2527a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(CheckTest, CheckTrue) {
2537a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK(true);
2547a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(mFatal);
2557a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2567a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2577a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(CheckTest, CheckTrueDoesNotEvaluateArguments) {
2587a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool flag = false;
2597a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK(true) << setFlag(&flag, "Flag was set!");
2607a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(flag);
2617a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(mFatal);
2627a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2637a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2647a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#if ENABLE_DCHECK != 0
2657a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(DCheckEnabledTest, DCheckIsOnReturnsTrue) {
2667a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_TRUE(DCHECK_IS_ON());
2677a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2687a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2697a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(DCheckEnabledTest, DCheckFalse) {
2707a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool flag = false;
2717a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpected(LOG_FATAL, __LINE__ + 1, "Check failed: false. Flag was set!");
2727a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    DCHECK(false) << setFlag(&flag, "Flag was set!");
2737a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
2747a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2757a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2767a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(DCheckEnabledTest, DCheckTrue) {
2777a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool flag = false;
2787a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    DCHECK(true) << setFlag(&flag, "Flag was set!");
2797a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(flag);
2807a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(mFatal);
2817a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2827a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#endif  // ENABLE_DCHECK != 0
2837a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2847a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#if ENABLE_DCHECK != 2
2857a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(DCheckDisabledTest, DCheckIsOnReturnsFalse) {
2867a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(DCHECK_IS_ON());
2877a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2887a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2897a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(DCheckDisabledTest, DCheckFalse) {
2907a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    bool flag = false;
2917a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    DCHECK(false) << setFlag(&flag, "Flag was set!");
2927a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(flag);
2937a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_FALSE(mFatal);
2947a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2957a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
2967a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(DCheckDisabledTest, DCheckTrue) {
2977a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    DCHECK(true);
2987a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
2997a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner#endif  // ENABLE_DCHECK != 2
3007a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
3017a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(PLogTest, PLogInfoEmpty) {
3027a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpectedErrno(LOG_INFO, __LINE__ + 2, EINVAL, "");
3037a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    errno = EINVAL;
3047a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    PLOG(INFO);
3057a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
3067a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
3077a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
3087a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' TurnerTEST_F(PLogTest, PLogInfoPreservesErrno) {
3097a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    // Select a value that is unlikely to ever be raised by the logging
3107a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    // machinery.
3117a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    const int kErrnoCode = ENOEXEC;
3127a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setForcedErrno(EINVAL);
3137a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    setExpectedErrno(LOG_INFO, __LINE__ + 2, kErrnoCode, "Hi");
3147a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    errno = kErrnoCode;
3157a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    PLOG(INFO) << "Hi";
3167a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    EXPECT_EQ(kErrnoCode, errno);
3177a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner    CHECK_EXPECTATIONS();
3187a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}
3197a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner
3207a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}  // namespace base
3217a41eef6b64b2a07cf170844c897e4ae1cda998eDavid 'Digit' Turner}  // namespace android
322