| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 5 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 #import <WebKit/WebKit.h> | 8 #import <WebKit/WebKit.h> |
| 9 | 9 |
| 10 #include "base/mac/foundation_util.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 11 #import "ios/chrome/test/app/chrome_test_util.h" | 12 #import "ios/chrome/test/app/chrome_test_util.h" |
| 12 #import "ios/chrome/test/app/history_test_util.h" | 13 #import "ios/chrome/test/app/history_test_util.h" |
| 13 #include "ios/chrome/test/app/navigation_test_util.h" | 14 #include "ios/chrome/test/app/navigation_test_util.h" |
| 14 #import "ios/testing/wait_util.h" | 15 #import "ios/testing/wait_util.h" |
| 15 #import "ios/web/public/test/earl_grey/js_test_util.h" | 16 #import "ios/web/public/test/earl_grey/js_test_util.h" |
| 16 #import "ios/web/public/test/web_view_interaction_test_util.h" | 17 #import "ios/web/public/test/web_view_interaction_test_util.h" |
| 17 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 18 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 18 #import "ios/web/public/web_state/web_state.h" | 19 #import "ios/web/public/web_state/web_state.h" |
| 19 | 20 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 60 |
| 60 #pragma mark - History Utilities | 61 #pragma mark - History Utilities |
| 61 | 62 |
| 62 + (void)clearBrowsingHistory { | 63 + (void)clearBrowsingHistory { |
| 63 chrome_test_util::ClearBrowsingHistory(); | 64 chrome_test_util::ClearBrowsingHistory(); |
| 64 // After clearing browsing history via code, wait for the UI to be done | 65 // After clearing browsing history via code, wait for the UI to be done |
| 65 // with any updates. This includes icons from the new tab page being removed. | 66 // with any updates. This includes icons from the new tab page being removed. |
| 66 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; | 67 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 67 } | 68 } |
| 68 | 69 |
| 70 #pragma mark - Cookie Utilities |
| 71 |
| 72 + (NSDictionary*)cookies { |
| 73 NSString* const kGetCookiesScript = |
| 74 @"document.cookie ? document.cookie.split(/;\\s*/) : [];"; |
| 75 |
| 76 // TODO(crbug.com/690057): Remove __unsafe_unretained once all callers of |
| 77 // |ExecuteJavaScript| are converted to ARC. |
| 78 NSError* __unsafe_unretained error = nil; |
| 79 id result = chrome_test_util::ExecuteJavaScript(kGetCookiesScript, &error); |
| 80 |
| 81 GREYAssertTrue(result && !error, @"Failed to get cookies."); |
| 82 |
| 83 NSArray* nameValuePairs = base::mac::ObjCCastStrict<NSArray>(result); |
| 84 NSMutableDictionary* cookies = [NSMutableDictionary dictionary]; |
| 85 for (NSString* nameValuePair in nameValuePairs) { |
| 86 NSArray* cookieNameValue = [nameValuePair componentsSeparatedByString:@"="]; |
| 87 GREYAssertEqual(2U, cookieNameValue.count, @"Cookie has invalid format."); |
| 88 |
| 89 NSString* cookieName = cookieNameValue[0]; |
| 90 NSString* cookieValue = cookieNameValue[1]; |
| 91 cookies[cookieName] = cookieValue; |
| 92 } |
| 93 |
| 94 return cookies; |
| 95 } |
| 96 |
| 69 #pragma mark - Navigation Utilities | 97 #pragma mark - Navigation Utilities |
| 70 | 98 |
| 71 + (void)loadURL:(GURL)URL { | 99 + (void)loadURL:(GURL)URL { |
| 72 chrome_test_util::LoadUrl(URL); | 100 chrome_test_util::LoadUrl(URL); |
| 73 // Make sure that the page started loading. | 101 // Make sure that the page started loading. |
| 74 GREYAssert(chrome_test_util::IsLoading(), @"Page did not start loading."); | 102 GREYAssert(chrome_test_util::IsLoading(), @"Page did not start loading."); |
| 75 [ChromeEarlGrey waitForPageToFinishLoading]; | 103 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 76 | 104 |
| 77 web::WebState* webState = chrome_test_util::GetCurrentWebState(); | 105 web::WebState* webState = chrome_test_util::GetCurrentWebState(); |
| 78 if (webState->ContentIsHTML()) | 106 if (webState->ContentIsHTML()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 91 | 119 |
| 92 + (void)tapWebViewElementWithID:(NSString*)elementID { | 120 + (void)tapWebViewElementWithID:(NSString*)elementID { |
| 93 BOOL success = | 121 BOOL success = |
| 94 web::test::TapWebViewElementWithId(chrome_test_util::GetCurrentWebState(), | 122 web::test::TapWebViewElementWithId(chrome_test_util::GetCurrentWebState(), |
| 95 base::SysNSStringToUTF8(elementID)); | 123 base::SysNSStringToUTF8(elementID)); |
| 96 GREYAssertTrue(success, @"Failed to tap web view element with ID: %@", | 124 GREYAssertTrue(success, @"Failed to tap web view element with ID: %@", |
| 97 elementID); | 125 elementID); |
| 98 } | 126 } |
| 99 | 127 |
| 100 @end | 128 @end |
| OLD | NEW |