| 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 const int64 kTestFileSize = 193; | |
| 39 | |
| 40 base::FilePath CreateTestFile() { | |
| 41 base::FilePath file; | |
| 42 PathService::Get(chrome::DIR_TEST_DATA, &file); | |
| 43 file = file.AppendASCII("web_intents").AppendASCII("test.png"); | |
| 44 return file; | |
| 45 } | |
| 46 | |
| 47 class TestIntentsDispatcher : public content::WebIntentsDispatcher { | |
| 48 public: | |
| 49 TestIntentsDispatcher() { | |
| 50 intent_.action = ASCIIToUTF16(web_intents::kActionPick); | |
| 51 intent_.type = ASCIIToUTF16("image/*"); | |
| 52 } | |
| 53 | |
| 54 virtual const webkit_glue::WebIntentData& GetIntent() OVERRIDE { | |
| 55 return intent_; | |
| 56 } | |
| 57 | |
| 58 virtual void DispatchIntent(content::WebContents* web_contents) OVERRIDE {} | |
| 59 virtual void ResetDispatch() OVERRIDE {} | |
| 60 | |
| 61 virtual void SendReply(const webkit_glue::WebIntentReply& reply) OVERRIDE { | |
| 62 reply_.reset(new webkit_glue::WebIntentReply(reply)); | |
| 63 } | |
| 64 | |
| 65 virtual void RegisterReplyNotification( | |
| 66 const base::Callback<void(webkit_glue::WebIntentReplyType)>&) OVERRIDE { | |
| 67 } | |
| 68 | |
| 69 webkit_glue::WebIntentData intent_; | |
| 70 scoped_ptr<webkit_glue::WebIntentReply> reply_; | |
| 71 }; | |
| 72 | |
| 73 class FakeSelectFileDialog : public ui::SelectFileDialog { | |
| 74 public: | |
| 75 FakeSelectFileDialog( | |
| 76 Listener* listener, ui::SelectFilePolicy* policy, bool should_succeed) | |
| 77 : SelectFileDialog(listener, policy), should_succeed_(should_succeed), | |
| 78 test_file_(CreateTestFile()) { | |
| 79 } | |
| 80 virtual bool IsRunning(gfx::NativeWindow parent_window) const OVERRIDE { | |
| 81 return false; | |
| 82 } | |
| 83 virtual void ListenerDestroyed() OVERRIDE {} | |
| 84 virtual bool HasMultipleFileTypeChoicesImpl() OVERRIDE { | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 protected: | |
| 89 virtual void SelectFileImpl( | |
| 90 Type type, | |
| 91 const string16& title, | |
| 92 const base::FilePath& default_path, | |
| 93 const FileTypeInfo* file_types, | |
| 94 int file_type_index, | |
| 95 const base::FilePath::StringType& default_extension, | |
| 96 gfx::NativeWindow owning_window, | |
| 97 void* params) OVERRIDE { | |
| 98 if (should_succeed_) | |
| 99 listener_->FileSelected(test_file_, kTestFileSize, params); | |
| 100 else | |
| 101 listener_->FileSelectionCanceled(params); | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 bool should_succeed_; | |
| 106 base::FilePath test_file_; | |
| 107 DISALLOW_COPY_AND_ASSIGN(FakeSelectFileDialog); | |
| 108 }; | |
| 109 | |
| 110 class TestSelectFileDialogFactory : public ui::SelectFileDialogFactory { | |
| 111 public: | |
| 112 virtual ui::SelectFileDialog* Create( | |
| 113 ui::SelectFileDialog::Listener* listener, | |
| 114 ui::SelectFilePolicy* policy) OVERRIDE { | |
| 115 return new FakeSelectFileDialog(listener, policy, should_succeed_); | |
| 116 } | |
| 117 bool should_succeed_; | |
| 118 }; | |
| 119 | |
| 120 class NativeServicesBrowserTest : public InProcessBrowserTest { | |
| 121 protected: | |
| 122 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 123 // We start the test server now instead of in | |
| 124 // SetUpInProcessBrowserTestFixture so that we can get its port number. | |
| 125 ASSERT_TRUE(test_server()->Start()); | |
| 126 | |
| 127 command_line->AppendSwitch(switches::kWebIntentsNativeServicesEnabled); | |
| 128 test_file_ = CreateTestFile(); | |
| 129 } | |
| 130 | |
| 131 virtual void SetUpOnMainThread() OVERRIDE { | |
| 132 dispatcher_.reset(new TestIntentsDispatcher()); | |
| 133 factory_.reset(new TestSelectFileDialogFactory()); | |
| 134 ui::SelectFileDialog::SetFactory(factory_.get()); | |
| 135 } | |
| 136 | |
| 137 void DispatchIntent(bool should_succeed) { | |
| 138 factory_->should_succeed_ = should_succeed; | |
| 139 | |
| 140 content::WebContents* tab = | |
| 141 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 142 | |
| 143 web_intents::NativeServiceFactory factory; | |
| 144 GURL url(web_intents::kNativeFilePickerUrl); | |
| 145 scoped_ptr<web_intents::IntentServiceHost> service( | |
| 146 factory.CreateServiceInstance(url, dispatcher_->intent_, tab)); | |
| 147 service->HandleIntent(dispatcher_.get()); | |
| 148 | |
| 149 // Reads of file size are done on the FILE thread, then posted | |
| 150 // back to the UI thread. So these are necessary for the | |
| 151 // should_succeed case and noop for the !should_succeed | |
| 152 content::RunAllPendingInMessageLoop(BrowserThread::FILE); | |
| 153 content::RunAllPendingInMessageLoop(BrowserThread::UI); | |
| 154 } | |
| 155 | |
| 156 scoped_ptr<TestIntentsDispatcher> dispatcher_; | |
| 157 scoped_ptr<TestSelectFileDialogFactory> factory_; | |
| 158 base::FilePath test_file_; | |
| 159 }; | |
| 160 | |
| 161 IN_PROC_BROWSER_TEST_F(NativeServicesBrowserTest, PickFileSelected) { | |
| 162 DispatchIntent(true); | |
| 163 ASSERT_TRUE(dispatcher_->reply_); | |
| 164 EXPECT_EQ( | |
| 165 webkit_glue::WebIntentReply( | |
| 166 webkit_glue::WEB_INTENT_REPLY_SUCCESS, | |
| 167 test_file_, | |
| 168 kTestFileSize), | |
| 169 *dispatcher_->reply_.get()); | |
| 170 } | |
| 171 | |
| 172 IN_PROC_BROWSER_TEST_F(NativeServicesBrowserTest, PickFileCancelled) { | |
| 173 DispatchIntent(false); | |
| 174 ASSERT_TRUE(dispatcher_->reply_); | |
| 175 EXPECT_EQ( | |
| 176 webkit_glue::WebIntentReply( | |
| 177 webkit_glue::WEB_INTENT_REPLY_FAILURE, | |
| 178 string16()), | |
| 179 *dispatcher_->reply_.get()); | |
| 180 } | |
| 181 | |
| 182 } // namespace | |
| OLD | NEW |