blob: 507e178a7a4391bba3699287ff5c96532c2888b6 (
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
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
|
// Copyright (c) 2010 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 "base/file_path.h"
#include "base/file_util.h"
#include "base/task.h"
#include "base/values.h"
#include "base/path_service.h"
#include "chrome/browser/dom_ui/dom_ui.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/views/html_dialog_view.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
namespace {
typedef DOMElementProxy::By By;
class FileBrowseBrowserTest : public InProcessBrowserTest {
public:
FileBrowseBrowserTest() {
EnableDOMAutomation();
}
};
class FileBrowseUiObserver : public NotificationObserver {
public:
FileBrowseUiObserver() : file_browse_tab_(NULL), is_waiting_(false) {
registrar_.Add(this, NotificationType::LOAD_STOP,
NotificationService::AllSources());
}
void WaitForFileBrowseLoad() {
if (file_browse_tab_ == NULL) {
is_waiting_ = true;
ui_test_utils::RunMessageLoop();
}
}
// File browse tab deletion is a non-nestable task and BrowserTest would
// not get related notification because test body runs in a task already.
// Uses a periodical check of the dialog window to implement the wait.
void WaitForFileBrowseClose() {
CheckFileBrowseClosed();
if (file_browse_tab_ != NULL) {
is_waiting_ = true;
ui_test_utils::RunMessageLoop();
}
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::LOAD_STOP) {
NavigationController* controller =
Source<NavigationController>(source).ptr();
if (controller) {
TabContents* tab_contents = controller->tab_contents();
if (tab_contents &&
tab_contents->GetURL().SchemeIs(chrome::kChromeUIScheme) &&
tab_contents->GetURL().host() == chrome::kChromeUIFileBrowseHost) {
file_browse_tab_ = tab_contents;
if (is_waiting_) {
is_waiting_ = false;
MessageLoopForUI::current()->Quit();
}
}
}
}
}
TabContents* file_browse_tab() {
return file_browse_tab_;
}
DOMUI* file_browse_ui() {
return file_browse_tab_ ? file_browse_tab_->render_manager()->dom_ui() :
NULL;
}
private:
class CheckFileBrowseClosedTask : public Task {
public:
explicit CheckFileBrowseClosedTask(FileBrowseUiObserver* owner)
: owner_(owner) {
}
virtual void Run() {
owner_->CheckFileBrowseClosed();
}
private:
FileBrowseUiObserver* owner_;
};
void CheckFileBrowseClosed() {
HtmlDialogView* dialog_view =
static_cast<HtmlDialogView*>(file_browse_tab_->delegate());
if (dialog_view->native_view()) {
MessageLoopForUI::current()->PostDelayedTask(
FROM_HERE, new CheckFileBrowseClosedTask(this), 100);
} else {
file_browse_tab_ = NULL;
if (is_waiting_) {
is_waiting_ = false;
MessageLoopForUI::current()->Quit();
}
}
}
NotificationRegistrar registrar_;
TabContents* file_browse_tab_;
bool is_waiting_;
DISALLOW_COPY_AND_ASSIGN(FileBrowseUiObserver);
};
IN_PROC_BROWSER_TEST_F(FileBrowseBrowserTest, InputFileTriggerFileBrowse) {
HTTPTestServer* server = StartHTTPServer();
ui_test_utils::NavigateToURL(browser(),
server->TestServerPage("files/input_file.html"));
DOMElementProxyRef doc = ui_test_utils::GetActiveDOMDocument(browser());
DOMElementProxyRef input_file = doc->FindElement(By::Selectors(".single"));
ASSERT_TRUE(input_file);
// Creates FileBrowseUiObserver before we click.
FileBrowseUiObserver observer;
// Click on the input control. This should bring up the FileBrowseUI.
input_file->Click();
observer.WaitForFileBrowseLoad();
DOMUI* file_browser_ui = observer.file_browse_ui();
ASSERT_TRUE(file_browser_ui);
file_browser_ui->CallJavascriptFunction(L"dialogCancelClick");
observer.WaitForFileBrowseClose();
}
} // namespace
|