File: hung_renderer_view_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 (198 lines) | stat: -rw-r--r-- 9,252 bytes parent folder | download | duplicates (6)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/hung_renderer_view.h"

#include <string>

#include "base/functional/callback_helpers.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tab_dialogs.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/common/logging_chrome.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/views/accessibility/view_accessibility.h"

// Interactive UI tests for the hung renderer (aka page unresponsive) dialog.
class HungRendererDialogViewBrowserTest : public DialogBrowserTest {
 public:
  HungRendererDialogViewBrowserTest() {
    HungRendererDialogView::BypassActiveBrowserRequirementForTests();
  }

  HungRendererDialogViewBrowserTest(const HungRendererDialogViewBrowserTest&) =
      delete;
  HungRendererDialogViewBrowserTest& operator=(
      const HungRendererDialogViewBrowserTest&) = delete;

  // Normally the dialog only shows multiple WebContents when they're all part
  // of the same process, but that's hard to achieve in a test.
  void AddWebContents(HungRendererDialogView* dialog,
                      content::WebContents* web_contents) {
    HungPagesTableModel* model = dialog->hung_pages_table_model_.get();
    model->tab_observers_.push_back(
        std::make_unique<HungPagesTableModel::WebContentsObserverImpl>(
            model, web_contents));
    if (model->observer_) {
      model->observer_->OnModelChanged();
    }
    dialog->UpdateLabels();
  }

  // DialogBrowserTest:
  void ShowUi(const std::string& name) override {
    auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
    HungRendererDialogView::Show(
        web_contents,
        web_contents->GetPrimaryMainFrame()->GetRenderViewHost()->GetWidget(),
        base::DoNothing());

    if (name == "MultiplePages") {
      HungRendererDialogView* view =
          HungRendererDialogView::GetInstanceForWebContentsForTests(
              web_contents);
      auto* web_contents2 = chrome::DuplicateTabAt(browser(), 0);
      AddWebContents(view, web_contents2);
    }
  }

  HungRendererDialogView* CreateDialogView() {
    auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

    return HungRendererDialogView::CreateInstance(
        web_contents, browser()->window()->GetNativeWindow());
  }

  void EndForWebContents(HungRendererDialogView* dialog,
                         content::WebContents* contents) {
    dialog->EndDialog(
        contents->GetPrimaryMainFrame()->GetRenderViewHost()->GetWidget());
  }
};

// TODO(tapted): The framework sometimes doesn't pick up the spawned dialog and
// the ASSERT_EQ in TestBrowserUi::ShowAndVerifyUi() fails. This seems to only
// happen on the bots. So these tests are disabled for now.
IN_PROC_BROWSER_TEST_F(HungRendererDialogViewBrowserTest,
                       DISABLED_InvokeUi_Default) {
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(HungRendererDialogViewBrowserTest,
                       DISABLED_InvokeUi_MultiplePages) {
  ShowAndVerifyUi();
}

// This is a regression test for https://crbug.com/855369.
IN_PROC_BROWSER_TEST_F(HungRendererDialogViewBrowserTest, InactiveWindow) {
  auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

  // Simulate creation of the dialog, without initializing or showing it yet.
  // This is what happens when HungRendererDialogView::ShowForWebContents
  // returns early if the frame or the dialog are not active.
  CreateDialogView();
  ASSERT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents));

  // Simulate the renderer becoming responsive again.
  content::RenderWidgetHost* render_widget_host =
      web_contents->GetRenderWidgetHostView()->GetRenderWidgetHost();
  content::WebContentsDelegate* web_contents_delegate = browser();
  web_contents_delegate->RendererResponsive(web_contents, render_widget_host);
}

IN_PROC_BROWSER_TEST_F(HungRendererDialogViewBrowserTest, ProcessClosed) {
  auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

  HungRendererDialogView* dialog = CreateDialogView();
  ASSERT_TRUE(dialog);
  ASSERT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents));

  // The Hung Dialog requires window activation, window activations does not
  // work reliably in browser tests, especially in Mac because of the activation
  // policy for obtaining window key status is set to prohibited. Instead of
  // showing the window, populate the table model instead.
  dialog->table_model_for_testing()->InitForWebContents(
      web_contents,
      web_contents->GetPrimaryMainFrame()->GetRenderViewHost()->GetWidget(),
      base::DoNothing());

  // Makes sure the virtual accessibility views are in sync with the model when
  // the dialog is created. Should consist of a single item.
  views::ViewAccessibility& view_accessibility =
      dialog->table_for_testing()->GetViewAccessibility();
  EXPECT_EQ(size_t{1}, view_accessibility.virtual_children().size());

  // Simulate an abrupt ending to the WebContents. The accessibility tree for
  // the TableView depends on the Hung Render View table model to send the right
  // events. By checking the virtual tree, we can guarantee the sync is
  // correct.
  EndForWebContents(dialog, web_contents);
  EXPECT_EQ(size_t{0}, view_accessibility.virtual_children().size());
}

// This dialog used to be a singleton, so this is a basic test that it can now
// be shown on two different browser windows without crashing or obviously
// misbehaving.
IN_PROC_BROWSER_TEST_F(HungRendererDialogViewBrowserTest, TwoHungBrowsers) {
  if (logging::DialogsAreSuppressed()) {
    // If dialogs are suppressed then the hung renderer dialog can't be shown,
    // and this test cannot pass, so bail.
    LOG(ERROR) << "HungRendererDialogViewBrowserTest.TwoHungBrowsers cannot "
                  "run on bots, as hang dialogs are suppressed on them. You "
                  "will need to run this test locally to verify your fixes.";
    return;
  }

  Browser* browser1 = browser();
  content::WebContents* web_contents1 =
      browser1->tab_strip_model()->GetActiveWebContents();
  content::RenderWidgetHost* widget_host1 =
      web_contents1->GetPrimaryMainFrame()->GetRenderViewHost()->GetWidget();

  Browser* browser2 =
      Browser::Create(Browser::CreateParams(browser1->profile(), true));
  chrome::NewTab(browser2);
  content::WebContents* web_contents2 =
      browser2->tab_strip_model()->GetActiveWebContents();
  content::RenderWidgetHost* widget_host2 =
      web_contents2->GetPrimaryMainFrame()->GetRenderViewHost()->GetWidget();

  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
  HungRendererDialogView::Show(web_contents1, widget_host1, base::DoNothing());
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
  HungRendererDialogView::Show(web_contents2, widget_host2, base::DoNothing());
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
  HungRendererDialogView::Hide(web_contents1, widget_host1);
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
  HungRendererDialogView::Hide(web_contents2, widget_host2);
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents2));

  HungRendererDialogView::Show(web_contents1, widget_host1, base::DoNothing());
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
  HungRendererDialogView::Show(web_contents2, widget_host2, base::DoNothing());
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
  HungRendererDialogView::Hide(web_contents2, widget_host2);
  EXPECT_TRUE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
  HungRendererDialogView::Hide(web_contents1, widget_host1);
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents1));
  EXPECT_FALSE(HungRendererDialogView::IsShowingForWebContents(web_contents2));
}