File: in_process_browser_test_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (200 lines) | stat: -rw-r--r-- 6,505 bytes parent folder | download | duplicates (5)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/test/base/in_process_browser_test.h"

#include <stddef.h>
#include <string.h>

#include <array>

#include "base/files/file_util.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "chrome/browser/after_startup_task_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "net/base/filename_util.h"
#include "net/base/net_errors.h"
#include "net/dns/public/resolve_error_info.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/browser_app_menu_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/view.h"
#endif

namespace {

// WebContents observer that can detect provisional load failures.
class LoadFailObserver : public content::WebContentsObserver {
 public:
  explicit LoadFailObserver(content::WebContents* contents)
      : content::WebContentsObserver(contents) {}
  LoadFailObserver(const LoadFailObserver&) = delete;
  LoadFailObserver& operator=(const LoadFailObserver&) = delete;

  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override {
    if (navigation_handle->GetNetErrorCode() == net::OK)
      return;

    failed_load_ = true;
    error_code_ = navigation_handle->GetNetErrorCode();
    resolve_error_info_ = navigation_handle->GetResolveErrorInfo();
    validated_url_ = navigation_handle->GetURL();
  }

  bool failed_load() const { return failed_load_; }
  net::Error error_code() const { return error_code_; }
  net::ResolveErrorInfo resolve_error_info() const {
    return resolve_error_info_;
  }
  const GURL& validated_url() const { return validated_url_; }

 private:
  bool failed_load_ = false;
  net::Error error_code_ = net::OK;
  net::ResolveErrorInfo resolve_error_info_ = net::ResolveErrorInfo(net::OK);
  GURL validated_url_;
};

}  // namespace

class InProcessBrowserTestP
    : public InProcessBrowserTest,
      public ::testing::WithParamInterface<const char*> {};

IN_PROC_BROWSER_TEST_P(InProcessBrowserTestP, TestP) {
  EXPECT_EQ(0, strcmp("foo", GetParam()));
}

INSTANTIATE_TEST_SUITE_P(IPBTP,
                         InProcessBrowserTestP,
                         ::testing::Values("foo"));

// Tests that InProcessBrowserTest cannot resolve external host, in this case
// "google.com" and "cnn.com". Using external resources is disabled by default
// in InProcessBrowserTest because it causes flakiness.
IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, ExternalConnectionFail) {
  content::WebContents* contents =
      browser()->tab_strip_model()->GetActiveWebContents();

  const auto kURLs = std::to_array<const char*>(
      {"https://www.google.com/", "https://www.cnn.com/"});
  for (size_t i = 0; i < std::size(kURLs); ++i) {
    GURL url(kURLs[i]);
    LoadFailObserver observer(contents);
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
    EXPECT_TRUE(observer.failed_load());
    EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, observer.error_code());
    EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, observer.resolve_error_info().error);
    EXPECT_EQ(url, observer.validated_url());
  }
}

// Verify that AfterStartupTaskUtils considers startup to be complete
// prior to test execution so tasks posted by tests are never deferred.
IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, AfterStartupTaskUtils) {
  EXPECT_TRUE(AfterStartupTaskUtils::IsBrowserStartupComplete());
}

// On Mac this crashes inside cc::SingleThreadProxy::SetNeedsCommit. See
// https://ci.chromium.org/b/8923336499994443392
#if !BUILDFLAG(IS_MAC)
class SingleProcessBrowserTest : public InProcessBrowserTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(switches::kSingleProcess);
  }
};

// TODO(crbug.com/40190525): Flaky / times out on many bots.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
IN_PROC_BROWSER_TEST_F(SingleProcessBrowserTest, MAYBE_Test) {
  // Should not crash.
}

#endif

#if defined(TOOLKIT_VIEWS)

namespace {

class LayoutTrackingView : public views::View {
  METADATA_HEADER(LayoutTrackingView, views::View)

 public:
  LayoutTrackingView() = default;
  ~LayoutTrackingView() override = default;

  void ResetLayoutCount() { layout_count_ = 0; }
  int layout_count() const { return layout_count_; }

  // views::View:
  void Layout(PassKey) override {
    ++layout_count_;
    LayoutSuperclass<views::View>(this);
  }

 private:
  int layout_count_ = 0;
};

BEGIN_METADATA(LayoutTrackingView)
END_METADATA

}  // namespace

IN_PROC_BROWSER_TEST_F(InProcessBrowserTest,
                       RunsScheduledLayoutOnAnchoredBubbles) {
  views::View* const anchor_view =
      BrowserView::GetBrowserViewForBrowser(browser())
          ->toolbar()
          ->app_menu_button();

  // Temporarily owned.
  views::BubbleDialogDelegateView* const bubble =
      new views::BubbleDialogDelegateView(
          views::BubbleDialogDelegateView::CreatePassKey(), anchor_view,
          views::BubbleBorder::TOP_RIGHT);
  LayoutTrackingView* layout_tracker =
      bubble->AddChildView(std::make_unique<LayoutTrackingView>());

  // Takes ownership.
  views::Widget* const bubble_widget =
      views::BubbleDialogDelegateView::CreateBubble(bubble);
  bubble_widget->Show();

  layout_tracker->ResetLayoutCount();
  layout_tracker->InvalidateLayout();
  EXPECT_EQ(layout_tracker->layout_count(), 0);

  RunScheduledLayouts();
  EXPECT_GT(layout_tracker->layout_count(), 0);
}

#endif  // defined(TOOLKIT_VIEWS)