OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <algorithm> | |
6 #include <iterator> | |
7 #include <string> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/file_path.h" | |
11 #include "base/path_service.h" | |
12 #include "base/string_util.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "chrome/browser/intents/intent_service_host.h" | |
17 #include "chrome/browser/intents/native_services.h" | |
18 #include "chrome/browser/intents/web_intents_util.h" | |
19 #include "chrome/browser/ui/browser.h" | |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
21 #include "chrome/common/chrome_paths.h" | |
22 #include "chrome/common/chrome_switches.h" | |
23 #include "chrome/test/base/in_process_browser_test.h" | |
24 #include "chrome/test/base/ui_test_utils.h" | |
25 #include "content/public/browser/web_contents.h" | |
26 #include "content/public/browser/web_intents_dispatcher.h" | |
27 #include "testing/gtest/include/gtest/gtest.h" | |
28 #include "ui/shell_dialogs/select_file_dialog_factory.h" | |
29 #include "webkit/glue/web_intent_data.h" | |
30 #include "webkit/glue/web_intent_reply_data.h" | |
31 #include "webkit/glue/web_intent_service_data.h" | |
32 | |
33 using content::BrowserThread; | |
34 | |
35 namespace { | |
36 | |
37 const std::string kPoodlePath = "/home/poodles/skippy.png"; | |
38 bool picker_success_mode = true; | |
39 const int64 kTestFileSize = 193; | |
40 | |
41 FilePath CreateTestFile() { | |
42 FilePath file; | |
43 PathService::Get(chrome::DIR_TEST_DATA, &file); | |
44 file = file.AppendASCII("web_intents").AppendASCII("test.png"); | |
45 return file; | |
46 } | |
47 | |
48 class TestIntentsDispatcher : public content::WebIntentsDispatcher { | |
49 public: | |
50 explicit TestIntentsDispatcher(const webkit_glue::WebIntentData& intent) | |
51 : intent_(intent) {} | |
52 | |
53 virtual const webkit_glue::WebIntentData& GetIntent() OVERRIDE { | |
54 return intent_; | |
55 } | |
56 | |
57 virtual void DispatchIntent(content::WebContents* web_contents) OVERRIDE {} | |
58 virtual void ResetDispatch() OVERRIDE {} | |
59 | |
60 virtual void SendReply(const webkit_glue::WebIntentReply& reply) OVERRIDE { | |
61 reply_.reset(new webkit_glue::WebIntentReply(reply)); | |
62 } | |
63 | |
64 virtual void RegisterReplyNotification( | |
65 const base::Callback<void(webkit_glue::WebIntentReplyType)>&) OVERRIDE { | |
66 } | |
67 | |
68 webkit_glue::WebIntentData intent_; | |
69 | |
70 scoped_ptr<webkit_glue::WebIntentReply> reply_; | |
71 }; | |
72 | |
73 // Stub SelectFileDialog designed for testing. | |
74 class TestSelectFileDialog : public ui::SelectFileDialog { | |
75 public: | |
76 // Main factory method which returns correct type. | |
77 static ui::SelectFileDialog* Create( | |
78 Listener* listener, | |
79 ui::SelectFilePolicy* policy) { | |
80 return new TestSelectFileDialog(listener, policy); | |
81 } | |
82 | |
83 // SelectFileDialog implementation. | |
84 virtual bool IsRunning(gfx::NativeWindow parent_window) const OVERRIDE { | |
85 return false; | |
86 } | |
87 virtual void ListenerDestroyed() OVERRIDE {} | |
88 virtual bool HasMultipleFileTypeChoicesImpl() OVERRIDE { | |
89 return false; | |
90 } | |
91 | |
92 protected: | |
93 TestSelectFileDialog( | |
94 Listener* listener, ui::SelectFilePolicy* policy) | |
95 : ui::SelectFileDialog(listener, policy), | |
96 listener_(listener) { | |
97 test_file_ = CreateTestFile(); | |
98 } | |
99 virtual ~TestSelectFileDialog() {} | |
100 | |
101 // SelectFileDialog implementation. | |
102 // |params| is user data we pass back via the Listener interface. | |
103 virtual void SelectFileImpl( | |
104 Type type, | |
105 const string16& title, | |
106 const FilePath& default_path, | |
107 const FileTypeInfo* file_types, | |
108 int file_type_index, | |
109 const FilePath::StringType& default_extension, | |
110 gfx::NativeWindow owning_window, | |
111 void* params) OVERRIDE { | |
112 if (picker_success_mode) | |
113 listener_->FileSelected(test_file_, kTestFileSize, NULL); | |
114 else | |
115 listener_->FileSelectionCanceled(NULL); | |
116 } | |
117 | |
118 private: | |
119 // Weak pointer to handler for dialog events. | |
120 Listener* listener_; | |
121 FilePath test_file_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(TestSelectFileDialog); | |
124 }; | |
125 | |
126 class TestSelectFileDialogFactory : public ui::SelectFileDialogFactory { | |
127 public: | |
128 virtual ui::SelectFileDialog* Create( | |
129 ui::SelectFileDialog::Listener* listener, | |
130 ui::SelectFilePolicy* policy) { | |
131 return TestSelectFileDialog::Create(listener, policy); | |
132 } | |
133 }; | |
134 | |
135 } // namespace | |
136 | |
137 class NativeServicesBrowserTest : public InProcessBrowserTest { | |
138 protected: | |
139 NativeServicesBrowserTest() {} | |
140 | |
141 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
142 // We start the test server now instead of in | |
143 // SetUpInProcessBrowserTestFixture so that we can get its port number. | |
144 ASSERT_TRUE(test_server()->Start()); | |
145 | |
146 command_line->AppendSwitch(switches::kWebIntentsNativeServicesEnabled); | |
147 test_file_ = CreateTestFile(); | |
148 } | |
149 | |
150 virtual void SetUpOnMainThread() OVERRIDE { | |
151 factory_.reset(new TestSelectFileDialogFactory()); | |
152 ui::SelectFileDialog::SetFactory(factory_.get()); | |
153 } | |
154 | |
155 virtual Browser* GetBrowser() { return browser(); } | |
156 scoped_ptr<TestSelectFileDialogFactory> factory_; | |
157 FilePath test_file_; | |
158 }; | |
159 | |
160 IN_PROC_BROWSER_TEST_F(NativeServicesBrowserTest, PickFileSelected) { | |
161 content::WebContents* tab = | |
162 GetBrowser()->tab_strip_model()->GetActiveWebContents(); | |
163 | |
164 webkit_glue::WebIntentData intent; | |
165 intent.action = ASCIIToUTF16(web_intents::kActionPick); | |
166 intent.type = ASCIIToUTF16("image/*"); | |
167 | |
168 TestIntentsDispatcher dispatcher(intent); | |
169 web_intents::NativeServiceFactory factory; | |
170 GURL url(web_intents::kNativeFilePickerUrl); | |
171 scoped_ptr<web_intents::IntentServiceHost> service( | |
172 factory.CreateServiceInstance(url, intent, tab)); | |
173 service->HandleIntent(&dispatcher); | |
174 | |
175 // Reads of file size are done on the FILE thread, then posted | |
176 // back to the UI thread. | |
177 content::RunAllPendingInMessageLoop(BrowserThread::FILE); | |
178 content::RunAllPendingInMessageLoop(BrowserThread::UI); | |
179 | |
180 ASSERT_TRUE(dispatcher.reply_); | |
181 EXPECT_EQ( | |
182 webkit_glue::WebIntentReply( | |
183 webkit_glue::WEB_INTENT_REPLY_SUCCESS, | |
184 test_file_, | |
185 kTestFileSize), | |
186 *dispatcher.reply_.get()); | |
187 } | |
188 | |
189 IN_PROC_BROWSER_TEST_F(NativeServicesBrowserTest, PickFileCancelled) { | |
190 picker_success_mode = false; | |
191 content::WebContents* tab = | |
192 GetBrowser()->tab_strip_model()->GetActiveWebContents(); | |
193 | |
194 webkit_glue::WebIntentData intent; | |
195 intent.action = ASCIIToUTF16(web_intents::kActionPick); | |
196 intent.type = ASCIIToUTF16("image/*"); | |
197 | |
198 TestIntentsDispatcher dispatcher(intent); | |
199 web_intents::NativeServiceFactory factory; | |
200 GURL url(web_intents::kNativeFilePickerUrl); | |
201 scoped_ptr<web_intents::IntentServiceHost> service( | |
202 factory.CreateServiceInstance(url, intent, tab)); | |
203 service->HandleIntent(&dispatcher); | |
204 | |
205 ASSERT_TRUE(dispatcher.reply_); | |
206 EXPECT_EQ( | |
207 webkit_glue::WebIntentReply( | |
208 webkit_glue::WEB_INTENT_REPLY_FAILURE, | |
209 string16()), | |
210 *dispatcher.reply_.get()); | |
211 } | |
OLD | NEW |