| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 void SetUp() override { | 29 void SetUp() override { |
| 30 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 30 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 31 "EmbeddedSearch", "Group1 use_cacheable_ntp:1")); | 31 "EmbeddedSearch", "Group1 use_cacheable_ntp:1")); |
| 32 InstantUnitTestBase::SetUp(); | 32 InstantUnitTestBase::SetUp(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 protected: | 35 protected: |
| 36 friend class FakeWebContentsObserver; | 36 friend class FakeWebContentsObserver; |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 const struct TabReloadTestCase { | 39 struct TabReloadTestCase { |
| 40 const char* description; | 40 const char* description; |
| 41 const char* start_url; | 41 const char* start_url; |
| 42 bool start_in_instant_process; | 42 bool start_in_instant_process; |
| 43 bool should_reload; | 43 bool should_reload; |
| 44 bool end_in_local_ntp; |
| 44 bool end_in_instant_process; | 45 bool end_in_instant_process; |
| 45 } kTabReloadTestCases[] = { | 46 }; |
| 47 |
| 48 // Test cases for when Google is the initial, but not final provider. |
| 49 const TabReloadTestCase kTabReloadTestCasesFinalProviderNotGoogle[] = { |
| 46 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl, | 50 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl, |
| 47 true, true, true}, | 51 true, true, true, true}, |
| 48 {"Remote Embedded NTP", "https://www.google.com/instant?strk", | 52 {"Remote Embedded NTP", "https://www.google.com/newtab", |
| 49 true, true, false}, | 53 true, true, false, false}, |
| 50 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms", | 54 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms", |
| 51 true, true, false}, | 55 true, true, false, false}, |
| 52 {"Other NTP", "https://bar.com/instant?strk", | 56 {"Other NTP", "https://bar.com/newtab", |
| 53 false, false, false} | 57 false, false, false, false} |
| 54 }; | 58 }; |
| 55 | 59 |
| 60 // Test cases for when Google is both the initial and final provider. |
| 61 const TabReloadTestCase kTabReloadTestCasesFinalProviderGoogle[] = { |
| 62 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl, |
| 63 true, true, true, true}, |
| 64 {"Remote Embedded NTP", "https://www.google.com/newtab", |
| 65 true, false, true, true}, |
| 66 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms", |
| 67 true, true, false, false}, |
| 68 {"Other NTP", "https://bar.com/newtab", |
| 69 false, false, false, false} |
| 70 }; |
| 71 |
| 72 |
| 56 class FakeWebContentsObserver : public content::WebContentsObserver { | 73 class FakeWebContentsObserver : public content::WebContentsObserver { |
| 57 public: | 74 public: |
| 58 explicit FakeWebContentsObserver(content::WebContents* contents) | 75 explicit FakeWebContentsObserver(content::WebContents* contents) |
| 59 : WebContentsObserver(contents), | 76 : WebContentsObserver(contents), |
| 60 contents_(contents), | 77 contents_(contents), |
| 61 url_(contents->GetURL()), | 78 url_(contents->GetURL()), |
| 62 num_reloads_(0) {} | 79 num_reloads_(0) {} |
| 63 | 80 |
| 64 void DidStartNavigationToPendingEntry( | 81 void DidStartNavigationToPendingEntry( |
| 65 const GURL& url, | 82 const GURL& url, |
| 66 content::NavigationController::ReloadType reload_type) override { | 83 content::NavigationController::ReloadType reload_type) override { |
| 67 if (url_ == url) | 84 if (url_ == url) |
| 68 num_reloads_++; | 85 num_reloads_++; |
| 86 current_url_ = url; |
| 69 } | 87 } |
| 70 | 88 |
| 71 const GURL url() const { | 89 const GURL url() const { |
| 72 return url_; | 90 return url_; |
| 73 } | 91 } |
| 74 | 92 |
| 93 const GURL current_url() const { |
| 94 return contents_->GetURL(); |
| 95 } |
| 96 |
| 75 int num_reloads() const { | 97 int num_reloads() const { |
| 76 return num_reloads_; | 98 return num_reloads_; |
| 77 } | 99 } |
| 78 | 100 |
| 101 bool can_go_back() const { |
| 102 return contents_->GetController().CanGoBack(); |
| 103 } |
| 104 |
| 79 protected: | 105 protected: |
| 80 friend class BrowserInstantControllerTest; | 106 friend class BrowserInstantControllerTest; |
| 81 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, | 107 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, |
| 82 DefaultSearchProviderChanged); | 108 DefaultSearchProviderChanged); |
| 83 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, | 109 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, |
| 84 GoogleBaseURLUpdated); | 110 GoogleBaseURLUpdated); |
| 85 | 111 |
| 86 private: | 112 private: |
| 87 content::WebContents* contents_; | 113 content::WebContents* contents_; |
| 88 const GURL& url_; | 114 const GURL& url_; |
| 115 GURL current_url_; |
| 89 int num_reloads_; | 116 int num_reloads_; |
| 90 }; | 117 }; |
| 91 | 118 |
| 92 TEST_F(BrowserInstantControllerTest, DefaultSearchProviderChanged) { | 119 TEST_F(BrowserInstantControllerTest, DefaultSearchProviderChanged) { |
| 93 size_t num_tests = arraysize(kTabReloadTestCases); | 120 size_t num_tests = arraysize(kTabReloadTestCasesFinalProviderNotGoogle); |
| 94 ScopedVector<FakeWebContentsObserver> observers; | 121 ScopedVector<FakeWebContentsObserver> observers; |
| 95 for (size_t i = 0; i < num_tests; ++i) { | 122 for (size_t i = 0; i < num_tests; ++i) { |
| 96 const TabReloadTestCase& test = kTabReloadTestCases[i]; | 123 const TabReloadTestCase& test = |
| 124 kTabReloadTestCasesFinalProviderNotGoogle[i]; |
| 97 AddTab(browser(), GURL(test.start_url)); | 125 AddTab(browser(), GURL(test.start_url)); |
| 98 content::WebContents* contents = | 126 content::WebContents* contents = |
| 99 browser()->tab_strip_model()->GetActiveWebContents(); | 127 browser()->tab_strip_model()->GetActiveWebContents(); |
| 100 | 128 |
| 101 // Validate initial instant state. | 129 // Validate initial instant state. |
| 102 EXPECT_EQ(test.start_in_instant_process, | 130 EXPECT_EQ(test.start_in_instant_process, |
| 103 instant_service_->IsInstantProcess( | 131 instant_service_->IsInstantProcess( |
| 104 contents->GetRenderProcessHost()->GetID())) | 132 contents->GetRenderProcessHost()->GetID())) |
| 105 << test.description; | 133 << test.description; |
| 106 | 134 |
| 107 // Setup an observer to verify reload or absence thereof. | 135 // Setup an observer to verify reload or absence thereof. |
| 108 observers.push_back(new FakeWebContentsObserver(contents)); | 136 observers.push_back(new FakeWebContentsObserver(contents)); |
| 109 } | 137 } |
| 110 | 138 |
| 111 SetUserSelectedDefaultSearchProvider("https://bar.com/"); | 139 SetUserSelectedDefaultSearchProvider("https://bar.com/"); |
| 112 | 140 |
| 113 for (size_t i = 0; i < num_tests; ++i) { | 141 for (size_t i = 0; i < num_tests; ++i) { |
| 114 FakeWebContentsObserver* observer = observers[i]; | 142 FakeWebContentsObserver* observer = observers[i]; |
| 115 const TabReloadTestCase& test = kTabReloadTestCases[i]; | 143 const TabReloadTestCase& test = |
| 144 kTabReloadTestCasesFinalProviderNotGoogle[i]; |
| 116 | 145 |
| 117 if (test.should_reload) { | 146 if (test.should_reload) { |
| 118 // Validate final instant state. | 147 // Validate final instant state. |
| 119 EXPECT_EQ( | 148 EXPECT_EQ( |
| 120 test.end_in_instant_process, | 149 test.end_in_instant_process, |
| 121 chrome::ShouldAssignURLToInstantRenderer(observer->url(), profile())) | 150 chrome::ShouldAssignURLToInstantRenderer( |
| 151 observer->current_url(), profile())) |
| 122 << test.description; | 152 << test.description; |
| 123 } | 153 } |
| 124 | 154 |
| 125 // Ensure only the expected tabs(contents) reloaded. | 155 // Ensure only the expected tabs(contents) reloaded. |
| 126 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) | 156 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) |
| 127 << test.description; | 157 << test.description; |
| 158 |
| 159 if (test.end_in_local_ntp) { |
| 160 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), observer->current_url()) |
| 161 << test.description; |
| 162 } |
| 128 } | 163 } |
| 129 } | 164 } |
| 130 | 165 |
| 131 TEST_F(BrowserInstantControllerTest, GoogleBaseURLUpdated) { | 166 TEST_F(BrowserInstantControllerTest, GoogleBaseURLUpdated) { |
| 132 const size_t num_tests = arraysize(kTabReloadTestCases); | 167 const size_t num_tests = arraysize(kTabReloadTestCasesFinalProviderGoogle); |
| 133 ScopedVector<FakeWebContentsObserver> observers; | 168 ScopedVector<FakeWebContentsObserver> observers; |
| 134 for (size_t i = 0; i < num_tests; ++i) { | 169 for (size_t i = 0; i < num_tests; ++i) { |
| 135 const TabReloadTestCase& test = kTabReloadTestCases[i]; | 170 const TabReloadTestCase& test = kTabReloadTestCasesFinalProviderGoogle[i]; |
| 136 AddTab(browser(), GURL(test.start_url)); | 171 AddTab(browser(), GURL(test.start_url)); |
| 137 content::WebContents* contents = | 172 content::WebContents* contents = |
| 138 browser()->tab_strip_model()->GetActiveWebContents(); | 173 browser()->tab_strip_model()->GetActiveWebContents(); |
| 139 | 174 |
| 140 // Validate initial instant state. | 175 // Validate initial instant state. |
| 141 EXPECT_EQ(test.start_in_instant_process, | 176 EXPECT_EQ(test.start_in_instant_process, |
| 142 instant_service_->IsInstantProcess( | 177 instant_service_->IsInstantProcess( |
| 143 contents->GetRenderProcessHost()->GetID())) | 178 contents->GetRenderProcessHost()->GetID())) |
| 144 << test.description; | 179 << test.description; |
| 145 | 180 |
| 146 // Setup an observer to verify reload or absence thereof. | 181 // Setup an observer to verify reload or absence thereof. |
| 147 observers.push_back(new FakeWebContentsObserver(contents)); | 182 observers.push_back(new FakeWebContentsObserver(contents)); |
| 148 } | 183 } |
| 149 | 184 |
| 150 NotifyGoogleBaseURLUpdate("https://www.google.es/"); | 185 NotifyGoogleBaseURLUpdate("https://www.google.es/"); |
| 151 | 186 |
| 152 for (size_t i = 0; i < num_tests; ++i) { | 187 for (size_t i = 0; i < num_tests; ++i) { |
| 153 const TabReloadTestCase& test = kTabReloadTestCases[i]; | 188 const TabReloadTestCase& test = kTabReloadTestCasesFinalProviderGoogle[i]; |
| 154 FakeWebContentsObserver* observer = observers[i]; | 189 FakeWebContentsObserver* observer = observers[i]; |
| 155 | 190 |
| 156 if (test.should_reload) { | 191 // Validate final instant state. |
| 157 // Validate final instant state. | 192 EXPECT_EQ( |
| 158 EXPECT_EQ( | 193 test.end_in_instant_process, |
| 159 test.end_in_instant_process, | 194 chrome::ShouldAssignURLToInstantRenderer( |
| 160 chrome::ShouldAssignURLToInstantRenderer(observer->url(), profile())) | 195 observer->current_url(), profile())) |
| 161 << test.description; | 196 << test.description; |
| 162 } | |
| 163 | 197 |
| 164 // Ensure only the expected tabs(contents) reloaded. | 198 // Ensure only the expected tabs(contents) reloaded. |
| 165 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) | 199 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) |
| 166 << test.description; | 200 << test.description; |
| 201 |
| 202 if (test.end_in_local_ntp) { |
| 203 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), observer->current_url()) |
| 204 << test.description; |
| 205 // The navigation to Local NTP should be definitive i.e. can't go back. |
| 206 EXPECT_FALSE(observer->can_go_back()); |
| 207 } |
| 167 } | 208 } |
| 168 } | 209 } |
| 169 | 210 |
| 170 TEST_F(BrowserInstantControllerTest, BrowserWindowLifecycle) { | 211 TEST_F(BrowserInstantControllerTest, BrowserWindowLifecycle) { |
| 171 scoped_ptr<BrowserWindow> window(CreateBrowserWindow()); | 212 scoped_ptr<BrowserWindow> window(CreateBrowserWindow()); |
| 172 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); | 213 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 173 params.window = window.get(); | 214 params.window = window.get(); |
| 174 scoped_ptr<Browser> browser(new Browser(params)); | 215 scoped_ptr<Browser> browser(new Browser(params)); |
| 175 InstantServiceObserver* bic; | 216 InstantServiceObserver* bic; |
| 176 bic = browser->instant_controller(); | 217 bic = browser->instant_controller(); |
| 177 EXPECT_TRUE(IsInstantServiceObserver(bic)) | 218 EXPECT_TRUE(IsInstantServiceObserver(bic)) |
| 178 << "New BrowserInstantController should register as InstantServiceObserver"; | 219 << "New BrowserInstantController should register as InstantServiceObserver"; |
| 179 | 220 |
| 180 browser.reset(NULL); | 221 browser.reset(NULL); |
| 181 window.reset(NULL); | 222 window.reset(NULL); |
| 182 EXPECT_FALSE(IsInstantServiceObserver(bic)) | 223 EXPECT_FALSE(IsInstantServiceObserver(bic)) |
| 183 << "New BrowserInstantController should register as InstantServiceObserver"; | 224 << "New BrowserInstantController should register as InstantServiceObserver"; |
| 184 } | 225 } |
| 185 | 226 |
| 186 } // namespace | 227 } // namespace |
| 187 | 228 |
| 188 } // namespace chrome | 229 } // namespace chrome |
| OLD | NEW |