libchromeos: Add Location to MessageLoop interface.

This patch adds an optional first parameter Location to PostTask()
and PostDelayedTask() to help track where the callbacks are being
scheduled from when running with DEBUG enabled.

This patch also logs these locations from the GlibMessageLoop when
verbose logging >= 1 is enabled and libchromeos is compiled with debug,
and logs from FakeMessageLoop when verbose logging >= 1 is enabled
regardless of libchromeos being compiled with debug since
FakeMessageLoop is only used from unittest code.

BUG=brillo:91
TEST=Ran unittests with '--gtest_filter=*MessageLoop*' --v=1; log messages are displayed.

Change-Id: I7e28c575f6f9103e7ed1aa90590cc09389134245
Reviewed-on: https://chromium-review.googlesource.com/280567
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/chromeos/message_loops/fake_message_loop_unittest.cc b/chromeos/message_loops/fake_message_loop_unittest.cc
new file mode 100644
index 0000000..d2582fb
--- /dev/null
+++ b/chromeos/message_loops/fake_message_loop_unittest.cc
@@ -0,0 +1,90 @@
+// Copyright 2015 The Chromium OS 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 <chromeos/message_loops/fake_message_loop.h>
+
+#include <memory>
+#include <vector>
+
+#include <base/bind.h>
+#include <base/location.h>
+#include <base/test/simple_test_clock.h>
+#include <gtest/gtest.h>
+
+#include <chromeos/bind_lambda.h>
+#include <chromeos/message_loops/message_loop.h>
+
+using base::Bind;
+using base::Time;
+using base::TimeDelta;
+using std::vector;
+
+namespace chromeos {
+
+using TaskId = MessageLoop::TaskId;
+
+class FakeMessageLoopTest : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    loop_.reset(new FakeMessageLoop(nullptr));
+    EXPECT_TRUE(loop_.get());
+  }
+  void TearDown() override {
+    EXPECT_FALSE(loop_->PendingTasks());
+  }
+
+  base::SimpleTestClock clock_;
+  std::unique_ptr<FakeMessageLoop> loop_;
+};
+
+TEST_F(FakeMessageLoopTest, CancelTaskInvalidValuesTest) {
+  EXPECT_FALSE(loop_->CancelTask(MessageLoop::kTaskIdNull));
+  EXPECT_FALSE(loop_->CancelTask(1234));
+}
+
+TEST_F(FakeMessageLoopTest, PostDelayedTaskRunsInOrder) {
+  vector<int> order;
+  loop_->PostDelayedTask(Bind([&order]() { order.push_back(1); }),
+                         TimeDelta::FromSeconds(1));
+  loop_->PostDelayedTask(Bind([&order]() { order.push_back(4); }),
+                         TimeDelta::FromSeconds(4));
+  loop_->PostDelayedTask(Bind([&order]() { order.push_back(3); }),
+                         TimeDelta::FromSeconds(3));
+  loop_->PostDelayedTask(Bind([&order]() { order.push_back(2); }),
+                         TimeDelta::FromSeconds(2));
+  // Run until all the tasks are run.
+  loop_->Run();
+  EXPECT_EQ((vector<int>{1, 2, 3, 4}), order);
+}
+
+TEST_F(FakeMessageLoopTest, PostDelayedTaskAdvancesTheTime) {
+  Time start = Time::FromInternalValue(1000000);
+  clock_.SetNow(start);
+  loop_.reset(new FakeMessageLoop(&clock_));
+  loop_->PostDelayedTask(Bind(&base::DoNothing), TimeDelta::FromSeconds(1));
+  loop_->PostDelayedTask(Bind(&base::DoNothing), TimeDelta::FromSeconds(2));
+  EXPECT_FALSE(loop_->RunOnce(false));
+  // If the callback didn't run, the time shouldn't change.
+  EXPECT_EQ(start, clock_.Now());
+
+  // If we run only one callback, the time should be set to the time that
+  // callack ran.
+  EXPECT_TRUE(loop_->RunOnce(true));
+  EXPECT_EQ(start + TimeDelta::FromSeconds(1), clock_.Now());
+
+  // If the clock is advanced manually, we should be able to run the
+  // callback without blocking, since the firing time is in the past.
+  clock_.SetNow(start + TimeDelta::FromSeconds(3));
+  EXPECT_TRUE(loop_->RunOnce(false));
+  // The time should not change even if the callback is due in the past.
+  EXPECT_EQ(start + TimeDelta::FromSeconds(3), clock_.Now());
+}
+
+TEST_F(FakeMessageLoopTest, PendingTasksTest) {
+  loop_->PostDelayedTask(Bind(&base::DoNothing), TimeDelta::FromSeconds(1));
+  EXPECT_TRUE(loop_->PendingTasks());
+  loop_->Run();
+}
+
+}  // namespace chromeos