Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/task_runner_util.h"
      6 
      7 #include <utility>
      8 
      9 #include "base/bind.h"
     10 #include "base/location.h"
     11 #include "base/run_loop.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 namespace base {
     15 
     16 namespace {
     17 
     18 int ReturnFourtyTwo() {
     19   return 42;
     20 }
     21 
     22 void StoreValue(int* destination, int value) {
     23   *destination = value;
     24 }
     25 
     26 void StoreDoubleValue(double* destination, double value) {
     27   *destination = value;
     28 }
     29 
     30 int g_foo_destruct_count = 0;
     31 int g_foo_free_count = 0;
     32 
     33 struct Foo {
     34   ~Foo() {
     35     ++g_foo_destruct_count;
     36   }
     37 };
     38 
     39 std::unique_ptr<Foo> CreateFoo() {
     40   return std::unique_ptr<Foo>(new Foo);
     41 }
     42 
     43 void ExpectFoo(std::unique_ptr<Foo> foo) {
     44   EXPECT_TRUE(foo.get());
     45   std::unique_ptr<Foo> local_foo(std::move(foo));
     46   EXPECT_TRUE(local_foo.get());
     47   EXPECT_FALSE(foo.get());
     48 }
     49 
     50 struct FooDeleter {
     51   void operator()(Foo* foo) const {
     52     ++g_foo_free_count;
     53     delete foo;
     54   };
     55 };
     56 
     57 std::unique_ptr<Foo, FooDeleter> CreateScopedFoo() {
     58   return std::unique_ptr<Foo, FooDeleter>(new Foo);
     59 }
     60 
     61 void ExpectScopedFoo(std::unique_ptr<Foo, FooDeleter> foo) {
     62   EXPECT_TRUE(foo.get());
     63   std::unique_ptr<Foo, FooDeleter> local_foo(std::move(foo));
     64   EXPECT_TRUE(local_foo.get());
     65   EXPECT_FALSE(foo.get());
     66 }
     67 
     68 }  // namespace
     69 
     70 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResult) {
     71   int result = 0;
     72 
     73   MessageLoop message_loop;
     74   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
     75                              Bind(&ReturnFourtyTwo),
     76                              Bind(&StoreValue, &result));
     77 
     78   RunLoop().RunUntilIdle();
     79 
     80   EXPECT_EQ(42, result);
     81 }
     82 
     83 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultImplicitConvert) {
     84   double result = 0;
     85 
     86   MessageLoop message_loop;
     87   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
     88                              Bind(&ReturnFourtyTwo),
     89                              Bind(&StoreDoubleValue, &result));
     90 
     91   RunLoop().RunUntilIdle();
     92 
     93   EXPECT_DOUBLE_EQ(42.0, result);
     94 }
     95 
     96 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassed) {
     97   g_foo_destruct_count = 0;
     98   g_foo_free_count = 0;
     99 
    100   MessageLoop message_loop;
    101   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
    102                              Bind(&CreateFoo), Bind(&ExpectFoo));
    103 
    104   RunLoop().RunUntilIdle();
    105 
    106   EXPECT_EQ(1, g_foo_destruct_count);
    107   EXPECT_EQ(0, g_foo_free_count);
    108 }
    109 
    110 TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassedFreeProc) {
    111   g_foo_destruct_count = 0;
    112   g_foo_free_count = 0;
    113 
    114   MessageLoop message_loop;
    115   PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE,
    116                              Bind(&CreateScopedFoo), Bind(&ExpectScopedFoo));
    117 
    118   RunLoop().RunUntilIdle();
    119 
    120   EXPECT_EQ(1, g_foo_destruct_count);
    121   EXPECT_EQ(1, g_foo_free_count);
    122 }
    123 
    124 }  // namespace base
    125