Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: blimp/engine/browser_tests/blimp_browser_test.cc

Issue 2256363003: Clean up thread handling in Blimp browser tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "blimp/engine/browser_tests/blimp_browser_test.h" 5 #include "blimp/engine/browser_tests/blimp_browser_test.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/base_switches.h" 9 #include "base/base_switches.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "blimp/client/core/blimp_client_switches.h" 16 #include "blimp/client/core/blimp_client_switches.h"
17 #include "blimp/client/core/session/assignment_source.h" 17 #include "blimp/client/core/session/assignment_source.h"
18 #include "blimp/common/switches.h" 18 #include "blimp/common/switches.h"
19 #include "blimp/engine/app/blimp_browser_main_parts.h" 19 #include "blimp/engine/app/blimp_browser_main_parts.h"
20 #include "blimp/engine/app/blimp_content_browser_client.h" 20 #include "blimp/engine/app/blimp_content_browser_client.h"
21 #include "blimp/engine/app/blimp_engine_config.h" 21 #include "blimp/engine/app/blimp_engine_config.h"
22 #include "blimp/engine/app/switches.h" 22 #include "blimp/engine/app/switches.h"
23 #include "blimp/engine/app/test_content_main_delegate.h" 23 #include "blimp/engine/app/test_content_main_delegate.h"
24 #include "blimp/engine/session/blimp_engine_session.h" 24 #include "blimp/engine/session/blimp_engine_session.h"
25 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/render_process_host.h" 26 #include "content/public/browser/render_process_host.h"
27 #include "content/public/common/url_constants.h" 27 #include "content/public/common/url_constants.h"
28 #include "content/public/test/test_utils.h"
28 29
29 namespace blimp { 30 namespace blimp {
30 namespace { 31 namespace {
31 const char kTestDataFilePath[] = "blimp/test/data"; 32 const char kTestDataFilePath[] = "blimp/test/data";
32 const char kClientTokenFilePath[] = "blimp/test/data/test_client_token"; 33 const char kClientTokenFilePath[] = "blimp/test/data/test_client_token";
33 const char kClientToken[] = "MyVoiceIsMyPassport"; 34 const char kClientToken[] = "MyVoiceIsMyPassport";
34 } // namespace 35 } // namespace
35 36
36 BlimpBrowserTest::BlimpBrowserTest() { 37
38 BlimpBrowserTest::BlimpBrowserTest()
39 : completion_event_(base::WaitableEvent::ResetPolicy::MANUAL,
40 base::WaitableEvent::InitialState::NOT_SIGNALED) {
37 CreateTestServer(base::FilePath(FILE_PATH_LITERAL(kTestDataFilePath))); 41 CreateTestServer(base::FilePath(FILE_PATH_LITERAL(kTestDataFilePath)));
38 } 42 }
39 43
40 BlimpBrowserTest::~BlimpBrowserTest() {} 44 BlimpBrowserTest::~BlimpBrowserTest() {}
41 45
46 void BlimpBrowserTest::RunUntilCompletion() {
47 while (!completion_event_.IsSignaled()) {
48 content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
49 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
50 }
51 completion_event_.Reset();
52 }
53
54 void BlimpBrowserTest::SignalCompletion() {
55 completion_event_.Signal();
56 }
57
42 void BlimpBrowserTest::SetUp() { 58 void BlimpBrowserTest::SetUp() {
43 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 59 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
44 SetUpCommandLine(command_line); 60 SetUpCommandLine(command_line);
45 BrowserTestBase::SetUp(); 61 BrowserTestBase::SetUp();
46 } 62 }
47 63
48 engine::BlimpEngineSession* BlimpBrowserTest::GetEngineSession() { 64 engine::BlimpEngineSession* BlimpBrowserTest::GetEngineSession() {
49 return engine::TestContentMainDelegate::GetInstance() 65 return engine::TestContentMainDelegate::GetInstance()
50 ->browser_client() 66 ->browser_client()
51 ->blimp_browser_main_parts() 67 ->blimp_browser_main_parts()
52 ->GetBlimpEngineSession(); 68 ->GetBlimpEngineSession();
53 } 69 }
54 70
55 void BlimpBrowserTest::OnGetEnginePort(uint16_t port) {
56 DCHECK_GT(port, 0);
57
58 // A test client is started after BlimpBrowserTest::SetUpOnMainThread().
59 engine_port_ = port;
60 QuitRunLoop();
61 }
62
63 client::Assignment BlimpBrowserTest::GetAssignment() { 71 client::Assignment BlimpBrowserTest::GetAssignment() {
64 client::Assignment assignment; 72 client::Assignment assignment;
65 assignment.client_token = kClientToken; 73 assignment.client_token = kClientToken;
66 assignment.engine_endpoint = 74 assignment.engine_endpoint =
67 net::IPEndPoint(net::IPAddress::IPv4Localhost(), engine_port_); 75 net::IPEndPoint(net::IPAddress::IPv4Localhost(), engine_port_);
68 assignment.transport_protocol = client::Assignment::TransportProtocol::TCP; 76 assignment.transport_protocol = client::Assignment::TransportProtocol::TCP;
69 return assignment; 77 return assignment;
70 } 78 }
71 79
72 void BlimpBrowserTest::SetUpOnMainThread() {
73 GetEngineSession()->GetEnginePortForTesting(
74 base::Bind(&BlimpBrowserTest::OnGetEnginePort, base::Unretained(this)));
75 RunUntilQuit();
76 }
77
78 void BlimpBrowserTest::TearDownOnMainThread() {
79 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI)
80 ->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
81 }
82
83 void BlimpBrowserTest::SetUpCommandLine(base::CommandLine* command_line) { 80 void BlimpBrowserTest::SetUpCommandLine(base::CommandLine* command_line) {
84 // Engine switches. 81 // Engine switches.
85 blimp::engine::SetCommandLineDefaults(command_line); 82 blimp::engine::SetCommandLineDefaults(command_line);
86 83
87 // Pass through the engine port if it is passed to the test. 84 // Pass through the engine port if it is passed to the test.
88 // Otherwise, use a dynamic port. 85 // Otherwise, use a dynamic port.
89 if (!command_line->HasSwitch(blimp::engine::kEnginePort)) { 86 if (!command_line->HasSwitch(blimp::engine::kEnginePort)) {
90 command_line->AppendSwitchASCII(blimp::engine::kEnginePort, "0"); 87 command_line->AppendSwitchASCII(blimp::engine::kEnginePort, "0");
91 } 88 }
92 89
93 base::FilePath src_root; 90 base::FilePath src_root;
94 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); 91 PathService::Get(base::DIR_SOURCE_ROOT, &src_root);
95 command_line->AppendSwitchASCII(kClientTokenPath, 92 command_line->AppendSwitchASCII(kClientTokenPath,
96 src_root.Append(kClientTokenFilePath).value()); 93 src_root.Append(kClientTokenFilePath).value());
97 } 94 }
98 95
96 void BlimpBrowserTest::SetUpOnMainThread() {
97 // Get the connection's port number across the IO/UI thread boundary.
98 GetEngineSession()->GetEnginePortForTesting(
99 base::Bind(&BlimpBrowserTest::set_engine_port, base::Unretained(this)));
100 content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
101 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
Wez 2016/08/19 19:15:50 There is an assumption here that GetEnginePortForT
Kevin M 2016/08/19 21:43:33 Done.
102 }
103
104 void BlimpBrowserTest::TearDownOnMainThread() {
105 base::MessageLoop::current()->QuitWhenIdle();
106 }
107
99 void BlimpBrowserTest::RunTestOnMainThreadLoop() { 108 void BlimpBrowserTest::RunTestOnMainThreadLoop() {
100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 109 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
101 110
102 // Pump startup related events.
103 base::RunLoop().RunUntilIdle();
104
105 SetUpOnMainThread(); 111 SetUpOnMainThread();
106 RunTestOnMainThread(); 112 RunTestOnMainThread();
107 TearDownOnMainThread(); 113 TearDownOnMainThread();
108 114
109 for (content::RenderProcessHost::iterator i( 115 for (content::RenderProcessHost::iterator i(
110 content::RenderProcessHost::AllHostsIterator()); 116 content::RenderProcessHost::AllHostsIterator());
111 !i.IsAtEnd(); i.Advance()) { 117 !i.IsAtEnd(); i.Advance()) {
112 i.GetCurrentValue()->FastShutdownIfPossible(); 118 i.GetCurrentValue()->FastShutdownIfPossible();
113 } 119 }
114 } 120 }
115 121
116 void BlimpBrowserTest::RunUntilQuit() {
117 base::MessageLoop::ScopedNestableTaskAllower nestable_allower(
118 base::MessageLoop::current());
119 EXPECT_FALSE(run_loop_);
120 run_loop_ = base::WrapUnique(new base::RunLoop());
121 run_loop_->Run();
122 run_loop_ = nullptr;
123 }
124
125 void BlimpBrowserTest::QuitRunLoop() {
126 run_loop_->Quit();
127 }
128
129 } // namespace blimp 122 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698