Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: chrome/browser/extensions/api/downloads/downloads_api_unittest.cc

Issue 11416382: ********** Content tests with blob hacking. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | content/content_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 30 matching lines...) Expand all
41 #include "content/public/test/test_file_error_injector.h" 41 #include "content/public/test/test_file_error_injector.h"
42 #include "content/test/net/url_request_slow_download_job.h" 42 #include "content/test/net/url_request_slow_download_job.h"
43 #include "net/base/data_url.h" 43 #include "net/base/data_url.h"
44 #include "net/base/net_util.h" 44 #include "net/base/net_util.h"
45 #include "net/url_request/url_request.h" 45 #include "net/url_request/url_request.h"
46 #include "net/url_request/url_request_context.h" 46 #include "net/url_request/url_request_context.h"
47 #include "net/url_request/url_request_job.h" 47 #include "net/url_request/url_request_job.h"
48 #include "net/url_request/url_request_job_factory.h" 48 #include "net/url_request/url_request_job_factory.h"
49 #include "net/url_request/url_request_job_factory_impl.h" 49 #include "net/url_request/url_request_job_factory_impl.h"
50 #include "webkit/blob/blob_data.h" 50 #include "webkit/blob/blob_data.h"
51 #include "webkit/blob/blob_storage_controller.h" 51 #include "webkit/blob/blob_storage_context.h"
52 #include "webkit/blob/blob_url_request_job.h" 52 #include "webkit/blob/blob_url_request_job.h"
53 #include "webkit/fileapi/file_system_context.h" 53 #include "webkit/fileapi/file_system_context.h"
54 #include "webkit/fileapi/file_system_operation.h" 54 #include "webkit/fileapi/file_system_operation.h"
55 #include "webkit/fileapi/file_system_url.h" 55 #include "webkit/fileapi/file_system_url.h"
56 56
57 using content::BrowserContext; 57 using content::BrowserContext;
58 using content::BrowserThread; 58 using content::BrowserThread;
59 using content::DownloadItem; 59 using content::DownloadItem;
60 using content::DownloadManager; 60 using content::DownloadManager;
61 using content::URLRequestSlowDownloadJob; 61 using content::URLRequestSlowDownloadJob;
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 (*item), base::Bind(&ItemNotInProgress)); 642 (*item), base::Bind(&ItemNotInProgress));
643 observer.WaitForEvent(); 643 observer.WaitForEvent();
644 } 644 }
645 } 645 }
646 646
647 private: 647 private:
648 DownloadManager::DownloadVector* items_; 648 DownloadManager::DownloadVector* items_;
649 DISALLOW_COPY_AND_ASSIGN(ScopedItemVectorCanceller); 649 DISALLOW_COPY_AND_ASSIGN(ScopedItemVectorCanceller);
650 }; 650 };
651 651
652 // TODO: fixme? (delete me)
653 #if 0
652 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { 654 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
653 public: 655 public:
654 explicit TestProtocolHandler( 656 explicit TestProtocolHandler(
655 webkit_blob::BlobStorageController* blob_storage_controller, 657 webkit_blob::BlobStorageContext* blob_storage_context,
656 fileapi::FileSystemContext* file_system_context) 658 fileapi::FileSystemContext* file_system_context)
657 : blob_storage_controller_(blob_storage_controller), 659 : blob_storage_context_(blob_storage_context),
658 file_system_context_(file_system_context) {} 660 file_system_context_(file_system_context) {}
659 661
660 virtual ~TestProtocolHandler() {} 662 virtual ~TestProtocolHandler() {}
661 663
662 virtual net::URLRequestJob* MaybeCreateJob( 664 virtual net::URLRequestJob* MaybeCreateJob(
663 net::URLRequest* request, 665 net::URLRequest* request,
664 net::NetworkDelegate* network_delegate) const OVERRIDE { 666 net::NetworkDelegate* network_delegate) const OVERRIDE {
665 return new webkit_blob::BlobURLRequestJob( 667 return new webkit_blob::BlobURLRequestJob(
666 request, 668 request,
667 network_delegate, 669 network_delegate,
668 blob_storage_controller_->GetBlobDataFromUrl(request->url()), 670 blob_storage_context_->GetBlobDataFromUrl(request->url()),
669 file_system_context_, 671 file_system_context_,
670 base::MessageLoopProxy::current()); 672 base::MessageLoopProxy::current());
671 } 673 }
672 674
673 private: 675 private:
674 webkit_blob::BlobStorageController* const blob_storage_controller_; 676 webkit_blob::BlobStorageContext* const blob_storage_context_;
675 fileapi::FileSystemContext* const file_system_context_; 677 fileapi::FileSystemContext* const file_system_context_;
676 678
677 DISALLOW_COPY_AND_ASSIGN(TestProtocolHandler); 679 DISALLOW_COPY_AND_ASSIGN(TestProtocolHandler);
678 }; 680 };
679 681
680 class TestURLRequestContext : public net::URLRequestContext { 682 class TestURLRequestContext : public net::URLRequestContext {
681 public: 683 public:
682 explicit TestURLRequestContext( 684 explicit TestURLRequestContext(
683 fileapi::FileSystemContext* file_system_context) 685 fileapi::FileSystemContext* file_system_context)
684 : blob_storage_controller_(new webkit_blob::BlobStorageController) { 686 : blob_storage_context_(new webkit_blob::BlobStorageContext) {
685 // Job factory owns the protocol handler. 687 // Job factory owns the protocol handler.
686 job_factory_.SetProtocolHandler( 688 job_factory_.SetProtocolHandler(
687 "blob", new TestProtocolHandler(blob_storage_controller_.get(), 689 "blob", new TestProtocolHandler(blob_storage_context_.get(),
688 file_system_context)); 690 file_system_context));
689 set_job_factory(&job_factory_); 691 set_job_factory(&job_factory_);
690 } 692 }
691 693
692 virtual ~TestURLRequestContext() {} 694 virtual ~TestURLRequestContext() {}
693 695
694 webkit_blob::BlobStorageController* blob_storage_controller() const { 696 webkit_blob::BlobStorageContext* blob_storage_context() const {
695 return blob_storage_controller_.get(); 697 return blob_storage_context_.get();
696 } 698 }
697 699
698 private: 700 private:
699 net::URLRequestJobFactoryImpl job_factory_; 701 net::URLRequestJobFactoryImpl job_factory_;
700 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; 702 scoped_ptr<webkit_blob::BlobStorageContext> blob_storage_context_;
701 703
702 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext); 704 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext);
703 }; 705 };
704 706
705 // Writes an HTML5 file so that it can be downloaded. 707 // Writes an HTML5 file so that it can be downloaded.
708 //
709 // TODO(michaeln): Wow, lots of complexity to create a filesystem file as a
710 // precondition for the DownloadExtensionTest_Download_FileSystemURL.
711 // Some alternatives come to mind:
712 // - load an html page that will produce the file
713 // - more directly write and create the file using lower level
714 // fileapi::FileSystemFileUtilProxy to create the dir entry
715 // and write directly into the platform file path using base file_utils.
706 class HTML5FileWriter { 716 class HTML5FileWriter {
707 public: 717 public:
708 HTML5FileWriter( 718 HTML5FileWriter(
709 Profile* profile, 719 Profile* profile,
710 const std::string& filename, 720 const std::string& filename,
711 const std::string& origin, 721 const std::string& origin,
712 DownloadsEventsListener* events_listener, 722 DownloadsEventsListener* events_listener,
713 const std::string& payload) 723 const std::string& payload)
714 : profile_(profile), 724 : profile_(profile),
715 filename_(filename), 725 filename_(filename),
716 origin_(origin), 726 origin_(origin),
717 events_listener_(events_listener), 727 events_listener_(events_listener),
718 blob_data_(new webkit_blob::BlobData()), 728 blob_data_(new webkit_blob::BlobData()),
719 payload_(payload), 729 payload_(payload),
720 fs_(BrowserContext::GetDefaultStoragePartition(profile_)-> 730 fs_(BrowserContext::GetDefaultStoragePartition(profile_)->
721 GetFileSystemContext()) { 731 GetFileSystemContext()) {
722 CHECK(profile_); 732 CHECK(profile_);
723 CHECK(events_listener_); 733 CHECK(events_listener_);
724 CHECK(fs_); 734 CHECK(fs_);
725 } 735 }
726 736
727 ~HTML5FileWriter() { 737 ~HTML5FileWriter() {
728 CHECK(BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( 738 CHECK(BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
729 &HTML5FileWriter::TearDownURLRequestContext, base::Unretained(this)))); 739 &HTML5FileWriter::TearDownURLRequestContext, base::Unretained(this))));
730 events_listener_->WaitFor(profile_, kURLRequestContextToreDown, ""); 740 events_listener_->WaitFor(profile_, kURLRequestContextToreDown, "");
731 } 741 }
732 742
733 bool WriteFile() { 743 bool WriteFile() {
734 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 744 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
745 // TODO(michaeln): FSContext should not be used on the UI thread,
746 // this part of the sequence should also be invoked on the IO thread.
735 fs_->OpenFileSystem( 747 fs_->OpenFileSystem(
736 GURL(origin_), 748 GURL(origin_),
737 fileapi::kFileSystemTypeTemporary, 749 fileapi::kFileSystemTypeTemporary,
738 kCreateFileSystem, 750 kCreateFileSystem,
739 base::Bind(&HTML5FileWriter::OpenFileSystemCallback, 751 base::Bind(&HTML5FileWriter::OpenFileSystemCallback,
740 base::Unretained(this))); 752 base::Unretained(this)));
741 return events_listener_->WaitFor(profile_, kHTML5FileWritten, filename_); 753 return events_listener_->WaitFor(profile_, kHTML5FileWritten, filename_);
742 } 754 }
743 755
744 private: 756 private:
(...skipping 23 matching lines...) Expand all
768 operation()->CreateFile(fs_->CrackURL(GURL(root_ + filename_)), 780 operation()->CreateFile(fs_->CrackURL(GURL(root_ + filename_)),
769 kExclusive, base::Bind( 781 kExclusive, base::Bind(
770 &HTML5FileWriter::CreateFileCallback, base::Unretained(this))); 782 &HTML5FileWriter::CreateFileCallback, base::Unretained(this)));
771 } 783 }
772 784
773 void CreateFileCallback(base::PlatformFileError result) { 785 void CreateFileCallback(base::PlatformFileError result) {
774 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 786 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
775 CHECK_EQ(base::PLATFORM_FILE_OK, result); 787 CHECK_EQ(base::PLATFORM_FILE_OK, result);
776 blob_data_->AppendData(payload_); 788 blob_data_->AppendData(payload_);
777 url_request_context_.reset(new TestURLRequestContext(fs_)); 789 url_request_context_.reset(new TestURLRequestContext(fs_));
778 url_request_context_->blob_storage_controller()->AddFinishedBlob( 790 url_request_context_->blob_storage_context()->AddFinishedBlob(
779 blob_url(), blob_data_); 791 blob_url(), blob_data_);
780 operation()->Write( 792 operation()->Write(
781 url_request_context_.get(), 793 url_request_context_.get(),
782 fs_->CrackURL(GURL(root_ + filename_)), 794 fs_->CrackURL(GURL(root_ + filename_)),
783 blob_url(), 795 blob_url(),
784 0, // offset 796 0, // offset
785 base::Bind(&HTML5FileWriter::WriteCallback, base::Unretained(this))); 797 base::Bind(&HTML5FileWriter::WriteCallback, base::Unretained(this)));
786 } 798 }
787 799
788 void WriteCallback( 800 void WriteCallback(
(...skipping 13 matching lines...) Expand all
802 notification_source.profile = profile_; 814 notification_source.profile = profile_;
803 content::NotificationService::current()->Notify( 815 content::NotificationService::current()->Notify(
804 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, 816 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT,
805 content::Source<DownloadsEventsListener::DownloadsNotificationSource>( 817 content::Source<DownloadsEventsListener::DownloadsNotificationSource>(
806 &notification_source), 818 &notification_source),
807 content::Details<std::string>(&filename_)); 819 content::Details<std::string>(&filename_));
808 } 820 }
809 821
810 void TearDownURLRequestContext() { 822 void TearDownURLRequestContext() {
811 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 823 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
812 url_request_context_->blob_storage_controller()->RemoveBlob(blob_url()); 824 url_request_context_->blob_storage_context()->RemoveBlob(blob_url());
813 url_request_context_.reset(); 825 url_request_context_.reset();
814 CHECK(BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( 826 CHECK(BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
815 &HTML5FileWriter::NotifyURLRequestContextToreDown, 827 &HTML5FileWriter::NotifyURLRequestContextToreDown,
816 base::Unretained(this)))); 828 base::Unretained(this))));
817 } 829 }
818 830
819 void NotifyURLRequestContextToreDown() { 831 void NotifyURLRequestContextToreDown() {
820 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 832 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
821 DownloadsEventsListener::DownloadsNotificationSource notification_source; 833 DownloadsEventsListener::DownloadsNotificationSource notification_source;
822 notification_source.event_name = kURLRequestContextToreDown; 834 notification_source.event_name = kURLRequestContextToreDown;
(...skipping 15 matching lines...) Expand all
838 std::string payload_; 850 std::string payload_;
839 scoped_ptr<TestURLRequestContext> url_request_context_; 851 scoped_ptr<TestURLRequestContext> url_request_context_;
840 fileapi::FileSystemContext* fs_; 852 fileapi::FileSystemContext* fs_;
841 853
842 DISALLOW_COPY_AND_ASSIGN(HTML5FileWriter); 854 DISALLOW_COPY_AND_ASSIGN(HTML5FileWriter);
843 }; 855 };
844 856
845 const char HTML5FileWriter::kHTML5FileWritten[] = "html5_file_written"; 857 const char HTML5FileWriter::kHTML5FileWritten[] = "html5_file_written";
846 const char HTML5FileWriter::kURLRequestContextToreDown[] = 858 const char HTML5FileWriter::kURLRequestContextToreDown[] =
847 "url_request_context_tore_down"; 859 "url_request_context_tore_down";
860 #endif
848 861
849 } // namespace 862 } // namespace
850 863
851 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 864 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
852 DownloadExtensionTest_Open) { 865 DownloadExtensionTest_Open) {
853 EXPECT_STREQ(download_extension_errors::kInvalidOperationError, 866 EXPECT_STREQ(download_extension_errors::kInvalidOperationError,
854 RunFunctionAndReturnError( 867 RunFunctionAndReturnError(
855 new DownloadsOpenFunction(), 868 new DownloadsOpenFunction(),
856 "[-42]").c_str()); 869 "[-42]").c_str());
857 870
(...skipping 1256 matching lines...) Expand 10 before | Expand all | Expand 10 after
2114 item->Cancel(true); 2127 item->Cancel(true);
2115 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 2128 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2116 base::StringPrintf("[{\"id\": %d," 2129 base::StringPrintf("[{\"id\": %d,"
2117 " \"error\": {\"current\": 40}," 2130 " \"error\": {\"current\": 40},"
2118 " \"state\": {" 2131 " \"state\": {"
2119 " \"previous\": \"in_progress\"," 2132 " \"previous\": \"in_progress\","
2120 " \"current\": \"interrupted\"}}]", 2133 " \"current\": \"interrupted\"}}]",
2121 result_id))); 2134 result_id)));
2122 } 2135 }
2123 2136
2137 // TODO: fixme
2138 #if 0
2124 // Test downloading filesystem: URLs. 2139 // Test downloading filesystem: URLs.
2125 // NOTE: chrome disallows creating HTML5 FileSystem Files in incognito. 2140 // NOTE: chrome disallows creating HTML5 FileSystem Files in incognito.
2126 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 2141 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
2127 DownloadExtensionTest_Download_FileSystemURL) { 2142 DownloadExtensionTest_Download_FileSystemURL) {
2128 static const char* kPayloadData = "on the record\ndata"; 2143 static const char* kPayloadData = "on the record\ndata";
2129 GoOnTheRecord(); 2144 GoOnTheRecord();
2130 LoadExtension("downloads_split"); 2145 LoadExtension("downloads_split");
2131 HTML5FileWriter html5_file_writer( 2146 HTML5FileWriter html5_file_writer(
2132 browser()->profile(), 2147 browser()->profile(),
2133 "on_record.txt", 2148 "on_record.txt",
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2166 " \"previous\": \"in_progress\"," 2181 " \"previous\": \"in_progress\","
2167 " \"current\": \"complete\"}}]", 2182 " \"current\": \"complete\"}}]",
2168 result_id, 2183 result_id,
2169 GetFilename("on_record.txt.crdownload").c_str(), 2184 GetFilename("on_record.txt.crdownload").c_str(),
2170 GetFilename("on_record.txt").c_str()))); 2185 GetFilename("on_record.txt").c_str())));
2171 std::string disk_data; 2186 std::string disk_data;
2172 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2187 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2173 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2188 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2174 } 2189 }
2175 2190
2191 #endif
2192
2176 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 2193 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
2177 DownloadExtensionTest_OnDeterminingFilename_NoChange) { 2194 DownloadExtensionTest_OnDeterminingFilename_NoChange) {
2178 GoOnTheRecord(); 2195 GoOnTheRecord();
2179 LoadExtension("downloads_split"); 2196 LoadExtension("downloads_split");
2180 AddFilenameDeterminer(); 2197 AddFilenameDeterminer();
2181 CHECK(StartTestServer()); 2198 CHECK(StartTestServer());
2182 std::string download_url = test_server()->GetURL("slow?0").spec(); 2199 std::string download_url = test_server()->GetURL("slow?0").spec();
2183 2200
2184 // Start downloading a file. 2201 // Start downloading a file.
2185 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 2202 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
(...skipping 1008 matching lines...) Expand 10 before | Expand all | Expand 10 after
3194 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 3211 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3195 base::StringPrintf("[{\"id\": %d," 3212 base::StringPrintf("[{\"id\": %d,"
3196 " \"state\": {" 3213 " \"state\": {"
3197 " \"previous\": \"in_progress\"," 3214 " \"previous\": \"in_progress\","
3198 " \"current\": \"complete\"}}]", 3215 " \"current\": \"complete\"}}]",
3199 result_id))); 3216 result_id)));
3200 } 3217 }
3201 3218
3202 // TODO(benjhayden) Figure out why DisableExtension() does not fire 3219 // TODO(benjhayden) Figure out why DisableExtension() does not fire
3203 // OnListenerRemoved. 3220 // OnListenerRemoved.
OLDNEW
« no previous file with comments | « no previous file | content/content_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698