| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/gtk/omnibox/omnibox_popup_view_gtk.h" | 5 #include "chrome/browser/ui/gtk/omnibox/omnibox_popup_view_gtk.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 14 #include "chrome/common/metrics/entropy_provider.h" |
| 11 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 12 #include "ui/base/gtk/gtk_hig_constants.h" | 16 #include "ui/base/gtk/gtk_hig_constants.h" |
| 17 #include "ui/gfx/font.h" |
| 18 #include "ui/gfx/rect.h" |
| 13 | 19 |
| 14 namespace { | 20 namespace { |
| 15 | 21 |
| 16 const float kLargeWidth = 10000; | 22 const float kLargeWidth = 10000; |
| 17 | 23 |
| 18 const GdkColor kContentTextColor = GDK_COLOR_RGB(0x00, 0x00, 0x00); | 24 const GdkColor kContentTextColor = GDK_COLOR_RGB(0x00, 0x00, 0x00); |
| 19 const GdkColor kDimContentTextColor = GDK_COLOR_RGB(0x80, 0x80, 0x80); | 25 const GdkColor kDimContentTextColor = GDK_COLOR_RGB(0x80, 0x80, 0x80); |
| 20 const GdkColor kURLTextColor = GDK_COLOR_RGB(0x00, 0x88, 0x00); | 26 const GdkColor kURLTextColor = GDK_COLOR_RGB(0x00, 0x88, 0x00); |
| 21 | 27 |
| 28 class TestableOmniboxPopupViewGtk : public OmniboxPopupViewGtk { |
| 29 public: |
| 30 TestableOmniboxPopupViewGtk() |
| 31 : OmniboxPopupViewGtk(gfx::Font(), NULL, NULL, NULL), |
| 32 show_called_(false), |
| 33 hide_called_(false) { |
| 34 } |
| 35 |
| 36 virtual ~TestableOmniboxPopupViewGtk() { |
| 37 } |
| 38 |
| 39 virtual void Show(size_t num_results) OVERRIDE { |
| 40 show_called_ = true; |
| 41 } |
| 42 |
| 43 virtual void Hide() OVERRIDE { |
| 44 hide_called_ = true; |
| 45 } |
| 46 |
| 47 virtual const AutocompleteResult& GetResult() const OVERRIDE { |
| 48 return result_; |
| 49 } |
| 50 |
| 51 using OmniboxPopupViewGtk::GetRectForLine; |
| 52 using OmniboxPopupViewGtk::LineFromY; |
| 53 using OmniboxPopupViewGtk::GetHiddenMatchCount; |
| 54 |
| 55 AutocompleteResult result_; |
| 56 bool show_called_; |
| 57 bool hide_called_; |
| 58 }; |
| 59 |
| 22 } // namespace | 60 } // namespace |
| 23 | 61 |
| 24 class OmniboxPopupViewGtkTest : public PlatformTest { | 62 class OmniboxPopupViewGtkTest : public PlatformTest { |
| 25 public: | 63 public: |
| 26 OmniboxPopupViewGtkTest() {} | 64 OmniboxPopupViewGtkTest() {} |
| 27 | 65 |
| 28 virtual void SetUp() { | 66 virtual void SetUp() { |
| 29 PlatformTest::SetUp(); | 67 PlatformTest::SetUp(); |
| 30 | 68 |
| 31 window_ = gtk_window_new(GTK_WINDOW_POPUP); | 69 window_ = gtk_window_new(GTK_WINDOW_POPUP); |
| 32 layout_ = gtk_widget_create_pango_layout(window_, NULL); | 70 layout_ = gtk_widget_create_pango_layout(window_, NULL); |
| 71 view_.reset(new TestableOmniboxPopupViewGtk); |
| 72 field_trial_list_.reset(new base::FieldTrialList( |
| 73 new metrics::SHA1EntropyProvider("42"))); |
| 33 } | 74 } |
| 34 | 75 |
| 35 virtual void TearDown() { | 76 virtual void TearDown() { |
| 36 g_object_unref(layout_); | 77 g_object_unref(layout_); |
| 37 gtk_widget_destroy(window_); | 78 gtk_widget_destroy(window_); |
| 38 | 79 |
| 39 PlatformTest::TearDown(); | 80 PlatformTest::TearDown(); |
| 40 } | 81 } |
| 41 | 82 |
| 42 // The google C++ Testing Framework documentation suggests making | 83 // The google C++ Testing Framework documentation suggests making |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 attribute); | 214 attribute); |
| 174 | 215 |
| 175 pango_attribute_destroy(attribute); | 216 pango_attribute_destroy(attribute); |
| 176 | 217 |
| 177 return retval; | 218 return retval; |
| 178 } | 219 } |
| 179 | 220 |
| 180 GtkWidget* window_; | 221 GtkWidget* window_; |
| 181 PangoLayout* layout_; | 222 PangoLayout* layout_; |
| 182 | 223 |
| 224 scoped_ptr<TestableOmniboxPopupViewGtk> view_; |
| 225 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 226 |
| 183 private: | 227 private: |
| 184 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupViewGtkTest); | 228 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupViewGtkTest); |
| 185 }; | 229 }; |
| 186 | 230 |
| 187 // Simple inputs with no matches should result in styled output who's | 231 // Simple inputs with no matches should result in styled output who's |
| 188 // text matches the input string, with the passed-in color, and | 232 // text matches the input string, with the passed-in color, and |
| 189 // nothing bolded. | 233 // nothing bolded. |
| 190 TEST_F(OmniboxPopupViewGtkTest, DecorateMatchedStringNoMatch) { | 234 TEST_F(OmniboxPopupViewGtkTest, DecorateMatchedStringNoMatch) { |
| 191 const string16 kContents = ASCIIToUTF16("This is a test"); | 235 const string16 kContents = ASCIIToUTF16("This is a test"); |
| 192 | 236 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 &kDimContentTextColor, | 408 &kDimContentTextColor, |
| 365 &kURLTextColor, | 409 &kURLTextColor, |
| 366 std::string()); | 410 std::string()); |
| 367 | 411 |
| 368 // One color for the entire string, and it's not the one we passed | 412 // One color for the entire string, and it's not the one we passed |
| 369 // in. | 413 // in. |
| 370 EXPECT_EQ(kContents.length(), RunLengthForAttrType(0U, kContents.length(), | 414 EXPECT_EQ(kContents.length(), RunLengthForAttrType(0U, kContents.length(), |
| 371 PANGO_ATTR_FOREGROUND)); | 415 PANGO_ATTR_FOREGROUND)); |
| 372 EXPECT_TRUE(RunHasColor(0U, kContents.length(), kURLTextColor)); | 416 EXPECT_TRUE(RunHasColor(0U, kContents.length(), kURLTextColor)); |
| 373 } | 417 } |
| 418 |
| 419 // Test that the popup is not shown if there is only one hidden match. |
| 420 TEST_F(OmniboxPopupViewGtkTest, HidesIfOnlyOneHiddenMatch) { |
| 421 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 422 "InstantExtended", "Group1 hide_verbatim:1")); |
| 423 ACMatches matches; |
| 424 AutocompleteMatch match; |
| 425 match.destination_url = GURL("http://verbatim/"); |
| 426 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; |
| 427 matches.push_back(match); |
| 428 view_->result_.AppendMatches(matches); |
| 429 ASSERT_TRUE(view_->result_.ShouldHideTopMatch()); |
| 430 |
| 431 // Since there is only one match which is hidden, the popup should close. |
| 432 view_->UpdatePopupAppearance(); |
| 433 EXPECT_TRUE(view_->hide_called_); |
| 434 } |
| 435 |
| 436 // Test that the top match is skipped if the model indicates it should be |
| 437 // hidden. |
| 438 TEST_F(OmniboxPopupViewGtkTest, SkipsTopMatchIfHidden) { |
| 439 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 440 "InstantExtended", "Group1 hide_verbatim:1")); |
| 441 ACMatches matches; |
| 442 { |
| 443 AutocompleteMatch match; |
| 444 match.destination_url = GURL("http://verbatim/"); |
| 445 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; |
| 446 matches.push_back(match); |
| 447 } |
| 448 { |
| 449 AutocompleteMatch match; |
| 450 match.destination_url = GURL("http://not-verbatim/"); |
| 451 match.type = AutocompleteMatchType::SEARCH_OTHER_ENGINE; |
| 452 matches.push_back(match); |
| 453 } |
| 454 view_->result_.AppendMatches(matches); |
| 455 ASSERT_TRUE(view_->result_.ShouldHideTopMatch()); |
| 456 |
| 457 EXPECT_EQ(1U, view_->GetHiddenMatchCount()); |
| 458 EXPECT_EQ(1U, view_->LineFromY(0)); |
| 459 gfx::Rect rect = view_->GetRectForLine(1, 100); |
| 460 EXPECT_EQ(1, rect.y()); |
| 461 } |
| 462 |
| 463 // Test that the top match is not skipped if the model does not indicate it |
| 464 // should be hidden. |
| 465 TEST_F(OmniboxPopupViewGtkTest, DoesNotSkipTopMatchIfVisible) { |
| 466 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 467 "InstantExtended", "Group1 hide_verbatim:1")); |
| 468 ACMatches matches; |
| 469 AutocompleteMatch match; |
| 470 match.destination_url = GURL("http://not-verbatim/"); |
| 471 match.type = AutocompleteMatchType::SEARCH_OTHER_ENGINE; |
| 472 matches.push_back(match); |
| 473 view_->result_.AppendMatches(matches); |
| 474 ASSERT_FALSE(view_->result_.ShouldHideTopMatch()); |
| 475 |
| 476 EXPECT_EQ(0U, view_->GetHiddenMatchCount()); |
| 477 EXPECT_EQ(0U, view_->LineFromY(0)); |
| 478 gfx::Rect rect = view_->GetRectForLine(1, 100); |
| 479 EXPECT_EQ(25, rect.y()); |
| 480 |
| 481 // The single match is visible so the popup should be open. |
| 482 view_->UpdatePopupAppearance(); |
| 483 EXPECT_TRUE(view_->show_called_); |
| 484 } |
| OLD | NEW |