Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: ios/web/web_state/web_state_impl_unittest.mm

Issue 2074733002: Add public API for handling Javascript alerts and prompt. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add unittests. Address missed comments. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "ios/web/web_state/web_state_impl.h" 5 #include "ios/web/web_state/web_state_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/mac/bind_objc_block.h" 14 #include "base/mac/bind_objc_block.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/ios/wait_util.h" 16 #include "base/test/ios/wait_util.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "ios/web/public/load_committed_details.h" 18 #include "ios/web/public/load_committed_details.h"
19 #include "ios/web/public/test/test_browser_state.h" 19 #include "ios/web/public/test/test_browser_state.h"
20 #include "ios/web/public/test/web_test.h" 20 #include "ios/web/public/test/web_test.h"
21 #include "ios/web/public/web_state/context_menu_params.h"
21 #include "ios/web/public/web_state/global_web_state_observer.h" 22 #include "ios/web/public/web_state/global_web_state_observer.h"
22 #include "ios/web/public/web_state/web_state_delegate.h" 23 #include "ios/web/public/web_state/web_state_delegate.h"
23 #include "ios/web/public/web_state/web_state_observer.h" 24 #include "ios/web/public/web_state/web_state_observer.h"
24 #include "ios/web/public/web_state/web_state_policy_decider.h" 25 #include "ios/web/public/web_state/web_state_policy_decider.h"
25 #include "ios/web/web_state/global_web_state_event_tracker.h" 26 #include "ios/web/web_state/global_web_state_event_tracker.h"
26 #import "ios/web/web_state/ui/crw_web_controller.h" 27 #import "ios/web/web_state/ui/crw_web_controller.h"
27 #include "net/http/http_response_headers.h" 28 #include "net/http/http_response_headers.h"
28 #include "net/http/http_util.h" 29 #include "net/http/http_util.h"
29 #include "testing/gmock/include/gmock/gmock.h" 30 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 bool did_start_loading_called_; 110 bool did_start_loading_called_;
110 bool did_stop_loading_called_; 111 bool did_stop_loading_called_;
111 bool page_loaded_called_with_success_; 112 bool page_loaded_called_with_success_;
112 bool web_state_destroyed_called_; 113 bool web_state_destroyed_called_;
113 }; 114 };
114 115
115 // Test delegate to check that the WebStateDelegate methods are called as 116 // Test delegate to check that the WebStateDelegate methods are called as
116 // expected. 117 // expected.
117 class TestWebStateDelegate : public WebStateDelegate { 118 class TestWebStateDelegate : public WebStateDelegate {
118 public: 119 public:
119 TestWebStateDelegate() : load_progress_changed_called_(false) {} 120 TestWebStateDelegate()
121 : load_progress_changed_called_(false),
122 handle_context_menu_called_(false),
123 get_java_script_dialog_presenter_called_(false) {}
120 124
121 // Methods returning true if the corresponding WebStateObserver method has 125 // True if the WebStateDelegate LoadProgressChanged method has been called.
122 // been called.
123 bool load_progress_changed_called() const { 126 bool load_progress_changed_called() const {
124 return load_progress_changed_called_; 127 return load_progress_changed_called_;
125 } 128 }
126 129
130 // True if the WebStateDelegate HandleContextMenu method has been called.
131 bool handle_context_menu_called() const {
132 return handle_context_menu_called_;
133 }
134
135 // True if the WebStateDelegate GetJavaScriptDialogPresenter method has been
136 // called.
137 bool get_java_script_dialog_presenter_called() const {
138 return get_java_script_dialog_presenter_called_;
139 }
140
127 private: 141 private:
128 // WebStateObserver implementation: 142 // WebStateDelegate implementation:
129 void LoadProgressChanged(WebState* source, double progress) override { 143 void LoadProgressChanged(WebState* source, double progress) override {
130 load_progress_changed_called_ = true; 144 load_progress_changed_called_ = true;
131 } 145 }
132 146
147 bool HandleContextMenu(WebState* source,
148 const ContextMenuParams& params) override {
149 handle_context_menu_called_ = true;
150 return NO;
151 }
152
153 JavaScriptDialogPresenter* GetJavaScriptDialogPresenter(
154 WebState* source) override {
155 get_java_script_dialog_presenter_called_ = true;
156 return nullptr;
Eugene But (OOO till 7-30) 2016/06/28 18:14:48 How about adding SetJavaScriptDialogPresenter and
michaeldo 2016/06/28 21:58:27 I like that idea. Done.
157 }
158
133 bool load_progress_changed_called_; 159 bool load_progress_changed_called_;
160 bool handle_context_menu_called_;
161 bool get_java_script_dialog_presenter_called_;
134 }; 162 };
135 163
136 // Test observer to check that the WebStateObserver methods are called as 164 // Test observer to check that the WebStateObserver methods are called as
137 // expected. 165 // expected.
138 class TestWebStateObserver : public WebStateObserver { 166 class TestWebStateObserver : public WebStateObserver {
139 public: 167 public:
140 TestWebStateObserver(WebState* web_state) 168 TestWebStateObserver(WebState* web_state)
141 : WebStateObserver(web_state), 169 : WebStateObserver(web_state),
142 provisional_navigation_started_called_(false), 170 provisional_navigation_started_called_(false),
143 navigation_items_pruned_called_(false), 171 navigation_items_pruned_called_(false),
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 436
409 // Tests that WebStateDelegate methods appropriately called. 437 // Tests that WebStateDelegate methods appropriately called.
410 TEST_F(WebStateTest, DelegateTest) { 438 TEST_F(WebStateTest, DelegateTest) {
411 TestWebStateDelegate delegate; 439 TestWebStateDelegate delegate;
412 web_state_->SetDelegate(&delegate); 440 web_state_->SetDelegate(&delegate);
413 441
414 // Test that LoadProgressChanged() is called. 442 // Test that LoadProgressChanged() is called.
415 EXPECT_FALSE(delegate.load_progress_changed_called()); 443 EXPECT_FALSE(delegate.load_progress_changed_called());
416 web_state_->SendChangeLoadProgress(0.0); 444 web_state_->SendChangeLoadProgress(0.0);
417 EXPECT_TRUE(delegate.load_progress_changed_called()); 445 EXPECT_TRUE(delegate.load_progress_changed_called());
446
447 // Test that HandleContextMenu() is called.
448 EXPECT_FALSE(delegate.handle_context_menu_called());
449 web::ContextMenuParams context_menu_params;
450 web_state_->HandleContextMenu(context_menu_params);
451 EXPECT_TRUE(delegate.handle_context_menu_called());
452
453 // Test that GetJavaScriptDialogPresenter() is called.
454 EXPECT_FALSE(delegate.get_java_script_dialog_presenter_called());
455 __block bool callback_called = false;
456 web_state_->RunJavaScriptDialog(GURL(), JAVASCRIPT_DIALOG_TYPE_ALERT, @"",
457 nil, base::BindBlock(^(BOOL, NSString*) {
458 callback_called = true;
459 }));
460 EXPECT_TRUE(delegate.get_java_script_dialog_presenter_called());
461 // Callback will be immediately run since we provide no presenter in
462 // our TestWebStateDelegate's |GetJavaScriptDialogPresenter| method.
463 EXPECT_TRUE(callback_called);
418 } 464 }
419 465
420 // Verifies that GlobalWebStateObservers are called when expected. 466 // Verifies that GlobalWebStateObservers are called when expected.
421 TEST_F(WebStateTest, GlobalObserverTest) { 467 TEST_F(WebStateTest, GlobalObserverTest) {
422 std::unique_ptr<TestGlobalWebStateObserver> observer( 468 std::unique_ptr<TestGlobalWebStateObserver> observer(
423 new TestGlobalWebStateObserver()); 469 new TestGlobalWebStateObserver());
424 470
425 // Test that NavigationItemsPruned() is called. 471 // Test that NavigationItemsPruned() is called.
426 EXPECT_FALSE(observer->navigation_items_pruned_called()); 472 EXPECT_FALSE(observer->navigation_items_pruned_called());
427 web_state_->OnNavigationItemsPruned(1); 473 web_state_->OnNavigationItemsPruned(1);
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 "</script>"); 671 "</script>");
626 672
627 base::test::ios::WaitUntilCondition(^{ 673 base::test::ios::WaitUntilCondition(^{
628 return message_received; 674 return message_received;
629 }); 675 });
630 web_state_->RemoveScriptCommandCallback("test"); 676 web_state_->RemoveScriptCommandCallback("test");
631 } 677 }
632 678
633 } // namespace 679 } // namespace
634 } // namespace web 680 } // namespace web
OLDNEW
« ios/web/web_state/web_state_delegate_bridge.mm ('K') | « ios/web/web_state/web_state_impl.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698