| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/test/browser_tests/blimp_browser_test.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/base_switches.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "blimp/client/app/blimp_client_switches.h" |
| 16 #include "blimp/client/session/assignment_source.h" |
| 17 #include "blimp/engine/app/blimp_browser_main_parts.h" |
| 18 #include "blimp/engine/app/blimp_content_browser_client.h" |
| 19 #include "blimp/engine/app/blimp_engine_config.h" |
| 20 #include "blimp/engine/app/switches.h" |
| 21 #include "blimp/engine/app/test_content_main_delegate.h" |
| 22 #include "blimp/engine/session/blimp_engine_session.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/common/url_constants.h" |
| 26 |
| 27 namespace blimp { |
| 28 namespace { |
| 29 const char kTestDataFilePath[] = "blimp/engine/test/data"; |
| 30 const char kClientTokenFilePath[] = "blimp/test/data/test_client_token"; |
| 31 } // namespace |
| 32 |
| 33 BlimpBrowserTest::BlimpBrowserTest() { |
| 34 CreateTestServer(base::FilePath(FILE_PATH_LITERAL(kTestDataFilePath))); |
| 35 } |
| 36 |
| 37 BlimpBrowserTest::~BlimpBrowserTest() {} |
| 38 |
| 39 void BlimpBrowserTest::SetUp() { |
| 40 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 41 SetUpCommandLine(command_line); |
| 42 BrowserTestBase::SetUp(); |
| 43 } |
| 44 |
| 45 engine::BlimpEngineSession* BlimpBrowserTest::GetEngineSession() { |
| 46 return engine::TestContentMainDelegate::GetInstance() |
| 47 ->browser_client() |
| 48 ->blimp_browser_main_parts() |
| 49 ->GetBlimpEngineSession(); |
| 50 } |
| 51 |
| 52 void BlimpBrowserTest::OnGetEnginePort(uint16_t port) { |
| 53 DCHECK_GT(port, 0); |
| 54 |
| 55 // A test client is started after BlimpBrowserTest::SetUpOnMainThread(). |
| 56 engine_port_ = port; |
| 57 QuitRunLoop(); |
| 58 } |
| 59 |
| 60 client::Assignment BlimpBrowserTest::GetAssignment() { |
| 61 client::Assignment assignment; |
| 62 assignment.client_token = client::kDummyClientToken; |
| 63 assignment.engine_endpoint = |
| 64 net::IPEndPoint(net::IPAddress::IPv4Localhost(), engine_port_); |
| 65 assignment.transport_protocol = client::Assignment::TransportProtocol::TCP; |
| 66 return assignment; |
| 67 } |
| 68 |
| 69 void BlimpBrowserTest::SetUpOnMainThread() { |
| 70 GetEngineSession()->GetEnginePortForTesting( |
| 71 base::Bind(&BlimpBrowserTest::OnGetEnginePort, base::Unretained(this))); |
| 72 RunUntilQuit(); |
| 73 } |
| 74 |
| 75 void BlimpBrowserTest::TearDownOnMainThread() { |
| 76 content::BrowserThread::GetMessageLoopProxyForThread( |
| 77 content::BrowserThread::UI) |
| 78 ->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 79 } |
| 80 |
| 81 void BlimpBrowserTest::SetUpCommandLine(base::CommandLine* command_line) { |
| 82 // Engine switches. |
| 83 blimp::engine::SetCommandLineDefaults(command_line); |
| 84 |
| 85 // Use dynamically allocated port for browser test. |
| 86 command_line->AppendSwitchASCII(blimp::engine::kEnginePort, "0"); |
| 87 command_line->AppendSwitchASCII(blimp::engine::kClientTokenPath, |
| 88 kClientTokenFilePath); |
| 89 } |
| 90 |
| 91 void BlimpBrowserTest::RunTestOnMainThreadLoop() { |
| 92 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 93 |
| 94 // Pump startup related events. |
| 95 base::MessageLoop::current()->RunUntilIdle(); |
| 96 |
| 97 SetUpOnMainThread(); |
| 98 RunTestOnMainThread(); |
| 99 TearDownOnMainThread(); |
| 100 |
| 101 for (content::RenderProcessHost::iterator i( |
| 102 content::RenderProcessHost::AllHostsIterator()); |
| 103 !i.IsAtEnd(); i.Advance()) { |
| 104 i.GetCurrentValue()->FastShutdownIfPossible(); |
| 105 } |
| 106 } |
| 107 |
| 108 void BlimpBrowserTest::RunUntilQuit() { |
| 109 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( |
| 110 base::MessageLoop::current()); |
| 111 EXPECT_FALSE(run_loop_); |
| 112 run_loop_ = base::WrapUnique(new base::RunLoop()); |
| 113 run_loop_->Run(); |
| 114 run_loop_ = nullptr; |
| 115 } |
| 116 |
| 117 void BlimpBrowserTest::QuitRunLoop() { |
| 118 run_loop_->Quit(); |
| 119 } |
| 120 |
| 121 } // namespace blimp |
| OLD | NEW |