Commit b2bec813 authored by xunjieli's avatar xunjieli Committed by Commit bot

Convert CookieMonster tests to use base::RunLoop

CookieMonster tests post a
base::MessageLoop::QuitWhenIdleClosure task which
could be run at the start of some other test, since
the unit tests are invoked in the same fixture on
Android. This CL converts these CookieMonster tests
to use base::RunLoop so they don't influence other
tests.

BUG=568282

Review URL: https://codereview.chromium.org/1634803004

Cr-Commit-Position: refs/heads/master@{#371908}
parent 72d9ed4e
This diff is collapsed.
......@@ -13,18 +13,12 @@
namespace net {
CookieCallback::CookieCallback(base::Thread* run_in_thread)
: did_run_(false),
run_in_thread_(run_in_thread),
run_in_loop_(NULL),
parent_loop_(base::MessageLoop::current()),
loop_to_quit_(base::MessageLoop::current()) {}
: run_in_thread_(run_in_thread), run_in_loop_(NULL) {}
CookieCallback::CookieCallback()
: did_run_(false),
run_in_thread_(NULL),
run_in_loop_(base::MessageLoop::current()),
parent_loop_(NULL),
loop_to_quit_(base::MessageLoop::current()) {}
: run_in_thread_(NULL), run_in_loop_(base::MessageLoop::current()) {}
CookieCallback::~CookieCallback() {}
void CookieCallback::CallbackEpilogue() {
base::MessageLoop* expected_loop = NULL;
......@@ -36,10 +30,12 @@ void CookieCallback::CallbackEpilogue() {
}
ASSERT_TRUE(expected_loop != NULL);
did_run_ = true;
EXPECT_EQ(expected_loop, base::MessageLoop::current());
loop_to_quit_->task_runner()->PostTask(
FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
loop_to_quit_.Quit();
}
void CookieCallback::WaitUntilDone() {
loop_to_quit_.Run();
}
StringResultCookieCallback::StringResultCookieCallback() {}
......
......@@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include "base/run_loop.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_store.h"
......@@ -23,28 +24,27 @@ namespace net {
// quit to the thread in which it was constructed.
class CookieCallback {
public:
// Indicates whether the callback has been called.
bool did_run() { return did_run_; }
// Waits until the callback is invoked.
void WaitUntilDone();
protected:
// Constructs a callback that expects to be called in the given thread and
// will, upon execution, send a QUIT to the constructing thread.
// Constructs a callback that expects to be called in the given thread.
explicit CookieCallback(base::Thread* run_in_thread);
// Constructs a callback that expects to be called in current thread and will
// send a QUIT to the constructing thread.
CookieCallback();
~CookieCallback();
// Tests whether the current thread was the caller's thread.
// Sends a QUIT to the constructing thread.
void CallbackEpilogue();
private:
bool did_run_;
base::Thread* run_in_thread_;
base::MessageLoop* run_in_loop_;
base::MessageLoop* parent_loop_;
base::MessageLoop* loop_to_quit_;
base::RunLoop loop_to_quit_;
};
// Callback implementations for the asynchronous CookieStore methods.
......
......@@ -109,8 +109,7 @@ class CookieStoreTest : public testing::Test {
url, options,
base::Bind(&StringResultCookieCallback::Run,
base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
return callback.result();
}
......@@ -122,8 +121,7 @@ class CookieStoreTest : public testing::Test {
cs->GetCookiesWithOptionsAsync(
url, options, base::Bind(&StringResultCookieCallback::Run,
base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
return callback.result();
}
......@@ -132,8 +130,7 @@ class CookieStoreTest : public testing::Test {
GetCookieListCallback callback;
cs->GetAllCookiesAsync(
base::Bind(&GetCookieListCallback::Run, base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
return callback.cookies();
}
......@@ -148,8 +145,7 @@ class CookieStoreTest : public testing::Test {
base::Bind(
&ResultSavingCookieCallback<bool>::Run,
base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
return callback.result();
}
......@@ -183,8 +179,7 @@ class CookieStoreTest : public testing::Test {
cs->DeleteCookieAsync(
url, cookie_name,
base::Bind(&NoResultCookieCallback::Run, base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
}
int DeleteCreatedBetween(CookieStore* cs,
......@@ -197,8 +192,7 @@ class CookieStoreTest : public testing::Test {
base::Bind(
&ResultSavingCookieCallback<int>::Run,
base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
return callback.result();
}
......@@ -213,8 +207,7 @@ class CookieStoreTest : public testing::Test {
base::Bind(
&ResultSavingCookieCallback<int>::Run,
base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
return callback.result();
}
......@@ -225,21 +218,10 @@ class CookieStoreTest : public testing::Test {
base::Bind(
&ResultSavingCookieCallback<int>::Run,
base::Unretained(&callback)));
RunFor(kTimeout);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
return callback.result();
}
void RunFor(int ms) {
// Runs the test thread message loop for up to |ms| milliseconds.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::Bind(&base::MessageLoop::QuitWhenIdle,
weak_factory_->GetWeakPtr()),
base::TimeDelta::FromMilliseconds(ms));
base::MessageLoop::current()->Run();
weak_factory_->InvalidateWeakPtrs();
}
scoped_refptr<CookieStore> GetCookieStore() {
return CookieStoreTestTraits::Create();
}
......@@ -1218,7 +1200,6 @@ class MultiThreadedCookieStoreTest :
void RunOnOtherThread(const base::Closure& task) {
other_thread_.Start();
other_thread_.task_runner()->PostTask(FROM_HERE, task);
CookieStoreTest<CookieStoreTestTraits>::RunFor(kTimeout);
other_thread_.Stop();
}
......@@ -1240,7 +1221,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookies) {
&MultiThreadedCookieStoreTest<TypeParam>::GetCookiesTask,
base::Unretained(this), cs, this->http_www_google_.url(), &callback);
this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
EXPECT_EQ("A=B", callback.result());
}
......@@ -1259,7 +1240,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookiesWithOptions) {
base::Unretained(this), cs, this->http_www_google_.url(), options,
&callback);
this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
EXPECT_EQ("A=B", callback.result());
}
......@@ -1276,7 +1257,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckSetCookieWithOptions) {
base::Unretained(this), cs, this->http_www_google_.url(), "A=B", options,
&callback);
this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
EXPECT_TRUE(callback.result());
}
......@@ -1295,7 +1276,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteCookie) {
&MultiThreadedCookieStoreTest<TypeParam>::DeleteCookieTask,
base::Unretained(this), cs, this->http_www_google_.url(), "A", &callback);
this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
}
TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) {
......@@ -1317,7 +1298,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) {
&MultiThreadedCookieStoreTest<TypeParam>::DeleteSessionCookiesTask,
base::Unretained(this), cs, &callback);
this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run());
callback.WaitUntilDone();
EXPECT_EQ(1, callback.result());
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment