OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/search/instant_page.h" | 5 #include "chrome/browser/ui/search/instant_page.h" |
6 | 6 |
7 #include "base/command_line.h" | |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "chrome/browser/ui/search/search_tab_helper.h" | |
10 #include "chrome/common/chrome_switches.h" | |
8 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
9 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "content/public/browser/navigation_entry.h" | |
11 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
12 #include "content/public/test/mock_render_process_host.h" | 16 #include "content/public/test/mock_render_process_host.h" |
13 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
14 #include "ipc/ipc_test_sink.h" | 18 #include "ipc/ipc_test_sink.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 class FakePageDelegate : public InstantPage::Delegate { | 24 class FakePageDelegate : public InstantPage::Delegate { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 | 61 |
58 class FakePage : public InstantPage { | 62 class FakePage : public InstantPage { |
59 public: | 63 public: |
60 FakePage(Delegate* delegate, const std::string& instant_url) | 64 FakePage(Delegate* delegate, const std::string& instant_url) |
61 : InstantPage(delegate, instant_url) { | 65 : InstantPage(delegate, instant_url) { |
62 } | 66 } |
63 virtual ~FakePage() { | 67 virtual ~FakePage() { |
64 } | 68 } |
65 | 69 |
66 using InstantPage::SetContents; | 70 using InstantPage::SetContents; |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(FakePage); | |
67 }; | 74 }; |
68 | 75 |
69 } // namespace | 76 } // namespace |
70 | 77 |
71 class InstantPageTest : public ChromeRenderViewHostTestHarness { | 78 class InstantPageTest : public ChromeRenderViewHostTestHarness { |
72 public: | 79 public: |
80 virtual void SetUp() OVERRIDE; | |
81 | |
73 scoped_ptr<FakePage> page; | 82 scoped_ptr<FakePage> page; |
74 FakePageDelegate delegate; | 83 FakePageDelegate delegate; |
75 }; | 84 }; |
76 | 85 |
86 void InstantPageTest::SetUp() { | |
87 CommandLine::ForCurrentProcess()->AppendSwitch( | |
88 switches::kEnableInstantExtendedAPI); | |
89 ChromeRenderViewHostTestHarness::SetUp(); | |
90 SearchTabHelper::CreateForWebContents(web_contents()); | |
91 page.reset(new FakePage(&delegate, "")); | |
92 EXPECT_FALSE(page->supports_instant()); | |
samarth
2013/05/10 01:32:38
I'd prefer keeping the EXPECT checks in the tests
kmadhusu
2013/05/13 17:13:24
Done.
| |
93 page->SetContents(web_contents()); | |
94 } | |
95 | |
77 TEST_F(InstantPageTest, IsLocal) { | 96 TEST_F(InstantPageTest, IsLocal) { |
78 page.reset(new FakePage(&delegate, "")); | |
79 EXPECT_FALSE(page->IsLocal()); | 97 EXPECT_FALSE(page->IsLocal()); |
80 page->SetContents(web_contents()); | |
81 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 98 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
82 EXPECT_TRUE(page->IsLocal()); | 99 EXPECT_TRUE(page->IsLocal()); |
83 NavigateAndCommit(GURL("http://example.com")); | 100 NavigateAndCommit(GURL("http://example.com")); |
84 EXPECT_FALSE(page->IsLocal()); | 101 EXPECT_FALSE(page->IsLocal()); |
85 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 102 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
86 EXPECT_TRUE(page->IsLocal()); | 103 EXPECT_TRUE(page->IsLocal()); |
87 } | 104 } |
88 | 105 |
89 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { | 106 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { |
90 page.reset(new FakePage(&delegate, "")); | |
91 EXPECT_FALSE(page->supports_instant()); | |
92 page->SetContents(web_contents()); | |
93 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 107 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
94 EXPECT_TRUE(page->IsLocal()); | 108 EXPECT_TRUE(page->IsLocal()); |
95 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 109 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
96 .Times(1); | 110 .Times(1); |
97 page->DetermineIfPageSupportsInstant(); | 111 SearchTabHelper::FromWebContents(web_contents())-> |
112 DetermineIfPageSupportsInstant(); | |
98 EXPECT_TRUE(page->supports_instant()); | 113 EXPECT_TRUE(page->supports_instant()); |
99 } | 114 } |
100 | 115 |
101 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { | 116 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { |
102 page.reset(new FakePage(&delegate, "")); | |
103 EXPECT_FALSE(page->supports_instant()); | |
104 page->SetContents(web_contents()); | |
105 NavigateAndCommit(GURL("http://example.com/")); | 117 NavigateAndCommit(GURL("http://example.com/")); |
106 EXPECT_FALSE(page->IsLocal()); | 118 EXPECT_FALSE(page->IsLocal()); |
107 process()->sink().ClearMessages(); | 119 process()->sink().ClearMessages(); |
108 page->DetermineIfPageSupportsInstant(); | 120 SearchTabHelper::FromWebContents(web_contents())-> |
121 DetermineIfPageSupportsInstant(); | |
109 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 122 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
110 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 123 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
111 ASSERT_TRUE(message != NULL); | 124 ASSERT_TRUE(message != NULL); |
112 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 125 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
113 } | 126 } |
127 | |
128 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message | |
129 // reply handler updates the instant support state in InstantPage. | |
130 TEST_F(InstantPageTest, PageSupportsInstant) { | |
131 NavigateAndCommit(GURL("http://example.com/")); | |
132 process()->sink().ClearMessages(); | |
133 SearchTabHelper::FromWebContents(web_contents())-> | |
134 DetermineIfPageSupportsInstant(); | |
135 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | |
136 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | |
137 ASSERT_TRUE(message != NULL); | |
138 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | |
139 | |
140 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | |
141 .Times(1); | |
142 | |
143 // Assume the page supports instant. Invoke the message reply handler to make | |
144 // sure the InstantPage is notified about the instant support state. | |
145 const content::NavigationEntry* entry = | |
146 web_contents()->GetController().GetActiveEntry(); | |
147 EXPECT_TRUE(entry); | |
148 SearchTabHelper::FromWebContents(web_contents())-> | |
149 OnInstantSupportDeterminedMsgReceived(entry->GetPageID(), true); | |
150 EXPECT_TRUE(page->supports_instant()); | |
151 } | |
OLD | NEW |