OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "blimp/common/mandatory_callback.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/test/gtest_util.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace blimp { | |
15 namespace { | |
16 | |
17 class MandatoryCallbackTest : public testing::Test { | |
18 public: | |
19 class ResultCollector { | |
20 public: | |
21 MOCK_METHOD2(MarkInvokedTwoArgs, void(int, float)); | |
22 MOCK_METHOD1(MarkInvoked, void(int)); | |
23 }; | |
24 | |
25 MandatoryCallbackTest() {} | |
26 ~MandatoryCallbackTest() override {} | |
27 | |
28 protected: | |
29 testing::StrictMock<ResultCollector> result_; | |
30 | |
31 private: | |
32 DISALLOW_COPY_AND_ASSIGN(MandatoryCallbackTest); | |
33 }; | |
34 | |
35 #if !DCHECK_IS_ON() | |
36 | |
37 TEST_F(MandatoryCallbackTest, NoEffectForReleaseBuilds) { | |
38 auto mandatory_cb = CreateMandatoryCallback(base::Bind(&base::DoNothing)); | |
39 | |
40 // Test should not crash when |mandatory_cb| is leaked. | |
41 } | |
42 | |
43 #else | |
44 | |
45 TEST_F(MandatoryCallbackTest, PassesWhenDestroyedAfterRun) { | |
46 EXPECT_CALL(result_, MarkInvoked(3)); | |
47 base::Callback<void(int)> cb = | |
48 base::Bind(&MandatoryCallbackTest::ResultCollector::MarkInvoked, | |
49 base::Unretained(&result_)); | |
50 MandatoryCallback<void(int)> mandatory_cb = CreateMandatoryCallback(cb); | |
51 mandatory_cb.Run(3); | |
52 } | |
53 | |
54 TEST_F(MandatoryCallbackTest, FailsWhenDestroyedBeforeRun) { | |
55 base::Callback<void(int)> cb = | |
56 base::Bind(&MandatoryCallbackTest::ResultCollector::MarkInvoked, | |
57 base::Unretained(&result_)); | |
58 ASSERT_DCHECK_DEATH(auto mandatory_cb = CreateMandatoryCallback(cb)); | |
59 } | |
60 | |
61 TEST_F(MandatoryCallbackTest, FailsOnMultipleInvocations) { | |
62 EXPECT_CALL(result_, MarkInvoked(3)); | |
63 base::Callback<void(int)> cb = | |
64 base::Bind(&MandatoryCallbackTest::ResultCollector::MarkInvoked, | |
65 base::Unretained(&result_)); | |
66 auto mandatory_cb = CreateMandatoryCallback(cb); | |
67 mandatory_cb.Run(3); | |
68 EXPECT_DCHECK_DEATH(mandatory_cb.Run(3)); | |
69 } | |
70 | |
71 TEST_F(MandatoryCallbackTest, FailsOnMoveThenMultipleCall) { | |
72 EXPECT_CALL(result_, MarkInvoked(3)); | |
73 base::Callback<void(int)> cb = | |
74 base::Bind(&MandatoryCallbackTest::ResultCollector::MarkInvoked, | |
75 base::Unretained(&result_)); | |
76 auto mandatory_cb = CreateMandatoryCallback(cb); | |
77 auto moved = std::move(mandatory_cb); | |
78 moved.Run(3); | |
79 EXPECT_DCHECK_DEATH(mandatory_cb.Run(3)); | |
80 } | |
81 | |
82 // Allows death checks to copy-and-discard as a one-liner statement, | |
83 // which is necessary for isolating the moved variable inside the forked death | |
84 // check process. | |
85 template <typename CallbackType> | |
86 void MoveAndToss(CallbackType&& cb) { | |
87 auto copy = std::move(cb); | |
88 } | |
89 | |
90 TEST_F(MandatoryCallbackTest, FailsOnMoveThenDrop) { | |
91 base::Callback<void(int)> cb = | |
92 base::Bind(&MandatoryCallbackTest::ResultCollector::MarkInvoked, | |
93 base::Unretained(&result_)); | |
94 ASSERT_DCHECK_DEATH(MoveAndToss(CreateMandatoryCallback(cb))); | |
95 } | |
96 | |
97 #endif | |
98 | |
99 } // namespace | |
100 } // namespace blimp | |
OLD | NEW |