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 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #import "base/strings/sys_string_conversions.h" | 9 #import "base/strings/sys_string_conversions.h" |
10 #import "ios/testing/wait_util.h" | 10 #import "ios/testing/wait_util.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/observer.h" |
| 12 #import "ios/web_view/test/web_view_test.h" |
13 #import "ios/web_view/test/web_view_test_util.h" | 13 #import "ios/web_view/test/web_view_test_util.h" |
14 #import "net/base/mac/url_conversions.h" | 14 #import "net/base/mac/url_conversions.h" |
15 #include "testing/gtest_mac.h" | 15 #include "testing/gtest_mac.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
19 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
20 #endif | 20 #endif |
21 | 21 |
22 namespace ios_web_view { | 22 namespace ios_web_view { |
23 | 23 |
24 // Tests that the KVO compliant properties of CWVWebView correctly report | 24 // Tests that the KVO compliant properties of CWVWebView correctly report |
25 // changes. | 25 // changes. |
26 typedef ios_web_view::ChromeWebViewTest ChromeWebViewKvoTest; | 26 typedef ios_web_view::WebViewTest WebViewKvoTest; |
27 | 27 |
28 // Tests that CWVWebView correctly reports |canGoBack| and |canGoForward| state. | 28 // Tests that CWVWebView correctly reports |canGoBack| and |canGoForward| state. |
29 TEST_F(ChromeWebViewKvoTest, CanGoBackForward) { | 29 TEST_F(WebViewKvoTest, CanGoBackForward) { |
30 Observer* back_observer = [[Observer alloc] init]; | 30 Observer* back_observer = [[Observer alloc] init]; |
31 [back_observer setObservedObject:web_view_ keyPath:@"canGoBack"]; | 31 [back_observer setObservedObject:web_view_ keyPath:@"canGoBack"]; |
32 | 32 |
33 Observer* forward_observer = [[Observer alloc] init]; | 33 Observer* forward_observer = [[Observer alloc] init]; |
34 [forward_observer setObservedObject:web_view_ keyPath:@"canGoForward"]; | 34 [forward_observer setObservedObject:web_view_ keyPath:@"canGoForward"]; |
35 | 35 |
36 ASSERT_FALSE(back_observer.lastValue); | 36 ASSERT_FALSE(back_observer.lastValue); |
37 ASSERT_FALSE(forward_observer.lastValue); | 37 ASSERT_FALSE(forward_observer.lastValue); |
38 | 38 |
39 // Define pages in reverse order so the links can reference the "next" page. | 39 // Define pages in reverse order so the links can reference the "next" page. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 EXPECT_FALSE([forward_observer.lastValue boolValue]); | 77 EXPECT_FALSE([forward_observer.lastValue boolValue]); |
78 | 78 |
79 // Navigate back to page 2. | 79 // Navigate back to page 2. |
80 [web_view_ goBack]; | 80 [web_view_ goBack]; |
81 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | 81 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); |
82 EXPECT_TRUE([back_observer.lastValue boolValue]); | 82 EXPECT_TRUE([back_observer.lastValue boolValue]); |
83 EXPECT_TRUE([forward_observer.lastValue boolValue]); | 83 EXPECT_TRUE([forward_observer.lastValue boolValue]); |
84 } | 84 } |
85 | 85 |
86 // Tests that CWVWebView correctly reports current |title|. | 86 // Tests that CWVWebView correctly reports current |title|. |
87 TEST_F(ChromeWebViewKvoTest, Title) { | 87 TEST_F(WebViewKvoTest, Title) { |
88 Observer* observer = [[Observer alloc] init]; | 88 Observer* observer = [[Observer alloc] init]; |
89 [observer setObservedObject:web_view_ keyPath:@"title"]; | 89 [observer setObservedObject:web_view_ keyPath:@"title"]; |
90 | 90 |
91 NSString* page_2_title = @"Page 2"; | 91 NSString* page_2_title = @"Page 2"; |
92 GURL page_2_url = | 92 GURL page_2_url = |
93 GetUrlForPageWithTitle(base::SysNSStringToUTF8(page_2_title)); | 93 GetUrlForPageWithTitle(base::SysNSStringToUTF8(page_2_title)); |
94 | 94 |
95 NSString* page_1_title = @"Page 1"; | 95 NSString* page_1_title = @"Page 1"; |
96 std::string page_1_html = base::StringPrintf( | 96 std::string page_1_html = base::StringPrintf( |
97 "<a id='link_1' href='%s'>Link 1</a>", page_2_url.spec().c_str()); | 97 "<a id='link_1' href='%s'>Link 1</a>", page_2_url.spec().c_str()); |
98 GURL page_1_url = GetUrlForPageWithTitleAndBody( | 98 GURL page_1_url = GetUrlForPageWithTitleAndBody( |
99 base::SysNSStringToUTF8(page_1_title), page_1_html); | 99 base::SysNSStringToUTF8(page_1_title), page_1_html); |
100 | 100 |
101 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(page_1_url))); | 101 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(page_1_url))); |
102 EXPECT_NSEQ(page_1_title, observer.lastValue); | 102 EXPECT_NSEQ(page_1_title, observer.lastValue); |
103 | 103 |
104 // Navigate to page 2. | 104 // Navigate to page 2. |
105 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); | 105 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); |
106 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | 106 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); |
107 EXPECT_NSEQ(page_2_title, observer.lastValue); | 107 EXPECT_NSEQ(page_2_title, observer.lastValue); |
108 | 108 |
109 // Navigate back to page 1. | 109 // Navigate back to page 1. |
110 [web_view_ goBack]; | 110 [web_view_ goBack]; |
111 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | 111 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); |
112 EXPECT_NSEQ(page_1_title, observer.lastValue); | 112 EXPECT_NSEQ(page_1_title, observer.lastValue); |
113 } | 113 } |
114 | 114 |
115 // Tests that CWVWebView correctly reports |isLoading| value. | 115 // Tests that CWVWebView correctly reports |isLoading| value. |
116 TEST_F(ChromeWebViewKvoTest, Loading) { | 116 TEST_F(WebViewKvoTest, Loading) { |
117 Observer* observer = [[Observer alloc] init]; | 117 Observer* observer = [[Observer alloc] init]; |
118 [observer setObservedObject:web_view_ keyPath:@"loading"]; | 118 [observer setObservedObject:web_view_ keyPath:@"loading"]; |
119 | 119 |
120 GURL page_2_url = GetUrlForPageWithTitle("Page 2"); | 120 GURL page_2_url = GetUrlForPageWithTitle("Page 2"); |
121 | 121 |
122 std::string page_1_html = base::StringPrintf( | 122 std::string page_1_html = base::StringPrintf( |
123 "<a id='link_1' href='%s'>Link 1</a>", page_2_url.spec().c_str()); | 123 "<a id='link_1' href='%s'>Link 1</a>", page_2_url.spec().c_str()); |
124 GURL page_1_url = GetUrlForPageWithTitleAndBody("Page 1", page_1_html); | 124 GURL page_1_url = GetUrlForPageWithTitleAndBody("Page 1", page_1_html); |
125 | 125 |
126 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(page_1_url))); | 126 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(page_1_url))); |
127 EXPECT_TRUE([observer.previousValue boolValue]); | 127 EXPECT_TRUE([observer.previousValue boolValue]); |
128 EXPECT_FALSE([observer.lastValue boolValue]); | 128 EXPECT_FALSE([observer.lastValue boolValue]); |
129 | 129 |
130 // Navigate to page 2. | 130 // Navigate to page 2. |
131 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); | 131 EXPECT_TRUE(test::TapWebViewElementWithId(web_view_, @"link_1")); |
132 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | 132 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); |
133 EXPECT_TRUE([observer.previousValue boolValue]); | 133 EXPECT_TRUE([observer.previousValue boolValue]); |
134 EXPECT_FALSE([observer.lastValue boolValue]); | 134 EXPECT_FALSE([observer.lastValue boolValue]); |
135 | 135 |
136 // Navigate back to page 1. | 136 // Navigate back to page 1. |
137 [web_view_ goBack]; | 137 [web_view_ goBack]; |
138 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | 138 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); |
139 EXPECT_TRUE([observer.previousValue boolValue]); | 139 EXPECT_TRUE([observer.previousValue boolValue]); |
140 EXPECT_FALSE([observer.lastValue boolValue]); | 140 EXPECT_FALSE([observer.lastValue boolValue]); |
141 } | 141 } |
142 | 142 |
143 // Tests that CWVWebView correctly reports |visibleURL| and |lastCommittedURL|. | 143 // Tests that CWVWebView correctly reports |visibleURL| and |lastCommittedURL|. |
144 TEST_F(ChromeWebViewKvoTest, URLs) { | 144 TEST_F(WebViewKvoTest, URLs) { |
145 Observer* last_committed_url_observer = [[Observer alloc] init]; | 145 Observer* last_committed_url_observer = [[Observer alloc] init]; |
146 [last_committed_url_observer setObservedObject:web_view_ | 146 [last_committed_url_observer setObservedObject:web_view_ |
147 keyPath:@"lastCommittedURL"]; | 147 keyPath:@"lastCommittedURL"]; |
148 | 148 |
149 Observer* visible_url_observer = [[Observer alloc] init]; | 149 Observer* visible_url_observer = [[Observer alloc] init]; |
150 [visible_url_observer setObservedObject:web_view_ keyPath:@"visibleURL"]; | 150 [visible_url_observer setObservedObject:web_view_ keyPath:@"visibleURL"]; |
151 | 151 |
152 GURL page_2 = GetUrlForPageWithTitle("Page 2"); | 152 GURL page_2 = GetUrlForPageWithTitle("Page 2"); |
153 NSURL* page_2_url = net::NSURLWithGURL(page_2); | 153 NSURL* page_2_url = net::NSURLWithGURL(page_2); |
154 | 154 |
(...skipping 18 matching lines...) Expand all Loading... |
173 EXPECT_NSEQ(page_2_url, visible_url_observer.lastValue); | 173 EXPECT_NSEQ(page_2_url, visible_url_observer.lastValue); |
174 | 174 |
175 // Navigate back to page 1. | 175 // Navigate back to page 1. |
176 [web_view_ goBack]; | 176 [web_view_ goBack]; |
177 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); | 177 ASSERT_TRUE(test::WaitForWebViewLoadCompletionOrTimeout(web_view_)); |
178 EXPECT_NSEQ(page_1_url, last_committed_url_observer.lastValue); | 178 EXPECT_NSEQ(page_1_url, last_committed_url_observer.lastValue); |
179 EXPECT_NSEQ(page_1_url, visible_url_observer.lastValue); | 179 EXPECT_NSEQ(page_1_url, visible_url_observer.lastValue); |
180 } | 180 } |
181 | 181 |
182 } // namespace ios_web_view | 182 } // namespace ios_web_view |
OLD | NEW |