OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #import "ios/chrome/browser/ui/downloads/download_manager_controller.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include <memory> |
| 10 |
| 11 #import "base/mac/scoped_nsobject.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #import "ios/chrome/browser/storekit_launcher.h" |
| 14 #include "ios/web/public/test/test_web_thread.h" |
| 15 #include "net/base/net_errors.h" |
| 16 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gtest_mac.h" |
| 21 #include "testing/platform_test.h" |
| 22 |
| 23 using net::HttpResponseHeaders; |
| 24 using net::URLRequestStatus; |
| 25 |
| 26 @interface DownloadManagerController (ExposedForTesting) |
| 27 - (UIView*)documentContainer; |
| 28 - (UIView*)progressBar; |
| 29 - (UIImageView*)documentIcon; |
| 30 - (UIImageView*)foldIcon; |
| 31 - (UILabel*)timeLeftLabel; |
| 32 - (UILabel*)fileTypeLabel; |
| 33 - (UILabel*)fileNameLabel; |
| 34 - (UILabel*)errorOrSizeLabel; |
| 35 - (UIImageView*)errorIcon; |
| 36 - (UIView*)actionBar; |
| 37 - (UIButton*)downloadButton; |
| 38 - (UIButton*)cancelButton; |
| 39 - (UIButton*)openInButton; |
| 40 - (UIButton*)googleDriveButton; |
| 41 - (long long)totalFileSize; |
| 42 @end |
| 43 |
| 44 @interface TestStoreKitLauncher : NSObject<StoreKitLauncher> |
| 45 @end |
| 46 |
| 47 @implementation TestStoreKitLauncher |
| 48 - (void)openAppStore:(NSString*)appId { |
| 49 } |
| 50 @end |
| 51 |
| 52 namespace { |
| 53 |
| 54 const GURL kTestURL = GURL("http://www.example.com/test_download_file.txt"); |
| 55 |
| 56 class DownloadManagerControllerTest : public PlatformTest { |
| 57 public: |
| 58 DownloadManagerControllerTest() |
| 59 : _message_loop(base::MessageLoop::TYPE_UI), |
| 60 _ui_thread(web::WebThread::UI, &_message_loop) {} |
| 61 |
| 62 protected: |
| 63 void SetUp() override { |
| 64 PlatformTest::SetUp(); |
| 65 |
| 66 _request_context_getter = |
| 67 new net::TestURLRequestContextGetter(_message_loop.task_runner()); |
| 68 |
| 69 _fetcher_factory.reset(new net::TestURLFetcherFactory()); |
| 70 |
| 71 _store_kit_launcher.reset([[TestStoreKitLauncher alloc] init]); |
| 72 |
| 73 _controller.reset([[DownloadManagerController alloc] |
| 74 initWithURL:kTestURL |
| 75 requestContextGetter:_request_context_getter.get() |
| 76 storeKitLauncher:_store_kit_launcher.get()]); |
| 77 } |
| 78 |
| 79 base::MessageLoop _message_loop; |
| 80 web::TestWebThread _ui_thread; |
| 81 base::scoped_nsobject<TestStoreKitLauncher> _store_kit_launcher; |
| 82 scoped_refptr<net::TestURLRequestContextGetter> _request_context_getter; |
| 83 std::unique_ptr<net::TestURLFetcherFactory> _fetcher_factory; |
| 84 base::scoped_nsobject<DownloadManagerController> _controller; |
| 85 }; |
| 86 |
| 87 TEST_F(DownloadManagerControllerTest, TestXibViewConnections) { |
| 88 EXPECT_TRUE([_controller documentContainer]); |
| 89 EXPECT_TRUE([_controller progressBar]); |
| 90 EXPECT_TRUE([_controller documentIcon]); |
| 91 EXPECT_TRUE([_controller foldIcon]); |
| 92 EXPECT_TRUE([_controller timeLeftLabel]); |
| 93 EXPECT_TRUE([_controller fileTypeLabel]); |
| 94 EXPECT_TRUE([_controller fileNameLabel]); |
| 95 EXPECT_TRUE([_controller errorOrSizeLabel]); |
| 96 EXPECT_TRUE([_controller errorIcon]); |
| 97 EXPECT_TRUE([_controller actionBar]); |
| 98 EXPECT_TRUE([_controller downloadButton]); |
| 99 EXPECT_TRUE([_controller cancelButton]); |
| 100 EXPECT_TRUE([_controller openInButton]); |
| 101 EXPECT_TRUE([_controller googleDriveButton]); |
| 102 } |
| 103 |
| 104 TEST_F(DownloadManagerControllerTest, TestStart) { |
| 105 [_controller start]; |
| 106 EXPECT_TRUE( |
| 107 [[UIApplication sharedApplication] isNetworkActivityIndicatorVisible]); |
| 108 net::TestURLFetcher* fetcher = _fetcher_factory->GetFetcherByID(0); |
| 109 EXPECT_TRUE(fetcher != NULL); |
| 110 EXPECT_EQ(kTestURL, fetcher->GetOriginalURL()); |
| 111 } |
| 112 |
| 113 TEST_F(DownloadManagerControllerTest, TestOnHeadFetchCompleteSuccess) { |
| 114 [_controller start]; |
| 115 net::TestURLFetcher* fetcher = _fetcher_factory->GetFetcherByID(0); |
| 116 |
| 117 URLRequestStatus success_status(URLRequestStatus::SUCCESS, 0); |
| 118 fetcher->set_status(success_status); |
| 119 fetcher->set_response_code(200); |
| 120 scoped_refptr<HttpResponseHeaders> headers; |
| 121 headers = new HttpResponseHeaders("HTTP/1.x 200 OK\0"); |
| 122 headers->AddHeader("Content-Length: 1000000000"); // ~1GB |
| 123 fetcher->set_response_headers(headers); |
| 124 |
| 125 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 126 EXPECT_FALSE( |
| 127 [[UIApplication sharedApplication] isNetworkActivityIndicatorVisible]); |
| 128 |
| 129 EXPECT_EQ(1000000000, [_controller totalFileSize]); |
| 130 NSString* fileSizeText = [NSByteCountFormatter |
| 131 stringFromByteCount:1000000000 |
| 132 countStyle:NSByteCountFormatterCountStyleFile]; |
| 133 EXPECT_NSEQ(fileSizeText, [_controller errorOrSizeLabel].text); |
| 134 EXPECT_FALSE([_controller downloadButton].hidden); |
| 135 } |
| 136 |
| 137 TEST_F(DownloadManagerControllerTest, TestOnHeadFetchCompleteFailure) { |
| 138 [_controller start]; |
| 139 net::TestURLFetcher* fetcher = _fetcher_factory->GetFetcherByID(0); |
| 140 |
| 141 URLRequestStatus failure_status(URLRequestStatus::FAILED, |
| 142 net::ERR_DNS_TIMED_OUT); |
| 143 fetcher->set_status(failure_status); |
| 144 |
| 145 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 146 EXPECT_FALSE( |
| 147 [[UIApplication sharedApplication] isNetworkActivityIndicatorVisible]); |
| 148 |
| 149 EXPECT_TRUE([_controller fileTypeLabel].hidden); |
| 150 EXPECT_FALSE([_controller downloadButton].hidden); |
| 151 EXPECT_FALSE([_controller errorIcon].hidden); |
| 152 EXPECT_FALSE([_controller errorOrSizeLabel].hidden); |
| 153 } |
| 154 |
| 155 } // namespace |
OLD | NEW |