OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 #include <vector> | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/memory/linked_ptr.h" | |
10 #include "chrome/browser/chrome_notification_types.h" | |
11 #include "chrome/browser/search/instant_service.h" | |
12 #include "chrome/browser/search/instant_unittest_base.h" | |
13 #include "chrome/browser/search/search.h" | |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
15 #include "chrome/common/pref_names.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "content/public/browser/navigation_controller.h" | |
18 #include "content/public/browser/render_process_host.h" | |
19 #include "content/public/browser/web_contents.h" | |
20 #include "content/public/browser/web_contents_observer.h" | |
21 | |
22 namespace chrome { | |
23 | |
24 namespace { | |
25 | |
26 class BrowserInstantControllerTest : public InstantUnitTestBase { | |
27 protected: | |
28 friend class FakeWebContentsObserver; | |
29 }; | |
30 | |
31 const struct TabReloadTestCase { | |
32 const char* description; | |
33 const char* start_url; | |
34 bool start_in_instant_process; | |
35 bool should_reload; | |
36 bool end_in_instant_process; | |
37 } kTabReloadTestCases[] = { | |
38 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl, | |
39 true, true, true}, | |
40 {"Remote Embedded NTP", "https://www.google.com/instant?strk", | |
41 true, true, false}, | |
42 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms", | |
43 true, true, false}, | |
44 {"Other NTP", "https://bar.com/instant?strk", | |
45 false, false, false} | |
46 }; | |
47 | |
48 class FakeWebContentsObserver : public content::WebContentsObserver { | |
49 public: | |
50 FakeWebContentsObserver(BrowserInstantControllerTest* base_test, | |
51 content::WebContents* contents) | |
52 : WebContentsObserver(contents), | |
53 base_test_(base_test), | |
54 num_reloads_(0) {} | |
55 | |
56 virtual void NavigateToPendingEntry( | |
57 const GURL& url, | |
58 content::NavigationController::ReloadType reload_type) OVERRIDE { | |
59 reload_type_ = reload_type; | |
60 | |
61 // The tab reload event doesn't work with BrowserWithTestWindowTest. | |
62 // So we capture the NavigateToPendingEntry, and use the | |
63 // BrowserWithTestWindowTest::NavigateAndCommit to simulate the complete | |
64 // reload. Note that this will again trigger NavigateToPendingEntry, so we | |
65 // remove this as observer. | |
66 content::NavigationController* controller = | |
67 &web_contents()->GetController(); | |
68 Observe(NULL); | |
69 | |
70 if (web_contents()->GetURL() == url) | |
71 num_reloads_++; | |
72 | |
73 base_test_->NavigateAndCommit(controller, url); | |
74 } | |
75 | |
76 int num_reloads() const { | |
77 return num_reloads_; | |
78 } | |
79 | |
80 const content::NavigationController::ReloadType reload_type() { | |
81 return reload_type_; | |
82 } | |
83 | |
84 protected: | |
85 friend class BrowserInstantControllerTest; | |
86 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, | |
87 DefaultSearchProviderChanged); | |
88 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, | |
89 GoogleBaseURLUpdated); | |
90 | |
91 private: | |
92 BrowserInstantControllerTest* base_test_; | |
93 int num_reloads_; | |
94 content::NavigationController::ReloadType reload_type_; | |
95 }; | |
96 | |
97 TEST_F(BrowserInstantControllerTest, DefaultSearchProviderChanged) { | |
98 size_t numTestCases = arraysize(kTabReloadTestCases); | |
Jered
2013/08/16 00:13:23
const size_t num_tests =
Anuj
2013/08/22 00:40:45
Done.
| |
99 std::vector<linked_ptr<FakeWebContentsObserver> > observers; | |
Jered
2013/08/16 00:13:23
Instead of vector<linked_ptr> use ScopedVector.
Anuj
2013/08/22 00:40:45
Done.
| |
100 for (size_t i = 0; i < numTestCases; ++i) { | |
101 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
102 AddTab(browser(), GURL(test.start_url)); | |
103 content::WebContents* contents = | |
104 browser()->tab_strip_model()->GetActiveWebContents(); | |
105 | |
106 // Validate initial instant state. | |
107 EXPECT_EQ(test.start_in_instant_process, | |
108 instant_service_->IsInstantProcess( | |
109 contents->GetRenderProcessHost()->GetID())) | |
110 << test.description; | |
111 | |
112 // Setup an observer to verify reload or absence thereof. | |
113 observers.push_back(make_linked_ptr( | |
114 new FakeWebContentsObserver(this, contents))); | |
115 } | |
116 | |
117 SetSearchProvider("https://bar.com/"); | |
Jered
2013/08/16 00:13:23
Why are we queuing up a bunch of tabs, doing this,
Anuj
2013/08/22 00:40:45
I kind of followed process-isolation tests in sear
| |
118 | |
119 for (size_t i = 0; i < numTestCases; ++i) { | |
120 FakeWebContentsObserver* observer = observers[i].get(); | |
121 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
122 content::WebContents* contents = observer->web_contents(); | |
123 | |
124 // Validate final instant state. | |
125 EXPECT_EQ(test.end_in_instant_process, | |
126 instant_service_->IsInstantProcess( | |
127 contents->GetRenderProcessHost()->GetID())) | |
128 << test.description; | |
129 | |
130 // Ensure only the expected tabs(contents) reloaded. | |
131 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) | |
132 << test.description; | |
133 } | |
134 } | |
135 | |
136 TEST_F(BrowserInstantControllerTest, GoogleBaseURLUpdated) { | |
137 size_t numTestCases = arraysize(kTabReloadTestCases); | |
138 std::vector<linked_ptr<FakeWebContentsObserver> > observers; | |
139 for (size_t i = 0; i < numTestCases; ++i) { | |
140 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
141 AddTab(browser(), GURL(test.start_url)); | |
142 content::WebContents* contents = | |
143 browser()->tab_strip_model()->GetActiveWebContents(); | |
144 | |
145 // Validate initial instant state. | |
146 EXPECT_EQ(test.start_in_instant_process, chrome::IsInstantNTP(contents)) | |
147 << test.description; | |
148 EXPECT_EQ(test.start_in_instant_process, | |
149 instant_service_->IsInstantProcess( | |
150 contents->GetRenderProcessHost()->GetID())) | |
151 << test.description; | |
152 | |
153 // Setup an observer to verify reload or absence thereof. | |
154 observers.push_back(make_linked_ptr( | |
155 new FakeWebContentsObserver(this, contents))); | |
156 } | |
157 | |
158 NotifyGoogleBaseURLUpdate("https://www.google.es/"); | |
159 | |
160 for (size_t i = 0; i < numTestCases; ++i) { | |
161 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
162 FakeWebContentsObserver* observer = observers[i].get(); | |
163 content::WebContents* contents = observer->web_contents(); | |
164 | |
165 // Validate final instant state. | |
166 EXPECT_EQ(test.end_in_instant_process, chrome::IsInstantNTP(contents)) | |
167 << test.description; | |
168 EXPECT_EQ(test.end_in_instant_process, | |
169 instant_service_->IsInstantProcess( | |
170 contents->GetRenderProcessHost()->GetID())) | |
171 << test.description; | |
172 | |
173 // Ensure only the expected tabs(contents) reloaded. | |
174 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) | |
175 << test.description; | |
176 } | |
177 } | |
178 | |
179 } // namespace | |
180 | |
181 } // namespace chrome | |
OLD | NEW |