Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / ios / web / net / clients / crw_passkit_network_client_unittest.mm
blobea71b632bd741c643d0bed3ee8829fa73a523e5a
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"
25 namespace {
27 class CRWPassKitNetworkClientTest : public testing::Test {
28  public:
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
38     // RT instance.
39     static int gcount = 0;
40     request_group_id_.reset(
41         [[NSString stringWithFormat:@"passkittest%d", gcount++] retain]);
42     request_tracker_ = web::RequestTrackerImpl::CreateTrackerForRequestGroupID(
43         request_group_id_,
44         &browser_state_,
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())]);
58     [passkit_client_
59         setUnderlyingClient:(id<CRNNetworkClientProtocol>)mockWebProxy_];
60   }
62   void TearDown() override { request_tracker_->Close(); }
64  protected:
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_;
76 }  // namespace
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_);