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 @@ ...@@ -13,18 +13,12 @@
namespace net { namespace net {
CookieCallback::CookieCallback(base::Thread* run_in_thread) CookieCallback::CookieCallback(base::Thread* run_in_thread)
: did_run_(false), : run_in_thread_(run_in_thread), run_in_loop_(NULL) {}
run_in_thread_(run_in_thread),
run_in_loop_(NULL),
parent_loop_(base::MessageLoop::current()),
loop_to_quit_(base::MessageLoop::current()) {}
CookieCallback::CookieCallback() CookieCallback::CookieCallback()
: did_run_(false), : run_in_thread_(NULL), run_in_loop_(base::MessageLoop::current()) {}
run_in_thread_(NULL),
run_in_loop_(base::MessageLoop::current()), CookieCallback::~CookieCallback() {}
parent_loop_(NULL),
loop_to_quit_(base::MessageLoop::current()) {}
void CookieCallback::CallbackEpilogue() { void CookieCallback::CallbackEpilogue() {
base::MessageLoop* expected_loop = NULL; base::MessageLoop* expected_loop = NULL;
...@@ -36,10 +30,12 @@ void CookieCallback::CallbackEpilogue() { ...@@ -36,10 +30,12 @@ void CookieCallback::CallbackEpilogue() {
} }
ASSERT_TRUE(expected_loop != NULL); ASSERT_TRUE(expected_loop != NULL);
did_run_ = true;
EXPECT_EQ(expected_loop, base::MessageLoop::current()); EXPECT_EQ(expected_loop, base::MessageLoop::current());
loop_to_quit_->task_runner()->PostTask( loop_to_quit_.Quit();
FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); }
void CookieCallback::WaitUntilDone() {
loop_to_quit_.Run();
} }
StringResultCookieCallback::StringResultCookieCallback() {} StringResultCookieCallback::StringResultCookieCallback() {}
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/run_loop.h"
#include "net/cookies/canonical_cookie.h" #include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_store.h" #include "net/cookies/cookie_store.h"
...@@ -23,28 +24,27 @@ namespace net { ...@@ -23,28 +24,27 @@ namespace net {
// quit to the thread in which it was constructed. // quit to the thread in which it was constructed.
class CookieCallback { class CookieCallback {
public: public:
// Indicates whether the callback has been called. // Waits until the callback is invoked.
bool did_run() { return did_run_; } void WaitUntilDone();
protected: protected:
// Constructs a callback that expects to be called in the given thread and // Constructs a callback that expects to be called in the given thread.
// will, upon execution, send a QUIT to the constructing thread.
explicit CookieCallback(base::Thread* run_in_thread); explicit CookieCallback(base::Thread* run_in_thread);
// Constructs a callback that expects to be called in current thread and will // Constructs a callback that expects to be called in current thread and will
// send a QUIT to the constructing thread. // send a QUIT to the constructing thread.
CookieCallback(); CookieCallback();
~CookieCallback();
// Tests whether the current thread was the caller's thread. // Tests whether the current thread was the caller's thread.
// Sends a QUIT to the constructing thread. // Sends a QUIT to the constructing thread.
void CallbackEpilogue(); void CallbackEpilogue();
private: private:
bool did_run_;
base::Thread* run_in_thread_; base::Thread* run_in_thread_;
base::MessageLoop* run_in_loop_; base::MessageLoop* run_in_loop_;
base::MessageLoop* parent_loop_; base::RunLoop loop_to_quit_;
base::MessageLoop* loop_to_quit_;
}; };
// Callback implementations for the asynchronous CookieStore methods. // Callback implementations for the asynchronous CookieStore methods.
......
...@@ -109,8 +109,7 @@ class CookieStoreTest : public testing::Test { ...@@ -109,8 +109,7 @@ class CookieStoreTest : public testing::Test {
url, options, url, options,
base::Bind(&StringResultCookieCallback::Run, base::Bind(&StringResultCookieCallback::Run,
base::Unretained(&callback))); base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
return callback.result(); return callback.result();
} }
...@@ -122,8 +121,7 @@ class CookieStoreTest : public testing::Test { ...@@ -122,8 +121,7 @@ class CookieStoreTest : public testing::Test {
cs->GetCookiesWithOptionsAsync( cs->GetCookiesWithOptionsAsync(
url, options, base::Bind(&StringResultCookieCallback::Run, url, options, base::Bind(&StringResultCookieCallback::Run,
base::Unretained(&callback))); base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
return callback.result(); return callback.result();
} }
...@@ -132,8 +130,7 @@ class CookieStoreTest : public testing::Test { ...@@ -132,8 +130,7 @@ class CookieStoreTest : public testing::Test {
GetCookieListCallback callback; GetCookieListCallback callback;
cs->GetAllCookiesAsync( cs->GetAllCookiesAsync(
base::Bind(&GetCookieListCallback::Run, base::Unretained(&callback))); base::Bind(&GetCookieListCallback::Run, base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
return callback.cookies(); return callback.cookies();
} }
...@@ -148,8 +145,7 @@ class CookieStoreTest : public testing::Test { ...@@ -148,8 +145,7 @@ class CookieStoreTest : public testing::Test {
base::Bind( base::Bind(
&ResultSavingCookieCallback<bool>::Run, &ResultSavingCookieCallback<bool>::Run,
base::Unretained(&callback))); base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
return callback.result(); return callback.result();
} }
...@@ -183,8 +179,7 @@ class CookieStoreTest : public testing::Test { ...@@ -183,8 +179,7 @@ class CookieStoreTest : public testing::Test {
cs->DeleteCookieAsync( cs->DeleteCookieAsync(
url, cookie_name, url, cookie_name,
base::Bind(&NoResultCookieCallback::Run, base::Unretained(&callback))); base::Bind(&NoResultCookieCallback::Run, base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
} }
int DeleteCreatedBetween(CookieStore* cs, int DeleteCreatedBetween(CookieStore* cs,
...@@ -197,8 +192,7 @@ class CookieStoreTest : public testing::Test { ...@@ -197,8 +192,7 @@ class CookieStoreTest : public testing::Test {
base::Bind( base::Bind(
&ResultSavingCookieCallback<int>::Run, &ResultSavingCookieCallback<int>::Run,
base::Unretained(&callback))); base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
return callback.result(); return callback.result();
} }
...@@ -213,8 +207,7 @@ class CookieStoreTest : public testing::Test { ...@@ -213,8 +207,7 @@ class CookieStoreTest : public testing::Test {
base::Bind( base::Bind(
&ResultSavingCookieCallback<int>::Run, &ResultSavingCookieCallback<int>::Run,
base::Unretained(&callback))); base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
return callback.result(); return callback.result();
} }
...@@ -225,21 +218,10 @@ class CookieStoreTest : public testing::Test { ...@@ -225,21 +218,10 @@ class CookieStoreTest : public testing::Test {
base::Bind( base::Bind(
&ResultSavingCookieCallback<int>::Run, &ResultSavingCookieCallback<int>::Run,
base::Unretained(&callback))); base::Unretained(&callback)));
RunFor(kTimeout); callback.WaitUntilDone();
EXPECT_TRUE(callback.did_run());
return callback.result(); 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() { scoped_refptr<CookieStore> GetCookieStore() {
return CookieStoreTestTraits::Create(); return CookieStoreTestTraits::Create();
} }
...@@ -1218,7 +1200,6 @@ class MultiThreadedCookieStoreTest : ...@@ -1218,7 +1200,6 @@ class MultiThreadedCookieStoreTest :
void RunOnOtherThread(const base::Closure& task) { void RunOnOtherThread(const base::Closure& task) {
other_thread_.Start(); other_thread_.Start();
other_thread_.task_runner()->PostTask(FROM_HERE, task); other_thread_.task_runner()->PostTask(FROM_HERE, task);
CookieStoreTest<CookieStoreTestTraits>::RunFor(kTimeout);
other_thread_.Stop(); other_thread_.Stop();
} }
...@@ -1240,7 +1221,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookies) { ...@@ -1240,7 +1221,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookies) {
&MultiThreadedCookieStoreTest<TypeParam>::GetCookiesTask, &MultiThreadedCookieStoreTest<TypeParam>::GetCookiesTask,
base::Unretained(this), cs, this->http_www_google_.url(), &callback); base::Unretained(this), cs, this->http_www_google_.url(), &callback);
this->RunOnOtherThread(task); this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run()); callback.WaitUntilDone();
EXPECT_EQ("A=B", callback.result()); EXPECT_EQ("A=B", callback.result());
} }
...@@ -1259,7 +1240,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookiesWithOptions) { ...@@ -1259,7 +1240,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckGetCookiesWithOptions) {
base::Unretained(this), cs, this->http_www_google_.url(), options, base::Unretained(this), cs, this->http_www_google_.url(), options,
&callback); &callback);
this->RunOnOtherThread(task); this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run()); callback.WaitUntilDone();
EXPECT_EQ("A=B", callback.result()); EXPECT_EQ("A=B", callback.result());
} }
...@@ -1276,7 +1257,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckSetCookieWithOptions) { ...@@ -1276,7 +1257,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckSetCookieWithOptions) {
base::Unretained(this), cs, this->http_www_google_.url(), "A=B", options, base::Unretained(this), cs, this->http_www_google_.url(), "A=B", options,
&callback); &callback);
this->RunOnOtherThread(task); this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run()); callback.WaitUntilDone();
EXPECT_TRUE(callback.result()); EXPECT_TRUE(callback.result());
} }
...@@ -1295,7 +1276,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteCookie) { ...@@ -1295,7 +1276,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteCookie) {
&MultiThreadedCookieStoreTest<TypeParam>::DeleteCookieTask, &MultiThreadedCookieStoreTest<TypeParam>::DeleteCookieTask,
base::Unretained(this), cs, this->http_www_google_.url(), "A", &callback); base::Unretained(this), cs, this->http_www_google_.url(), "A", &callback);
this->RunOnOtherThread(task); this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run()); callback.WaitUntilDone();
} }
TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) { TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) {
...@@ -1317,7 +1298,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) { ...@@ -1317,7 +1298,7 @@ TYPED_TEST_P(MultiThreadedCookieStoreTest, ThreadCheckDeleteSessionCookies) {
&MultiThreadedCookieStoreTest<TypeParam>::DeleteSessionCookiesTask, &MultiThreadedCookieStoreTest<TypeParam>::DeleteSessionCookiesTask,
base::Unretained(this), cs, &callback); base::Unretained(this), cs, &callback);
this->RunOnOtherThread(task); this->RunOnOtherThread(task);
EXPECT_TRUE(callback.did_run()); callback.WaitUntilDone();
EXPECT_EQ(1, callback.result()); 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