Add a mojo::test::ScopedTestDir and convert tests to use it.

This centralizes our dependency on various base stuff (and it turns out
that our tests only need various base file things for very trivial
purposes).

R=vardhan@google.com

Review URL: https://codereview.chromium.org/1354433003 .
diff --git a/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc b/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc
index 8857ecc..5787e53 100644
--- a/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc
+++ b/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc
@@ -21,16 +21,12 @@
 #include "mojo/edk/embedder/platform_handle.h"
 #include "mojo/edk/embedder/platform_handle_vector.h"
 #include "mojo/edk/embedder/scoped_platform_handle.h"
+#include "mojo/edk/test/scoped_test_dir.h"
 #include "mojo/edk/test/test_utils.h"
 #include "mojo/edk/util/scoped_file.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
-#if defined(OS_ANDROID)
-#include "base/android/path_utils.h"
-#include "base/files/file_path.h"
-#endif
-
 namespace mojo {
 namespace embedder {
 namespace {
@@ -42,22 +38,6 @@
   CHECK_EQ(poll(&pfds, 1, -1), 1);
 }
 
-FILE* NewTmpFile() {
-#if defined(OS_ANDROID)
-  base::FilePath tmpdir;
-  if (!base::android::GetCacheDirectory(&tmpdir))
-    return nullptr;
-  std::string templ = tmpdir.Append("XXXXXXXX").value();
-  int fd = mkstemp(const_cast<char*>(templ.c_str()));
-  if (fd == -1)
-    return nullptr;
-  CHECK(unlink(templ.c_str()) == 0);
-  return fdopen(fd, "w+");
-#else
-  return tmpfile();
-#endif
-}
-
 class PlatformChannelPairPosixTest : public testing::Test {
  public:
   PlatformChannelPairPosixTest() {}
@@ -147,6 +127,8 @@
 }
 
 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) {
+  mojo::test::ScopedTestDir test_dir;
+
   static const char kHello[] = "hello";
 
   PlatformChannelPair channel_pair;
@@ -166,7 +148,7 @@
     const char c = '0' + (i % 10);
     ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector);
     for (size_t j = 1; j <= i; j++) {
-      util::ScopedFILE fp(NewTmpFile());
+      util::ScopedFILE fp(test_dir.CreateFile());
       ASSERT_TRUE(fp);
       ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get()));
       platform_handles->push_back(
@@ -208,6 +190,8 @@
 }
 
 TEST_F(PlatformChannelPairPosixTest, AppendReceivedFDs) {
+  mojo::test::ScopedTestDir test_dir;
+
   static const char kHello[] = "hello";
 
   PlatformChannelPair channel_pair;
@@ -217,7 +201,7 @@
   const std::string file_contents("hello world");
 
   {
-    util::ScopedFILE fp(NewTmpFile());
+    util::ScopedFILE fp(test_dir.CreateFile());
     ASSERT_TRUE(fp);
     ASSERT_EQ(file_contents.size(),
               fwrite(file_contents.data(), 1, file_contents.size(), fp.get()));
diff --git a/mojo/edk/system/multiprocess_message_pipe_unittest.cc b/mojo/edk/system/multiprocess_message_pipe_unittest.cc
index 9a03bc9..02b6465 100644
--- a/mojo/edk/system/multiprocess_message_pipe_unittest.cc
+++ b/mojo/edk/system/multiprocess_message_pipe_unittest.cc
@@ -10,9 +10,6 @@
 #include <vector>
 
 #include "base/bind.h"
-#include "base/files/file_path.h"
-#include "base/files/file_util.h"
-#include "base/files/scoped_temp_dir.h"
 #include "base/location.h"
 #include "base/logging.h"
 #include "build/build_config.h"  // TODO(vtl): Remove this.
@@ -26,6 +23,7 @@
 #include "mojo/edk/system/raw_channel.h"
 #include "mojo/edk/system/shared_buffer_dispatcher.h"
 #include "mojo/edk/system/test_utils.h"
+#include "mojo/edk/test/scoped_test_dir.h"
 #include "mojo/edk/test/test_utils.h"
 #include "mojo/edk/util/scoped_file.h"
 #include "testing/gtest/include/gtest/gtest.h"
@@ -447,8 +445,7 @@
       public testing::WithParamInterface<size_t> {};
 
 TEST_P(MultiprocessMessagePipeTestWithPipeCount, PlatformHandlePassing) {
-  base::ScopedTempDir temp_dir;
-  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+  mojo::test::ScopedTestDir test_dir;
 
   helper()->StartChild("CheckPlatformHandleFile");
 
@@ -461,9 +458,7 @@
 
   size_t pipe_count = GetParam();
   for (size_t i = 0; i < pipe_count; ++i) {
-    base::FilePath unused;
-    util::ScopedFILE fp(
-        CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
+    util::ScopedFILE fp(test_dir.CreateFile());
     const std::string world("world");
     CHECK_EQ(fwrite(&world[0], 1, world.size(), fp.get()), world.size());
     fflush(fp.get());
diff --git a/mojo/edk/system/platform_handle_dispatcher_unittest.cc b/mojo/edk/system/platform_handle_dispatcher_unittest.cc
index f3426a2..ecc4d3e 100644
--- a/mojo/edk/system/platform_handle_dispatcher_unittest.cc
+++ b/mojo/edk/system/platform_handle_dispatcher_unittest.cc
@@ -6,10 +6,8 @@
 
 #include <stdio.h>
 
-#include "base/files/file_path.h"
-#include "base/files/file_util.h"
-#include "base/files/scoped_temp_dir.h"
 #include "base/memory/ref_counted.h"
+#include "mojo/edk/test/scoped_test_dir.h"
 #include "mojo/edk/test/test_utils.h"
 #include "mojo/edk/util/scoped_file.h"
 #include "testing/gtest/include/gtest/gtest.h"
@@ -19,14 +17,11 @@
 namespace {
 
 TEST(PlatformHandleDispatcherTest, Basic) {
-  base::ScopedTempDir temp_dir;
-  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+  mojo::test::ScopedTestDir test_dir;
 
   static const char kHelloWorld[] = "hello world";
 
-  base::FilePath unused;
-  util::ScopedFILE fp(
-      CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
+  util::ScopedFILE fp(test_dir.CreateFile());
   ASSERT_TRUE(fp);
   EXPECT_EQ(sizeof(kHelloWorld),
             fwrite(kHelloWorld, 1, sizeof(kHelloWorld), fp.get()));
@@ -62,14 +57,11 @@
 }
 
 TEST(PlatformHandleDispatcherTest, CreateEquivalentDispatcherAndClose) {
-  base::ScopedTempDir temp_dir;
-  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+  mojo::test::ScopedTestDir test_dir;
 
   static const char kFooBar[] = "foo bar";
 
-  base::FilePath unused;
-  util::ScopedFILE fp(
-      CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
+  util::ScopedFILE fp(test_dir.CreateFile());
   EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get()));
 
   scoped_refptr<PlatformHandleDispatcher> dispatcher =
diff --git a/mojo/edk/system/raw_channel_unittest.cc b/mojo/edk/system/raw_channel_unittest.cc
index 42ffb9f..084ab86 100644
--- a/mojo/edk/system/raw_channel_unittest.cc
+++ b/mojo/edk/system/raw_channel_unittest.cc
@@ -10,9 +10,6 @@
 #include <vector>
 
 #include "base/bind.h"
-#include "base/files/file_path.h"
-#include "base/files/file_util.h"
-#include "base/files/scoped_temp_dir.h"
 #include "base/location.h"
 #include "base/logging.h"
 #include "base/memory/scoped_ptr.h"
@@ -26,6 +23,7 @@
 #include "mojo/edk/system/mutex.h"
 #include "mojo/edk/system/test_utils.h"
 #include "mojo/edk/system/transport_data.h"
+#include "mojo/edk/test/scoped_test_dir.h"
 #include "mojo/edk/test/test_io_thread.h"
 #include "mojo/edk/test/test_utils.h"
 #include "mojo/edk/util/scoped_file.h"
@@ -794,8 +792,7 @@
 };
 
 TEST_F(RawChannelTest, ReadWritePlatformHandles) {
-  base::ScopedTempDir temp_dir;
-  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+  mojo::test::ScopedTestDir test_dir;
 
   WriteOnlyRawChannelDelegate write_delegate;
   scoped_ptr<RawChannel> rc_write(RawChannel::Create(handles[0].Pass()));
@@ -809,12 +806,9 @@
                                base::Bind(&InitOnIOThread, rc_read.get(),
                                           base::Unretained(&read_delegate)));
 
-  base::FilePath unused;
-  util::ScopedFILE fp1(
-      base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
+  util::ScopedFILE fp1(test_dir.CreateFile());
   EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get()));
-  util::ScopedFILE fp2(
-      base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
+  util::ScopedFILE fp2(test_dir.CreateFile());
   EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get()));
 
   {
diff --git a/mojo/edk/system/remote_message_pipe_unittest.cc b/mojo/edk/system/remote_message_pipe_unittest.cc
index 146848f..26c5bf8 100644
--- a/mojo/edk/system/remote_message_pipe_unittest.cc
+++ b/mojo/edk/system/remote_message_pipe_unittest.cc
@@ -9,9 +9,6 @@
 #include <vector>
 
 #include "base/bind.h"
-#include "base/files/file_path.h"
-#include "base/files/file_util.h"
-#include "base/files/scoped_temp_dir.h"
 #include "base/location.h"
 #include "base/logging.h"
 #include "base/message_loop/message_loop.h"
@@ -30,6 +27,7 @@
 #include "mojo/edk/system/shared_buffer_dispatcher.h"
 #include "mojo/edk/system/test_utils.h"
 #include "mojo/edk/system/waiter.h"
+#include "mojo/edk/test/scoped_test_dir.h"
 #include "mojo/edk/test/test_io_thread.h"
 #include "mojo/edk/test/test_utils.h"
 #include "mojo/edk/util/scoped_file.h"
@@ -1011,8 +1009,7 @@
 }
 
 TEST_F(RemoteMessagePipeTest, PlatformHandlePassing) {
-  base::ScopedTempDir temp_dir;
-  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+  mojo::test::ScopedTestDir test_dir;
 
   static const char kHello[] = "hello";
   static const char kWorld[] = "world";
@@ -1026,9 +1023,7 @@
   scoped_refptr<MessagePipe> mp1(MessagePipe::CreateProxyLocal(&ep1));
   BootstrapChannelEndpoints(ep0, ep1);
 
-  base::FilePath unused;
-  util::ScopedFILE fp(
-      base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused));
+  util::ScopedFILE fp(test_dir.CreateFile());
   EXPECT_EQ(sizeof(kHello), fwrite(kHello, 1, sizeof(kHello), fp.get()));
   // We'll try to pass this dispatcher, which will cause a |PlatformHandle| to
   // be passed.
diff --git a/mojo/edk/test/BUILD.gn b/mojo/edk/test/BUILD.gn
index b91cc5f..cbe6e26 100644
--- a/mojo/edk/test/BUILD.gn
+++ b/mojo/edk/test/BUILD.gn
@@ -13,6 +13,8 @@
     "multiprocess_test_helper.h",
     "scoped_ipc_support.cc",
     "scoped_ipc_support.h",
+    "scoped_test_dir.cc",
+    "scoped_test_dir.h",
     "test_io_thread.cc",
     "test_io_thread.h",
     "test_utils.h",
diff --git a/mojo/edk/test/scoped_test_dir.cc b/mojo/edk/test/scoped_test_dir.cc
new file mode 100644
index 0000000..5bd768b
--- /dev/null
+++ b/mojo/edk/test/scoped_test_dir.cc
@@ -0,0 +1,27 @@
+// Copyright 2015 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/edk/test/scoped_test_dir.h"
+
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/logging.h"
+
+namespace mojo {
+namespace test {
+
+ScopedTestDir::ScopedTestDir() {
+  CHECK(temp_dir_.CreateUniqueTempDir());
+}
+
+ScopedTestDir::~ScopedTestDir() {}
+
+util::ScopedFILE ScopedTestDir::CreateFile() {
+  base::FilePath unused;
+  return util::ScopedFILE(
+      base::CreateAndOpenTemporaryFileInDir(temp_dir_.path(), &unused));
+}
+
+}  // namespace test
+}  // namespace mojo
diff --git a/mojo/edk/test/scoped_test_dir.h b/mojo/edk/test/scoped_test_dir.h
new file mode 100644
index 0000000..4db372f
--- /dev/null
+++ b/mojo/edk/test/scoped_test_dir.h
@@ -0,0 +1,33 @@
+// Copyright 2015 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_EDK_TEST_SCOPED_TEST_DIR_H_
+#define MOJO_EDK_TEST_SCOPED_TEST_DIR_H_
+
+#include "base/files/scoped_temp_dir.h"
+#include "mojo/edk/util/scoped_file.h"
+#include "mojo/public/cpp/system/macros.h"
+
+namespace mojo {
+namespace test {
+
+// Creates/destroyes a temporary directory for test purposes. (Unlike
+// |base::ScopedTempDir|, this automatically creates the temporary directory.)
+class ScopedTestDir {
+ public:
+  ScopedTestDir();
+  ~ScopedTestDir();
+
+  util::ScopedFILE CreateFile();
+
+ private:
+  base::ScopedTempDir temp_dir_;
+
+  MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedTestDir);
+};
+
+}  // namespace test
+}  // namespace mojo
+
+#endif  // MOJO_EDK_TEST_SCOPED_TEST_DIR_H_