| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <ChromeWebView/ChromeWebView.h> | 5 #import <ChromeWebView/ChromeWebView.h> |
| 6 #import <Foundation/Foundation.h> | 6 #import <Foundation/Foundation.h> |
| 7 | 7 |
| 8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "ios/web_view/test/boolean_observer.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #import "base/strings/sys_string_conversions.h" |
| 10 #import "ios/web_view/test/chrome_web_view_test.h" | 11 #import "ios/web_view/test/chrome_web_view_test.h" |
| 12 #import "ios/web_view/test/observer.h" |
| 11 #import "ios/web_view/test/web_view_interaction_test_util.h" | 13 #import "ios/web_view/test/web_view_interaction_test_util.h" |
| 12 #import "net/base/mac/url_conversions.h" | 14 #import "net/base/mac/url_conversions.h" |
| 13 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
| 14 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 15 | 17 |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 17 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
| 18 #endif | 20 #endif |
| 19 | 21 |
| 20 // Tests that the KVO compliant properties of CWVWebView correctly report | 22 // Tests that the KVO compliant properties of CWVWebView correctly report |
| 21 // changes. | 23 // changes. |
| 22 class ChromeWebViewKvoTest : public ios_web_view::ChromeWebViewTest { | 24 class ChromeWebViewKvoTest : public ios_web_view::ChromeWebViewTest { |
| 23 protected: | 25 protected: |
| 24 ChromeWebViewKvoTest() { | 26 ChromeWebViewKvoTest() { |
| 25 CWVWebViewConfiguration* configuration = | 27 CWVWebViewConfiguration* configuration = |
| 26 [CWVWebViewConfiguration defaultConfiguration]; | 28 [CWVWebViewConfiguration defaultConfiguration]; |
| 27 web_view_.reset([[CWVWebView alloc] | 29 web_view_.reset([[CWVWebView alloc] |
| 28 initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0) | 30 initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0) |
| 29 configuration:configuration]); | 31 configuration:configuration]); |
| 30 } | 32 } |
| 31 | 33 |
| 32 // Web View used to listen for expected KVO property changes. | 34 // Web View used to listen for expected KVO property changes. |
| 33 base::scoped_nsobject<CWVWebView> web_view_; | 35 base::scoped_nsobject<CWVWebView> web_view_; |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 namespace ios_web_view { | 38 namespace ios_web_view { |
| 37 | 39 |
| 38 // Tests that CWVWebView correctly reports |canGoBack| and |canGoForward| state. | 40 // Tests that CWVWebView correctly reports |canGoBack| and |canGoForward| state. |
| 39 TEST_F(ChromeWebViewKvoTest, CanGoBackForward) { | 41 TEST_F(ChromeWebViewKvoTest, CanGoBackForward) { |
| 40 BooleanObserver* back_observer = [[BooleanObserver alloc] init]; | 42 Observer* back_observer = [[Observer alloc] init]; |
| 41 [back_observer setObservedObject:web_view_ keyPath:@"canGoBack"]; | 43 [back_observer setObservedObject:web_view_ keyPath:@"canGoBack"]; |
| 42 | 44 |
| 43 BooleanObserver* forward_observer = [[BooleanObserver alloc] init]; | 45 Observer* forward_observer = [[Observer alloc] init]; |
| 44 [forward_observer setObservedObject:web_view_ keyPath:@"canGoForward"]; | 46 [forward_observer setObservedObject:web_view_ keyPath:@"canGoForward"]; |
| 45 | 47 |
| 46 ASSERT_FALSE(back_observer.lastValue); | 48 ASSERT_FALSE(back_observer.lastValue); |
| 47 ASSERT_FALSE(forward_observer.lastValue); | 49 ASSERT_FALSE(forward_observer.lastValue); |
| 48 | 50 |
| 49 // Define pages in reverse order so the links can reference the "next" page. | 51 // Define pages in reverse order so the links can reference the "next" page. |
| 50 GURL page_3_url = GetUrlForPageWithTitle("Page 3"); | 52 GURL page_3_url = GetUrlForPageWithTitle("Page 3"); |
| 51 | 53 |
| 52 std::string page_2_html = | 54 std::string page_2_html = |
| 53 "<a id='link_2' href='" + page_3_url.spec() + "'>Link 2</a>"; | 55 "<a id='link_2' href='" + page_3_url.spec() + "'>Link 2</a>"; |
| 54 GURL page_2_url = GetUrlForPageWithHTMLBody(page_2_html); | 56 GURL page_2_url = GetUrlForPageWithHtmlBody(page_2_html); |
| 55 | 57 |
| 56 std::string page_1_html = | 58 std::string page_1_html = |
| 57 "<a id='link_1' href='" + page_2_url.spec() + "'>Link 1</a>"; | 59 "<a id='link_1' href='" + page_2_url.spec() + "'>Link 1</a>"; |
| 58 GURL page_1_url = GetUrlForPageWithHTMLBody(page_1_html); | 60 GURL page_1_url = GetUrlForPageWithHtmlBody(page_1_html); |
| 59 | 61 |
| 60 LoadUrl(web_view_, net::NSURLWithGURL(page_1_url)); | 62 LoadUrl(web_view_, net::NSURLWithGURL(page_1_url)); |
| 61 // Loading initial URL should not affect back/forward navigation state. | 63 // Loading initial URL should not affect back/forward navigation state. |
| 62 EXPECT_FALSE([back_observer.lastValue boolValue]); | 64 EXPECT_FALSE([back_observer.lastValue boolValue]); |
| 63 EXPECT_FALSE([forward_observer.lastValue boolValue]); | 65 EXPECT_FALSE([forward_observer.lastValue boolValue]); |
| 64 | 66 |
| 65 // Navigate to page 2. | 67 // Navigate to page 2. |
| 66 EXPECT_TRUE(test::TapChromeWebViewElementWithId(web_view_, @"link_1")); | 68 EXPECT_TRUE(test::TapChromeWebViewElementWithId(web_view_, @"link_1")); |
| 67 WaitForPageLoadCompletion(web_view_); | 69 WaitForPageLoadCompletion(web_view_); |
| 68 EXPECT_TRUE([back_observer.lastValue boolValue]); | 70 EXPECT_TRUE([back_observer.lastValue boolValue]); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 86 EXPECT_TRUE([back_observer.lastValue boolValue]); | 88 EXPECT_TRUE([back_observer.lastValue boolValue]); |
| 87 EXPECT_FALSE([forward_observer.lastValue boolValue]); | 89 EXPECT_FALSE([forward_observer.lastValue boolValue]); |
| 88 | 90 |
| 89 // Navigate back to page 2. | 91 // Navigate back to page 2. |
| 90 [web_view_ goBack]; | 92 [web_view_ goBack]; |
| 91 WaitForPageLoadCompletion(web_view_); | 93 WaitForPageLoadCompletion(web_view_); |
| 92 EXPECT_TRUE([back_observer.lastValue boolValue]); | 94 EXPECT_TRUE([back_observer.lastValue boolValue]); |
| 93 EXPECT_TRUE([forward_observer.lastValue boolValue]); | 95 EXPECT_TRUE([forward_observer.lastValue boolValue]); |
| 94 } | 96 } |
| 95 | 97 |
| 98 // Tests that CWVWebView correctly reports current |title|. |
| 99 TEST_F(ChromeWebViewKvoTest, Title) { |
| 100 Observer* observer = [[Observer alloc] init]; |
| 101 [observer setObservedObject:web_view_ keyPath:@"title"]; |
| 102 |
| 103 NSString* page_2_title = @"Page 2"; |
| 104 GURL page_2_url = |
| 105 GetUrlForPageWithTitle(base::SysNSStringToUTF8(page_2_title)); |
| 106 |
| 107 NSString* page_1_title = @"Page 1"; |
| 108 std::string page_1_html = base::StringPrintf( |
| 109 "<a id='link_1' href='%s'>Link 1</a>", page_2_url.spec().c_str()); |
| 110 GURL page_1_url = GetUrlForPageWithTitleAndBody( |
| 111 base::SysNSStringToUTF8(page_1_title), page_1_html); |
| 112 |
| 113 LoadUrl(web_view_, net::NSURLWithGURL(page_1_url)); |
| 114 EXPECT_NSEQ(page_1_title, observer.lastValue); |
| 115 |
| 116 // Navigate to page 2. |
| 117 EXPECT_TRUE(test::TapChromeWebViewElementWithId(web_view_, @"link_1")); |
| 118 WaitForPageLoadCompletion(web_view_); |
| 119 EXPECT_NSEQ(page_2_title, observer.lastValue); |
| 120 |
| 121 // Navigate back to page 1. |
| 122 [web_view_ goBack]; |
| 123 WaitForPageLoadCompletion(web_view_); |
| 124 EXPECT_NSEQ(page_1_title, observer.lastValue); |
| 125 } |
| 126 |
| 96 } // namespace ios_web_view | 127 } // namespace ios_web_view |
| OLD | NEW |