Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / base / mac / bind_objc_block_unittest.mm
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 #import "base/mac/bind_objc_block.h"
6
7 #include <string>
8
9 #include "base/callback.h"
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 TEST(BindObjcBlockTest, TestScopedClosureRunnerExitScope) {
17   int run_count = 0;
18   int* ptr = &run_count;
19   {
20     base::ScopedClosureRunner runner(base::BindBlock(^{
21         (*ptr)++;
22     }));
23     EXPECT_EQ(0, run_count);
24   }
25   EXPECT_EQ(1, run_count);
26 }
27
28 TEST(BindObjcBlockTest, TestScopedClosureRunnerRelease) {
29   int run_count = 0;
30   int* ptr = &run_count;
31   base::Closure c;
32   {
33     base::ScopedClosureRunner runner(base::BindBlock(^{
34         (*ptr)++;
35     }));
36     c = runner.Release();
37     EXPECT_EQ(0, run_count);
38   }
39   EXPECT_EQ(0, run_count);
40   c.Run();
41   EXPECT_EQ(1, run_count);
42 }
43
44 TEST(BindObjcBlockTest, TestReturnValue) {
45   const int kReturnValue = 42;
46   base::Callback<int(void)> c = base::BindBlock(^{return kReturnValue;});
47   EXPECT_EQ(kReturnValue, c.Run());
48 }
49
50 TEST(BindObjcBlockTest, TestArgument) {
51   const int kArgument = 42;
52   base::Callback<int(int)> c = base::BindBlock(^(int a){return a + 1;});
53   EXPECT_EQ(kArgument + 1, c.Run(kArgument));
54 }
55
56 TEST(BindObjcBlockTest, TestTwoArguments) {
57   std::string result;
58   std::string* ptr = &result;
59   base::Callback<void(const std::string&, const std::string&)> c =
60       base::BindBlock(^(const std::string& a, const std::string& b) {
61           *ptr = a + b;
62       });
63   c.Run("forty", "two");
64   EXPECT_EQ(result, "fortytwo");
65 }
66
67 TEST(BindObjcBlockTest, TestThreeArguments) {
68   std::string result;
69   std::string* ptr = &result;
70   base::Callback<void(const std::string&,
71                       const std::string&,
72                       const std::string&)> c =
73       base::BindBlock(^(const std::string& a,
74                         const std::string& b,
75                         const std::string& c) {
76           *ptr = a + b + c;
77       });
78   c.Run("six", "times", "nine");
79   EXPECT_EQ(result, "sixtimesnine");
80 }
81
82 TEST(BindObjcBlockTest, TestSixArguments) {
83   std::string result1;
84   std::string* ptr = &result1;
85   int result2;
86   int* ptr2 = &result2;
87   base::Callback<void(int, int, const std::string&, const std::string&,
88                       int, const std::string&)> c =
89       base::BindBlock(^(int a, int b, const std::string& c,
90                         const std::string& d, int e, const std::string& f) {
91           *ptr = c + d + f;
92           *ptr2 = a + b + e;
93       });
94   c.Run(1, 2, "infinite", "improbability", 3, "drive");
95   EXPECT_EQ(result1, "infiniteimprobabilitydrive");
96   EXPECT_EQ(result2, 6);
97 }
98
99 }  // namespace