diff options
-rw-r--r-- | chromium/base/test/test_pending_task.cc | 81 | ||||
-rw-r--r-- | chromium/base/test/test_pending_task.h | 78 | ||||
-rw-r--r-- | chromium/base/test/test_pending_task_unittest.cc | 57 |
3 files changed, 216 insertions, 0 deletions
diff --git a/chromium/base/test/test_pending_task.cc b/chromium/base/test/test_pending_task.cc new file mode 100644 index 00000000000..f9cfa8e6bb1 --- /dev/null +++ b/chromium/base/test/test_pending_task.cc @@ -0,0 +1,81 @@ +// Copyright (c) 2012 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 "base/test/test_pending_task.h" + +#include <string> +#include <utility> + +namespace base { + +TestPendingTask::TestPendingTask() : nestability(NESTABLE) {} + +TestPendingTask::TestPendingTask(const Location& location, + OnceClosure task, + TimeTicks post_time, + TimeDelta delay, + TestNestability nestability) + : location(location), + task(std::move(task)), + post_time(post_time), + delay(delay), + nestability(nestability) {} + +TestPendingTask::TestPendingTask(TestPendingTask&& other) = default; + +TestPendingTask& TestPendingTask::operator=(TestPendingTask&& other) = default; + +TimeTicks TestPendingTask::GetTimeToRun() const { + return post_time + delay; +} + +bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const { + if (nestability != other.nestability) + return (nestability == NESTABLE); + return GetTimeToRun() < other.GetTimeToRun(); +} + +TestPendingTask::~TestPendingTask() = default; + +void TestPendingTask::AsValueInto(base::trace_event::TracedValue* state) const { + state->SetInteger("run_at", GetTimeToRun().ToInternalValue()); + state->SetString("posting_function", location.ToString()); + state->SetInteger("post_time", post_time.ToInternalValue()); + state->SetInteger("delay", delay.ToInternalValue()); + switch (nestability) { + case NESTABLE: + state->SetString("nestability", "NESTABLE"); + break; + case NON_NESTABLE: + state->SetString("nestability", "NON_NESTABLE"); + break; + } + state->SetInteger("delay", delay.ToInternalValue()); +} + +std::unique_ptr<base::trace_event::ConvertableToTraceFormat> +TestPendingTask::AsValue() const { + std::unique_ptr<base::trace_event::TracedValue> state( + new base::trace_event::TracedValue()); + AsValueInto(state.get()); + return std::move(state); +} + +std::string TestPendingTask::ToString() const { + std::string output("TestPendingTask("); + AsValue()->AppendAsTraceFormat(&output); + output += ")"; + return output; +} + +std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) { + PrintTo(task, &os); + return os; +} + +void PrintTo(const TestPendingTask& task, std::ostream* os) { + *os << task.ToString(); +} + +} // namespace base diff --git a/chromium/base/test/test_pending_task.h b/chromium/base/test/test_pending_task.h new file mode 100644 index 00000000000..dc8eea1fa2f --- /dev/null +++ b/chromium/base/test/test_pending_task.h @@ -0,0 +1,78 @@ +// Copyright (c) 2012 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 BASE_TEST_TEST_PENDING_TASK_H_ +#define BASE_TEST_TEST_PENDING_TASK_H_ + +#include <string> + +#include "base/callback.h" +#include "base/location.h" +#include "base/time/time.h" +#include "base/trace_event/traced_value.h" + +namespace base { + +// TestPendingTask is a helper class for test TaskRunner +// implementations. See test_simple_task_runner.h for example usage. + +struct TestPendingTask { + enum TestNestability { NESTABLE, NON_NESTABLE }; + + TestPendingTask(); + TestPendingTask(TestPendingTask&& other); + TestPendingTask(const Location& location, + OnceClosure task, + TimeTicks post_time, + TimeDelta delay, + TestNestability nestability); + ~TestPendingTask(); + + TestPendingTask& operator=(TestPendingTask&& other); + + // Returns post_time + delay. + TimeTicks GetTimeToRun() const; + + // Returns true if this task is nestable and |other| isn't, or if + // this task's time to run is strictly earlier than |other|'s time + // to run. + // + // Note that two tasks may both have the same nestability and delay. + // In that case, the caller must use some other criterion (probably + // the position in some queue) to break the tie. Conveniently, the + // following STL functions already do so: + // + // - std::min_element + // - std::stable_sort + // + // but the following STL functions don't: + // + // - std::max_element + // - std::sort. + bool ShouldRunBefore(const TestPendingTask& other) const; + + Location location; + OnceClosure task; + TimeTicks post_time; + TimeDelta delay; + TestNestability nestability; + + // Functions for using test pending task with tracing, useful in unit + // testing. + void AsValueInto(base::trace_event::TracedValue* state) const; + std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const; + std::string ToString() const; + + private: + DISALLOW_COPY_AND_ASSIGN(TestPendingTask); +}; + +// gtest helpers which allow pretty printing of the tasks, very useful in unit +// testing. +std::ostream& operator<<(std::ostream& os, const TestPendingTask& task); +void PrintTo(const TestPendingTask& task, std::ostream* os); + +} // namespace base + +#endif // BASE_TEST_TEST_PENDING_TASK_H_ diff --git a/chromium/base/test/test_pending_task_unittest.cc b/chromium/base/test/test_pending_task_unittest.cc new file mode 100644 index 00000000000..ad7723941e4 --- /dev/null +++ b/chromium/base/test/test_pending_task_unittest.cc @@ -0,0 +1,57 @@ +// Copyright (c) 2012 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 "base/test/test_pending_task.h" + +#include "base/bind.h" +#include "base/trace_event/trace_event.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest-spi.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace base { + +TEST(TestPendingTaskTest, TraceSupport) { + base::TestPendingTask task; + + // Check that TestPendingTask can be sent to the trace subsystem. + TRACE_EVENT1("test", "TestPendingTask::TraceSupport", "task", task.AsValue()); + + // Just a basic check that the trace output has *something* in it. + base::trace_event::TracedValueJSON task_value; + task.AsValueInto(&task_value); + EXPECT_THAT(task_value.ToJSON(), ::testing::HasSubstr("post_time")); +} + +TEST(TestPendingTaskTest, ToString) { + base::TestPendingTask task; + + // Just a basic check that ToString has *something* in it. + EXPECT_THAT(task.ToString(), ::testing::StartsWith("TestPendingTask(")); +} + +TEST(TestPendingTaskTest, GTestPrettyPrint) { + base::TestPendingTask task; + + // Check that gtest is calling the TestPendingTask's PrintTo method. + EXPECT_THAT(::testing::PrintToString(task), + ::testing::StartsWith("TestPendingTask(")); + + // Check that pretty printing works with the gtest iostreams operator. + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << task, "TestPendingTask("); +} + +TEST(TestPendingTaskTest, ShouldRunBefore) { + base::TestPendingTask task_first; + task_first.delay = base::TimeDelta::FromMilliseconds(1); + base::TestPendingTask task_after; + task_after.delay = base::TimeDelta::FromMilliseconds(2); + + EXPECT_FALSE(task_after.ShouldRunBefore(task_first)) + << task_after << ".ShouldRunBefore(" << task_first << ")\n"; + EXPECT_TRUE(task_first.ShouldRunBefore(task_after)) + << task_first << ".ShouldRunBefore(" << task_after << ")\n"; +} + +} // namespace base |