Home | History | Annotate | Download | only in location_bar
      1 // Copyright (c) 2010 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 #import <Cocoa/Cocoa.h>
      6 
      7 #include "base/utf_string_conversions.h"
      8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
      9 #import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #import "testing/gtest_mac.h"
     12 
     13 namespace {
     14 
     15 // A wide width which should fit everything.
     16 const CGFloat kWidth(300.0);
     17 
     18 // A narrow width for tests which test things that don't fit.
     19 const CGFloat kNarrowWidth(5.0);
     20 
     21 }  // namespace
     22 
     23 class SelectedKeywordDecorationTest : public CocoaTest {
     24  public:
     25   SelectedKeywordDecorationTest()
     26       : decoration_([NSFont userFontOfSize:12]) {
     27   }
     28 
     29   SelectedKeywordDecoration decoration_;
     30 };
     31 
     32 // Test that the cell correctly chooses the partial keyword if there's
     33 // not enough room.
     34 TEST_F(SelectedKeywordDecorationTest, UsesPartialKeywordIfNarrow) {
     35 
     36   const string16 kKeyword = ASCIIToUTF16("Engine");
     37   NSString* const kFullString = @"Search Engine:";
     38   NSString* const kPartialString = @"Search En\u2026:";  // ellipses
     39 
     40   decoration_.SetKeyword(kKeyword, false);
     41 
     42   // Wide width chooses the full string and image.
     43   const CGFloat all_width = decoration_.GetWidthForSpace(kWidth);
     44   EXPECT_TRUE(decoration_.image_);
     45   EXPECT_NSEQ(kFullString, decoration_.label_);
     46 
     47   // If not enough space to include the image, uses exactly the full
     48   // string.
     49   const CGFloat full_width = decoration_.GetWidthForSpace(all_width - 5.0);
     50   EXPECT_LT(full_width, all_width);
     51   EXPECT_FALSE(decoration_.image_);
     52   EXPECT_NSEQ(kFullString, decoration_.label_);
     53 
     54   // Narrow width chooses the partial string.
     55   const CGFloat partial_width = decoration_.GetWidthForSpace(kNarrowWidth);
     56   EXPECT_LT(partial_width, full_width);
     57   EXPECT_FALSE(decoration_.image_);
     58   EXPECT_NSEQ(kPartialString, decoration_.label_);
     59 
     60   // Narrow doesn't choose partial string if there is not one.
     61   decoration_.partial_string_.reset();
     62   decoration_.GetWidthForSpace(kNarrowWidth);
     63   EXPECT_FALSE(decoration_.image_);
     64   EXPECT_NSEQ(kFullString, decoration_.label_);
     65 }
     66