1// Copyright 2020 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. 4 5#import "ios/chrome/browser/ui/main/test/fake_scene_state.h" 6 7#import "ios/chrome/browser/main/browser.h" 8#import "ios/chrome/browser/main/test_browser.h" 9#import "ios/chrome/browser/ui/main/test/stub_browser_interface.h" 10#import "ios/chrome/browser/ui/main/test/stub_browser_interface_provider.h" 11#import "ios/chrome/browser/web_state_list/web_state_list.h" 12#import "ios/chrome/browser/web_state_list/web_state_opener.h" 13#import "ios/web/public/test/fakes/test_web_state.h" 14 15#if !defined(__has_feature) || !__has_feature(objc_arc) 16#error "This file requires ARC support." 17#endif 18 19@interface FakeSceneState () 20// Redeclare interface provider readwrite. 21@property(nonatomic, strong, readwrite) id<BrowserInterfaceProvider> 22 interfaceProvider; 23@end 24 25@implementation FakeSceneState { 26 // Owning pointer for the browser that backs the interface provider. 27 std::unique_ptr<TestBrowser> _browser; 28 UIWindow* _window; 29} 30 31@synthesize interfaceProvider = _interfaceProvider; 32 33- (instancetype)initWithAppState:(AppState*)appState { 34 if (self = [super initWithAppState:appState]) { 35 self.activationLevel = SceneActivationLevelForegroundInactive; 36 self.interfaceProvider = [[StubBrowserInterfaceProvider alloc] init]; 37 StubBrowserInterface* mainInterface = static_cast<StubBrowserInterface*>( 38 self.interfaceProvider.mainInterface); 39 _browser = std::make_unique<TestBrowser>(); 40 mainInterface.browser = _browser.get(); 41 } 42 return self; 43} 44 45+ (NSArray<FakeSceneState*>*)sceneArrayWithCount:(int)count { 46 NSMutableArray<SceneState*>* scenes = [NSMutableArray array]; 47 for (int i = 0; i < count; i++) { 48 [scenes addObject:[[self alloc] initWithAppState:nil]]; 49 } 50 return [scenes copy]; 51} 52 53- (void)appendWebStateWithURL:(const GURL)URL { 54 auto test_web_state = std::make_unique<web::TestWebState>(); 55 test_web_state->SetCurrentURL(URL); 56 WebStateList* web_state_list = 57 self.interfaceProvider.mainInterface.browser->GetWebStateList(); 58 web_state_list->InsertWebState( 59 WebStateList::kInvalidIndex, std::move(test_web_state), 60 WebStateList::INSERT_NO_FLAGS, WebStateOpener()); 61} 62 63- (void)appendWebStatesWithURL:(const GURL)URL count:(int)count { 64 for (int i = 0; i < count; i++) { 65 [self appendWebStateWithURL:URL]; 66 } 67} 68 69- (UIWindow*)window { 70 return _window; 71} 72 73- (void)setWindow:(UIWindow*)window { 74 _window = window; 75} 76 77@end 78