1// Copyright 2015 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 "chrome/test/base/in_process_browser_test.h"
6
7#include "chrome/browser/devtools/devtools_window.h"
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_commands.h"
11#include "chrome/browser/ui/browser_finder.h"
12#import "chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.h"
13#include "chrome/browser/ui/tabs/tab_strip_model.h"
14#include "content/public/test/test_navigation_observer.h"
15
16void InProcessBrowserTest::OpenDevToolsWindow(
17    content::WebContents* web_contents) {
18  // Opening a Devtools Window can cause AppKit to throw objects into the
19  // autorelease pool. Flush the pool when this function returns.
20  @autoreleasepool {
21    ASSERT_FALSE(content::DevToolsAgentHost::HasFor(web_contents));
22    DevToolsWindow::OpenDevToolsWindow(web_contents);
23    ASSERT_TRUE(content::DevToolsAgentHost::HasFor(web_contents));
24  }
25}
26
27Browser* InProcessBrowserTest::OpenURLOffTheRecord(Profile* profile,
28                                                   const GURL& url) {
29  // Opening an incognito window can cause AppKit to throw objects into the
30  // autorelease pool. Flush the pool when this function returns.
31  @autoreleasepool {
32    chrome::OpenURLOffTheRecord(profile, url);
33    Browser* browser =
34        chrome::FindTabbedBrowser(profile->GetPrimaryOTRProfile(), false);
35    content::TestNavigationObserver observer(
36        browser->tab_strip_model()->GetActiveWebContents());
37    observer.Wait();
38    return browser;
39  }
40}
41
42// Creates a browser with a single tab (about:blank), waits for the tab to
43// finish loading and shows the browser.
44Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) {
45  // Making a browser window can cause AppKit to throw objects into the
46  // autorelease pool. Flush the pool when this function returns.
47  @autoreleasepool {
48    Browser* browser = Browser::Create(Browser::CreateParams(profile, true));
49    AddBlankTabAndShow(browser);
50    return browser;
51  }
52}
53
54Browser* InProcessBrowserTest::CreateIncognitoBrowser(Profile* profile) {
55  // Making a browser window can cause AppKit to throw objects into the
56  // autorelease pool. Flush the pool when this function returns.
57  @autoreleasepool {
58    // Use active profile if default nullptr was passed.
59    if (!profile)
60      profile = browser()->profile();
61
62    // Create a new browser with using the incognito profile.
63    Browser* incognito = Browser::Create(
64        Browser::CreateParams(profile->GetPrimaryOTRProfile(), true));
65    AddBlankTabAndShow(incognito);
66    return incognito;
67  }
68}
69
70Browser* InProcessBrowserTest::CreateBrowserForPopup(Profile* profile) {
71  // Making a browser window can cause AppKit to throw objects into the
72  // autorelease pool. Flush the pool when this function returns.
73  @autoreleasepool {
74    Browser* browser = Browser::Create(
75        Browser::CreateParams(Browser::TYPE_POPUP, profile, true));
76    AddBlankTabAndShow(browser);
77    return browser;
78  }
79}
80
81Browser* InProcessBrowserTest::CreateBrowserForApp(const std::string& app_name,
82                                                   Profile* profile) {
83  // Making a browser window can cause AppKit to throw objects into the
84  // autorelease pool. Flush the pool when this function returns.
85  @autoreleasepool {
86    Browser* browser = Browser::Create(Browser::CreateParams::CreateForApp(
87        app_name, false /* trusted_source */, gfx::Rect(), profile, true));
88    AddBlankTabAndShow(browser);
89    return browser;
90  }
91}
92