OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/test/app/chrome_test_util.h" |
| 6 |
| 7 #include "base/mac/foundation_util.h" |
| 8 #import "breakpad/src/client/ios/BreakpadController.h" |
| 9 #include "components/metrics/metrics_pref_names.h" |
| 10 #include "components/metrics/metrics_service.h" |
| 11 #include "ios/chrome/app/application_delegate/metrics_mediator.h" |
| 12 #include "ios/chrome/app/application_delegate/metrics_mediator_testing.h" |
| 13 #import "ios/chrome/app/chrome_overlay_window.h" |
| 14 #import "ios/chrome/app/main_application_delegate_testing.h" |
| 15 #import "ios/chrome/app/main_controller.h" |
| 16 #import "ios/chrome/app/main_controller_private.h" |
| 17 #include "ios/chrome/browser/application_context.h" |
| 18 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 19 #import "ios/chrome/browser/metrics/previous_session_info.h" |
| 20 #import "ios/chrome/browser/metrics/previous_session_info_private.h" |
| 21 #import "ios/chrome/browser/tabs/tab.h" |
| 22 #import "ios/chrome/browser/ui/browser_list_ios.h" |
| 23 #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 24 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 25 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 26 #import "ios/chrome/browser/ui/main/main_view_controller.h" |
| 27 #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| 28 #import "ios/web/public/test/native_controller_test_util.h" |
| 29 |
| 30 // Methods to access private members for testing. |
| 31 @interface BreakpadController (Testing) |
| 32 - (BOOL)isEnabled; |
| 33 - (BOOL)isUploadingEnabled; |
| 34 @end |
| 35 @implementation BreakpadController (Testing) |
| 36 - (BOOL)isEnabled { |
| 37 return started_; |
| 38 } |
| 39 - (BOOL)isUploadingEnabled { |
| 40 return enableUploads_; |
| 41 } |
| 42 @end |
| 43 |
| 44 namespace { |
| 45 // Returns the current tab model. |
| 46 TabModel* GetCurrentTabModel() { |
| 47 MainController* main_controller = chrome_test_util::GetMainController(); |
| 48 DCHECK(main_controller); |
| 49 BrowserViewController* main_bvc = |
| 50 [[main_controller browserViewInformation] mainBVC]; |
| 51 BrowserViewController* current_bvc = |
| 52 [[main_controller browserViewInformation] currentBVC]; |
| 53 |
| 54 return current_bvc == main_bvc |
| 55 ? [[main_controller browserViewInformation] mainTabModel] |
| 56 : [[main_controller browserViewInformation] otrTabModel]; |
| 57 } |
| 58 |
| 59 // Returns the current tab. |
| 60 Tab* GetCurrentTab() { |
| 61 TabModel* tab_model = GetCurrentTabModel(); |
| 62 return [tab_model currentTab]; |
| 63 } |
| 64 |
| 65 // Returns the original ChromeBrowserState if |incognito| is false. If |
| 66 // |ingonito| is true, returns an off-the-record ChromeBrowserState. |
| 67 ios::ChromeBrowserState* GetBrowserState(bool incognito) { |
| 68 DCHECK(!BrowserListIOS::empty()); |
| 69 id<BrowserIOS> browser = *BrowserListIOS::begin(); |
| 70 DCHECK(browser); |
| 71 ios::ChromeBrowserState* browser_state = [browser browserState]; |
| 72 DCHECK(browser_state); |
| 73 browser_state = incognito ? browser_state->GetOffTheRecordChromeBrowserState() |
| 74 : browser_state->GetOriginalChromeBrowserState(); |
| 75 return browser_state; |
| 76 } |
| 77 |
| 78 // Gets the root UIViewController. |
| 79 UIViewController* GetActiveViewController() { |
| 80 UIWindow* main_window = [[UIApplication sharedApplication] keyWindow]; |
| 81 DCHECK([main_window isKindOfClass:[ChromeOverlayWindow class]]); |
| 82 MainViewController* main_view_controller = |
| 83 base::mac::ObjCCast<MainViewController>([main_window rootViewController]); |
| 84 return main_view_controller.activeViewController; |
| 85 } |
| 86 |
| 87 } // namespace |
| 88 |
| 89 namespace chrome_test_util { |
| 90 |
| 91 MainController* GetMainController() { |
| 92 return [MainApplicationDelegate sharedMainController]; |
| 93 } |
| 94 |
| 95 DeviceSharingManager* GetDeviceSharingManager() { |
| 96 return [GetMainController() deviceSharingManager]; |
| 97 } |
| 98 |
| 99 NewTabPageController* GetCurrentNewTabPageController() { |
| 100 web::WebState* web_state = GetCurrentWebState(); |
| 101 id nativeController = web::test::GetCurrentNativeController(web_state); |
| 102 if (![nativeController isKindOfClass:[NewTabPageController class]]) |
| 103 return nil; |
| 104 return base::mac::ObjCCast<NewTabPageController>(nativeController); |
| 105 } |
| 106 |
| 107 web::WebState* GetCurrentWebState() { |
| 108 return GetCurrentTab().webState; |
| 109 } |
| 110 |
| 111 ios::ChromeBrowserState* GetOriginalBrowserState() { |
| 112 return GetBrowserState(false); |
| 113 } |
| 114 |
| 115 ios::ChromeBrowserState* GetCurrentIncognitoBrowserState() { |
| 116 return GetBrowserState(true); |
| 117 } |
| 118 |
| 119 NSUInteger GetRegisteredKeyCommandsCount() { |
| 120 BrowserViewController* mainBVC = |
| 121 GetMainController().browserViewInformation.mainBVC; |
| 122 return mainBVC.keyCommands.count; |
| 123 } |
| 124 |
| 125 void RunCommandWithActiveViewController(GenericChromeCommand* command) { |
| 126 [GetActiveViewController() chromeExecuteCommand:command]; |
| 127 } |
| 128 |
| 129 void RemoveAllInfoBars() { |
| 130 infobars::InfoBarManager* info_bar_manager = [GetCurrentTab() infoBarManager]; |
| 131 if (info_bar_manager) { |
| 132 info_bar_manager->RemoveAllInfoBars(false /* animate */); |
| 133 } |
| 134 } |
| 135 |
| 136 void ClearPresentedState() { |
| 137 [GetMainController() dismissModalDialogsWithCompletion:nil]; |
| 138 } |
| 139 |
| 140 void ResetAllWebViews() { |
| 141 id<BrowserViewInformation> browser_view_info = |
| 142 [GetMainController() browserViewInformation]; |
| 143 [[browser_view_info mainTabModel] resetAllWebViews]; |
| 144 [[browser_view_info otrTabModel] resetAllWebViews]; |
| 145 } |
| 146 |
| 147 void SetBooleanLocalStatePref(const char* pref_name, bool value) { |
| 148 DCHECK(GetApplicationContext()); |
| 149 DCHECK(GetApplicationContext()->GetLocalState()); |
| 150 BooleanPrefMember pref; |
| 151 pref.Init(pref_name, GetApplicationContext()->GetLocalState()); |
| 152 pref.SetValue(value); |
| 153 } |
| 154 |
| 155 void SetBooleanUserPref(ios::ChromeBrowserState* browser_state, |
| 156 const char* pref_name, |
| 157 bool value) { |
| 158 DCHECK(browser_state); |
| 159 DCHECK(browser_state->GetPrefs()); |
| 160 BooleanPrefMember pref; |
| 161 pref.Init(pref_name, browser_state->GetPrefs()); |
| 162 pref.SetValue(value); |
| 163 } |
| 164 |
| 165 void SetWWANStateTo(bool value) { |
| 166 MainController* mainController = chrome_test_util::GetMainController(); |
| 167 net::NetworkChangeNotifier::ConnectionType connectionType = |
| 168 value ? net::NetworkChangeNotifier::CONNECTION_4G |
| 169 : net::NetworkChangeNotifier::CONNECTION_WIFI; |
| 170 [mainController.metricsMediator connectionTypeChanged:connectionType]; |
| 171 } |
| 172 |
| 173 void SetFirstLaunchStateTo(bool value) { |
| 174 [[PreviousSessionInfo sharedInstance] setIsFirstSessionAfterUpgrade:value]; |
| 175 } |
| 176 |
| 177 bool IsMetricsRecordingEnabled() { |
| 178 DCHECK(GetApplicationContext()); |
| 179 DCHECK(GetApplicationContext()->GetMetricsService()); |
| 180 return GetApplicationContext()->GetMetricsService()->recording_active(); |
| 181 } |
| 182 |
| 183 bool IsMetricsReportingEnabled() { |
| 184 DCHECK(GetApplicationContext()); |
| 185 DCHECK(GetApplicationContext()->GetMetricsService()); |
| 186 return GetApplicationContext()->GetMetricsService()->reporting_active(); |
| 187 } |
| 188 |
| 189 bool IsBreakpadEnabled() { |
| 190 return [[BreakpadController sharedInstance] isEnabled]; |
| 191 } |
| 192 |
| 193 bool IsBreakpadReportingEnabled() { |
| 194 return [[BreakpadController sharedInstance] isUploadingEnabled]; |
| 195 } |
| 196 |
| 197 bool IsFirstLaunchAfterUpgrade() { |
| 198 return [chrome_test_util::GetMainController() isFirstLaunchAfterUpgrade]; |
| 199 } |
| 200 |
| 201 void OpenChromeFromExternalApp(const GURL& url) { |
| 202 [[[UIApplication sharedApplication] delegate] |
| 203 applicationWillResignActive:[UIApplication sharedApplication]]; |
| 204 [GetMainController() setStartupParametersWithURL:url]; |
| 205 |
| 206 [[[UIApplication sharedApplication] delegate] |
| 207 applicationDidBecomeActive:[UIApplication sharedApplication]]; |
| 208 } |
| 209 |
| 210 } // namespace chrome_test_util |
OLD | NEW |