11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/test/chromecast_browser_test.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/command_line.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/logging.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/message_loop/message_loop.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/cast_browser_context.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/cast_browser_process.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/browser_thread.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/render_process_host.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/web_contents.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/test/browser_test_utils.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/test/test_navigation_observer.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace chromecast {
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace shell {
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciChromecastBrowserTest::ChromecastBrowserTest()
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : setup_called_(false) {
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciChromecastBrowserTest::~ChromecastBrowserTest() {
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CHECK(setup_called_) << "Overridden SetUp() did not call parent "
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       << "implementation, so test not run.";
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ChromecastBrowserTest::SetUp() {
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SetUpCommandLine(CommandLine::ForCurrentProcess());
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  setup_called_ = true;
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  BrowserTestBase::SetUp();
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ChromecastBrowserTest::RunTestOnMainThreadLoop() {
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Pump startup related events.
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::RunLoop().RunUntilIdle();
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SetUpOnMainThread();
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RunTestOnMainThread();
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  TearDownOnMainThread();
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  for (content::RenderProcessHost::iterator i(
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci           content::RenderProcessHost::AllHostsIterator());
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       !i.IsAtEnd(); i.Advance()) {
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    i.GetCurrentValue()->FastShutdownIfPossible();
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  web_contents_.reset();
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ChromecastBrowserTest::NavigateToURL(content::WebContents* window,
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                          const GURL& url) {
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content::WaitForLoadStop(window);
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content::TestNavigationObserver same_tab_observer(window, 1);
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content::NavigationController::LoadURLParams params(url);
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  params.transition_type = ui::PageTransitionFromInt(
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ui::PAGE_TRANSITION_TYPED |
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  window->GetController().LoadURLWithParams(params);
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  same_tab_observer.Wait();
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccicontent::WebContents* ChromecastBrowserTest::CreateBrowser() {
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content::WebContents::CreateParams create_params(
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      CastBrowserProcess::GetInstance()->browser_context(),
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      NULL);
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  create_params.routing_id = MSG_ROUTING_NONE;
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  create_params.initial_size = gfx::Size(1280, 720);
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  web_contents_.reset(content::WebContents::Create(create_params));
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return web_contents_.get();
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace shell
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace chromecast
80