2013-01-03 18:24:24 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
#include "include/base/cef_bind.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
#include "include/cef_task.h"
|
2016-03-16 03:55:59 +01:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void GetForCurrentThread(bool* ran_test) {
|
2016-11-15 22:18:41 +01:00
|
|
|
// Currently on the FILE thread.
|
2013-01-03 18:24:24 +01:00
|
|
|
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForCurrentThread();
|
|
|
|
EXPECT_TRUE(runner.get());
|
|
|
|
EXPECT_TRUE(runner->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(runner->BelongsToThread(TID_FILE));
|
|
|
|
EXPECT_FALSE(runner->BelongsToThread(TID_DB));
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner->IsSame(runner));
|
|
|
|
|
|
|
|
CefRefPtr<CefTaskRunner> runner2 = CefTaskRunner::GetForCurrentThread();
|
|
|
|
EXPECT_TRUE(runner2.get());
|
|
|
|
EXPECT_TRUE(runner->IsSame(runner2));
|
|
|
|
EXPECT_TRUE(runner2->IsSame(runner));
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
// Not on the DB thread.
|
|
|
|
CefRefPtr<CefTaskRunner> runner3 = CefTaskRunner::GetForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner3.get());
|
|
|
|
EXPECT_FALSE(runner->IsSame(runner3));
|
|
|
|
EXPECT_FALSE(runner3->IsSame(runner));
|
|
|
|
|
|
|
|
*ran_test = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetForThread(bool* ran_test) {
|
2016-11-15 22:18:41 +01:00
|
|
|
// Currently on the FILE thread.
|
|
|
|
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner.get());
|
|
|
|
EXPECT_FALSE(runner->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(runner->BelongsToThread(TID_DB));
|
|
|
|
EXPECT_FALSE(runner->BelongsToThread(TID_FILE));
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner->IsSame(runner));
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefTaskRunner> runner2 = CefTaskRunner::GetForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner2.get());
|
|
|
|
EXPECT_TRUE(runner->IsSame(runner2));
|
|
|
|
EXPECT_TRUE(runner2->IsSame(runner));
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefTaskRunner> runner3 = CefTaskRunner::GetForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner3.get());
|
|
|
|
EXPECT_FALSE(runner->IsSame(runner3));
|
|
|
|
EXPECT_FALSE(runner3->IsSame(runner));
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
*ran_test = true;
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PostTaskEvent1(bool* got_it, CefRefPtr<CefTaskRunner> runner) {
|
2016-11-15 22:18:41 +01:00
|
|
|
// Currently on the DB thread.
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(runner->BelongsToThread(TID_DB));
|
|
|
|
EXPECT_FALSE(runner->BelongsToThread(TID_FILE));
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
// Current thread should be the DB thread.
|
2013-01-03 18:24:24 +01:00
|
|
|
CefRefPtr<CefTaskRunner> runner2 = CefTaskRunner::GetForCurrentThread();
|
|
|
|
EXPECT_TRUE(runner2.get());
|
|
|
|
EXPECT_TRUE(runner2->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(runner2->BelongsToThread(TID_DB));
|
|
|
|
EXPECT_FALSE(runner2->BelongsToThread(TID_FILE));
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner->IsSame(runner2));
|
|
|
|
EXPECT_TRUE(runner2->IsSame(runner));
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
// Current thread should be the DB thread.
|
|
|
|
CefRefPtr<CefTaskRunner> runner3 = CefTaskRunner::GetForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner3.get());
|
|
|
|
EXPECT_TRUE(runner3->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(runner3->BelongsToThread(TID_DB));
|
|
|
|
EXPECT_FALSE(runner3->BelongsToThread(TID_FILE));
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner->IsSame(runner3));
|
|
|
|
EXPECT_TRUE(runner3->IsSame(runner));
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
// Current thread should not be the FILE thread.
|
|
|
|
CefRefPtr<CefTaskRunner> runner4 = CefTaskRunner::GetForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner4.get());
|
|
|
|
EXPECT_FALSE(runner4->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_FALSE(runner4->BelongsToThread(TID_DB));
|
|
|
|
EXPECT_TRUE(runner4->BelongsToThread(TID_FILE));
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_FALSE(runner->IsSame(runner4));
|
|
|
|
EXPECT_FALSE(runner4->IsSame(runner));
|
|
|
|
|
|
|
|
*got_it = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostTask1(bool* ran_test) {
|
2016-11-15 22:18:41 +01:00
|
|
|
// Currently on the FILE thread.
|
|
|
|
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner.get());
|
|
|
|
EXPECT_FALSE(runner->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(runner->BelongsToThread(TID_DB));
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
bool got_it = false;
|
2016-03-16 03:55:59 +01:00
|
|
|
runner->PostTask(
|
|
|
|
CefCreateClosureTask(base::Bind(&PostTaskEvent1, &got_it, runner)));
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
WaitForThread(runner);
|
|
|
|
EXPECT_TRUE(got_it);
|
|
|
|
|
|
|
|
*ran_test = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostDelayedTask1(bool* ran_test) {
|
2016-11-15 22:18:41 +01:00
|
|
|
// Currently on the FILE thread.
|
|
|
|
CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(runner.get());
|
|
|
|
EXPECT_FALSE(runner->BelongsToCurrentThread());
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(runner->BelongsToThread(TID_DB));
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
bool got_it = false;
|
|
|
|
runner->PostDelayedTask(
|
2016-03-16 03:55:59 +01:00
|
|
|
CefCreateClosureTask(base::Bind(&PostTaskEvent1, &got_it, runner)), 0);
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
WaitForThread(runner);
|
|
|
|
EXPECT_TRUE(got_it);
|
|
|
|
|
|
|
|
*ran_test = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostTaskEvent2(bool* got_it) {
|
2016-11-15 22:18:41 +01:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_DB));
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_FILE));
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
*got_it = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostTask2(bool* ran_test) {
|
2016-11-15 22:18:41 +01:00
|
|
|
// Currently on the FILE thread.
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_DB));
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
bool got_it = false;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefPostTask(TID_DB,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefCreateClosureTask(base::Bind(&PostTaskEvent2, &got_it)));
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(got_it);
|
|
|
|
|
|
|
|
*ran_test = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostDelayedTask2(bool* ran_test) {
|
2016-11-15 22:18:41 +01:00
|
|
|
// Currently on the FILE thread.
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_DB));
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
bool got_it = false;
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostDelayedTask(
|
|
|
|
TID_DB, CefCreateClosureTask(base::Bind(&PostTaskEvent2, &got_it)), 0);
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_DB);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(got_it);
|
|
|
|
|
|
|
|
*ran_test = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(TaskTest, GetForCurrentThread) {
|
|
|
|
bool ran_test = false;
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostTask(TID_FILE, CefCreateClosureTask(
|
|
|
|
base::Bind(&GetForCurrentThread, &ran_test)));
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(ran_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TaskTest, GetForThread) {
|
|
|
|
bool ran_test = false;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefPostTask(TID_FILE,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefCreateClosureTask(base::Bind(&GetForThread, &ran_test)));
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(ran_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TaskTest, PostTask1) {
|
|
|
|
bool ran_test = false;
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
CefCreateClosureTask(base::Bind(&PostTask1, &ran_test)));
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(ran_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TaskTest, PostDelayedTask1) {
|
|
|
|
bool ran_test = false;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefPostTask(TID_FILE,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefCreateClosureTask(base::Bind(&PostDelayedTask1, &ran_test)));
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(ran_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TaskTest, PostTask2) {
|
|
|
|
bool ran_test = false;
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
CefCreateClosureTask(base::Bind(&PostTask2, &ran_test)));
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(ran_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TaskTest, PostDelayedTask2) {
|
|
|
|
bool ran_test = false;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefPostTask(TID_FILE,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefCreateClosureTask(base::Bind(&PostDelayedTask2, &ran_test)));
|
2016-11-15 22:18:41 +01:00
|
|
|
WaitForThread(TID_FILE);
|
2013-01-03 18:24:24 +01:00
|
|
|
EXPECT_TRUE(ran_test);
|
|
|
|
}
|