blob: 1c622c2793cb8d68a840aa973bc6f04087e8809c [file] [log] [blame]
[email protected]e7b3a612012-01-05 02:18:181// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]c31af70db22011-08-18 23:13:015#include "base/bind.h"
[email protected]c6944272012-01-06 22:12:286#include "base/bind_helpers.h"
skyostil95082a62015-06-05 19:53:077#include "base/location.h"
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/scoped_ptr.h"
[email protected]fb441962013-05-08 05:35:249#include "base/sequenced_task_runner_helpers.h"
skyostil95082a62015-06-05 19:53:0710#include "base/single_thread_task_runner.h"
[email protected]c38831a12011-10-28 12:44:4911#include "content/browser/browser_thread_impl.h"
[email protected]e97882f2012-06-04 02:23:1712#include "content/public/test/test_browser_thread.h"
initial.commit09911bf2008-07-26 23:55:2913#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1514#include "testing/platform_test.h"
initial.commit09911bf2008-07-26 23:55:2915
[email protected]c38831a12011-10-28 12:44:4916namespace content {
17
[email protected]092b04e2010-10-12 23:23:4418class BrowserThreadTest : public testing::Test {
[email protected]f6710622009-11-02 06:10:3019 public:
[email protected]00ed48f2010-10-22 22:19:2420 void Release() const {
[email protected]092b04e2010-10-12 23:23:4421 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ki.stfu800779242015-10-12 22:46:2622 loop_.task_runner()->PostTask(FROM_HERE,
23 base::MessageLoop::QuitWhenIdleClosure());
[email protected]f6710622009-11-02 06:10:3024 }
[email protected]64cd0d122008-10-17 21:16:1325
[email protected]f6710622009-11-02 06:10:3026 protected:
dchengfa85b152014-10-28 01:13:4227 void SetUp() override {
[email protected]c38831a12011-10-28 12:44:4928 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI));
29 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE));
[email protected]2cbac9e2010-04-29 03:31:3430 ui_thread_->Start();
[email protected]f6710622009-11-02 06:10:3031 file_thread_->Start();
[email protected]f6710622009-11-02 06:10:3032 }
initial.commit09911bf2008-07-26 23:55:2933
dchengfa85b152014-10-28 01:13:4234 void TearDown() override {
[email protected]2cbac9e2010-04-29 03:31:3435 ui_thread_->Stop();
[email protected]f6710622009-11-02 06:10:3036 file_thread_->Stop();
[email protected]f6710622009-11-02 06:10:3037 }
initial.commit09911bf2008-07-26 23:55:2938
[email protected]dd32b1272013-05-04 14:17:1139 static void BasicFunction(base::MessageLoop* message_loop) {
[email protected]092b04e2010-10-12 23:23:4440 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
ki.stfu800779242015-10-12 22:46:2641 message_loop->task_runner()->PostTask(
42 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]f6710622009-11-02 06:10:3043 }
initial.commit09911bf2008-07-26 23:55:2944
[email protected]2cbac9e2010-04-29 03:31:3445 class DeletedOnFile
[email protected]f6710622009-11-02 06:10:3046 : public base::RefCountedThreadSafe<
[email protected]092b04e2010-10-12 23:23:4447 DeletedOnFile, BrowserThread::DeleteOnFileThread> {
[email protected]f6710622009-11-02 06:10:3048 public:
[email protected]dd32b1272013-05-04 14:17:1149 explicit DeletedOnFile(base::MessageLoop* message_loop)
50 : message_loop_(message_loop) {}
initial.commit09911bf2008-07-26 23:55:2951
[email protected]fb90c942012-04-27 23:40:5052 private:
53 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>;
54 friend class base::DeleteHelper<DeletedOnFile>;
55
[email protected]2cbac9e2010-04-29 03:31:3456 ~DeletedOnFile() {
[email protected]092b04e2010-10-12 23:23:4457 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
ki.stfu800779242015-10-12 22:46:2658 message_loop_->task_runner()->PostTask(
59 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]f6710622009-11-02 06:10:3060 }
initial.commit09911bf2008-07-26 23:55:2961
[email protected]dd32b1272013-05-04 14:17:1162 base::MessageLoop* message_loop_;
[email protected]f6710622009-11-02 06:10:3063 };
initial.commit09911bf2008-07-26 23:55:2964
[email protected]f6710622009-11-02 06:10:3065 private:
[email protected]c38831a12011-10-28 12:44:4966 scoped_ptr<BrowserThreadImpl> ui_thread_;
67 scoped_ptr<BrowserThreadImpl> file_thread_;
[email protected]00ed48f2010-10-22 22:19:2468 // It's kind of ugly to make this mutable - solely so we can post the Quit
69 // Task from Release(). This should be fixed.
[email protected]dd32b1272013-05-04 14:17:1170 mutable base::MessageLoop loop_;
[email protected]f6710622009-11-02 06:10:3071};
initial.commit09911bf2008-07-26 23:55:2972
[email protected]092b04e2010-10-12 23:23:4473TEST_F(BrowserThreadTest, PostTask) {
74 BrowserThread::PostTask(
[email protected]dd32b1272013-05-04 14:17:1175 BrowserThread::FILE,
76 FROM_HERE,
77 base::Bind(&BasicFunction, base::MessageLoop::current()));
78 base::MessageLoop::current()->Run();
[email protected]f6710622009-11-02 06:10:3079}
initial.commit09911bf2008-07-26 23:55:2980
[email protected]092b04e2010-10-12 23:23:4481TEST_F(BrowserThreadTest, Release) {
82 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this);
[email protected]dd32b1272013-05-04 14:17:1183 base::MessageLoop::current()->Run();
[email protected]f6710622009-11-02 06:10:3084}
initial.commit09911bf2008-07-26 23:55:2985
[email protected]092b04e2010-10-12 23:23:4486TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) {
[email protected]f6710622009-11-02 06:10:3087 {
[email protected]2cbac9e2010-04-29 03:31:3488 scoped_refptr<DeletedOnFile> test(
[email protected]dd32b1272013-05-04 14:17:1189 new DeletedOnFile(base::MessageLoop::current()));
[email protected]f6710622009-11-02 06:10:3090 }
[email protected]dd32b1272013-05-04 14:17:1191 base::MessageLoop::current()->Run();
[email protected]f6710622009-11-02 06:10:3092}
93
skyostil95082a62015-06-05 19:53:0794TEST_F(BrowserThreadTest, PostTaskViaTaskRunner) {
95 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
[email protected]092b04e2010-10-12 23:23:4496 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
skyostil95082a62015-06-05 19:53:0797 task_runner->PostTask(
[email protected]dd32b1272013-05-04 14:17:1198 FROM_HERE, base::Bind(&BasicFunction, base::MessageLoop::current()));
99 base::MessageLoop::current()->Run();
[email protected]2cbac9e2010-04-29 03:31:34100}
101
skyostil95082a62015-06-05 19:53:07102TEST_F(BrowserThreadTest, ReleaseViaTaskRunner) {
103 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
[email protected]092b04e2010-10-12 23:23:44104 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
skyostil95082a62015-06-05 19:53:07105 task_runner->ReleaseSoon(FROM_HERE, this);
[email protected]dd32b1272013-05-04 14:17:11106 base::MessageLoop::current()->Run();
[email protected]2cbac9e2010-04-29 03:31:34107}
108
[email protected]c31af70db22011-08-18 23:13:01109TEST_F(BrowserThreadTest, PostTaskAndReply) {
110 // Most of the heavy testing for PostTaskAndReply() is done inside the
skyostil95082a62015-06-05 19:53:07111 // task runner test. This just makes sure we get piped through at all.
[email protected]c31af70db22011-08-18 23:13:01112 ASSERT_TRUE(BrowserThread::PostTaskAndReply(
ki.stfu800779242015-10-12 22:46:26113 BrowserThread::FILE, FROM_HERE, base::Bind(&base::DoNothing),
114 base::Bind(&base::MessageLoop::QuitWhenIdle,
[email protected]dd32b1272013-05-04 14:17:11115 base::Unretained(base::MessageLoop::current()->current()))));
116 base::MessageLoop::current()->Run();
[email protected]c31af70db22011-08-18 23:13:01117}
118
[email protected]2b9eb3872013-03-30 18:58:30119} // namespace content