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 | |
7 #include "base/basictypes.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/search/instant_service.h" | |
10 #include "chrome/browser/search/instant_unittest_base.h" | |
11 #include "chrome/browser/search/search.h" | |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "chrome/common/url_constants.h" | |
15 #include "content/public/browser/navigation_controller.h" | |
16 #include "content/public/browser/render_process_host.h" | |
17 #include "content/public/browser/web_contents.h" | |
18 #include "content/public/browser/web_contents_observer.h" | |
19 | |
20 namespace chrome { | |
21 | |
22 namespace { | |
23 | |
24 class BrowserInstantControllerTest : public InstantUnitTestBase { | |
25 protected: | |
26 friend class FakeWebContentsObserver; | |
27 }; | |
28 | |
29 class FakeWebContentsObserver : public content::WebContentsObserver { | |
30 public: | |
31 FakeWebContentsObserver(BrowserInstantControllerTest* base_test, | |
32 const GURL& url, | |
33 content::NavigationController::ReloadType reload_type, | |
34 const char* description) | |
35 : base_test_(base_test), | |
36 url_(url), | |
37 reload_type_(reload_type), | |
38 description_(description), | |
39 validated_(0) {} | |
40 | |
41 void NavigateToPendingEntry( | |
samarth
2013/08/12 19:07:28
This is an OVERRIDE, right? (and should be virtual
Anuj
2013/08/13 00:18:09
Done.
| |
42 const GURL& url, | |
43 content::NavigationController::ReloadType reload_type) { | |
44 EXPECT_EQ(url_, url) << description_; | |
45 EXPECT_EQ(reload_type_, reload_type) << description_; | |
46 content::NavigationController* controller = | |
47 &web_contents()->GetController(); | |
48 Observe(NULL); | |
49 base_test_->NavigateAndCommit(controller, url_); | |
50 validated_++; | |
51 } | |
52 | |
53 bool Validated() const { | |
54 return validated_ == 1; | |
samarth
2013/08/12 19:07:28
What is being validated here? Please use a more de
Anuj
2013/08/13 00:18:09
Done.
| |
55 } | |
56 | |
57 protected: | |
58 friend class BrowserInstantControllerTabReloadTest; | |
59 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTabReloadTest, | |
60 DefaultSearchProviderChanged); | |
61 | |
62 private: | |
63 BrowserInstantControllerTest* base_test_; | |
64 const GURL& url_; | |
65 content::NavigationController::ReloadType reload_type_; | |
66 const char* description_; | |
67 int validated_; | |
68 }; | |
69 | |
70 const struct TabReloadTestCase { | |
71 const char* description; | |
72 const char* start_url; | |
73 bool start_in_instant_process; | |
74 bool should_reload; | |
75 bool end_in_instant_process; | |
76 } kTabReloadTestCases[] = { | |
77 {"Local Embedded SEHP", chrome::kChromeSearchLocalNtpUrl, | |
samarth
2013/08/12 19:07:28
s/Embedded SEHP/NTP
Anuj
2013/08/13 00:18:09
Done.
| |
78 true, true, true}, | |
79 {"Remote Embedded SEHP", "https://www.google.com/instant?strk", | |
80 true, true, false}, | |
81 {"Remote Embedded SERP", "https://www.google.com/url?strk", | |
82 true, true, false}, | |
83 {"Other SEHP", "https://bar.com/instant?strk", | |
84 false, false, false} | |
85 }; | |
86 | |
87 struct TabInfo { | |
88 content::WebContents* contents; | |
89 FakeWebContentsObserver* observer; | |
90 const GURL* url; | |
91 int pid; | |
92 }; | |
93 | |
94 class BrowserInstantControllerTabReloadTest | |
95 : public BrowserInstantControllerTest { | |
96 protected: | |
97 TabInfo* SetUpTabReloadTestCases() { | |
samarth
2013/08/12 19:07:28
This is way too complex to follow. I shouldn't fee
Anuj
2013/08/13 00:18:09
Done.
| |
98 size_t numTestCases = arraysize(kTabReloadTestCases); | |
99 TabInfo* tabInfos = new TabInfo[numTestCases]; | |
100 for (size_t i = 0; i < numTestCases; ++i) { | |
101 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
102 TabInfo& tabInfo = tabInfos[i]; | |
103 AddTab(browser(), GURL(test.start_url)); | |
104 tabInfo.contents = browser()->tab_strip_model()->GetActiveWebContents(); | |
105 tabInfo.url = &tabInfo.contents->GetURL(); | |
106 tabInfo.pid = tabInfo.contents->GetRenderProcessHost()->GetID(); | |
107 EXPECT_EQ(test.start_in_instant_process, | |
108 chrome::IsInstantNTP(tabInfo.contents)) | |
109 << test.description; | |
110 EXPECT_EQ(test.start_in_instant_process, | |
111 instant_service_->IsInstantProcess(tabInfos[i].pid)) | |
112 << test.description; | |
113 if (test.should_reload) { | |
114 tabInfo.observer = new FakeWebContentsObserver( | |
115 this, | |
116 tabInfo.contents->GetURL(), | |
117 (test.start_in_instant_process == test.end_in_instant_process) | |
118 ? content::NavigationController::RELOAD | |
119 : content::NavigationController::NO_RELOAD, | |
120 test.description); | |
121 tabInfo.observer->Observe(tabInfo.contents); | |
122 } | |
123 } | |
124 return tabInfos; | |
125 } | |
126 | |
127 void ValidateTabReloadTestCases(TabInfo* tabInfos) { | |
128 size_t numTestCases = arraysize(kTabReloadTestCases); | |
129 for (size_t i = 0; i < numTestCases; ++i) { | |
130 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
131 TabInfo& tabInfo = tabInfos[i]; | |
132 if (test.should_reload) { | |
133 EXPECT_TRUE(tabInfo.observer->Validated()) << test.description; | |
134 } | |
135 int pid = tabInfo.contents->GetRenderProcessHost()->GetID(); | |
136 EXPECT_EQ(*tabInfo.url, tabInfo.contents->GetURL()) << test.description; | |
137 if (test.should_reload) { | |
138 EXPECT_EQ(test.end_in_instant_process, | |
139 ShouldAssignURLToInstantRenderer(tabInfo.contents->GetURL(), | |
140 profile())) | |
141 << test.description; | |
142 } | |
143 EXPECT_EQ(test.end_in_instant_process, | |
144 chrome::IsInstantNTP(tabInfo.contents)) | |
145 << test.description; | |
146 EXPECT_EQ(test.end_in_instant_process, | |
147 instant_service_->IsInstantProcess(pid)) | |
148 << test.description; | |
149 EXPECT_EQ(test.start_in_instant_process == test.end_in_instant_process, | |
150 pid == tabInfo.pid) | |
151 << test.description; | |
152 } | |
153 } | |
154 | |
155 friend class FakeWebContentsObserver; | |
156 }; | |
157 | |
158 TEST_F(BrowserInstantControllerTabReloadTest, DefaultSearchProviderChanged) { | |
159 EXPECT_EQ(1, instant_service_->GetInstantProcessCount()); | |
160 TabInfo* tabInfos = SetUpTabReloadTestCases(); | |
161 | |
162 SetSearchProvider("https://bar.com/"); | |
163 | |
164 ValidateTabReloadTestCases(tabInfos); | |
165 } | |
166 | |
167 TEST_F(BrowserInstantControllerTabReloadTest, GoogleBaseURLUpdated) { | |
168 EXPECT_EQ(1, instant_service_->GetInstantProcessCount()); | |
169 TabInfo* tabInfos = SetUpTabReloadTestCases(); | |
170 | |
171 NotifyGoogleBaseURLUpdate("https://www.google.es/"); | |
172 | |
173 ValidateTabReloadTestCases(tabInfos); | |
174 } | |
175 | |
176 } // namespace | |
177 } // namespace chrome | |
OLD | NEW |