OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
8 #include "chrome/browser/mac/closure_blocks_leopard_compat.h" | 8 #include "chrome/browser/mac/closure_blocks_leopard_compat.h" |
9 | 9 |
| 10 // Used in ClosureBlocksLeopardCompatTest.Global. Putting a block at global |
| 11 // scope results in a block structure whose isa field is set to |
| 12 // NSConcreteGlobalBlock, ensuring that symbol is referenced. This test needs |
| 13 // to be able to load on 10.5 where that symbol is not present at runtime, so |
| 14 // the real test here is that the symbol is weakly imported. If not, the |
| 15 // executable will fail to load. |
| 16 void (^global_block)(bool*) = ^(bool* ran_pointer) { *ran_pointer = true; }; |
| 17 |
10 namespace { | 18 namespace { |
11 | 19 |
12 // It should be possible to declare a block on OSes as early as Leopard | 20 // It should be possible to declare a block on OSes as early as Leopard |
13 // (10.5). This should be buildable with the 10.5 SDK and a 10.5 deployment | 21 // (10.5). This should be buildable with the 10.5 SDK and a 10.5 deployment |
14 // target, and runnable on 10.5. However, the block itself is not expected to | 22 // target, and runnable on 10.5. However, the block itself is not expected to |
15 // be runnable on 10.5. This test verfies that blocks are buildable on 10.5 | 23 // be runnable on 10.5. This test verfies that blocks are buildable on 10.5 |
16 // and runnable on Snow Leopard (10.6) and later. | 24 // and runnable on Snow Leopard (10.6) and later. |
17 TEST(ClosureBlocksLeopardCompatTest, Basic) { | 25 |
| 26 TEST(ClosureBlocksLeopardCompatTest, Stack) { |
18 bool ran = false; | 27 bool ran = false; |
19 | 28 |
| 29 // This should result in a block structure whose isa field is set to |
| 30 // NSConcreteStackBlock, ensuring that symbol is referenced. This test |
| 31 // needs to be able to load on 10.5 where that symbol is not present at |
| 32 // runtime, so the real test here checks that the symbol is weakly imported. |
| 33 // If not, the executable will fail to load. |
| 34 |
20 void (^test_block)(bool*) = ^(bool* ran_pointer) { *ran_pointer = true; }; | 35 void (^test_block)(bool*) = ^(bool* ran_pointer) { *ran_pointer = true; }; |
21 | 36 |
22 ASSERT_FALSE(ran); | 37 ASSERT_FALSE(ran); |
23 | 38 |
24 if (base::mac::IsOSSnowLeopardOrLater()) { | 39 if (base::mac::IsOSSnowLeopardOrLater()) { |
25 test_block(&ran); | 40 test_block(&ran); |
26 | 41 |
27 ASSERT_TRUE(ran); | 42 EXPECT_TRUE(ran); |
| 43 } |
| 44 } |
| 45 |
| 46 TEST(ClosureBlocksLeopardCompatTest, Global) { |
| 47 bool ran = false; |
| 48 |
| 49 ASSERT_FALSE(ran); |
| 50 |
| 51 if (base::mac::IsOSSnowLeopardOrLater()) { |
| 52 global_block(&ran); |
| 53 |
| 54 EXPECT_TRUE(ran); |
| 55 } |
| 56 } |
| 57 |
| 58 TEST(ClosureBlocksLeopardCompatTest, CopyRelease) { |
| 59 if (base::mac::IsOSSnowLeopardOrLater()) { |
| 60 // Protect the entire test in this conditional, because __block won't run |
| 61 // on 10.5. |
| 62 __block int value = 0; |
| 63 |
| 64 typedef int (^TestBlockType)(); |
| 65 TestBlockType local_block = ^(void) { return ++value; }; |
| 66 |
| 67 ASSERT_EQ(0, value); |
| 68 |
| 69 int rv = local_block(); |
| 70 EXPECT_EQ(1, rv); |
| 71 EXPECT_EQ(rv, value); |
| 72 |
| 73 // Using Block_copy, Block_release, and these other operations ensures |
| 74 // that _Block_copy, _Block_release, _Block_object_assign, and |
| 75 // _Block_object_dispose are referenced. This test needs to be able to |
| 76 // load on 10.5 where these symbols are not present at runtime, so the |
| 77 // real test here is that the symbol is weakly imported. If not, the |
| 78 // executable will fail to load. |
| 79 |
| 80 TestBlockType copied_block = Block_copy(local_block); |
| 81 |
| 82 rv = copied_block(); |
| 83 EXPECT_EQ(2, rv); |
| 84 EXPECT_EQ(rv, value); |
| 85 |
| 86 rv = copied_block(); |
| 87 EXPECT_EQ(3, rv); |
| 88 EXPECT_EQ(rv, value); |
| 89 |
| 90 Block_release(copied_block); |
| 91 |
| 92 rv = local_block(); |
| 93 EXPECT_EQ(4, rv); |
| 94 EXPECT_EQ(rv, value); |
28 } | 95 } |
29 } | 96 } |
30 | 97 |
31 } // namespace | 98 } // namespace |
OLD | NEW |