summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/environment
diff options
context:
space:
mode:
Diffstat (limited to 'mojo/public/cpp/environment')
-rw-r--r--mojo/public/cpp/environment/BUILD.gn41
-rw-r--r--mojo/public/cpp/environment/DEPS3
-rw-r--r--mojo/public/cpp/environment/async_waiter.h40
-rw-r--r--mojo/public/cpp/environment/environment.h49
-rw-r--r--mojo/public/cpp/environment/lib/DEPS4
-rw-r--r--mojo/public/cpp/environment/lib/async_waiter.cc34
-rw-r--r--mojo/public/cpp/environment/lib/default_async_waiter.cc85
-rw-r--r--mojo/public/cpp/environment/lib/default_async_waiter.h18
-rw-r--r--mojo/public/cpp/environment/lib/default_logger.cc69
-rw-r--r--mojo/public/cpp/environment/lib/default_logger.h18
-rw-r--r--mojo/public/cpp/environment/lib/environment.cc80
-rw-r--r--mojo/public/cpp/environment/lib/logging.cc45
-rw-r--r--mojo/public/cpp/environment/logging.h86
-rw-r--r--mojo/public/cpp/environment/tests/BUILD.gn29
-rw-r--r--mojo/public/cpp/environment/tests/async_wait_unittest.cc114
-rw-r--r--mojo/public/cpp/environment/tests/async_waiter_unittest.cc107
-rw-r--r--mojo/public/cpp/environment/tests/logger_unittest.cc56
-rw-r--r--mojo/public/cpp/environment/tests/logging_unittest.cc479
18 files changed, 0 insertions, 1357 deletions
diff --git a/mojo/public/cpp/environment/BUILD.gn b/mojo/public/cpp/environment/BUILD.gn
deleted file mode 100644
index 87f24ff..0000000
--- a/mojo/public/cpp/environment/BUILD.gn
+++ /dev/null
@@ -1,41 +0,0 @@
-# Copyright 2014 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.
-
-import("../../mojo_sdk.gni")
-
-mojo_sdk_source_set("environment") {
- sources = [
- "async_waiter.h",
- "logging.h",
- "environment.h",
- ]
-
- mojo_sdk_public_deps = [ "mojo/public/c/environment" ]
-
- mojo_sdk_deps = [
- "mojo/public/cpp/bindings:callback",
- "mojo/public/cpp/system",
- ]
-}
-
-mojo_sdk_source_set("standalone") {
- sources = [
- "lib/async_waiter.cc",
- "lib/default_async_waiter.cc",
- "lib/default_async_waiter.h",
- "lib/default_logger.cc",
- "lib/default_logger.h",
- "lib/environment.cc",
- "lib/logging.cc",
- ]
-
- public_deps = [
- ":environment",
- ]
-
- mojo_sdk_deps = [
- "mojo/public/c/environment",
- "mojo/public/cpp/utility",
- ]
-}
diff --git a/mojo/public/cpp/environment/DEPS b/mojo/public/cpp/environment/DEPS
deleted file mode 100644
index 04346d9..0000000
--- a/mojo/public/cpp/environment/DEPS
+++ /dev/null
@@ -1,3 +0,0 @@
-include_rules = [
- "+mojo/public/cpp/bindings/callback.h",
-]
diff --git a/mojo/public/cpp/environment/async_waiter.h b/mojo/public/cpp/environment/async_waiter.h
deleted file mode 100644
index 83b7c8a..0000000
--- a/mojo/public/cpp/environment/async_waiter.h
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright 2014 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.
-
-#ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_ASYNC_WAITER_H_
-#define MOJO_PUBLIC_CPP_ENVIRONMENT_ASYNC_WAITER_H_
-
-#include "mojo/public/c/environment/async_waiter.h"
-#include "mojo/public/cpp/bindings/callback.h"
-#include "mojo/public/cpp/environment/environment.h"
-#include "mojo/public/cpp/system/handle.h"
-
-namespace mojo {
-
-// A class that waits until a handle is ready and calls |callback| with the
-// result. If the AsyncWaiter is deleted before the handle is ready, the wait is
-// cancelled and the callback will not be called.
-class AsyncWaiter {
- public:
- typedef mojo::Callback<void(MojoResult)> Callback;
-
- AsyncWaiter(Handle handle,
- MojoHandleSignals signals,
- const Callback& callback);
- ~AsyncWaiter();
-
- private:
- static void WaitComplete(void* waiter, MojoResult result);
- void WaitCompleteInternal(MojoResult result);
-
- const MojoAsyncWaiter* waiter_;
- MojoAsyncWaitID id_;
- const Callback callback_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiter);
-};
-
-} // namespace mojo
-
-#endif // MOJO_PUBLIC_CPP_ENVIRONMENT_ASYNC_WAITER_H_
diff --git a/mojo/public/cpp/environment/environment.h b/mojo/public/cpp/environment/environment.h
deleted file mode 100644
index ce3f7d0..0000000
--- a/mojo/public/cpp/environment/environment.h
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright 2014 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.
-
-#ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_ENVIRONMENT_H_
-#define MOJO_PUBLIC_CPP_ENVIRONMENT_ENVIRONMENT_H_
-
-#include "mojo/public/cpp/system/macros.h"
-
-struct MojoAsyncWaiter;
-struct MojoLogger;
-
-namespace mojo {
-
-// Other parts of the Mojo C++ APIs use the *static* methods of this class.
-//
-// The "standalone" implementation of this class requires that this class (in
-// the lib/ subdirectory) be instantiated (and remain so) while using the Mojo
-// C++ APIs. I.e., the static methods depend on things set up by the constructor
-// and torn down by the destructor.
-//
-// Other implementations may not have this requirement.
-class Environment {
- public:
- Environment();
- // This constructor allows the standard implementations to be overridden (set
- // a parameter to null to get the standard implementation).
- Environment(const MojoAsyncWaiter* default_async_waiter,
- const MojoLogger* default_logger);
- ~Environment();
-
- static const MojoAsyncWaiter* GetDefaultAsyncWaiter();
- static const MojoLogger* GetDefaultLogger();
-
- // These instantiate and destroy an environment-specific run loop for the
- // current thread, allowing |GetDefaultAsyncWaiter()| to be used. (The run
- // loop itself should be accessible via thread-local storage, using methods
- // specific to the run loop implementation.) Creating and destroying nested
- // run loops is not supported.
- static void InstantiateDefaultRunLoop();
- static void DestroyDefaultRunLoop();
-
- private:
- MOJO_DISALLOW_COPY_AND_ASSIGN(Environment);
-};
-
-} // namespace mojo
-
-#endif // MOJO_PUBLIC_CPP_ENVIRONMENT_ENVIRONMENT_H_
diff --git a/mojo/public/cpp/environment/lib/DEPS b/mojo/public/cpp/environment/lib/DEPS
deleted file mode 100644
index 1889e1f..0000000
--- a/mojo/public/cpp/environment/lib/DEPS
+++ /dev/null
@@ -1,4 +0,0 @@
-include_rules = [
- "+mojo/public/cpp/environment",
- "+mojo/public/cpp/utility",
-]
diff --git a/mojo/public/cpp/environment/lib/async_waiter.cc b/mojo/public/cpp/environment/lib/async_waiter.cc
deleted file mode 100644
index 599a649..0000000
--- a/mojo/public/cpp/environment/lib/async_waiter.cc
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright 2014 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 "mojo/public/cpp/environment/async_waiter.h"
-
-namespace mojo {
-
-AsyncWaiter::AsyncWaiter(Handle handle,
- MojoHandleSignals signals,
- const Callback& callback)
- : waiter_(Environment::GetDefaultAsyncWaiter()),
- id_(0),
- callback_(callback) {
- id_ = waiter_->AsyncWait(handle.value(), signals, MOJO_DEADLINE_INDEFINITE,
- &AsyncWaiter::WaitComplete, this);
-}
-
-AsyncWaiter::~AsyncWaiter() {
- if (id_)
- waiter_->CancelWait(id_);
-}
-
-// static
-void AsyncWaiter::WaitComplete(void* waiter, MojoResult result) {
- static_cast<AsyncWaiter*>(waiter)->WaitCompleteInternal(result);
-}
-
-void AsyncWaiter::WaitCompleteInternal(MojoResult result) {
- id_ = 0;
- callback_.Run(result);
-}
-
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/lib/default_async_waiter.cc b/mojo/public/cpp/environment/lib/default_async_waiter.cc
deleted file mode 100644
index 4a588a9..0000000
--- a/mojo/public/cpp/environment/lib/default_async_waiter.cc
+++ /dev/null
@@ -1,85 +0,0 @@
-// Copyright 2014 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 "mojo/public/cpp/environment/lib/default_async_waiter.h"
-
-#include <assert.h>
-
-#include "mojo/public/c/environment/async_waiter.h"
-#include "mojo/public/cpp/utility/run_loop.h"
-#include "mojo/public/cpp/utility/run_loop_handler.h"
-
-namespace mojo {
-
-namespace {
-
-// RunLoopHandler implementation used for a request to AsyncWait(). There are
-// two ways RunLoopHandlerImpl is deleted:
-// . when the handle is ready (or errored).
-// . when CancelWait() is invoked.
-class RunLoopHandlerImpl : public RunLoopHandler {
- public:
- RunLoopHandlerImpl(const Handle& handle,
- MojoAsyncWaitCallback callback,
- void* closure)
- : handle_(handle), callback_(callback), closure_(closure) {}
-
- ~RunLoopHandlerImpl() override { RunLoop::current()->RemoveHandler(handle_); }
-
- // RunLoopHandler:
- void OnHandleReady(const Handle& handle) override {
- NotifyCallback(MOJO_RESULT_OK);
- }
-
- void OnHandleError(const Handle& handle, MojoResult result) override {
- NotifyCallback(result);
- }
-
- private:
- void NotifyCallback(MojoResult result) {
- // Delete this to unregister the handle. That way if the callback
- // reregisters everything is ok.
- MojoAsyncWaitCallback callback = callback_;
- void* closure = closure_;
- delete this;
-
- callback(closure, result);
- }
-
- const Handle handle_;
- MojoAsyncWaitCallback callback_;
- void* closure_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoopHandlerImpl);
-};
-
-MojoAsyncWaitID AsyncWait(MojoHandle handle,
- MojoHandleSignals signals,
- MojoDeadline deadline,
- MojoAsyncWaitCallback callback,
- void* closure) {
- RunLoop* run_loop = RunLoop::current();
- assert(run_loop);
-
- // |run_loop_handler| is destroyed either when the handle is ready or if
- // CancelWait is invoked.
- RunLoopHandlerImpl* run_loop_handler =
- new RunLoopHandlerImpl(Handle(handle), callback, closure);
- run_loop->AddHandler(run_loop_handler, Handle(handle), signals, deadline);
- return reinterpret_cast<MojoAsyncWaitID>(run_loop_handler);
-}
-
-void CancelWait(MojoAsyncWaitID wait_id) {
- delete reinterpret_cast<RunLoopHandlerImpl*>(wait_id);
-}
-
-} // namespace
-
-namespace internal {
-
-const MojoAsyncWaiter kDefaultAsyncWaiter = {AsyncWait, CancelWait};
-
-} // namespace internal
-
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/lib/default_async_waiter.h b/mojo/public/cpp/environment/lib/default_async_waiter.h
deleted file mode 100644
index 49ce233..0000000
--- a/mojo/public/cpp/environment/lib/default_async_waiter.h
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2014 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.
-
-#ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_LIB_DEFAULT_ASYNC_WAITER_H_
-#define MOJO_PUBLIC_CPP_ENVIRONMENT_LIB_DEFAULT_ASYNC_WAITER_H_
-
-struct MojoAsyncWaiter;
-
-namespace mojo {
-namespace internal {
-
-extern const MojoAsyncWaiter kDefaultAsyncWaiter;
-
-} // namespace internal
-} // namespace mojo
-
-#endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LIB_DEFAULT_ASYNC_WAITER_H_
diff --git a/mojo/public/cpp/environment/lib/default_logger.cc b/mojo/public/cpp/environment/lib/default_logger.cc
deleted file mode 100644
index 05cafbd..0000000
--- a/mojo/public/cpp/environment/lib/default_logger.cc
+++ /dev/null
@@ -1,69 +0,0 @@
-// Copyright 2014 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 "mojo/public/cpp/environment/lib/default_logger.h"
-
-#include <stdio.h>
-#include <stdlib.h> // For |abort()|.
-
-#include <algorithm>
-
-#include "mojo/public/c/environment/logger.h"
-
-namespace mojo {
-
-namespace {
-
-MojoLogLevel g_minimum_log_level = MOJO_LOG_LEVEL_INFO;
-
-const char* GetLogLevelString(MojoLogLevel log_level) {
- if (log_level <= MOJO_LOG_LEVEL_VERBOSE - 3)
- return "VERBOSE4+";
- switch (log_level) {
- case MOJO_LOG_LEVEL_VERBOSE - 2:
- return "VERBOSE3";
- case MOJO_LOG_LEVEL_VERBOSE - 1:
- return "VERBOSE2";
- case MOJO_LOG_LEVEL_VERBOSE:
- return "VERBOSE1";
- case MOJO_LOG_LEVEL_INFO:
- return "INFO";
- case MOJO_LOG_LEVEL_WARNING:
- return "WARNING";
- case MOJO_LOG_LEVEL_ERROR:
- return "ERROR";
- }
- // Consider everything higher to be fatal.
- return "FATAL";
-}
-
-void LogMessage(MojoLogLevel log_level, const char* message) {
- if (log_level < g_minimum_log_level)
- return;
-
- // TODO(vtl): Add timestamp also?
- fprintf(stderr, "%s: %s\n", GetLogLevelString(log_level), message);
- if (log_level >= MOJO_LOG_LEVEL_FATAL)
- abort();
-}
-
-MojoLogLevel GetMinimumLogLevel() {
- return g_minimum_log_level;
-}
-
-void SetMinimumLogLevel(MojoLogLevel minimum_log_level) {
- g_minimum_log_level = std::min(minimum_log_level, MOJO_LOG_LEVEL_FATAL);
-}
-
-} // namespace
-
-namespace internal {
-
-const MojoLogger kDefaultLogger = {LogMessage,
- GetMinimumLogLevel,
- SetMinimumLogLevel};
-
-} // namespace internal
-
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/lib/default_logger.h b/mojo/public/cpp/environment/lib/default_logger.h
deleted file mode 100644
index 4db3233..0000000
--- a/mojo/public/cpp/environment/lib/default_logger.h
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2014 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.
-
-#ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_LIB_DEFAULT_LOGGER_H_
-#define MOJO_PUBLIC_CPP_ENVIRONMENT_LIB_DEFAULT_LOGGER_H_
-
-struct MojoLogger;
-
-namespace mojo {
-namespace internal {
-
-extern const MojoLogger kDefaultLogger;
-
-} // namespace internal
-} // namespace mojo
-
-#endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LIB_DEFAULT_LOGGER_H_
diff --git a/mojo/public/cpp/environment/lib/environment.cc b/mojo/public/cpp/environment/lib/environment.cc
deleted file mode 100644
index 6fb7e22..0000000
--- a/mojo/public/cpp/environment/lib/environment.cc
+++ /dev/null
@@ -1,80 +0,0 @@
-// Copyright 2014 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 "mojo/public/cpp/environment/environment.h"
-
-#include <assert.h>
-
-#include "mojo/public/c/environment/logger.h"
-#include "mojo/public/cpp/environment/lib/default_async_waiter.h"
-#include "mojo/public/cpp/environment/lib/default_logger.h"
-#include "mojo/public/cpp/utility/run_loop.h"
-
-namespace mojo {
-
-namespace {
-
-const MojoAsyncWaiter* g_default_async_waiter = nullptr;
-const MojoLogger* g_default_logger = nullptr;
-
-void Init(const MojoAsyncWaiter* default_async_waiter,
- const MojoLogger* default_logger) {
- g_default_async_waiter = default_async_waiter
- ? default_async_waiter
- : &internal::kDefaultAsyncWaiter;
- g_default_logger =
- default_logger ? default_logger : &internal::kDefaultLogger;
-
- RunLoop::SetUp();
-}
-
-} // namespace
-
-Environment::Environment() {
- Init(nullptr, nullptr);
-}
-
-Environment::Environment(const MojoAsyncWaiter* default_async_waiter,
- const MojoLogger* default_logger) {
- Init(default_async_waiter, default_logger);
-}
-
-Environment::~Environment() {
- RunLoop::TearDown();
-
- // TODO(vtl): Maybe we should allow nesting, and restore previous default
- // async waiters and loggers?
- g_default_async_waiter = nullptr;
- g_default_logger = nullptr;
-}
-
-// static
-const MojoAsyncWaiter* Environment::GetDefaultAsyncWaiter() {
- assert(g_default_async_waiter); // Fails if not "inside" |Environment|.
- return g_default_async_waiter;
-}
-
-// static
-const MojoLogger* Environment::GetDefaultLogger() {
- assert(g_default_logger); // Fails if not "inside" |Environment|.
- return g_default_logger;
-}
-
-// static
-void Environment::InstantiateDefaultRunLoop() {
- assert(!RunLoop::current());
- // Not leaked: accessible from |RunLoop::current()|.
- RunLoop* run_loop = new RunLoop();
- MOJO_ALLOW_UNUSED_LOCAL(run_loop);
- assert(run_loop == RunLoop::current());
-}
-
-// static
-void Environment::DestroyDefaultRunLoop() {
- assert(RunLoop::current());
- delete RunLoop::current();
- assert(!RunLoop::current());
-}
-
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/lib/logging.cc b/mojo/public/cpp/environment/lib/logging.cc
deleted file mode 100644
index 990626d..0000000
--- a/mojo/public/cpp/environment/lib/logging.cc
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright 2014 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 "mojo/public/cpp/environment/logging.h"
-
-#include "mojo/public/cpp/environment/environment.h"
-
-namespace mojo {
-namespace internal {
-
-namespace {
-
-// Gets a pointer to the filename portion of |s|. Assumes that the filename
-// follows the last slash or backslash in |s|, or is |s| if no slash or
-// backslash is present.
-//
-// E.g., a pointer to "foo.cc" is returned for the following inputs: "foo.cc",
-// "./foo.cc", ".\foo.cc", "/absolute/path/to/foo.cc",
-// "relative/path/to/foo.cc", "C:\absolute\path\to\foo.cc", etc.
-const char* GetFilename(const char* s) {
- const char* rv = s;
- while (*s) {
- if (*s == '/' || *s == '\\')
- rv = s + 1;
- s++;
- }
- return rv;
-}
-
-} // namespace
-
-LogMessage::LogMessage(const char* file, int line, MojoLogLevel log_level)
- : log_level_(log_level) {
- // Note: Don't include the log level in the message, since that's passed on.
- stream_ << GetFilename(file) << '(' << line << "): ";
-}
-
-LogMessage::~LogMessage() {
- Environment::GetDefaultLogger()->LogMessage(log_level_,
- stream_.str().c_str());
-}
-
-} // namespace internal
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/logging.h b/mojo/public/cpp/environment/logging.h
deleted file mode 100644
index 5df18fb..0000000
--- a/mojo/public/cpp/environment/logging.h
+++ /dev/null
@@ -1,86 +0,0 @@
-// Copyright 2014 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.
-
-// Logging macros, similar to Chromium's base/logging.h, except with |MOJO_|
-// prefixes and missing some features (notably |CHECK_EQ()|, etc.).
-
-// TODO(vtl): It's weird that this is in the environment directory, since its
-// implementation (in environment/lib) is meant to be used by any implementation
-// of the environment.
-
-#ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
-#define MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
-
-#include <sstream>
-
-#include "mojo/public/c/environment/logger.h"
-#include "mojo/public/cpp/environment/environment.h"
-#include "mojo/public/cpp/system/macros.h"
-
-#define MOJO_LOG_STREAM(level) \
- ::mojo::internal::LogMessage(__FILE__, __LINE__, MOJO_LOG_LEVEL_##level) \
- .stream()
-
-#define MOJO_LAZY_LOG_STREAM(level, condition) \
- !(condition) ? (void)0 \
- : ::mojo::internal::VoidifyOstream() & MOJO_LOG_STREAM(level)
-
-#define MOJO_SHOULD_LOG(level) \
- (MOJO_LOG_LEVEL_##level >= \
- ::mojo::Environment::GetDefaultLogger()->GetMinimumLogLevel())
-
-#define MOJO_LOG(level) MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level))
-
-#define MOJO_LOG_IF(level, condition) \
- MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level) && (condition))
-
-#define MOJO_CHECK(condition) \
- MOJO_LAZY_LOG_STREAM(FATAL, !(condition)) << "Check failed: " #condition "." \
- " "
-
-// Note: For non-debug builds, |MOJO_DLOG_IF()| *eliminates* (i.e., doesn't
-// compile) the condition, whereas |MOJO_DCHECK()| "neuters" the condition
-// (i.e., compiles, but doesn't evaluate).
-#ifdef NDEBUG
-
-#define MOJO_DLOG(level) MOJO_LAZY_LOG_STREAM(level, false)
-#define MOJO_DLOG_IF(level, condition) MOJO_LAZY_LOG_STREAM(level, false)
-#define MOJO_DCHECK(condition) \
- MOJO_LAZY_LOG_STREAM(FATAL, false ? !(condition) : false)
-
-#else
-
-#define MOJO_DLOG(level) MOJO_LOG(level)
-#define MOJO_DLOG_IF(level, condition) MOJO_LOG_IF(level, condition)
-#define MOJO_DCHECK(condition) MOJO_CHECK(condition)
-
-#endif // NDEBUG
-
-namespace mojo {
-namespace internal {
-
-class LogMessage {
- public:
- LogMessage(const char* file, int line, MojoLogLevel log_level);
- ~LogMessage();
-
- std::ostream& stream() { return stream_; }
-
- private:
- const MojoLogLevel log_level_;
- std::ostringstream stream_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(LogMessage);
-};
-
-// Used to ignore a stream.
-struct VoidifyOstream {
- // Use & since it has precedence lower than << but higher than ?:.
- void operator&(std::ostream&) {}
-};
-
-} // namespace internal
-} // namespace mojo
-
-#endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
diff --git a/mojo/public/cpp/environment/tests/BUILD.gn b/mojo/public/cpp/environment/tests/BUILD.gn
deleted file mode 100644
index 10fd056..0000000
--- a/mojo/public/cpp/environment/tests/BUILD.gn
+++ /dev/null
@@ -1,29 +0,0 @@
-# Copyright 2014 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.
-
-import("../../../mojo_sdk.gni")
-
-mojo_sdk_source_set("tests") {
- testonly = true
-
- sources = [
- "async_wait_unittest.cc",
- "async_waiter_unittest.cc",
- "logger_unittest.cc",
- "logging_unittest.cc",
- ]
-
- deps = [
- "//testing/gtest",
- ]
-
- mojo_sdk_deps = [
- "mojo/public/c/environment",
- "mojo/public/cpp/bindings:callback",
- "mojo/public/cpp/environment:standalone",
- "mojo/public/cpp/system",
- "mojo/public/cpp/test_support:test_utils",
- "mojo/public/cpp/utility",
- ]
-}
diff --git a/mojo/public/cpp/environment/tests/async_wait_unittest.cc b/mojo/public/cpp/environment/tests/async_wait_unittest.cc
deleted file mode 100644
index 83c5ca0..0000000
--- a/mojo/public/cpp/environment/tests/async_wait_unittest.cc
+++ /dev/null
@@ -1,114 +0,0 @@
-// Copyright 2014 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 <string>
-
-#include "mojo/public/c/environment/async_waiter.h"
-#include "mojo/public/cpp/environment/environment.h"
-#include "mojo/public/cpp/system/core.h"
-#include "mojo/public/cpp/system/macros.h"
-#include "mojo/public/cpp/test_support/test_utils.h"
-#include "mojo/public/cpp/utility/run_loop.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace {
-
-class TestAsyncWaitCallback {
- public:
- TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {}
- ~TestAsyncWaitCallback() {}
-
- int result_count() const { return result_count_; }
-
- MojoResult last_result() const { return last_result_; }
-
- // MojoAsyncWaitCallback:
- static void OnHandleReady(void* closure, MojoResult result) {
- TestAsyncWaitCallback* self = static_cast<TestAsyncWaitCallback*>(closure);
- self->result_count_++;
- self->last_result_ = result;
- }
-
- private:
- int result_count_;
- MojoResult last_result_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback);
-};
-
-MojoAsyncWaitID CallAsyncWait(const Handle& handle,
- MojoHandleSignals signals,
- TestAsyncWaitCallback* callback) {
- return Environment::GetDefaultAsyncWaiter()->AsyncWait(
- handle.value(),
- signals,
- MOJO_DEADLINE_INDEFINITE,
- &TestAsyncWaitCallback::OnHandleReady,
- callback);
-}
-
-void CallCancelWait(MojoAsyncWaitID wait_id) {
- Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id);
-}
-
-class AsyncWaitTest : public testing::Test {
- public:
- AsyncWaitTest() {}
-
- private:
- Environment environment_;
- RunLoop run_loop_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaitTest);
-};
-
-// Verifies AsyncWaitCallback is notified when pipe is ready.
-TEST_F(AsyncWaitTest, CallbackNotified) {
- TestAsyncWaitCallback callback;
- MessagePipe test_pipe;
- EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string()));
-
- CallAsyncWait(
- test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback);
- RunLoop::current()->Run();
- EXPECT_EQ(1, callback.result_count());
- EXPECT_EQ(MOJO_RESULT_OK, callback.last_result());
-}
-
-// Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready.
-TEST_F(AsyncWaitTest, TwoCallbacksNotified) {
- TestAsyncWaitCallback callback1;
- TestAsyncWaitCallback callback2;
- MessagePipe test_pipe1;
- MessagePipe test_pipe2;
- EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string()));
- EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string()));
-
- CallAsyncWait(
- test_pipe1.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback1);
- CallAsyncWait(
- test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback2);
-
- RunLoop::current()->Run();
- EXPECT_EQ(1, callback1.result_count());
- EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result());
- EXPECT_EQ(1, callback2.result_count());
- EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result());
-}
-
-// Verifies cancel works.
-TEST_F(AsyncWaitTest, CancelCallback) {
- TestAsyncWaitCallback callback;
- MessagePipe test_pipe;
- EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string()));
-
- CallCancelWait(CallAsyncWait(
- test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback));
- RunLoop::current()->Run();
- EXPECT_EQ(0, callback.result_count());
-}
-
-} // namespace
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/tests/async_waiter_unittest.cc b/mojo/public/cpp/environment/tests/async_waiter_unittest.cc
deleted file mode 100644
index 1c1c2bf..0000000
--- a/mojo/public/cpp/environment/tests/async_waiter_unittest.cc
+++ /dev/null
@@ -1,107 +0,0 @@
-// Copyright 2014 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 "mojo/public/cpp/bindings/callback.h"
-#include "mojo/public/cpp/environment/async_waiter.h"
-#include "mojo/public/cpp/test_support/test_utils.h"
-#include "mojo/public/cpp/utility/run_loop.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace {
-
-class TestAsyncWaitCallback {
- public:
- TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {}
- ~TestAsyncWaitCallback() {}
-
- int result_count() const { return result_count_; }
-
- MojoResult last_result() const { return last_result_; }
-
- void OnHandleReady(MojoResult result) {
- result_count_++;
- last_result_ = result;
- }
-
- private:
- int result_count_;
- MojoResult last_result_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback);
-};
-
-// Manual code to create a callback since we don't have mojo::Bind yet.
-class ManualCallback {
- public:
- explicit ManualCallback(TestAsyncWaitCallback* callback)
- : callback_(callback) {}
-
- void Run(MojoResult result) const { callback_->OnHandleReady(result); }
-
- private:
- TestAsyncWaitCallback* callback_;
-};
-
-class AsyncWaiterTest : public testing::Test {
- public:
- AsyncWaiterTest() {}
-
- private:
- Environment environment_;
- RunLoop run_loop_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest);
-};
-
-// Verifies AsyncWaitCallback is notified when pipe is ready.
-TEST_F(AsyncWaiterTest, CallbackNotified) {
- TestAsyncWaitCallback callback;
- MessagePipe test_pipe;
- EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string()));
-
- AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
- ManualCallback(&callback));
- RunLoop::current()->Run();
- EXPECT_EQ(1, callback.result_count());
- EXPECT_EQ(MOJO_RESULT_OK, callback.last_result());
-}
-
-// Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready.
-TEST_F(AsyncWaiterTest, TwoCallbacksNotified) {
- TestAsyncWaitCallback callback1;
- TestAsyncWaitCallback callback2;
- MessagePipe test_pipe1;
- MessagePipe test_pipe2;
- EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string()));
- EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string()));
-
- AsyncWaiter waiter1(test_pipe1.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
- ManualCallback(&callback1));
- AsyncWaiter waiter2(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
- ManualCallback(&callback2));
-
- RunLoop::current()->Run();
- EXPECT_EQ(1, callback1.result_count());
- EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result());
- EXPECT_EQ(1, callback2.result_count());
- EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result());
-}
-
-// Verifies cancel works.
-TEST_F(AsyncWaiterTest, CancelCallback) {
- TestAsyncWaitCallback callback;
- MessagePipe test_pipe;
- EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string()));
-
- {
- AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
- ManualCallback(&callback));
- }
- RunLoop::current()->Run();
- EXPECT_EQ(0, callback.result_count());
-}
-
-} // namespace
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/tests/logger_unittest.cc b/mojo/public/cpp/environment/tests/logger_unittest.cc
deleted file mode 100644
index 7aef2ee..0000000
--- a/mojo/public/cpp/environment/tests/logger_unittest.cc
+++ /dev/null
@@ -1,56 +0,0 @@
-// Copyright 2014 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 "mojo/public/c/environment/logger.h"
-#include "mojo/public/cpp/environment/environment.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace {
-
-TEST(LoggerTest, Basic) {
- Environment environment;
- const MojoLogger* const logger = Environment::GetDefaultLogger();
-
- logger->LogMessage(MOJO_LOG_LEVEL_VERBOSE - 1, "Logged at VERBOSE-1 level");
- logger->LogMessage(MOJO_LOG_LEVEL_VERBOSE, "Logged at VERBOSE level");
- logger->LogMessage(MOJO_LOG_LEVEL_INFO, "Logged at INFO level");
- logger->LogMessage(MOJO_LOG_LEVEL_WARNING, "Logged at WARNING level");
- logger->LogMessage(MOJO_LOG_LEVEL_ERROR, "Logged at ERROR level");
-
- // This should kill us:
- EXPECT_DEATH_IF_SUPPORTED(
- { logger->LogMessage(MOJO_LOG_LEVEL_FATAL, "Logged at FATAL level"); },
- "");
-}
-
-TEST(LoggerTest, LogLevels) {
- Environment environment;
- const MojoLogger* const logger = Environment::GetDefaultLogger();
-
- for (MojoLogLevel log_level = MOJO_LOG_LEVEL_VERBOSE - 1;
- log_level <= MOJO_LOG_LEVEL_FATAL + 1;
- log_level++) {
- logger->SetMinimumLogLevel(log_level);
-
- if (log_level <= MOJO_LOG_LEVEL_FATAL)
- EXPECT_EQ(log_level, logger->GetMinimumLogLevel());
- else
- EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, logger->GetMinimumLogLevel());
-
- logger->LogMessage(MOJO_LOG_LEVEL_VERBOSE - 1, "Logged at VERBOSE-1 level");
- logger->LogMessage(MOJO_LOG_LEVEL_VERBOSE, "Logged at VERBOSE level");
- logger->LogMessage(MOJO_LOG_LEVEL_INFO, "Logged at INFO level");
- logger->LogMessage(MOJO_LOG_LEVEL_WARNING, "Logged at WARNING level");
- logger->LogMessage(MOJO_LOG_LEVEL_ERROR, "Logged at ERROR level");
-
- // This should kill us:
- EXPECT_DEATH_IF_SUPPORTED(
- { logger->LogMessage(MOJO_LOG_LEVEL_FATAL, "Logged at FATAL level"); },
- "");
- }
-}
-
-} // namespace
-} // namespace mojo
diff --git a/mojo/public/cpp/environment/tests/logging_unittest.cc b/mojo/public/cpp/environment/tests/logging_unittest.cc
deleted file mode 100644
index 53a1f88..0000000
--- a/mojo/public/cpp/environment/tests/logging_unittest.cc
+++ /dev/null
@@ -1,479 +0,0 @@
-// Copyright 2014 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 <stdlib.h>
-
-#include <sstream>
-#include <string>
-
-#include "mojo/public/cpp/environment/environment.h"
-#include "mojo/public/cpp/environment/logging.h"
-#include "mojo/public/cpp/system/macros.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-// A macro, so it can be automatically joined with other string literals. (Not
-// simply __FILE__, since that may contain a path.)
-#define OUR_FILENAME "logging_unittest.cc"
-
-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_(nullptr, &kMockLogger) {
- minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
- ResetMockLogger();
- }
- ~LoggingTest() override {}
-
- protected:
- // Note: Does not reset |minimum_log_level_|.
- static void ResetMockLogger() {
- log_message_was_called_ = false;
- last_log_level_ = MOJO_LOG_LEVEL_INFO;
- 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_|
- // (since the macros should mostly avoid this, and we want to be able to check
- // that they do).
- static void MockLogMessage(MojoLogLevel log_level, const char* message) {
- log_message_was_called_ = true;
- last_log_level_ = log_level;
- last_message_ = message;
- }
-
- static MojoLogLevel MockGetMinimumLogLevel() { return minimum_log_level_; }
-
- static void MockSetMinimumLogLevel(MojoLogLevel minimum_log_level) {
- minimum_log_level_ = minimum_log_level;
- }
-
- Environment environment_;
-
- static const MojoLogger kMockLogger;
- static MojoLogLevel minimum_log_level_;
- 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};
-
-// static
-MojoLogLevel LoggingTest::minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
-
-// static
-bool LoggingTest::log_message_was_called_ = MOJO_LOG_LEVEL_INFO;
-
-// static
-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();
-}
-
-TEST_F(LoggingTest, InternalLogMessage) {
- 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());
-
- ResetMockLogger();
-
- internal::LogMessage("./path/to/foo.cc", 123, MOJO_LOG_LEVEL_WARNING).stream()
- << "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());
-
- ResetMockLogger();
-
- internal::LogMessage("/path/to/foo.cc", 123, MOJO_LOG_LEVEL_ERROR).stream()
- << "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());
-
- ResetMockLogger();
-
- internal::LogMessage("path/to/foo.cc", 123, MOJO_LOG_LEVEL_FATAL).stream()
- << "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());
-
- ResetMockLogger();
-
- internal::LogMessage(".\\xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE).stream()
- << "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";
- EXPECT_TRUE(log_message_was_called());
- 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";
- EXPECT_TRUE(log_message_was_called());
- 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";
- EXPECT_TRUE(log_message_was_called());
- EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
- EXPECT_EQ(OUR_FILENAME "(123): hello world", last_message());
-}
-
-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());
-
- 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());
-}
-
-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());
-
- 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());
-
- ResetMockLogger();
-
- MOJO_LAZY_LOG_STREAM(INFO, false) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- 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 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, helper.*member_ptr == 0) << "hello";
- EXPECT_FALSE(log_message_was_called());
-}
-
-TEST_F(LoggingTest, ShouldLog) {
- // We start at |MOJO_LOG_LEVEL_INFO|.
- EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE));
- EXPECT_TRUE(MOJO_SHOULD_LOG(INFO));
- EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING));
- EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
- EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
-
- Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
- EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE));
- EXPECT_FALSE(MOJO_SHOULD_LOG(INFO));
- EXPECT_FALSE(MOJO_SHOULD_LOG(WARNING));
- EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
- EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
-
- 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));
- EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
- EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
-}
-
-TEST_F(LoggingTest, Log) {
- // We start at |MOJO_LOG_LEVEL_INFO|.
- MOJO_LOG(VERBOSE) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- 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());
-
- 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());
-
- ResetMockLogger();
-
- Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
-
- MOJO_LOG(VERBOSE) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_LOG(INFO) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- 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());
-}
-
-TEST_F(LoggingTest, LogIf) {
- // We start at |MOJO_LOG_LEVEL_INFO|.
- MOJO_LOG_IF(VERBOSE, true) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_LOG_IF(VERBOSE, false) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
- Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
-
- bool x = true;
- // Also try to make sure that we parenthesize the condition properly.
- MOJO_LOG_IF(INFO, false || x) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_LOG_IF(INFO, 0 != 1) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_LOG_IF(WARNING, 1 + 1 == 2) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- 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());
-
- ResetMockLogger();
-
- 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, NotCalledCondition()) << "hello";
- EXPECT_FALSE(not_called_condition_was_called());
- EXPECT_FALSE(log_message_was_called());
-}
-
-TEST_F(LoggingTest, Check) {
- MOJO_CHECK(true) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- PtrToMemberHelper helper;
- helper.member = 0;
- int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member;
-
- // Also try to make sure that we parenthesize the condition properly.
- 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: helper.*member_ptr == 1. hello"),
- last_message());
-
- ResetMockLogger();
-
- // Also test a "naked" |MOJO_CHECK()|s.
- MOJO_CHECK(1 + 2 == 3);
- EXPECT_FALSE(log_message_was_called());
-}
-
-TEST_F(LoggingTest, Dlog) {
- // We start at |MOJO_LOG_LEVEL_INFO|.
- MOJO_DLOG(VERBOSE) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_DLOG(INFO) << "hello";
-#ifdef NDEBUG
- EXPECT_FALSE(log_message_was_called());
-#else
- EXPECT_TRUE(log_message_was_called());
- EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
- 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, NotCalledCondition()) << "hello";
- EXPECT_FALSE(not_called_condition_was_called());
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_DLOG_IF(INFO, 1 == 0) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_DLOG_IF(INFO, 1 == 1) << "hello";
-#ifdef NDEBUG
- EXPECT_FALSE(log_message_was_called());
-#else
- EXPECT_TRUE(log_message_was_called());
- EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
- EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message());
-#endif
-
- ResetMockLogger();
-
-// |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds.
-#ifndef NDEBUG
- bool debug_only = true;
-#endif
- MOJO_DLOG_IF(WARNING, debug_only) << "hello";
-#ifdef NDEBUG
- EXPECT_FALSE(log_message_was_called());
-#else
- EXPECT_TRUE(log_message_was_called());
- EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level());
- EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message());
-#endif
-}
-
-TEST_F(LoggingTest, Dcheck) {
- MOJO_DCHECK(true);
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- MOJO_DCHECK(true) << "hello";
- EXPECT_FALSE(log_message_was_called());
-
- ResetMockLogger();
-
- // |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 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 - 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
-} // namespace mojo