OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/web/web_state/ui/crw_web_controller.h" | 5 #import "ios/web/web_state/ui/crw_web_controller.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
10 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
11 #include "base/values.h" | 9 #include "base/values.h" |
12 #import "ios/testing/ocmock_complex_type_helper.h" | 10 #import "ios/testing/ocmock_complex_type_helper.h" |
13 #include "ios/web/public/test/web_test_util.h" | |
14 #import "ios/web/public/web_state/crw_web_controller_observer.h" | 11 #import "ios/web/public/web_state/crw_web_controller_observer.h" |
15 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | 12 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" |
16 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
17 #import "ios/web/test/crw_fake_web_controller_observer.h" | 14 #import "ios/web/test/crw_fake_web_controller_observer.h" |
18 #import "ios/web/test/web_test.h" | 15 #import "ios/web/test/web_test.h" |
19 #include "testing/gtest_mac.h" | 16 #include "testing/gtest_mac.h" |
20 | 17 |
21 namespace { | 18 namespace web { |
22 | 19 |
23 // A mixin class for testing with CRWWKWebViewWebController or | 20 // Test fixture to test web controller observing. |
24 // CRWUIWebViewWebController. | 21 class CRWWebControllerObserverTest |
25 template <typename WebTestT> | 22 : public web::WebTestWithWKWebViewWebController { |
26 class CRWWebControllerObserverTest : public WebTestT { | |
27 protected: | 23 protected: |
28 virtual void SetUp() { | 24 void SetUp() override { |
29 WebTestT::SetUp(); | 25 web::WebTestWithWKWebViewWebController::SetUp(); |
30 fake_web_controller_observer_.reset( | 26 fake_web_controller_observer_.reset( |
31 [[CRWFakeWebControllerObserver alloc] initWithCommandPrefix:@"test"]); | 27 [[CRWFakeWebControllerObserver alloc] initWithCommandPrefix:@"test"]); |
32 [WebTestT::webController_ addObserver:fake_web_controller_observer_]; | 28 [webController_ addObserver:fake_web_controller_observer_]; |
33 } | 29 } |
34 | 30 |
35 virtual void TearDown() { | 31 void TearDown() override { |
36 [WebTestT::webController_ removeObserver:fake_web_controller_observer_]; | 32 [webController_ removeObserver:fake_web_controller_observer_]; |
37 fake_web_controller_observer_.reset(); | 33 fake_web_controller_observer_.reset(); |
38 WebTestT::TearDown(); | 34 web::WebTestWithWKWebViewWebController::TearDown(); |
39 } | 35 } |
40 | 36 |
41 base::scoped_nsobject<CRWFakeWebControllerObserver> | 37 base::scoped_nsobject<CRWFakeWebControllerObserver> |
42 fake_web_controller_observer_; | 38 fake_web_controller_observer_; |
43 }; | 39 }; |
44 | 40 |
45 // Concrete test fixture to test UIWebView-based web controller observing. | 41 TEST_F(CRWWebControllerObserverTest, PageLoaded) { |
46 typedef CRWWebControllerObserverTest<web::WebTestWithUIWebViewWebController> | 42 EXPECT_FALSE([fake_web_controller_observer_ pageLoaded]); |
47 CRWUIWebViewWebControllerObserverTest; | 43 LoadHtml(@"<p></p>"); |
48 | 44 EXPECT_TRUE([fake_web_controller_observer_ pageLoaded]); |
49 // Concrete test fixture to test WKWebView-based web controller observing. | |
50 typedef CRWWebControllerObserverTest<web::WebTestWithWKWebViewWebController> | |
51 CRWWKWebViewWebControllerObserverTest; | |
52 | |
53 WEB_TEST_F(CRWUIWebViewWebControllerObserverTest, | |
54 CRWWKWebViewWebControllerObserverTest, | |
55 PageLoaded) { | |
56 EXPECT_FALSE([this->fake_web_controller_observer_ pageLoaded]); | |
57 this->LoadHtml(@"<p></p>"); | |
58 EXPECT_TRUE([this->fake_web_controller_observer_ pageLoaded]); | |
59 } | 45 } |
60 | 46 |
61 // Tests that web controller receives a JS message from the page. | 47 // Tests that web controller receives a JS message from the page. |
62 WEB_TEST_F(CRWUIWebViewWebControllerObserverTest, | 48 TEST_F(CRWWebControllerObserverTest, HandleCommand) { |
63 CRWWKWebViewWebControllerObserverTest, | 49 LoadHtml(@"<p></p>"); |
64 HandleCommand) { | 50 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); |
65 this->LoadHtml(@"<p></p>"); | |
66 ASSERT_EQ(0U, [this->fake_web_controller_observer_ commandsReceived].size()); | |
67 base::DictionaryValue command; | 51 base::DictionaryValue command; |
68 command.SetString("command", "test.testMessage"); | 52 command.SetString("command", "test.testMessage"); |
69 std::string message; | 53 std::string message; |
70 base::JSONWriter::Write(command, &message); | 54 base::JSONWriter::Write(command, &message); |
71 this->RunJavaScript([NSString | 55 RunJavaScript([NSString |
72 stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", message.c_str()]); | 56 stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", message.c_str()]); |
73 this->WaitForBackgroundTasks(); | 57 WaitForBackgroundTasks(); |
74 ASSERT_EQ(1U, [this->fake_web_controller_observer_ commandsReceived].size()); | 58 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); |
75 EXPECT_TRUE([this->fake_web_controller_observer_ commandsReceived][0]->Equals( | 59 EXPECT_TRUE( |
76 &command)); | 60 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); |
77 } | 61 } |
78 | 62 |
79 // Tests that web controller receives an immediate JS message from the page. | 63 // Tests that web controller receives an immediate JS message from the page. |
80 WEB_TEST_F(CRWUIWebViewWebControllerObserverTest, | 64 TEST_F(CRWWebControllerObserverTest, HandleImmediateCommand) { |
81 CRWWKWebViewWebControllerObserverTest, | 65 LoadHtml(@"<p></p>"); |
82 HandleImmediateCommand) { | |
83 this->LoadHtml(@"<p></p>"); | |
84 ASSERT_NSNE(@"http://testimmediate#target", | 66 ASSERT_NSNE(@"http://testimmediate#target", |
85 [this->webController_ externalRequestWindowName]); | 67 [webController_ externalRequestWindowName]); |
86 // The only valid immediate commands are window.unload and externalRequest. | 68 // The only valid immediate commands are window.unload and externalRequest. |
87 base::DictionaryValue command; | 69 base::DictionaryValue command; |
88 command.SetString("command", "externalRequest"); | 70 command.SetString("command", "externalRequest"); |
89 command.SetString("href", "http://testimmediate"); | 71 command.SetString("href", "http://testimmediate"); |
90 command.SetString("target", "target"); | 72 command.SetString("target", "target"); |
91 command.SetString("referrerPolicy", "referrerPolicy"); | 73 command.SetString("referrerPolicy", "referrerPolicy"); |
92 std::string message; | 74 std::string message; |
93 base::JSONWriter::Write(command, &message); | 75 base::JSONWriter::Write(command, &message); |
94 this->RunJavaScript( | 76 RunJavaScript( |
95 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHostImmediate(%s)", | 77 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHostImmediate(%s)", |
96 message.c_str()]); | 78 message.c_str()]); |
97 this->WaitForBackgroundTasks(); | 79 WaitForBackgroundTasks(); |
98 ASSERT_NSEQ(@"http://testimmediate#target", | 80 ASSERT_NSEQ(@"http://testimmediate#target", |
99 [this->webController_ externalRequestWindowName]); | 81 [webController_ externalRequestWindowName]); |
100 } | 82 } |
101 | 83 |
102 // Send a large number of commands and check each one is immediately received. | 84 // Send a large number of commands and check each one is immediately received. |
103 WEB_TEST_F(CRWUIWebViewWebControllerObserverTest, | 85 TEST_F(CRWWebControllerObserverTest, HandleMultipleCommands) { |
104 CRWWKWebViewWebControllerObserverTest, | 86 LoadHtml(@"<p></p>"); |
105 HandleMultipleCommands) { | |
106 this->LoadHtml(@"<p></p>"); | |
107 | 87 |
108 base::DictionaryValue command; | 88 base::DictionaryValue command; |
109 command.SetString("command", "test.testMessage"); | 89 command.SetString("command", "test.testMessage"); |
110 int kNumberMessages = 200; | 90 int kNumberMessages = 200; |
111 for (int count = 0; count <= kNumberMessages; count++) { | 91 for (int count = 0; count <= kNumberMessages; count++) { |
112 std::string message; | 92 std::string message; |
113 command.SetInteger("number", count); | 93 command.SetInteger("number", count); |
114 base::JSONWriter::Write(command, &message); | 94 base::JSONWriter::Write(command, &message); |
115 ASSERT_EQ(0U, | 95 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); |
116 [this->fake_web_controller_observer_ commandsReceived].size()); | 96 RunJavaScript( |
117 this->RunJavaScript( | |
118 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", | 97 [NSString stringWithFormat:@"__gCrWeb.message.invokeOnHost(%s)", |
119 message.c_str()]); | 98 message.c_str()]); |
120 this->WaitForBackgroundTasks(); | 99 WaitForBackgroundTasks(); |
121 ASSERT_EQ(1U, | 100 ASSERT_EQ(1U, [fake_web_controller_observer_ commandsReceived].size()); |
122 [this->fake_web_controller_observer_ commandsReceived].size()); | |
123 EXPECT_TRUE( | 101 EXPECT_TRUE( |
124 [this->fake_web_controller_observer_ commandsReceived][0]->Equals( | 102 [fake_web_controller_observer_ commandsReceived][0]->Equals(&command)); |
125 &command)); | 103 [fake_web_controller_observer_ commandsReceived].clear(); |
126 [this->fake_web_controller_observer_ commandsReceived].clear(); | 104 ASSERT_EQ(0U, [fake_web_controller_observer_ commandsReceived].size()); |
127 ASSERT_EQ(0U, | |
128 [this->fake_web_controller_observer_ commandsReceived].size()); | |
129 } | 105 } |
130 } | 106 } |
131 | 107 |
132 } // namespace | 108 } // namespace web |
OLD | NEW |