| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/safe_browsing/incident_reporting/delayed_callback_runne
r.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting/delayed_callback_runne
r.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 14 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // A class of objects that invoke a callback upon destruction. This is used as | 20 // A class of objects that invoke a callback upon destruction. This is used as |
| 20 // an owned argument on callbacks given to a DelayedCallbackRunner under test. | 21 // an owned argument on callbacks given to a DelayedCallbackRunner under test. |
| 21 class CallbackArgument { | 22 class CallbackArgument { |
| 22 public: | 23 public: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 callbacks_[name].run = true; | 62 callbacks_[name].run = true; |
| 62 } | 63 } |
| 63 | 64 |
| 64 void OnDelete(const std::string& name) { | 65 void OnDelete(const std::string& name) { |
| 65 EXPECT_FALSE(callbacks_[name].deleted); | 66 EXPECT_FALSE(callbacks_[name].deleted); |
| 66 callbacks_[name].deleted = true; | 67 callbacks_[name].deleted = true; |
| 67 } | 68 } |
| 68 | 69 |
| 69 // Returns a callback argument that calls the test fixture's OnDelete method | 70 // Returns a callback argument that calls the test fixture's OnDelete method |
| 70 // on behalf of the given callback name. | 71 // on behalf of the given callback name. |
| 71 scoped_ptr<CallbackArgument> MakeCallbackArgument(const std::string& name) { | 72 std::unique_ptr<CallbackArgument> MakeCallbackArgument( |
| 72 return make_scoped_ptr(new CallbackArgument(base::Bind( | 73 const std::string& name) { |
| 74 return base::WrapUnique(new CallbackArgument(base::Bind( |
| 73 &DelayedCallbackRunnerTest::OnDelete, base::Unretained(this), name))); | 75 &DelayedCallbackRunnerTest::OnDelete, base::Unretained(this), name))); |
| 74 } | 76 } |
| 75 | 77 |
| 76 // Returns a closure that calls |OnRun| when run and |OnDelete| when deleted | 78 // Returns a closure that calls |OnRun| when run and |OnDelete| when deleted |
| 77 // on behalf of the given callback name. | 79 // on behalf of the given callback name. |
| 78 base::Closure MakeCallback(const std::string& name) { | 80 base::Closure MakeCallback(const std::string& name) { |
| 79 return base::Bind(&DelayedCallbackRunnerTest::OnRun, | 81 return base::Bind(&DelayedCallbackRunnerTest::OnRun, |
| 80 base::Unretained(this), | 82 base::Unretained(this), |
| 81 name, | 83 name, |
| 82 base::Owned(MakeCallbackArgument(name).release())); | 84 base::Owned(MakeCallbackArgument(name).release())); |
| 83 } | 85 } |
| 84 | 86 |
| 85 bool CallbackWasRun(const std::string& name) { return callbacks_[name].run; } | 87 bool CallbackWasRun(const std::string& name) { return callbacks_[name].run; } |
| 86 | 88 |
| 87 bool CallbackWasDeleted(const std::string& name) { | 89 bool CallbackWasDeleted(const std::string& name) { |
| 88 return callbacks_[name].deleted; | 90 return callbacks_[name].deleted; |
| 89 } | 91 } |
| 90 | 92 |
| 91 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 93 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 92 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 94 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 93 scoped_ptr<safe_browsing::DelayedCallbackRunner> instance_; | 95 std::unique_ptr<safe_browsing::DelayedCallbackRunner> instance_; |
| 94 | 96 |
| 95 private: | 97 private: |
| 96 struct CallbackState { | 98 struct CallbackState { |
| 97 CallbackState() : run(), deleted() {} | 99 CallbackState() : run(), deleted() {} |
| 98 bool run; | 100 bool run; |
| 99 bool deleted; | 101 bool deleted; |
| 100 }; | 102 }; |
| 101 | 103 |
| 102 std::map<std::string, CallbackState> callbacks_; | 104 std::map<std::string, CallbackState> callbacks_; |
| 103 }; | 105 }; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 task_runner_->RunUntilIdle(); | 154 task_runner_->RunUntilIdle(); |
| 153 EXPECT_TRUE(CallbackWasRun(name)); | 155 EXPECT_TRUE(CallbackWasRun(name)); |
| 154 EXPECT_TRUE(CallbackWasDeleted(name)); | 156 EXPECT_TRUE(CallbackWasDeleted(name)); |
| 155 | 157 |
| 156 RegisterTestCallback(name2); | 158 RegisterTestCallback(name2); |
| 157 instance_->Start(); | 159 instance_->Start(); |
| 158 task_runner_->RunUntilIdle(); | 160 task_runner_->RunUntilIdle(); |
| 159 EXPECT_TRUE(CallbackWasRun(name2)); | 161 EXPECT_TRUE(CallbackWasRun(name2)); |
| 160 EXPECT_TRUE(CallbackWasDeleted(name2)); | 162 EXPECT_TRUE(CallbackWasDeleted(name2)); |
| 161 } | 163 } |
| OLD | NEW |