1 // Copyright 2019 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 "weblayer/test/weblayer_browser_test.h"
6 
7 #include "base/base_paths.h"
8 #include "content/public/browser/browser_context.h"
9 #include "weblayer/browser/browser_context_impl.h"
10 #include "weblayer/browser/profile_impl.h"
11 #include "weblayer/browser/tab_impl.h"
12 #include "weblayer/common/features.h"
13 #include "weblayer/public/common/switches.h"
14 #include "weblayer/shell/browser/shell.h"
15 #include "weblayer/shell/common/shell_switches.h"
16 
17 namespace weblayer {
18 
WebLayerBrowserTest()19 WebLayerBrowserTest::WebLayerBrowserTest() {
20   CreateTestServer(base::FilePath(FILE_PATH_LITERAL("weblayer/test/data")));
21 }
22 
23 WebLayerBrowserTest::~WebLayerBrowserTest() = default;
24 
SetUp()25 void WebLayerBrowserTest::SetUp() {
26   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
27   command_line->AppendSwitch(switches::kNoInitialNavigation);
28 
29   // Disable auto reload since most browser tests do not expect error pages to
30   // reload automatically. Tests that want auto reload can explicitly append
31   // switches::kEnableAutoReload, which will override the disable here.
32   command_line->AppendSwitch(switches::kDisableAutoReload);
33 
34   if (start_in_incognito_mode_)
35     command_line->AppendSwitch(switches::kStartInIncognito);
36 
37   SetUpCommandLine(command_line);
38   content::BrowserTestBase::SetUp();
39 }
40 
PreRunTestOnMainThread()41 void WebLayerBrowserTest::PreRunTestOnMainThread() {
42   ASSERT_EQ(Shell::windows().size(), 1u);
43   shell_ = Shell::windows()[0];
44 
45   // Don't fill machine's download directory from tests; instead place downloads
46   // in the temporary user-data-dir for this test.
47   auto* tab_impl = static_cast<TabImpl*>(shell_->tab());
48   auto* browser_context = tab_impl->web_contents()->GetBrowserContext();
49   auto* browser_context_impl =
50       static_cast<BrowserContextImpl*>(browser_context);
51   browser_context_impl->profile_impl()->SetDownloadDirectory(
52       browser_context->GetPath());
53   // Accessing a browser context may involve storage partition initialization.
54   // Wait for the initialization to be completed.
55   base::RunLoop().RunUntilIdle();
56 }
57 
PostRunTestOnMainThread()58 void WebLayerBrowserTest::PostRunTestOnMainThread() {
59   Shell::CloseAllWindows();
60 }
61 
SetShellStartsInIncognitoMode()62 void WebLayerBrowserTest::SetShellStartsInIncognitoMode() {
63   DCHECK(!set_up_called());
64   start_in_incognito_mode_ = true;
65 }
66 
GetProfile()67 ProfileImpl* WebLayerBrowserTest::GetProfile() {
68   return static_cast<TabImpl*>(shell_->tab())->profile();
69 }
70 
GetBrowserContext()71 content::BrowserContext* WebLayerBrowserTest::GetBrowserContext() {
72   return GetProfile()->GetBrowserContext();
73 }
74 
75 }  // namespace weblayer
76