Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(666)

Side by Side Diff: chrome/browser/ui/javascript_dialogs/javascript_dialog_browsertest.cc

Issue 2641173004: Make the callback from JavaScript dialogs even when closing a WebContents. (Closed)
Patch Set: fix Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/strings/utf_string_conversions.h" 5 #include "base/strings/utf_string_conversions.h"
6 #include "base/test/scoped_command_line.h"
6 #include "base/test/scoped_feature_list.h" 7 #include "base/test/scoped_feature_list.h"
7 #include "chrome/browser/ui/browser.h" 8 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" 9 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h" 10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/chrome_features.h" 11 #include "chrome/common/chrome_features.h"
12 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/base/in_process_browser_test.h" 13 #include "chrome/test/base/in_process_browser_test.h"
12 #include "content/public/browser/render_frame_host.h" 14 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/browser_test_utils.h" 16 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/test_utils.h" 17 #include "content/public/test/test_utils.h"
16 18
17 using JavaScriptDialogTest = InProcessBrowserTest; 19 using JavaScriptDialogTest = InProcessBrowserTest;
18 20
19 IN_PROC_BROWSER_TEST_F(JavaScriptDialogTest, ReloadDoesntHang) { 21 IN_PROC_BROWSER_TEST_F(JavaScriptDialogTest, ReloadDoesntHang) {
20 base::test::ScopedFeatureList feature_list; 22 base::test::ScopedFeatureList feature_list;
21 feature_list.InitAndEnableFeature(features::kAutoDismissingDialogs); 23 feature_list.InitAndEnableFeature(features::kAutoDismissingDialogs);
22 24
23 content::WebContents* tab = 25 content::WebContents* tab =
24 browser()->tab_strip_model()->GetActiveWebContents(); 26 browser()->tab_strip_model()->GetActiveWebContents();
25 JavaScriptDialogTabHelper* js_helper = 27 JavaScriptDialogTabHelper* js_helper =
26 JavaScriptDialogTabHelper::FromWebContents(tab); 28 JavaScriptDialogTabHelper::FromWebContents(tab);
27 29
28 // Show a dialog. 30 // Show a dialog.
29
30 scoped_refptr<content::MessageLoopRunner> runner = 31 scoped_refptr<content::MessageLoopRunner> runner =
31 new content::MessageLoopRunner; 32 new content::MessageLoopRunner;
32 js_helper->SetDialogShownCallbackForTesting(runner->QuitClosure()); 33 js_helper->SetDialogShownCallbackForTesting(runner->QuitClosure());
33 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()")); 34 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
34 runner->Run(); 35 runner->Run();
35 36
36 // Try reloading. 37 // Try reloading.
37
38 tab->GetController().Reload(content::ReloadType::NORMAL, false); 38 tab->GetController().Reload(content::ReloadType::NORMAL, false);
39 content::WaitForLoadStop(tab); 39 content::WaitForLoadStop(tab);
40 40
41 // If the WaitForLoadStop doesn't hang forever, we've passed. 41 // If the WaitForLoadStop doesn't hang forever, we've passed.
42 } 42 }
43
44 IN_PROC_BROWSER_TEST_F(JavaScriptDialogTest,
45 ClosingPageSharingRendererDoesntHang) {
46 base::test::ScopedFeatureList feature_list;
47 feature_list.InitAndEnableFeature(features::kAutoDismissingDialogs);
48
49 // Turn off popup blocking.
50 base::test::ScopedCommandLine scoped_command_line;
51 scoped_command_line.GetProcessCommandLine()->AppendSwitch(
52 switches::kDisablePopupBlocking);
53
54 // Two tabs, one render process.
55 content::WebContents* tab1 =
56 browser()->tab_strip_model()->GetActiveWebContents();
57 content::WebContentsAddedObserver new_wc_observer;
58 tab1->GetMainFrame()->ExecuteJavaScriptForTests(
59 base::UTF8ToUTF16("window.open('about:blank');"));
60 content::WebContents* tab2 = new_wc_observer.GetWebContents();
61 ASSERT_NE(tab1, tab2);
62 ASSERT_EQ(tab1->GetRenderProcessHost(), tab2->GetRenderProcessHost());
63
64 // Tab two shows a dialog.
65 scoped_refptr<content::MessageLoopRunner> runner =
66 new content::MessageLoopRunner;
67 JavaScriptDialogTabHelper* js_helper2 =
68 JavaScriptDialogTabHelper::FromWebContents(tab2);
69 js_helper2->SetDialogShownCallbackForTesting(runner->QuitClosure());
70 tab2->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
71 runner->Run();
72
73 // Tab two is closed while the dialog is up.
74 int tab2_index = browser()->tab_strip_model()->GetIndexOfWebContents(tab2);
75 browser()->tab_strip_model()->CloseWebContentsAt(tab2_index,
76 TabStripModel::CLOSE_NONE);
77
78 // Try reloading tab one.
79 tab1->GetController().Reload(content::ReloadType::NORMAL, false);
80 content::WaitForLoadStop(tab1);
81
82 // If the WaitForLoadStop doesn't hang forever, we've passed.
83 }
84
85 IN_PROC_BROWSER_TEST_F(JavaScriptDialogTest,
86 ClosingPageWithSubframeAlertingDoesntCrash) {
87 base::test::ScopedFeatureList feature_list;
88 feature_list.InitAndEnableFeature(features::kAutoDismissingDialogs);
89
90 content::WebContents* tab =
91 browser()->tab_strip_model()->GetActiveWebContents();
92 JavaScriptDialogTabHelper* js_helper =
93 JavaScriptDialogTabHelper::FromWebContents(tab);
94
95 // A subframe shows a dialog.
96 std::string dialog_url = "data:text/html,<script>alert(\"hi\");</script>";
97 std::string script = "var iframe = document.createElement('iframe');"
98 "iframe.src = '" + dialog_url + "';"
99 "document.body.appendChild(iframe);";
100 scoped_refptr<content::MessageLoopRunner> runner =
101 new content::MessageLoopRunner;
102 js_helper->SetDialogShownCallbackForTesting(runner->QuitClosure());
103 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16(script));
104 runner->Run();
105
106 // The tab is closed while the dialog is up.
107 int tab_index = browser()->tab_strip_model()->GetIndexOfWebContents(tab);
108 browser()->tab_strip_model()->CloseWebContentsAt(tab_index,
109 TabStripModel::CLOSE_NONE);
110
111 // No crash is good news.
112 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698