15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/download/download_test_file_activity_observer.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h" 89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/download/chrome_download_manager_delegate.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/download/download_service.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/download/download_service_factory.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content { 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadItem; 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test ChromeDownloadManagerDelegate that controls whether how file chooser 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// dialogs are handled, and how files are opend. 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// By default, file chooser dialogs are disabled. 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DownloadTestFileActivityObserver::MockDownloadManagerDelegate 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : public ChromeDownloadManagerDelegate { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) explicit MockDownloadManagerDelegate(Profile* profile) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : ChromeDownloadManagerDelegate(profile), 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_chooser_enabled_(false), 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) file_chooser_displayed_(false), 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) weak_ptr_factory_(this) { 297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!profile->IsOffTheRecord()) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GetDownloadIdReceiverCallback().Run( 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) content::DownloadItem::kInvalidId + 1); 327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual ~MockDownloadManagerDelegate() {} 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void EnableFileChooser(bool enable) { 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_chooser_enabled_ = enable; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool TestAndResetDidShowFileChooser() { 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool did_show = file_chooser_displayed_; 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_chooser_displayed_ = false; 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return did_show; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() { 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return weak_ptr_factory_.GetWeakPtr(); 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual void PromptUserForDownloadPath(content::DownloadItem* item, 53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const base::FilePath& suggested_path, 54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const FileSelectedCallback& 55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) callback) OVERRIDE { 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_chooser_displayed_ = true; 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::MessageLoop::current()->PostTask( 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : base::FilePath()))); 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {} 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool file_chooser_enabled_; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool file_chooser_displayed_; 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_; 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* profile) { 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_ptr<MockDownloadManagerDelegate> mock_delegate( 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) new MockDownloadManagerDelegate(profile)); 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) test_delegate_ = mock_delegate->GetWeakPtr(); 75a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) DownloadServiceFactory::GetForBrowserContext(profile)-> 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SetDownloadManagerDelegateForTesting( 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) mock_delegate.PassAs<ChromeDownloadManagerDelegate>()); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (test_delegate_.get()) 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) test_delegate_->EnableFileChooser(enable); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return test_delegate_.get() && 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) test_delegate_->TestAndResetDidShowFileChooser(); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 92