| 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 <CoreGraphics/CoreGraphics.h> | 5 #import <CoreGraphics/CoreGraphics.h> |
| 6 #import <EarlGrey/EarlGrey.h> | 6 #import <EarlGrey/EarlGrey.h> |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 #import <XCTest/XCTest.h> | 8 #import <XCTest/XCTest.h> |
| 9 | 9 |
| 10 #include "base/ios/ios_util.h" | 10 #include "base/ios/ios_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // URLs for test pages. | 24 // URLs for test pages. |
| 25 const char kLongPage1[] = | 25 const char kLongPage1[] = |
| 26 "http://ios/web/shell/test/http_server_files/tall_page.html"; | 26 "http://ios/web/shell/test/http_server_files/tall_page.html"; |
| 27 const char kLongPage2[] = | 27 const char kLongPage2[] = |
| 28 "http://ios/web/shell/test/http_server_files/tall_page.html?2"; | 28 "http://ios/web/shell/test/http_server_files/tall_page.html?2"; |
| 29 | 29 |
| 30 // Test scroll offsets. | 30 // Test scroll offsets. |
| 31 const CGFloat kScrollOffset1 = 20.0f; | 31 const CGFloat kOffset1 = 20.0f; |
| 32 const CGFloat kScrollOffset2 = 40.0f; | 32 const CGFloat kOffset2 = 40.0f; |
| 33 | |
| 34 // Returns a matcher for asserting that element's content offset matches the | |
| 35 // given |offset|. | |
| 36 id<GREYMatcher> ContentOffset(CGPoint offset) { | |
| 37 MatchesBlock matches = ^BOOL(UIScrollView* element) { | |
| 38 return CGPointEqualToPoint([element contentOffset], offset); | |
| 39 }; | |
| 40 DescribeToBlock describe = ^(id<GREYDescription> description) { | |
| 41 [description appendText:@"contentOffset"]; | |
| 42 }; | |
| 43 return grey_allOf( | |
| 44 grey_kindOfClass([UIScrollView class]), | |
| 45 [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | |
| 46 descriptionBlock:describe], | |
| 47 nil); | |
| 48 } | |
| 49 | 33 |
| 50 // Waits for the web view scroll view is scrolled to |y_offset|. | 34 // Waits for the web view scroll view is scrolled to |y_offset|. |
| 51 void WaitForOffset(CGFloat y_offset) { | 35 void WaitForOffset(CGFloat y_offset) { |
| 52 CGPoint content_offset = CGPointMake(0.0, y_offset); | 36 CGPoint offset = CGPointMake(0.0, y_offset); |
| 53 NSString* content_offset_string = NSStringFromCGPoint(content_offset); | 37 NSString* content_offset_string = NSStringFromCGPoint(offset); |
| 54 NSString* name = | 38 NSString* name = |
| 55 [NSString stringWithFormat:@"Wait for scroll view to scroll to %@.", | 39 [NSString stringWithFormat:@"Wait for scroll view to scroll to %@.", |
| 56 content_offset_string]; | 40 content_offset_string]; |
| 57 GREYCondition* condition = [GREYCondition | 41 GREYCondition* condition = [GREYCondition |
| 58 conditionWithName:name | 42 conditionWithName:name |
| 59 block:^BOOL { | 43 block:^BOOL { |
| 60 NSError* error = nil; | 44 NSError* error = nil; |
| 61 [[EarlGrey | 45 [[EarlGrey |
| 62 selectElementWithMatcher:web::WebViewScrollView()] | 46 selectElementWithMatcher:web::WebViewScrollView()] |
| 63 assertWithMatcher:ContentOffset(content_offset) | 47 assertWithMatcher:grey_scrollViewContentOffset(offset) |
| 64 error:&error]; | 48 error:&error]; |
| 65 return (error == nil); | 49 return (error == nil); |
| 66 }]; | 50 }]; |
| 67 NSString* error_text = | 51 NSString* error_text = |
| 68 [NSString stringWithFormat:@"Scroll view did not scroll to %@", | 52 [NSString stringWithFormat:@"Scroll view did not scroll to %@", |
| 69 content_offset_string]; | 53 content_offset_string]; |
| 70 GREYAssert([condition waitWithTimeout:10], error_text); | 54 GREYAssert([condition waitWithTimeout:10], error_text); |
| 71 } | 55 } |
| 72 | 56 |
| 73 } // namespace | 57 } // namespace |
| 74 | 58 |
| 75 using web::test::HttpServer; | 59 using web::test::HttpServer; |
| 76 | 60 |
| 77 // Page state test cases for the web shell. | 61 // Page state test cases for the web shell. |
| 78 @interface PageStateTestCase : ShellBaseTestCase | 62 @interface PageStateTestCase : ShellBaseTestCase |
| 79 @end | 63 @end |
| 80 | 64 |
| 81 @implementation PageStateTestCase | 65 @implementation PageStateTestCase |
| 82 | 66 |
| 83 // Tests that page scroll position of a page is restored upon returning to the | 67 // Tests that page scroll position of a page is restored upon returning to the |
| 84 // page via the back/forward buttons. | 68 // page via the back/forward buttons. |
| 85 - (void)testScrollPositionRestoring { | 69 - (void)testScrollPositionRestoring { |
| 86 web::test::SetUpFileBasedHttpServer(); | 70 web::test::SetUpFileBasedHttpServer(); |
| 87 | 71 |
| 88 // Load first URL which is a long page. | 72 // Load first URL which is a long page. |
| 89 [ShellEarlGrey loadURL:HttpServer::MakeUrl(kLongPage1)]; | 73 [ShellEarlGrey loadURL:HttpServer::MakeUrl(kLongPage1)]; |
| 90 | 74 |
| 91 // Scroll the first page and verify the offset. | 75 // Scroll the first page and verify the offset. |
| 92 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] | 76 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] |
| 93 performAction:grey_scrollInDirection(kGREYDirectionDown, kScrollOffset1)]; | 77 performAction:grey_scrollInDirection(kGREYDirectionDown, kOffset1)]; |
| 94 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] | 78 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] |
| 95 assertWithMatcher:ContentOffset(CGPointMake(0, kScrollOffset1))]; | 79 assertWithMatcher:grey_scrollViewContentOffset(CGPointMake(0, kOffset1))]; |
| 96 | 80 |
| 97 // Load second URL, which is also a long page. | 81 // Load second URL, which is also a long page. |
| 98 [ShellEarlGrey loadURL:HttpServer::MakeUrl(kLongPage2)]; | 82 [ShellEarlGrey loadURL:HttpServer::MakeUrl(kLongPage2)]; |
| 99 | 83 |
| 100 // Scroll the second page and verify the offset. | 84 // Scroll the second page and verify the offset. |
| 101 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] | 85 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] |
| 102 performAction:grey_scrollInDirection(kGREYDirectionDown, kScrollOffset2)]; | 86 performAction:grey_scrollInDirection(kGREYDirectionDown, kOffset2)]; |
| 103 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] | 87 [[EarlGrey selectElementWithMatcher:web::WebViewScrollView()] |
| 104 assertWithMatcher:ContentOffset(CGPointMake(0, kScrollOffset2))]; | 88 assertWithMatcher:grey_scrollViewContentOffset(CGPointMake(0, kOffset2))]; |
| 105 | 89 |
| 106 // Go back and verify that the first page offset has been restored. | 90 // Go back and verify that the first page offset has been restored. |
| 107 [[EarlGrey selectElementWithMatcher:web::BackButton()] | 91 [[EarlGrey selectElementWithMatcher:web::BackButton()] |
| 108 performAction:grey_tap()]; | 92 performAction:grey_tap()]; |
| 109 WaitForOffset(kScrollOffset1); | 93 WaitForOffset(kOffset1); |
| 110 | 94 |
| 111 // Go forward and verify that the second page offset has been restored. | 95 // Go forward and verify that the second page offset has been restored. |
| 112 [[EarlGrey selectElementWithMatcher:web::ForwardButton()] | 96 [[EarlGrey selectElementWithMatcher:web::ForwardButton()] |
| 113 performAction:grey_tap()]; | 97 performAction:grey_tap()]; |
| 114 WaitForOffset(kScrollOffset2); | 98 WaitForOffset(kOffset2); |
| 115 } | 99 } |
| 116 | 100 |
| 117 @end | 101 @end |
| OLD | NEW |