Commit 2f735566 authored by Danyao Wang's avatar Danyao Wang Committed by Commit Bot

[Nav Experiment] Add a test helper to stub out WKBackForwardList.

Bug: 734150
Change-Id: I8b6aac965c5b627197f62f9f732f154452bfe3d4
Reviewed-on: https://chromium-review.googlesource.com/579607
Commit-Queue: Danyao Wang <danyao@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488486}
parent b9057f7d
...@@ -8,9 +8,12 @@ source_set("fakes") { ...@@ -8,9 +8,12 @@ source_set("fakes") {
deps = [ deps = [
"//ios/web:web", "//ios/web:web",
"//third_party/ocmock:ocmock",
] ]
sources = [ sources = [
"crw_test_back_forward_list.h",
"crw_test_back_forward_list.mm",
"test_navigation_manager_delegate.h", "test_navigation_manager_delegate.h",
"test_navigation_manager_delegate.mm", "test_navigation_manager_delegate.mm",
] ]
......
// Copyright 2017 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.
#ifndef IOS_WEB_NAVIGATION_CRW_TEST_BACK_FORWARD_LIST_H_
#define IOS_WEB_NAVIGATION_CRW_TEST_BACK_FORWARD_LIST_H_
#import <Foundation/Foundation.h>
@class WKBackForwardListItem;
// A CRWTestBackForwardList can be used to stub out WKBackForwardList in tests.
@interface CRWTestBackForwardList : NSObject
// WKBackForwardList interface
@property(nullable, nonatomic, copy) NSArray<WKBackForwardListItem*>* backList;
@property(nullable, nonatomic, copy)
NSArray<WKBackForwardListItem*>* forwardList;
@property(nullable, nonatomic, strong) WKBackForwardListItem* currentItem;
- (nullable WKBackForwardListItem*)itemAtIndex:(NSInteger)index;
// Resets this instance to simulate a session with no back/forward history, and
// a single entry for |currentItemURL| if it is not nil. If |currentItemURL| is
// nil, the session is reset to empty.
- (void)setCurrentURL:(nullable NSString*)currentItemURL;
// Resets this instance to simulate a session with the current entry at
// |currentItemURL|, and back and forward history entries as specified in
// |backListURLs| and |forwardListURLs|.
- (void)setCurrentURL:(nonnull NSString*)currentItemURL
backListURLs:(nullable NSArray<NSString*>*)backListURLs
forwardListURLs:(nullable NSArray<NSString*>*)forwardListURLs;
@end
#endif // IOS_WEB_NAVIGATION_CRW_TEST_BACK_FORWARD_LIST_H_
// Copyright 2017 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/test/fakes/crw_test_back_forward_list.h"
#import <WebKit/WebKit.h>
#include "third_party/ocmock/OCMock/OCMock.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface CRWTestBackForwardList (PrivateMethods)
- (WKBackForwardListItem*)mockItemWithURLString:(NSString*)URL;
- (NSArray*)mockSublistWithURLArray:(NSArray<NSString*>*)URLs;
@end
@implementation CRWTestBackForwardList
@synthesize backList;
@synthesize forwardList;
@synthesize currentItem;
- (WKBackForwardListItem*)itemAtIndex:(NSInteger)index {
if (index == 0) {
return self.currentItem;
} else if (index > 0 && self.forwardList.count) {
return self.forwardList[index - 1];
} else if (self.backList.count) {
return self.backList[self.backList.count + index];
}
return nil;
}
- (void)setCurrentURL:(NSString*)currentItemURL {
[self setCurrentURL:currentItemURL backListURLs:nil forwardListURLs:nil];
}
- (void)setCurrentURL:(NSString*)currentItemURL
backListURLs:(nullable NSArray<NSString*>*)backListURLs
forwardListURLs:(nullable NSArray<NSString*>*)forwardListURLs {
self.currentItem = [self mockItemWithURLString:currentItemURL];
self.backList = [self mockSublistWithURLArray:backListURLs];
self.forwardList = [self mockSublistWithURLArray:forwardListURLs];
}
- (WKBackForwardListItem*)mockItemWithURLString:(NSString*)URL {
id mock = OCMClassMock([WKBackForwardListItem class]);
OCMStub([mock URL]).andReturn([NSURL URLWithString:URL]);
return mock;
}
- (NSArray*)mockSublistWithURLArray:(NSArray<NSString*>*)URLs {
NSMutableArray* array = [NSMutableArray arrayWithCapacity:URLs.count];
for (NSString* URL : URLs) {
[array addObject:[self mockItemWithURLString:URL]];
}
return [NSArray arrayWithArray:array];
}
@end
...@@ -22,6 +22,12 @@ class TestNavigationManagerDelegate : public NavigationManagerDelegate { ...@@ -22,6 +22,12 @@ class TestNavigationManagerDelegate : public NavigationManagerDelegate {
const LoadCommittedDetails& load_details) override; const LoadCommittedDetails& load_details) override;
WebState* GetWebState() override; WebState* GetWebState() override;
id<CRWWebViewNavigationProxy> GetWebViewNavigationProxy() const override; id<CRWWebViewNavigationProxy> GetWebViewNavigationProxy() const override;
// Setters for tests to inject dependencies.
void SetWebViewNavigationProxy(id test_web_view);
private:
id test_web_view_;
}; };
} // namespace web } // namespace web
......
...@@ -25,7 +25,11 @@ WebState* TestNavigationManagerDelegate::GetWebState() { ...@@ -25,7 +25,11 @@ WebState* TestNavigationManagerDelegate::GetWebState() {
} }
id<CRWWebViewNavigationProxy> id<CRWWebViewNavigationProxy>
TestNavigationManagerDelegate::GetWebViewNavigationProxy() const { TestNavigationManagerDelegate::GetWebViewNavigationProxy() const {
return nil; return test_web_view_;
}
void TestNavigationManagerDelegate::SetWebViewNavigationProxy(id web_view) {
test_web_view_ = web_view;
} }
} // namespace web } // namespace web
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment