| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "ios/web/web_state/crw_pass_kit_downloader.h" | 5 #import "ios/web/web_state/crw_pass_kit_downloader.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #import "base/mac/scoped_nsobject.h" | |
| 12 #include "ios/web/public/test/web_test.h" | 11 #include "ios/web/public/test/web_test.h" |
| 13 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 14 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 15 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 18 #import "testing/gtest_mac.h" | 17 #import "testing/gtest_mac.h" |
| 19 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 20 | 19 |
| 20 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 21 #error "This file requires ARC support." |
| 22 #endif |
| 23 |
| 21 using net::HttpResponseHeaders; | 24 using net::HttpResponseHeaders; |
| 22 using net::URLRequestStatus; | 25 using net::URLRequestStatus; |
| 23 | 26 |
| 24 namespace web { | 27 namespace web { |
| 25 | 28 |
| 26 // Constant URL to use in tests. | 29 // Constant URL to use in tests. |
| 27 const char kTestUrlString[] = "http://www.example.com"; | 30 const char kTestUrlString[] = "http://www.example.com"; |
| 28 | 31 |
| 29 const char kPassKitMimeType[] = "application/vnd.apple.pkpass"; | 32 const char kPassKitMimeType[] = "application/vnd.apple.pkpass"; |
| 30 | 33 |
| 31 // Constant string which is the expected response from the PassKit Downloader. | 34 // Constant string which is the expected response from the PassKit Downloader. |
| 32 const char kExpectedString[] = "test string"; | 35 const char kExpectedString[] = "test string"; |
| 33 | 36 |
| 34 // Test fixture for testing CRWPassKitDownloader. | 37 // Test fixture for testing CRWPassKitDownloader. |
| 35 class CRWPassKitDownloaderTest : public WebTest { | 38 class CRWPassKitDownloaderTest : public WebTest { |
| 36 protected: | 39 protected: |
| 37 void SetUp() override { | 40 void SetUp() override { |
| 38 WebTest::SetUp(); | 41 WebTest::SetUp(); |
| 39 completion_handler_success_ = false; | 42 completion_handler_success_ = false; |
| 40 fetcher_factory_.reset(new net::TestURLFetcherFactory()); | 43 fetcher_factory_.reset(new net::TestURLFetcherFactory()); |
| 41 downloader_.reset([[CRWPassKitDownloader alloc] | 44 downloader_ = [[CRWPassKitDownloader alloc] |
| 42 initWithContextGetter:GetBrowserState()->GetRequestContext() | 45 initWithContextGetter:GetBrowserState()->GetRequestContext() |
| 43 completionHandler:^(NSData* data) { | 46 completionHandler:^(NSData* data) { |
| 44 NSData* expected_data = | 47 NSData* expected_data = |
| 45 [NSData dataWithBytes:kExpectedString | 48 [NSData dataWithBytes:kExpectedString |
| 46 length:strlen(kExpectedString)]; | 49 length:strlen(kExpectedString)]; |
| 47 completion_handler_success_ = [data isEqualToData:expected_data]; | 50 completion_handler_success_ = [data isEqualToData:expected_data]; |
| 48 }]); | 51 }]; |
| 49 } | 52 } |
| 50 | 53 |
| 51 // Sets up |fetcher|'s request status, HTTP response code, HTTP headers, and | 54 // Sets up |fetcher|'s request status, HTTP response code, HTTP headers, and |
| 52 // MIME type. | 55 // MIME type. |
| 53 void SetUpFetcher(net::TestURLFetcher* fetcher, | 56 void SetUpFetcher(net::TestURLFetcher* fetcher, |
| 54 URLRequestStatus status, | 57 URLRequestStatus status, |
| 55 int response_code, | 58 int response_code, |
| 56 const std::string& mime_type) { | 59 const std::string& mime_type) { |
| 57 fetcher->set_status(status); | 60 fetcher->set_status(status); |
| 58 fetcher->set_response_code(response_code); | 61 fetcher->set_response_code(response_code); |
| 59 scoped_refptr<HttpResponseHeaders> headers; | 62 scoped_refptr<HttpResponseHeaders> headers; |
| 60 headers = new HttpResponseHeaders("HTTP/1.x 200 OK\0"); | 63 headers = new HttpResponseHeaders("HTTP/1.x 200 OK\0"); |
| 61 headers->AddHeader("Content-Type: " + mime_type); | 64 headers->AddHeader("Content-Type: " + mime_type); |
| 62 fetcher->set_response_headers(headers); | 65 fetcher->set_response_headers(headers); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // Test fetcher factory from which we access and control the URLFetcher | 68 // Test fetcher factory from which we access and control the URLFetcher |
| 66 // used in CRWPassKitDownloader. | 69 // used in CRWPassKitDownloader. |
| 67 std::unique_ptr<net::TestURLFetcherFactory> fetcher_factory_; | 70 std::unique_ptr<net::TestURLFetcherFactory> fetcher_factory_; |
| 68 | 71 |
| 69 // The CRWPassKitDownloader that is being tested. | 72 // The CRWPassKitDownloader that is being tested. |
| 70 base::scoped_nsobject<CRWPassKitDownloader> downloader_; | 73 CRWPassKitDownloader* downloader_; |
| 71 | 74 |
| 72 // Indicates whether or not the downloader successfully downloaded data. It is | 75 // Indicates whether or not the downloader successfully downloaded data. It is |
| 73 // set from the completion handler based on whether actual data is equal to | 76 // set from the completion handler based on whether actual data is equal to |
| 74 // expected data. | 77 // expected data. |
| 75 bool completion_handler_success_; | 78 bool completion_handler_success_; |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 // Tests case where CRWPassKitDownloader successfully downloads data. | 81 // Tests case where CRWPassKitDownloader successfully downloads data. |
| 79 TEST_F(CRWPassKitDownloaderTest, TestDownloadPassKitSuccess) { | 82 TEST_F(CRWPassKitDownloaderTest, TestDownloadPassKitSuccess) { |
| 80 GURL test_url(kTestUrlString); | 83 GURL test_url(kTestUrlString); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 net::TestURLFetcher* fetcher = fetcher_factory_->GetFetcherByID(0); | 135 net::TestURLFetcher* fetcher = fetcher_factory_->GetFetcherByID(0); |
| 133 ASSERT_TRUE(fetcher); | 136 ASSERT_TRUE(fetcher); |
| 134 ASSERT_EQ(test_url, fetcher->GetOriginalURL()); | 137 ASSERT_EQ(test_url, fetcher->GetOriginalURL()); |
| 135 SetUpFetcher(fetcher, URLRequestStatus(), 200, kPassKitMimeType); | 138 SetUpFetcher(fetcher, URLRequestStatus(), 200, kPassKitMimeType); |
| 136 fetcher->delegate()->OnURLFetchComplete(fetcher); | 139 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 137 | 140 |
| 138 EXPECT_FALSE(completion_handler_success_); | 141 EXPECT_FALSE(completion_handler_success_); |
| 139 } | 142 } |
| 140 | 143 |
| 141 } // namespace | 144 } // namespace |
| OLD | NEW |