1 // Copyright 2012 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.
5 #import <Foundation/Foundation.h>
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "base/strings/sys_string_conversions.h"
13 #import "ios/net/clients/crn_network_client_protocol.h"
14 #import "ios/web/net/clients/crw_passkit_delegate.h"
15 #import "ios/web/net/clients/crw_passkit_network_client.h"
16 #include "ios/web/net/request_tracker_impl.h"
17 #include "ios/web/public/test/test_browser_state.h"
18 #include "ios/web/public/test/test_web_thread_bundle.h"
19 #include "net/base/net_errors.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #import "third_party/ocmock/OCMock/OCMock.h"
23 #import "third_party/ocmock/gtest_support.h"
27 class CRWPassKitNetworkClientTest : public testing::Test {
29 CRWPassKitNetworkClientTest() {}
31 void SetUp() override {
32 // The mock passkit delegate will be called when the Pass is loaded.
33 mockCRWPassKitDelegate_.reset([[OCMockObject
34 niceMockForProtocol:@protocol(CRWPassKitDelegate)] retain]);
36 // The request tracker needs to be set up with a unique tab id.
37 // TODO(marq): This can just be a mock or stub; it doesn't need to be a full
39 static int gcount = 0;
40 request_group_id_.reset(
41 [[NSString stringWithFormat:@"passkittest%d", gcount++] retain]);
42 request_tracker_ = web::RequestTrackerImpl::CreateTrackerForRequestGroupID(
45 browser_state_.GetRequestContext(),
46 nil /* CRWRequestTrackerDelegate */);
48 // Set up mock original UIWebView proxy.
49 OCMockObject* mockProxy_ = [[OCMockObject
50 niceMockForProtocol:@protocol(CRNNetworkClientProtocol)] retain];
51 mockWebProxy_.reset(mockProxy_);
53 // Link all the mock objects into the PassKit network client.
54 passkit_client_.reset([[CRWPassKitNetworkClient alloc]
55 initWithTracker:request_tracker_->GetWeakPtr()
56 delegate:static_cast<id<CRWPassKitDelegate> >(
57 mockCRWPassKitDelegate_.get())]);
59 setUnderlyingClient:(id<CRNNetworkClientProtocol>)mockWebProxy_];
62 void TearDown() override { request_tracker_->Close(); }
65 web::TestWebThreadBundle thread_bundle_;
66 base::scoped_nsobject<CRWPassKitNetworkClient> passkit_client_;
67 scoped_refptr<web::RequestTrackerImpl> request_tracker_;
68 // Holds a mock CRNNetworkClientProtocol object.
69 base::scoped_nsobject<OCMockObject> mockWebProxy_;
70 // Holds a mock CRWPassKitDelegate object.
71 base::scoped_nsobject<OCMockObject> mockCRWPassKitDelegate_;
72 base::scoped_nsobject<NSString> request_group_id_;
73 web::TestBrowserState browser_state_;
78 TEST_F(CRWPassKitNetworkClientTest, GoodPassKitObjectHandled) {
79 // This error is expected to be passed to the web proxy at the end of the
80 // PassKit loading sequence, successful or not.
81 [[mockWebProxy_ expect] didFailWithNSErrorCode:NSURLErrorCancelled
82 netErrorCode:net::ERR_ABORTED];
84 // Set the data in PassKitProtocolHandler to point to a good PassKit object.
85 base::FilePath pass_path;
86 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &pass_path));
87 pass_path = pass_path.AppendASCII("ios/web/test/data/testpass.pkpass");
88 NSData* passKitObject = [NSData dataWithContentsOfFile:
89 base::SysUTF8ToNSString(pass_path.value())];
90 EXPECT_TRUE(passKitObject);
92 // Load the PassKit object and trigger loading finished methods.
93 [passkit_client_ didLoadData:passKitObject];
94 [passkit_client_ didFinishLoading];
96 EXPECT_OCMOCK_VERIFY(mockWebProxy_);
99 TEST_F(CRWPassKitNetworkClientTest, BadPassKitObjectHandled) {
100 // This error is expected to be passed to the web proxy at the end of the
101 // PassKit loading sequence, successful or not.
102 [[mockWebProxy_ expect] didFailWithNSErrorCode:NSURLErrorCancelled
103 netErrorCode:net::ERR_ABORTED];
105 // Set the data in PassKitProtocolHandler to point to a bad PassKit object.
106 base::FilePath pass_path;
107 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &pass_path));
108 pass_path = pass_path.AppendASCII("ios/web/test/data/testbadpass.pkpass");
109 NSData* passKitObject = [NSData dataWithContentsOfFile:
110 base::SysUTF8ToNSString(pass_path.value())];
111 EXPECT_TRUE(passKitObject);
113 // Load the PassKit object and trigger error method.
114 [passkit_client_ didLoadData:passKitObject];
115 [passkit_client_ didFailWithNSErrorCode:NSURLErrorCancelled
116 netErrorCode:net::ERR_ABORTED];
118 EXPECT_OCMOCK_VERIFY(mockWebProxy_);