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