summaryrefslogtreecommitdiffstats
path: root/content/test/browser_side_navigation_test_utils.cc
blob: 904e0d1abf3262a751d0bb59ffd019cc7bddd139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/test/browser_side_navigation_test_utils.h"

#include "base/command_line.h"
#include "base/guid.h"
#include "base/lazy_instance.h"
#include "content/browser/streams/stream.h"
#include "content/browser/streams/stream_registry.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/stream_handle.h"
#include "content/public/common/content_switches.h"
#include "content/test/test_navigation_url_loader_factory.h"

namespace content {

namespace {

// PlzNavigate
// A UI thread singleton helper class for browser side navigations. When browser
// side navigations are enabled, initialize this class before doing any
// operation that may start a navigation request on the UI thread. Use TearDown
// at the end of the test.
class BrowserSideNavigationTestUtils {
 public:
  BrowserSideNavigationTestUtils()
      : stream_registry_(new StreamRegistry),
        loader_factory_(new TestNavigationURLLoaderFactory) {
  }

  ~BrowserSideNavigationTestUtils() {}
  StreamRegistry* stream_registry() { return stream_registry_.get();}

 private:
  scoped_ptr<StreamRegistry> stream_registry_;
  scoped_ptr<TestNavigationURLLoaderFactory> loader_factory_;

  DISALLOW_COPY_AND_ASSIGN(BrowserSideNavigationTestUtils);
};

base::LazyInstance<scoped_ptr<BrowserSideNavigationTestUtils>>
    browser_side_navigation_test_utils;

}  // namespace

void BrowserSideNavigationSetUp() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  browser_side_navigation_test_utils.Get().reset(
      new BrowserSideNavigationTestUtils);
}

void BrowserSideNavigationTearDown() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  browser_side_navigation_test_utils.Get().reset(nullptr);
}

scoped_ptr<StreamHandle> MakeEmptyStream() {
  GURL url(std::string(url::kBlobScheme) + "://" + base::GenerateGUID());
  StreamRegistry* stream_registry =
      browser_side_navigation_test_utils.Get()->stream_registry();
  scoped_refptr<Stream> stream(new Stream(stream_registry, NULL, url));
  stream->Finalize();
  return stream->CreateHandle();
}

void EnableBrowserSideNavigation() {
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kEnableBrowserSideNavigation);
}

}  // namespace content