This source file includes following definitions.
- Increment
- IncrementBy
- RefCountedParam
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "base/cancelable_callback.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class TestRefCounted : public RefCountedThreadSafe<TestRefCounted> {
private:
friend class RefCountedThreadSafe<TestRefCounted>;
~TestRefCounted() {};
};
void Increment(int* count) { (*count)++; }
void IncrementBy(int* count, int n) { (*count) += n; }
void RefCountedParam(const scoped_refptr<TestRefCounted>& ref_counted) {}
TEST(CancelableCallbackTest, Cancel) {
int count = 0;
CancelableClosure cancelable(
base::Bind(&Increment, base::Unretained(&count)));
base::Closure callback = cancelable.callback();
callback.Run();
EXPECT_EQ(1, count);
callback.Run();
EXPECT_EQ(2, count);
cancelable.Cancel();
callback.Run();
EXPECT_EQ(2, count);
}
TEST(CancelableCallbackTest, MultipleCancel) {
int count = 0;
CancelableClosure cancelable(
base::Bind(&Increment, base::Unretained(&count)));
base::Closure callback1 = cancelable.callback();
base::Closure callback2 = cancelable.callback();
cancelable.Cancel();
callback1.Run();
EXPECT_EQ(0, count);
callback2.Run();
EXPECT_EQ(0, count);
cancelable.Cancel();
base::Closure callback3 = cancelable.callback();
EXPECT_TRUE(callback3.is_null());
}
TEST(CancelableCallbackTest, CallbackCanceledOnDestruction) {
int count = 0;
base::Closure callback;
{
CancelableClosure cancelable(
base::Bind(&Increment, base::Unretained(&count)));
callback = cancelable.callback();
callback.Run();
EXPECT_EQ(1, count);
}
callback.Run();
EXPECT_EQ(1, count);
}
TEST(CancelableCallbackTest, CancelDropsCallback) {
scoped_refptr<TestRefCounted> ref_counted = new TestRefCounted;
EXPECT_TRUE(ref_counted->HasOneRef());
CancelableClosure cancelable(base::Bind(RefCountedParam, ref_counted));
EXPECT_FALSE(cancelable.IsCancelled());
EXPECT_TRUE(ref_counted.get());
EXPECT_FALSE(ref_counted->HasOneRef());
cancelable.Cancel();
EXPECT_TRUE(cancelable.IsCancelled());
EXPECT_TRUE(ref_counted.get());
EXPECT_TRUE(ref_counted->HasOneRef());
}
TEST(CancelableCallbackTest, Reset) {
int count = 0;
CancelableClosure cancelable(
base::Bind(&Increment, base::Unretained(&count)));
base::Closure callback = cancelable.callback();
callback.Run();
EXPECT_EQ(1, count);
callback.Run();
EXPECT_EQ(2, count);
cancelable.Reset(
base::Bind(&IncrementBy, base::Unretained(&count), 3));
EXPECT_FALSE(cancelable.IsCancelled());
ASSERT_FALSE(callback.is_null());
callback.Run();
EXPECT_EQ(2, count);
base::Closure callback2 = cancelable.callback();
ASSERT_FALSE(callback2.is_null());
callback2.Run();
EXPECT_EQ(5, count);
}
TEST(CancelableCallbackTest, IsNull) {
CancelableClosure cancelable;
EXPECT_TRUE(cancelable.IsCancelled());
int count = 0;
cancelable.Reset(base::Bind(&Increment,
base::Unretained(&count)));
EXPECT_FALSE(cancelable.IsCancelled());
cancelable.Cancel();
EXPECT_TRUE(cancelable.IsCancelled());
}
TEST(CancelableCallbackTest, PostTask) {
MessageLoop loop;
int count = 0;
CancelableClosure cancelable(base::Bind(&Increment,
base::Unretained(&count)));
MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback());
RunLoop().RunUntilIdle();
EXPECT_EQ(1, count);
MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback());
cancelable.Cancel();
RunLoop().RunUntilIdle();
EXPECT_EQ(1, count);
}
}
}