| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/memory/weak_ptr.h" | |
| 7 #include "base/utf_string_conversions.h" | |
| 8 #include "content/browser/intents/intent_injector.h" | |
| 9 #include "content/browser/intents/internal_web_intents_dispatcher.h" | |
| 10 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 11 #include "content/browser/web_contents/test_web_contents.h" | |
| 12 #include "content/common/intents_messages.h" | |
| 13 #include "content/common/view_messages.h" | |
| 14 #include "content/public/test/mock_render_process_host.h" | |
| 15 #include "content/public/test/web_contents_tester.h" | |
| 16 #include "webkit/glue/web_intent_data.h" | |
| 17 #include "webkit/glue/web_intent_reply_data.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 class IntentInjectorTest : public RenderViewHostImplTestHarness { | |
| 22 public: | |
| 23 IntentInjectorTest() { | |
| 24 webkit_glue::WebIntentData intent(ASCIIToUTF16("action"), | |
| 25 ASCIIToUTF16("type"), | |
| 26 ASCIIToUTF16("unserialized_data")); | |
| 27 // Will be destroyed when the IntentInjector shuts down. | |
| 28 dispatcher_ = new InternalWebIntentsDispatcher(intent); | |
| 29 } | |
| 30 | |
| 31 ~IntentInjectorTest() {} | |
| 32 | |
| 33 InternalWebIntentsDispatcher* dispatcher_; | |
| 34 }; | |
| 35 | |
| 36 TEST_F(IntentInjectorTest, TestDispatchLimitedToSameOrigin) { | |
| 37 contents()->transition_cross_site = true; | |
| 38 // Set up GetURL within the web_contents, and enable cross-site RVH | |
| 39 // transitions. | |
| 40 NavigateAndCommit(GURL("http://www.ddd.com/")); | |
| 41 | |
| 42 // Set up the injector, which will lock to the origin of GetURL. | |
| 43 // The injector is a WebContentsObserver, destroyed when the | |
| 44 // web_contents() is destroyed at the end of the test. | |
| 45 IntentInjector* injector = new IntentInjector(web_contents()); | |
| 46 webkit_glue::WebIntentData intent(ASCIIToUTF16("action"), | |
| 47 ASCIIToUTF16("type"), | |
| 48 ASCIIToUTF16("unserialized_data")); | |
| 49 injector->SetIntent(dispatcher_, intent); | |
| 50 | |
| 51 // Navigate to a same-origin page. The intent data should be sent. | |
| 52 controller().LoadURL(GURL("http://www.ddd.com/page1"), | |
| 53 Referrer(), | |
| 54 PAGE_TRANSITION_TYPED, | |
| 55 std::string()); | |
| 56 test_rvh()->OnMessageReceived(ViewHostMsg_ShouldClose_ACK( | |
| 57 rvh()->GetRoutingID(), true, base::TimeTicks(), base::TimeTicks())); | |
| 58 | |
| 59 ASSERT_FALSE(pending_rvh()); | |
| 60 injector->RenderViewCreated(rvh()); | |
| 61 | |
| 62 EXPECT_FALSE(NULL == process()->sink().GetFirstMessageMatching( | |
| 63 IntentsMsg_SetWebIntentData::ID)); | |
| 64 process()->sink().ClearMessages(); | |
| 65 | |
| 66 // Intent data is sent to a different same-origin page. | |
| 67 controller().LoadURL(GURL("http://www.ddd.com/page2"), | |
| 68 Referrer(), | |
| 69 PAGE_TRANSITION_TYPED, | |
| 70 std::string()); | |
| 71 test_rvh()->OnMessageReceived(ViewHostMsg_ShouldClose_ACK( | |
| 72 rvh()->GetRoutingID(), true, base::TimeTicks(), base::TimeTicks())); | |
| 73 ASSERT_FALSE(pending_rvh()); | |
| 74 injector->RenderViewCreated(rvh()); | |
| 75 EXPECT_FALSE(NULL == process()->sink().GetFirstMessageMatching( | |
| 76 IntentsMsg_SetWebIntentData::ID)); | |
| 77 process()->sink().ClearMessages(); | |
| 78 | |
| 79 controller().LoadURL(GURL("http://www.domain2.com/"), | |
| 80 Referrer(), | |
| 81 PAGE_TRANSITION_TYPED, | |
| 82 std::string()); | |
| 83 ASSERT_TRUE(contents()->cross_navigation_pending()); | |
| 84 ASSERT_TRUE(pending_rvh()); | |
| 85 injector->RenderViewCreated(pending_test_rvh()); | |
| 86 | |
| 87 // The injector is still operating on the WebContents with the ddd.com | |
| 88 // initial page, so no intent data is sent to this new renderer. | |
| 89 EXPECT_TRUE(NULL == process()->sink().GetFirstMessageMatching( | |
| 90 IntentsMsg_SetWebIntentData::ID)); | |
| 91 } | |
| 92 | |
| 93 TEST_F(IntentInjectorTest, AbandonDeletes) { | |
| 94 IntentInjector* injector = new IntentInjector(web_contents()); | |
| 95 | |
| 96 base::WeakPtr<IntentInjector> weak_injector = | |
| 97 injector->weak_factory_.GetWeakPtr(); | |
| 98 EXPECT_FALSE(weak_injector.get() == NULL); | |
| 99 weak_injector->Abandon(); | |
| 100 EXPECT_TRUE(weak_injector.get() == NULL); | |
| 101 | |
| 102 // Sending a message will both auto-delete |dispatcher_| to clean up and | |
| 103 // verify that messages don't get sent to the (now deleted) |injector|. | |
| 104 dispatcher_->SendReply( | |
| 105 webkit_glue::WebIntentReply( | |
| 106 webkit_glue::WEB_INTENT_SERVICE_CONTENTS_CLOSED, | |
| 107 string16())); | |
| 108 } | |
| 109 | |
| 110 } // namespace content | |
| OLD | NEW |