OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ios/chrome/browser/test/perf_test_with_bvc_ios.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 12 #include "ios/chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 13 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 14 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #import "ios/chrome/browser/sessions/session_service.h" |
| 16 #import "ios/chrome/browser/sessions/session_window.h" |
| 17 #import "ios/chrome/browser/tabs/tab_model.h" |
| 18 #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 19 #import "ios/chrome/browser/ui/browser_view_controller_dependency_factory.h" |
| 20 #import "ios/chrome/browser/ui/preload_controller.h" |
| 21 #import "ios/chrome/browser/web/chrome_web_client.h" |
| 22 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 23 |
| 24 // Subclass of PrerenderController so it isn't actually used. Using a mock for |
| 25 // this makes cleanup on shutdown simpler, by minimizing the number of profile |
| 26 // observers registered with the profiles. The profile observers have to be |
| 27 // deallocated before the profiles themselves, but in practice it is very hard |
| 28 // to ensure that happens. Also, for performance testing, not having the |
| 29 // PrerenderController makes the test far simpler to analyze. |
| 30 namespace { |
| 31 static GURL emptyGurl_ = GURL("foo", 3, url::Parsed(), false); |
| 32 } |
| 33 |
| 34 @interface TestPreloadController : PreloadController |
| 35 - (Tab*)releasePrerenderContents; |
| 36 @end |
| 37 |
| 38 @implementation TestPreloadController |
| 39 |
| 40 - (Tab*)releasePrerenderContents { |
| 41 return nil; |
| 42 } |
| 43 |
| 44 - (id<PreloadControllerDelegate>)delegate { |
| 45 return nil; |
| 46 } |
| 47 |
| 48 - (GURL)prerenderedURL { |
| 49 return emptyGurl_; |
| 50 } |
| 51 @end |
| 52 |
| 53 // Subclass the factory that creates the PreloadController for BVC to return |
| 54 // the TestPrerenderController. |
| 55 @interface TestBVCDependencyFactory : BrowserViewControllerDependencyFactory |
| 56 - (PreloadController*)newPreloadController; |
| 57 @end |
| 58 |
| 59 @implementation TestBVCDependencyFactory |
| 60 - (PreloadController*)newPreloadController { |
| 61 return [[TestPreloadController alloc] init]; |
| 62 } |
| 63 @end |
| 64 |
| 65 PerfTestWithBVC::PerfTestWithBVC(std::string testGroup) |
| 66 : PerfTest(testGroup), |
| 67 slow_teardown_(false), |
| 68 web_client_(base::MakeUnique<ChromeWebClient>()), |
| 69 provider_(ios::CreateChromeBrowserProvider()) {} |
| 70 |
| 71 PerfTestWithBVC::PerfTestWithBVC(std::string testGroup, |
| 72 std::string firstLabel, |
| 73 std::string averageLabel, |
| 74 bool isWaterfall, |
| 75 bool verbose, |
| 76 bool slowTeardown, |
| 77 int repeat) |
| 78 : PerfTest(testGroup, |
| 79 firstLabel, |
| 80 averageLabel, |
| 81 isWaterfall, |
| 82 verbose, |
| 83 repeat), |
| 84 slow_teardown_(slowTeardown), |
| 85 web_client_(base::MakeUnique<ChromeWebClient>()), |
| 86 provider_(ios::CreateChromeBrowserProvider()) {} |
| 87 |
| 88 PerfTestWithBVC::~PerfTestWithBVC() {} |
| 89 |
| 90 void PerfTestWithBVC::SetUp() { |
| 91 PerfTest::SetUp(); |
| 92 |
| 93 // Set up the ChromeBrowserState instances. |
| 94 TestChromeBrowserState::Builder test_cbs_builder; |
| 95 test_cbs_builder.AddTestingFactory( |
| 96 ios::TemplateURLServiceFactory::GetInstance(), |
| 97 ios::TemplateURLServiceFactory::GetDefaultFactory()); |
| 98 test_cbs_builder.AddTestingFactory( |
| 99 ios::AutocompleteClassifierFactory::GetInstance(), |
| 100 ios::AutocompleteClassifierFactory::GetDefaultFactory()); |
| 101 chrome_browser_state_ = test_cbs_builder.Build(); |
| 102 chrome_browser_state_->CreateBookmarkModel(false); |
| 103 bookmarks::test::WaitForBookmarkModelToLoad( |
| 104 ios::BookmarkModelFactory::GetForBrowserState( |
| 105 chrome_browser_state_.get())); |
| 106 ASSERT_TRUE(chrome_browser_state_->CreateHistoryService(false)); |
| 107 |
| 108 // Force creation of AutocompleteClassifier instance. |
| 109 ios::AutocompleteClassifierFactory::GetForBrowserState( |
| 110 chrome_browser_state_.get()); |
| 111 |
| 112 // Use the session to create a window which will contain the tab models. |
| 113 SessionWindowIOS* sessionWindow = [[SessionServiceIOS sharedService] |
| 114 loadWindowForBrowserState:chrome_browser_state_.get()]; |
| 115 |
| 116 // Tab models. The off-the-record (OTR) tab model is required for the stack |
| 117 // view controller, which is created in OpenStackView(). |
| 118 tab_model_.reset([[TabModel alloc] |
| 119 initWithSessionWindow:sessionWindow |
| 120 sessionService:[SessionServiceIOS sharedService] |
| 121 browserState:chrome_browser_state_.get()]); |
| 122 otr_tab_model_.reset([[TabModel alloc] |
| 123 initWithSessionWindow:sessionWindow |
| 124 sessionService:[SessionServiceIOS sharedService] |
| 125 browserState:chrome_browser_state_ |
| 126 ->GetOffTheRecordChromeBrowserState()]); |
| 127 |
| 128 // Create the browser view controller with its testing factory. |
| 129 bvc_factory_.reset([[TestBVCDependencyFactory alloc] |
| 130 initWithBrowserState:chrome_browser_state_.get()]); |
| 131 bvc_.reset([[BrowserViewController alloc] |
| 132 initWithTabModel:tab_model_ |
| 133 browserState:chrome_browser_state_.get() |
| 134 dependencyFactory:bvc_factory_]); |
| 135 [bvc_ setActive:YES]; |
| 136 |
| 137 // Create a real window to give to the browser view controller. |
| 138 window_.reset( |
| 139 [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]); |
| 140 [window_ makeKeyAndVisible]; |
| 141 [window_ addSubview:[bvc_ view]]; |
| 142 [[bvc_ view] setFrame:[[UIScreen mainScreen] bounds]]; |
| 143 } |
| 144 |
| 145 void PerfTestWithBVC::TearDown() { |
| 146 [[bvc_ tabModel] closeAllTabs]; |
| 147 [[bvc_ view] removeFromSuperview]; |
| 148 |
| 149 // Documented example of how to clear out the browser view controller |
| 150 // and its associated data. |
| 151 window_.reset(); |
| 152 [bvc_ browserStateDestroyed]; |
| 153 bvc_.reset(); |
| 154 bvc_factory_.reset(); |
| 155 tab_model_.reset(); |
| 156 [otr_tab_model_ browserStateDestroyed]; |
| 157 otr_tab_model_.reset(); |
| 158 |
| 159 // The base class |TearDown| method calls the run loop so the |
| 160 // NSAutoreleasePool can drain. This needs to be done before |
| 161 // |chrome_browser_state_| can be cleared. For tests that allocate more |
| 162 // objects, more runloop time may be required. |
| 163 if (slow_teardown_) |
| 164 SpinRunLoop(.5); |
| 165 PerfTest::TearDown(); |
| 166 |
| 167 // The profiles can be deallocated only after the BVC has been deallocated. |
| 168 chrome_browser_state_.reset(); |
| 169 } |
OLD | NEW |