Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / extensions / alert_apitest.cc
bloba4f99f0255c0e05744fcca348ea57d6f8cb9dfb6
1 // Copyright (c) 2012 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.
5 #include "base/strings/string_number_conversions.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/test/base/ui_test_utils.h"
11 #include "components/app_modal/app_modal_dialog.h"
12 #include "components/app_modal/app_modal_dialog_queue.h"
13 #include "components/app_modal/javascript_app_modal_dialog.h"
14 #include "components/app_modal/native_app_modal_dialog.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/test/test_utils.h"
17 #include "extensions/browser/extension_host.h"
18 #include "extensions/browser/process_manager.h"
19 #include "extensions/common/extension.h"
21 namespace {
23 void GetNextDialog(app_modal::NativeAppModalDialog** native_dialog) {
24 DCHECK(native_dialog);
25 *native_dialog = nullptr;
26 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
27 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
28 app_modal::JavaScriptAppModalDialog* js_dialog =
29 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
30 *native_dialog = js_dialog->native_dialog();
31 ASSERT_TRUE(*native_dialog);
34 void CloseDialog() {
35 app_modal::NativeAppModalDialog* dialog = nullptr;
36 ASSERT_NO_FATAL_FAILURE(GetNextDialog(&dialog));
37 dialog->CloseAppModalDialog();
40 void AcceptDialog() {
41 app_modal::NativeAppModalDialog* dialog = nullptr;
42 ASSERT_NO_FATAL_FAILURE(GetNextDialog(&dialog));
43 dialog->AcceptAppModalDialog();
46 void CancelDialog() {
47 app_modal::NativeAppModalDialog* dialog = nullptr;
48 ASSERT_NO_FATAL_FAILURE(GetNextDialog(&dialog));
49 dialog->CancelAppModalDialog();
52 void CheckAlertResult(const std::string& dialog_name,
53 size_t* call_count,
54 const base::Value* value) {
55 ASSERT_TRUE(value) << dialog_name;
56 ASSERT_TRUE(value->IsType(base::Value::TYPE_NULL));
57 ++*call_count;
60 void CheckConfirmResult(const std::string& dialog_name,
61 bool expected_value,
62 size_t* call_count,
63 const base::Value* value) {
64 ASSERT_TRUE(value) << dialog_name;
65 bool current_value;
66 ASSERT_TRUE(value->GetAsBoolean(&current_value)) << dialog_name;
67 ASSERT_EQ(expected_value, current_value) << dialog_name;
68 ++*call_count;
71 } // namespace
73 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, AlertBasic) {
74 ASSERT_TRUE(RunExtensionTest("alert")) << message_;
76 const extensions::Extension* extension = GetSingleLoadedExtension();
77 extensions::ExtensionHost* host =
78 extensions::ProcessManager::Get(browser()->profile())
79 ->GetBackgroundHostForExtension(extension->id());
80 ASSERT_TRUE(host);
81 host->host_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
82 base::ASCIIToUTF16("alert('This should not crash.');"));
84 ASSERT_NO_FATAL_FAILURE(CloseDialog());
87 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, AlertQueue) {
88 ASSERT_TRUE(RunExtensionTest("alert")) << message_;
90 const extensions::Extension* extension = GetSingleLoadedExtension();
91 extensions::ExtensionHost* host =
92 extensions::ProcessManager::Get(browser()->profile())
93 ->GetBackgroundHostForExtension(extension->id());
94 ASSERT_TRUE(host);
96 // Creates several dialogs at the same time.
97 const size_t num_dialogs = 3;
98 size_t call_count = 0;
99 for (size_t i = 0; i != num_dialogs; ++i) {
100 const std::string dialog_name = "Dialog #" + base::SizeTToString(i) + ".";
101 host->host_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
102 base::ASCIIToUTF16("alert('" + dialog_name + "');"),
103 base::Bind(&CheckAlertResult, dialog_name,
104 base::Unretained(&call_count)));
107 // Closes these dialogs.
108 for (size_t i = 0; i != num_dialogs; ++i) {
109 ASSERT_NO_FATAL_FAILURE(AcceptDialog());
112 // All dialogs must be closed now.
113 app_modal::AppModalDialogQueue* queue =
114 app_modal::AppModalDialogQueue::GetInstance();
115 ASSERT_TRUE(queue);
116 EXPECT_FALSE(queue->HasActiveDialog());
117 EXPECT_EQ(0, queue->end() - queue->begin());
118 while (call_count < num_dialogs)
119 ASSERT_NO_FATAL_FAILURE(content::RunAllPendingInMessageLoop());
122 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ConfirmQueue) {
123 ASSERT_TRUE(RunExtensionTest("alert")) << message_;
125 const extensions::Extension* extension = GetSingleLoadedExtension();
126 extensions::ExtensionHost* host =
127 extensions::ProcessManager::Get(browser()->profile())
128 ->GetBackgroundHostForExtension(extension->id());
129 ASSERT_TRUE(host);
131 // Creates several dialogs at the same time.
132 const size_t num_accepted_dialogs = 3;
133 const size_t num_cancelled_dialogs = 3;
134 size_t call_count = 0;
135 for (size_t i = 0; i != num_accepted_dialogs; ++i) {
136 const std::string dialog_name =
137 "Accepted dialog #" + base::SizeTToString(i) + ".";
138 host->host_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
139 base::ASCIIToUTF16("confirm('" + dialog_name + "');"),
140 base::Bind(&CheckConfirmResult, dialog_name, true,
141 base::Unretained(&call_count)));
143 for (size_t i = 0; i != num_cancelled_dialogs; ++i) {
144 const std::string dialog_name =
145 "Cancelled dialog #" + base::SizeTToString(i) + ".";
146 host->host_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
147 base::ASCIIToUTF16("confirm('" + dialog_name + "');"),
148 base::Bind(&CheckConfirmResult, dialog_name, false,
149 base::Unretained(&call_count)));
152 // Closes these dialogs.
153 for (size_t i = 0; i != num_accepted_dialogs; ++i)
154 ASSERT_NO_FATAL_FAILURE(AcceptDialog());
155 for (size_t i = 0; i != num_cancelled_dialogs; ++i)
156 ASSERT_NO_FATAL_FAILURE(CancelDialog());
158 // All dialogs must be closed now.
159 app_modal::AppModalDialogQueue* queue =
160 app_modal::AppModalDialogQueue::GetInstance();
161 ASSERT_TRUE(queue);
162 EXPECT_FALSE(queue->HasActiveDialog());
163 EXPECT_EQ(0, queue->end() - queue->begin());
164 while (call_count < num_accepted_dialogs + num_cancelled_dialogs)
165 ASSERT_NO_FATAL_FAILURE(content::RunAllPendingInMessageLoop());