OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/metrics/histogram.h" | 6 #include "base/metrics/histogram.h" |
7 #include "base/metrics/histogram_samples.h" | 7 #include "base/metrics/histogram_samples.h" |
8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "chrome/browser/content_settings/host_content_settings_map.h" | 10 #include "chrome/browser/content_settings/host_content_settings_map.h" |
11 #include "chrome/browser/search/search.h" | 11 #include "chrome/browser/search/search.h" |
12 #include "chrome/browser/ui/search/instant_controller.h" | 12 #include "chrome/browser/ui/search/instant_controller.h" |
13 #include "chrome/browser/ui/search/instant_ntp.h" | 13 #include "chrome/browser/ui/search/instant_ntp.h" |
14 #include "chrome/browser/ui/search/instant_overlay.h" | |
15 #include "chrome/common/content_settings.h" | 14 #include "chrome/common/content_settings.h" |
16 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
17 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
18 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
20 | 19 |
21 using base::HistogramBase; | 20 using base::HistogramBase; |
22 using base::HistogramSamples; | 21 using base::HistogramSamples; |
23 using base::StatisticsRecorder; | 22 using base::StatisticsRecorder; |
24 | 23 |
25 class TestableInstantOverlay : public InstantOverlay { | |
26 public: | |
27 TestableInstantOverlay(InstantController* controller, | |
28 const std::string& instant_url) | |
29 : InstantOverlay(controller, instant_url, false) { | |
30 } | |
31 | |
32 // Overrides from InstantPage | |
33 virtual bool supports_instant() const OVERRIDE { | |
34 return test_supports_instant_; | |
35 } | |
36 | |
37 virtual bool IsLocal() const OVERRIDE { | |
38 return test_is_local_; | |
39 }; | |
40 | |
41 void set_supports_instant(bool supports_instant) { | |
42 test_supports_instant_ = supports_instant; | |
43 } | |
44 | |
45 void set_is_local(bool is_local) { | |
46 test_is_local_ = is_local; | |
47 } | |
48 | |
49 private: | |
50 std::string test_instant_url_; | |
51 bool test_supports_instant_; | |
52 bool test_is_local_; | |
53 }; | |
54 | |
55 class TestableInstantNTP : public InstantNTP { | 24 class TestableInstantNTP : public InstantNTP { |
56 public: | 25 public: |
57 TestableInstantNTP(InstantController* controller, | 26 TestableInstantNTP(InstantController* controller, |
58 const std::string& instant_url) | 27 const std::string& instant_url) |
59 : InstantNTP(controller, instant_url, false) { | 28 : InstantNTP(controller, instant_url, false) { |
60 } | 29 } |
61 | 30 |
62 // Overrides from InstantPage | 31 // Overrides from InstantPage |
63 virtual bool supports_instant() const OVERRIDE { | 32 virtual bool supports_instant() const OVERRIDE { |
64 return test_supports_instant_; | 33 return test_supports_instant_; |
(...skipping 27 matching lines...) Expand all Loading... |
92 | 61 |
93 class TestableInstantController : public InstantController { | 62 class TestableInstantController : public InstantController { |
94 public: | 63 public: |
95 TestableInstantController() | 64 TestableInstantController() |
96 : InstantController(NULL, true), | 65 : InstantController(NULL, true), |
97 test_instant_url_("http://test_url"), | 66 test_instant_url_("http://test_url"), |
98 test_extended_enabled_(true), | 67 test_extended_enabled_(true), |
99 override_javascript_enabled_(true), | 68 override_javascript_enabled_(true), |
100 test_javascript_enabled_(true), | 69 test_javascript_enabled_(true), |
101 test_in_startup_(false), | 70 test_in_startup_(false), |
102 test_overlay_(NULL), | |
103 test_ntp_(NULL) {} | 71 test_ntp_(NULL) {} |
104 | 72 |
105 // Overrides from InstantController | 73 // Overrides from InstantController |
106 virtual std::string GetInstantURL() const OVERRIDE { | 74 virtual std::string GetInstantURL() const OVERRIDE { |
107 return test_instant_url_; | 75 return test_instant_url_; |
108 } | 76 } |
109 | 77 |
110 virtual std::string GetLocalInstantURL() const OVERRIDE { | 78 virtual std::string GetLocalInstantURL() const OVERRIDE { |
111 return "http://local_instant_url"; | 79 return "http://local_instant_url"; |
112 } | 80 } |
113 | 81 |
114 virtual bool extended_enabled() const OVERRIDE { | 82 virtual bool extended_enabled() const OVERRIDE { |
115 return test_extended_enabled_; | 83 return test_extended_enabled_; |
116 } | 84 } |
117 | 85 |
118 virtual InstantOverlay* overlay() const OVERRIDE { | |
119 return test_overlay_; | |
120 } | |
121 | |
122 virtual InstantNTP* ntp() const OVERRIDE { | 86 virtual InstantNTP* ntp() const OVERRIDE { |
123 return test_ntp_; | 87 return test_ntp_; |
124 } | 88 } |
125 | 89 |
126 void set_instant_url(std::string instant_url) { | 90 void set_instant_url(std::string instant_url) { |
127 test_instant_url_ = instant_url; | 91 test_instant_url_ = instant_url; |
128 } | 92 } |
129 | 93 |
130 void set_extended_enabled(bool extended_enabled) { | 94 void set_extended_enabled(bool extended_enabled) { |
131 test_extended_enabled_ = extended_enabled; | 95 test_extended_enabled_ = extended_enabled; |
132 } | 96 } |
133 | 97 |
134 void set_overlay(InstantOverlay* overlay) { | |
135 test_overlay_ = overlay; | |
136 } | |
137 | |
138 void set_ntp(InstantNTP* ntp) { | 98 void set_ntp(InstantNTP* ntp) { |
139 test_ntp_ = ntp; | 99 test_ntp_ = ntp; |
140 } | 100 } |
141 | 101 |
142 void set_javascript_enabled(bool javascript_enabled) { | 102 void set_javascript_enabled(bool javascript_enabled) { |
143 override_javascript_enabled_ = true; | 103 override_javascript_enabled_ = true; |
144 test_javascript_enabled_ = javascript_enabled; | 104 test_javascript_enabled_ = javascript_enabled; |
145 } | 105 } |
146 | 106 |
147 void set_override_javascript_enabled(bool override_javascript_enabled) { | 107 void set_override_javascript_enabled(bool override_javascript_enabled) { |
(...skipping 18 matching lines...) Expand all Loading... |
166 virtual Profile* profile() const OVERRIDE { | 126 virtual Profile* profile() const OVERRIDE { |
167 return &profile_; | 127 return &profile_; |
168 } | 128 } |
169 | 129 |
170 private: | 130 private: |
171 std::string test_instant_url_; | 131 std::string test_instant_url_; |
172 bool test_extended_enabled_; | 132 bool test_extended_enabled_; |
173 bool override_javascript_enabled_; | 133 bool override_javascript_enabled_; |
174 bool test_javascript_enabled_; | 134 bool test_javascript_enabled_; |
175 bool test_in_startup_; | 135 bool test_in_startup_; |
176 InstantOverlay* test_overlay_; | |
177 InstantNTP* test_ntp_; | 136 InstantNTP* test_ntp_; |
178 mutable TestingProfile profile_; | 137 mutable TestingProfile profile_; |
179 }; | 138 }; |
180 | 139 |
181 class InstantControllerTest : public testing::Test { | 140 class InstantControllerTest : public testing::Test { |
182 public: | 141 public: |
183 InstantControllerTest() | 142 InstantControllerTest() |
184 : ui_thread_(content::BrowserThread::UI), | 143 : ui_thread_(content::BrowserThread::UI), |
185 instant_controller_(new TestableInstantController()) { | 144 instant_controller_(new TestableInstantController()) { |
186 } | 145 } |
187 | 146 |
188 virtual void SetUp() OVERRIDE { | 147 virtual void SetUp() OVERRIDE { |
189 base::StatisticsRecorder::Initialize(); | 148 base::StatisticsRecorder::Initialize(); |
190 } | 149 } |
191 | 150 |
192 TestableInstantController* instant_controller() { | 151 TestableInstantController* instant_controller() { |
193 return instant_controller_.get(); | 152 return instant_controller_.get(); |
194 } | 153 } |
195 | 154 |
196 private: | 155 private: |
197 content::TestBrowserThread ui_thread_; | 156 content::TestBrowserThread ui_thread_; |
198 scoped_ptr<TestableInstantController> instant_controller_; | 157 scoped_ptr<TestableInstantController> instant_controller_; |
199 }; | 158 }; |
200 | 159 |
201 TEST_F(InstantControllerTest, ShouldSwitchToLocalOverlay) { | |
202 InstantController::InstantFallbackReason fallback_reason; | |
203 | |
204 instant_controller()->set_extended_enabled(false); | |
205 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
206 ASSERT_EQ(fallback_reason, InstantController::INSTANT_FALLBACK_NONE); | |
207 | |
208 instant_controller()->set_extended_enabled(true); | |
209 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
210 ASSERT_EQ(fallback_reason, InstantController::INSTANT_FALLBACK_NO_OVERLAY); | |
211 | |
212 std::string instant_url("http://test_url"); | |
213 scoped_ptr<TestableInstantOverlay> test_overlay( | |
214 new TestableInstantOverlay(instant_controller(), instant_url)); | |
215 test_overlay->set_is_local(true); | |
216 instant_controller()->set_overlay(test_overlay.get()); | |
217 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
218 ASSERT_EQ(fallback_reason, InstantController::INSTANT_FALLBACK_NONE); | |
219 | |
220 instant_controller()->set_javascript_enabled(false); | |
221 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
222 ASSERT_EQ(fallback_reason, | |
223 InstantController::INSTANT_FALLBACK_JAVASCRIPT_DISABLED); | |
224 instant_controller()->set_javascript_enabled(true); | |
225 | |
226 test_overlay->set_is_local(false); | |
227 instant_controller()->set_instant_url(""); | |
228 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
229 ASSERT_EQ(fallback_reason, | |
230 InstantController::INSTANT_FALLBACK_INSTANT_URL_EMPTY); | |
231 | |
232 instant_controller()->set_instant_url("http://instant_url"); | |
233 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
234 ASSERT_EQ(fallback_reason, | |
235 InstantController::INSTANT_FALLBACK_ORIGIN_PATH_MISMATCH); | |
236 | |
237 instant_controller()->set_instant_url(instant_url); | |
238 test_overlay->set_supports_instant(false); | |
239 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
240 ASSERT_EQ(fallback_reason, | |
241 InstantController::INSTANT_FALLBACK_INSTANT_NOT_SUPPORTED); | |
242 | |
243 test_overlay->set_supports_instant(true); | |
244 fallback_reason = instant_controller()->ShouldSwitchToLocalOverlay(); | |
245 ASSERT_EQ(fallback_reason, InstantController::INSTANT_FALLBACK_NONE); | |
246 } | |
247 | |
248 TEST_F(InstantControllerTest, PrefersRemoteNTPOnStartup) { | 160 TEST_F(InstantControllerTest, PrefersRemoteNTPOnStartup) { |
249 std::string instant_url("http://instant_url"); | 161 std::string instant_url("http://instant_url"); |
250 scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP( | 162 scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP( |
251 instant_controller(), instant_url)); | 163 instant_controller(), instant_url)); |
252 ntp->set_is_local(false); | 164 ntp->set_is_local(false); |
253 instant_controller()->set_ntp(ntp.get()); | 165 instant_controller()->set_ntp(ntp.get()); |
254 instant_controller()->set_javascript_enabled(true); | 166 instant_controller()->set_javascript_enabled(true); |
255 instant_controller()->set_extended_enabled(true); | 167 instant_controller()->set_extended_enabled(true); |
256 instant_controller()->set_instant_url(instant_url); | 168 instant_controller()->set_instant_url(instant_url); |
257 ntp->set_instant_url(instant_url); | 169 ntp->set_instant_url(instant_url); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 280 |
369 TEST_F(InstantControllerTest, IsJavascriptEnabledChecksPrefs) { | 281 TEST_F(InstantControllerTest, IsJavascriptEnabledChecksPrefs) { |
370 instant_controller()->set_override_javascript_enabled(false); | 282 instant_controller()->set_override_javascript_enabled(false); |
371 instant_controller()->profile()->GetPrefs()->SetBoolean( | 283 instant_controller()->profile()->GetPrefs()->SetBoolean( |
372 prefs::kWebKitJavascriptEnabled, true); | 284 prefs::kWebKitJavascriptEnabled, true); |
373 EXPECT_TRUE(instant_controller()->IsJavascriptEnabled()); | 285 EXPECT_TRUE(instant_controller()->IsJavascriptEnabled()); |
374 instant_controller()->profile()->GetPrefs()->SetBoolean( | 286 instant_controller()->profile()->GetPrefs()->SetBoolean( |
375 prefs::kWebKitJavascriptEnabled, false); | 287 prefs::kWebKitJavascriptEnabled, false); |
376 EXPECT_FALSE(instant_controller()->IsJavascriptEnabled()); | 288 EXPECT_FALSE(instant_controller()->IsJavascriptEnabled()); |
377 } | 289 } |
OLD | NEW |