diff options
author | gchatz <gchatz@chromium.org> | 2015-11-20 13:45:36 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-11-20 21:46:33 +0000 |
commit | 1cf3f2fab172f2362d6ecf7a944498de2c275a6a (patch) | |
tree | 24c22f361dd5fa4c75c820293e82c91ce2f6cf0b /ios/web/web_state/crw_pass_kit_downloader_unittest.mm | |
parent | 1e37d76d682ec823d16163e9129999f714f41e0b (diff) | |
download | chromium_src-1cf3f2fab172f2362d6ecf7a944498de2c275a6a.zip chromium_src-1cf3f2fab172f2362d6ecf7a944498de2c275a6a.tar.gz chromium_src-1cf3f2fab172f2362d6ecf7a944498de2c275a6a.tar.bz2 |
Add PassKit support for WKWebView.
UIWebView PassKit support was dealt with in the network stack. Since
the net stack is not available in WKWebView, we need to build in
support in the web layer in order for PassKit to work.
BUG=541730
Review URL: https://codereview.chromium.org/1453943002
Cr-Commit-Position: refs/heads/master@{#360921}
Diffstat (limited to 'ios/web/web_state/crw_pass_kit_downloader_unittest.mm')
-rw-r--r-- | ios/web/web_state/crw_pass_kit_downloader_unittest.mm | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/ios/web/web_state/crw_pass_kit_downloader_unittest.mm b/ios/web/web_state/crw_pass_kit_downloader_unittest.mm new file mode 100644 index 0000000..c65b238 --- /dev/null +++ b/ios/web/web_state/crw_pass_kit_downloader_unittest.mm @@ -0,0 +1,156 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/web/web_state/crw_pass_kit_downloader.h" + +#import <Foundation/Foundation.h> + +#import "base/mac/scoped_nsobject.h" +#include "base/memory/scoped_ptr.h" +#import "ios/web/test/web_test.h" +#include "net/base/net_errors.h" +#include "net/http/http_response_headers.h" +#include "net/url_request/test_url_fetcher_factory.h" +#include "net/url_request/url_fetcher_delegate.h" +#include "net/url_request/url_request_test_util.h" +#include "testing/gtest_mac.h" +#include "url/gurl.h" + +using net::HttpResponseHeaders; +using net::URLRequestStatus; + +namespace web { + +// Constant URL to use in tests. +const char kTestUrlString[] = "http://www.example.com"; + +const char kPassKitMimeType[] = "application/vnd.apple.pkpass"; + +// Constant string which is the expected response from the PassKit Downloader. +const char kExpectedString[] = "test string"; + +// Test fixture for testing CRWPassKitDownloader. +class CRWPassKitDownloaderTest : public WebTest { + protected: + void SetUp() override { + WebTest::SetUp(); + completion_handler_success_ = false; + fetcher_factory_.reset(new net::TestURLFetcherFactory()); + downloader_.reset([[CRWPassKitDownloader alloc] + initWithContextGetter:GetBrowserState()->GetRequestContext() + completionHandler:^(NSData* data) { + NSData* expected_data = + [NSData dataWithBytes:kExpectedString + length:strlen(kExpectedString)]; + completion_handler_success_ = [data isEqualToData:expected_data]; + }]); + } + + // Sets up |fetcher|'s request status, HTTP response code, HTTP headers, and + // MIME type. + void SetUpFetcher(net::TestURLFetcher* fetcher, + URLRequestStatus status, + int response_code, + const std::string& mime_type) { + fetcher->set_status(status); + fetcher->set_response_code(response_code); + scoped_refptr<HttpResponseHeaders> headers; + headers = new HttpResponseHeaders("HTTP/1.x 200 OK\0"); + headers->AddHeader("Content-Type: " + mime_type); + fetcher->set_response_headers(headers); + } + + // Test fetcher factory from which we access and control the URLFetcher + // used in CRWPassKitDownloader. + scoped_ptr<net::TestURLFetcherFactory> fetcher_factory_; + + // The CRWPassKitDownloader that is being tested. + base::scoped_nsobject<CRWPassKitDownloader> downloader_; + + // Indicates whether or not the downloader successfully downloaded data. It is + // set from the completion handler based on whether actual data is equal to + // expected data. + bool completion_handler_success_; +}; + +// Tests case where CRWPassKitDownloader successfully downloads data. +TEST_F(CRWPassKitDownloaderTest, TestDownloadPassKitSuccess) { + GURL test_url(kTestUrlString); + [downloader_ downloadPassKitFileWithURL:test_url]; + + UIApplication* shared_app = [UIApplication sharedApplication]; + EXPECT_TRUE([shared_app isNetworkActivityIndicatorVisible]); + + net::TestURLFetcher* fetcher = fetcher_factory_->GetFetcherByID(0); + ASSERT_TRUE(fetcher); + ASSERT_EQ(test_url, fetcher->GetOriginalURL()); + SetUpFetcher(fetcher, URLRequestStatus(), 200, kPassKitMimeType); + fetcher->SetResponseString(kExpectedString); + fetcher->delegate()->OnURLFetchComplete(fetcher); + + EXPECT_FALSE([shared_app isNetworkActivityIndicatorVisible]); + EXPECT_TRUE(completion_handler_success_); +} + +// Tests case where CRWPassKitDownloader fails download due to a bad HTTP +// response code. |completion_handler_success_| should be false. +TEST_F(CRWPassKitDownloaderTest, TestDownloadPassKitBadErrorCodeFailure) { + GURL test_url(kTestUrlString); + [downloader_ downloadPassKitFileWithURL:test_url]; + + UIApplication* shared_app = [UIApplication sharedApplication]; + EXPECT_TRUE([shared_app isNetworkActivityIndicatorVisible]); + + net::TestURLFetcher* fetcher = fetcher_factory_->GetFetcherByID(0); + ASSERT_TRUE(fetcher); + ASSERT_EQ(test_url, fetcher->GetOriginalURL()); + SetUpFetcher(fetcher, URLRequestStatus(), 403, kPassKitMimeType); + fetcher->SetResponseString(kExpectedString); + fetcher->delegate()->OnURLFetchComplete(fetcher); + + EXPECT_FALSE([shared_app isNetworkActivityIndicatorVisible]); + EXPECT_FALSE(completion_handler_success_); +} + +// Tests case where CRWPassKitDownloader fails download with a status set to +// failure. |completion_handler_success_| should be false. +TEST_F(CRWPassKitDownloaderTest, TestDownloadPassKitStatusFailedFailure) { + GURL test_url(kTestUrlString); + [downloader_ downloadPassKitFileWithURL:test_url]; + + UIApplication* shared_app = [UIApplication sharedApplication]; + EXPECT_TRUE([shared_app isNetworkActivityIndicatorVisible]); + + net::TestURLFetcher* fetcher = fetcher_factory_->GetFetcherByID(0); + ASSERT_TRUE(fetcher); + ASSERT_EQ(test_url, fetcher->GetOriginalURL()); + SetUpFetcher(fetcher, URLRequestStatus::FromError(net::ERR_ACCESS_DENIED), + 200, kPassKitMimeType); + fetcher->SetResponseString(kExpectedString); + fetcher->delegate()->OnURLFetchComplete(fetcher); + + EXPECT_FALSE([shared_app isNetworkActivityIndicatorVisible]); + EXPECT_FALSE(completion_handler_success_); +} + +// Tests case where CRWPassKitDownloader fails download with no response data. +// |completion_handler_success_| should be false. +TEST_F(CRWPassKitDownloaderTest, TestDownloadPassKitNoResponseFailure) { + GURL test_url(kTestUrlString); + [downloader_ downloadPassKitFileWithURL:test_url]; + + UIApplication* shared_app = [UIApplication sharedApplication]; + EXPECT_TRUE([shared_app isNetworkActivityIndicatorVisible]); + + net::TestURLFetcher* fetcher = fetcher_factory_->GetFetcherByID(0); + ASSERT_TRUE(fetcher); + ASSERT_EQ(test_url, fetcher->GetOriginalURL()); + SetUpFetcher(fetcher, URLRequestStatus(), 200, kPassKitMimeType); + fetcher->delegate()->OnURLFetchComplete(fetcher); + + EXPECT_FALSE([shared_app isNetworkActivityIndicatorVisible]); + EXPECT_FALSE(completion_handler_success_); +} + +} // namespace |