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_matchers.h" | 5 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
6 | 6 |
7 #import <OCHamcrest/OCHamcrest.h> | 7 #import <OCHamcrest/OCHamcrest.h> |
8 | 8 |
9 #import <WebKit/WebKit.h> | 9 #import <WebKit/WebKit.h> |
10 | 10 |
11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
13 #import "base/test/ios/wait_util.h" | 13 #import "base/test/ios/wait_util.h" |
14 #include "components/strings/grit/components_strings.h" | 14 #include "components/strings/grit/components_strings.h" |
15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" | 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
16 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h" | 16 #import "ios/chrome/browser/ui/omnibox/omnibox_text_field_ios.h" |
17 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h" | 17 #import "ios/chrome/browser/ui/static_content/static_html_view_controller.h" |
18 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" | 18 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" |
19 #include "ios/chrome/grit/ios_strings.h" | 19 #include "ios/chrome/grit/ios_strings.h" |
20 #import "ios/chrome/test/app/chrome_test_util.h" | 20 #import "ios/chrome/test/app/chrome_test_util.h" |
21 #import "ios/testing/wait_util.h" | 21 #import "ios/testing/wait_util.h" |
22 #import "ios/web/public/block_types.h" | 22 #import "ios/web/public/block_types.h" |
23 #import "ios/web/public/test/earl_grey/web_view_matchers.h" | 23 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
25 | 25 |
| 26 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 27 #error "This file requires ARC support." |
| 28 #endif |
| 29 |
26 namespace { | 30 namespace { |
27 | 31 |
28 // Script that returns document.body as a string. | 32 // Script that returns document.body as a string. |
29 NSString* const kGetDocumentBodyJavaScript = | 33 NSString* const kGetDocumentBodyJavaScript = |
30 @"document.body ? document.body.textContent : null"; | 34 @"document.body ? document.body.textContent : null"; |
31 | 35 |
32 // Synchronously returns the result of executed JavaScript. | 36 // Synchronously returns the result of executed JavaScript. |
33 id ExecuteScriptInStaticController( | 37 id ExecuteScriptInStaticController( |
34 StaticHtmlViewController* html_view_controller, | 38 StaticHtmlViewController* html_view_controller, |
35 NSString* script) { | 39 NSString* script) { |
36 __block id result = nil; | 40 __block id result = nil; |
37 __block bool did_finish = false; | 41 __block bool did_finish = false; |
38 web::JavaScriptResultBlock completion_handler = | 42 web::JavaScriptResultBlock completion_handler = |
39 ^(id script_result, NSError* error) { | 43 ^(id script_result, NSError* error) { |
40 result = [script_result copy]; | 44 result = [script_result copy]; |
41 did_finish = true; | 45 did_finish = true; |
42 }; | 46 }; |
43 [html_view_controller executeJavaScript:script | 47 [html_view_controller executeJavaScript:script |
44 completionHandler:completion_handler]; | 48 completionHandler:completion_handler]; |
45 | 49 |
46 GREYAssert( | 50 GREYAssert( |
47 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, | 51 testing::WaitUntilConditionOrTimeout(testing::kWaitForJSCompletionTimeout, |
48 ^{ | 52 ^{ |
49 return did_finish; | 53 return did_finish; |
50 }), | 54 }), |
51 @"JavaScript did not complete"); | 55 @"JavaScript did not complete"); |
52 | 56 |
53 return [result autorelease]; | 57 return result; |
54 } | 58 } |
55 | 59 |
56 // TODO(crbug.com/684142): This matcher uses too many implementation details, | 60 // TODO(crbug.com/684142): This matcher uses too many implementation details, |
57 // it would be good to replace it. | 61 // it would be good to replace it. |
58 id<GREYMatcher> WebViewWithNavDelegateOfClass(Class cls) { | 62 id<GREYMatcher> WebViewWithNavDelegateOfClass(Class cls) { |
59 MatchesBlock matches = ^BOOL(UIView* view) { | 63 MatchesBlock matches = ^BOOL(UIView* view) { |
60 return [view isKindOfClass:[WKWebView class]] && | 64 return [view isKindOfClass:[WKWebView class]] && |
61 [base::mac::ObjCCast<WKWebView>(view).navigationDelegate | 65 [base::mac::ObjCCast<WKWebView>(view).navigationDelegate |
62 isKindOfClass:cls]; | 66 isKindOfClass:cls]; |
63 }; | 67 }; |
64 | 68 |
65 DescribeToBlock describe = ^(id<GREYDescription> description) { | 69 DescribeToBlock describe = ^(id<GREYDescription> description) { |
66 [description appendText:@"web view with "]; | 70 [description appendText:@"web view with "]; |
67 [description appendText:NSStringFromClass(cls)]; | 71 [description appendText:NSStringFromClass(cls)]; |
68 [description appendText:@"navigation delegate"]; | 72 [description appendText:@"navigation delegate"]; |
69 }; | 73 }; |
70 | 74 |
71 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 75 return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
72 descriptionBlock:describe] | 76 descriptionBlock:describe]; |
73 autorelease]; | |
74 } | 77 } |
75 | 78 |
76 id<GREYMatcher> CollectionViewSwitchIsOn(BOOL isOn) { | 79 id<GREYMatcher> CollectionViewSwitchIsOn(BOOL isOn) { |
77 MatchesBlock matches = ^BOOL(id element) { | 80 MatchesBlock matches = ^BOOL(id element) { |
78 CollectionViewSwitchCell* switchCell = | 81 CollectionViewSwitchCell* switchCell = |
79 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(element); | 82 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(element); |
80 UISwitch* switchView = switchCell.switchView; | 83 UISwitch* switchView = switchCell.switchView; |
81 return (switchView.on && isOn) || (!switchView.on && !isOn); | 84 return (switchView.on && isOn) || (!switchView.on && !isOn); |
82 }; | 85 }; |
83 DescribeToBlock describe = ^void(id<GREYDescription> description) { | 86 DescribeToBlock describe = ^void(id<GREYDescription> description) { |
84 NSString* name = | 87 NSString* name = |
85 [NSString stringWithFormat:@"collectionViewSwitchInState(%@)", | 88 [NSString stringWithFormat:@"collectionViewSwitchInState(%@)", |
86 isOn ? @"ON" : @"OFF"]; | 89 isOn ? @"ON" : @"OFF"]; |
87 [description appendText:name]; | 90 [description appendText:name]; |
88 }; | 91 }; |
89 return [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 92 return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
90 descriptionBlock:describe] | 93 descriptionBlock:describe]; |
91 autorelease]; | |
92 } | 94 } |
93 | 95 |
94 } // namespace | 96 } // namespace |
95 | 97 |
96 namespace chrome_test_util { | 98 namespace chrome_test_util { |
97 | 99 |
98 id<GREYMatcher> ButtonWithAccessibilityLabel(NSString* label) { | 100 id<GREYMatcher> ButtonWithAccessibilityLabel(NSString* label) { |
99 return grey_allOf(grey_accessibilityLabel(label), | 101 return grey_allOf(grey_accessibilityLabel(label), |
100 grey_accessibilityTrait(UIAccessibilityTraitButton), nil); | 102 grey_accessibilityTrait(UIAccessibilityTraitButton), nil); |
101 } | 103 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 return did_succeed; | 151 return did_succeed; |
150 }; | 152 }; |
151 | 153 |
152 DescribeToBlock describe = ^(id<GREYDescription> description) { | 154 DescribeToBlock describe = ^(id<GREYDescription> description) { |
153 [description appendText:@"static HTML web view containing "]; | 155 [description appendText:@"static HTML web view containing "]; |
154 [description appendText:text]; | 156 [description appendText:text]; |
155 }; | 157 }; |
156 | 158 |
157 return grey_allOf( | 159 return grey_allOf( |
158 WebViewWithNavDelegateOfClass([StaticHtmlViewController class]), | 160 WebViewWithNavDelegateOfClass([StaticHtmlViewController class]), |
159 [[[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 161 [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
160 descriptionBlock:describe] | 162 descriptionBlock:describe], |
161 autorelease], | |
162 nil); | 163 nil); |
163 } | 164 } |
164 | 165 |
165 id<GREYMatcher> WebViewContainingBlockedImage(std::string image_id) { | 166 id<GREYMatcher> WebViewContainingBlockedImage(std::string image_id) { |
166 return web::WebViewContainingBlockedImage( | 167 return web::WebViewContainingBlockedImage( |
167 std::move(image_id), chrome_test_util::GetCurrentWebState()); | 168 std::move(image_id), chrome_test_util::GetCurrentWebState()); |
168 } | 169 } |
169 | 170 |
170 id<GREYMatcher> WebViewContainingLoadedImage(std::string image_id) { | 171 id<GREYMatcher> WebViewContainingLoadedImage(std::string image_id) { |
171 return web::WebViewContainingLoadedImage( | 172 return web::WebViewContainingLoadedImage( |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 } | 221 } |
221 | 222 |
222 id<GREYMatcher> CollectionViewSwitchCell(NSString* accessibilityIdentifier, | 223 id<GREYMatcher> CollectionViewSwitchCell(NSString* accessibilityIdentifier, |
223 BOOL isOn) { | 224 BOOL isOn) { |
224 return grey_allOf(grey_accessibilityID(accessibilityIdentifier), | 225 return grey_allOf(grey_accessibilityID(accessibilityIdentifier), |
225 CollectionViewSwitchIsOn(isOn), grey_sufficientlyVisible(), | 226 CollectionViewSwitchIsOn(isOn), grey_sufficientlyVisible(), |
226 nil); | 227 nil); |
227 } | 228 } |
228 | 229 |
229 } // namespace chrome_test_util | 230 } // namespace chrome_test_util |
OLD | NEW |