Home | History | Annotate | Download | only in corewm
      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 "ui/views/corewm/tooltip_aura.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "ui/aura/test/aura_test_base.h"
      9 #include "ui/base/resource/resource_bundle.h"
     10 #include "ui/gfx/font_list.h"
     11 #include "ui/gfx/text_elider.h"
     12 #include "ui/gfx/text_utils.h"
     13 
     14 using base::ASCIIToUTF16;
     15 using base::UTF8ToUTF16;
     16 
     17 namespace views {
     18 namespace corewm {
     19 
     20 typedef aura::test::AuraTestBase TooltipAuraTest;
     21 
     22 TEST_F(TooltipAuraTest, TrimTooltipToFitTests) {
     23   const gfx::FontList font_list;
     24   const int max_width = 4000;
     25   base::string16 tooltip;
     26   int width, line_count, expect_lines;
     27   int max_pixel_width = 400;  // copied from constants in tooltip_controller.cc
     28   int max_lines = 10;  // copied from constants in tooltip_controller.cc
     29   size_t tooltip_len;
     30 
     31   // Error in computed size vs. expected size should not be greater than the
     32   // size of the longest word.
     33   int error_in_pixel_width = gfx::GetStringWidth(ASCIIToUTF16("tooltip"),
     34                                                  font_list);
     35 
     36   // Long tooltips should wrap to next line
     37   tooltip.clear();
     38   width = line_count = -1;
     39   expect_lines = 3;
     40   for (; gfx::GetStringWidth(tooltip, font_list) <=
     41            (expect_lines - 1) * max_pixel_width;)
     42     tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
     43   tooltip_len = tooltip.length();
     44   TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
     45                                 &line_count);
     46   EXPECT_NEAR(max_pixel_width, width, error_in_pixel_width);
     47   EXPECT_EQ(expect_lines, line_count);
     48   EXPECT_EQ(tooltip_len + expect_lines - 1, tooltip.length());
     49 
     50   // More than |max_lines| lines should get truncated at 10 lines.
     51   tooltip.clear();
     52   width = line_count = -1;
     53   expect_lines = 13;
     54   for (; gfx::GetStringWidth(tooltip, font_list) <=
     55            (expect_lines - 1) * max_pixel_width;)
     56     tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
     57   TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
     58                                 &line_count);
     59   EXPECT_NEAR(max_pixel_width, width, error_in_pixel_width);
     60   EXPECT_EQ(max_lines, line_count);
     61 
     62   // Long multi line tooltips should wrap individual lines.
     63   tooltip.clear();
     64   width = line_count = -1;
     65   expect_lines = 4;
     66   for (; gfx::GetStringWidth(tooltip, font_list) <=
     67            (expect_lines - 2) * max_pixel_width;)
     68     tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
     69   tooltip.insert(tooltip.length() / 2, ASCIIToUTF16("\n"));
     70   tooltip_len = tooltip.length();
     71   TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
     72                                 &line_count);
     73   EXPECT_NEAR(max_pixel_width, width, error_in_pixel_width);
     74   EXPECT_EQ(expect_lines, line_count);
     75   // We may have inserted the line break above near a space which will get
     76   // trimmed. Hence we may be off by 1 in the final tooltip length calculation.
     77   EXPECT_NEAR(tooltip_len + expect_lines - 2, tooltip.length(), 1);
     78 
     79 #if !defined(OS_WIN)
     80   // Tooltip with really long word gets elided.
     81   tooltip.clear();
     82   width = line_count = -1;
     83   tooltip = UTF8ToUTF16(std::string('a', max_pixel_width));
     84   TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
     85                                 &line_count);
     86   EXPECT_NEAR(max_pixel_width, width, 5);
     87   EXPECT_EQ(1, line_count);
     88   EXPECT_EQ(gfx::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)),
     89                            font_list, max_pixel_width, gfx::ELIDE_TAIL),
     90             tooltip);
     91 #endif
     92 
     93   // Normal small tooltip should stay as is.
     94   tooltip.clear();
     95   width = line_count = -1;
     96   tooltip = ASCIIToUTF16("Small Tooltip");
     97   TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
     98                                 &line_count);
     99   EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tooltip"), font_list),
    100             width);
    101   EXPECT_EQ(1, line_count);
    102   EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip);
    103 
    104   // Normal small multi-line tooltip should stay as is.
    105   tooltip.clear();
    106   width = line_count = -1;
    107   tooltip = ASCIIToUTF16("Multi line\nTooltip");
    108   TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
    109                                 &line_count);
    110   int expected_width = gfx::GetStringWidth(ASCIIToUTF16("Multi line"),
    111                                            font_list);
    112   expected_width = std::max(expected_width,
    113                             gfx::GetStringWidth(ASCIIToUTF16("Tooltip"),
    114                                                 font_list));
    115   EXPECT_EQ(expected_width, width);
    116   EXPECT_EQ(2, line_count);
    117   EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip);
    118 
    119   // Whitespaces in tooltips are preserved.
    120   tooltip.clear();
    121   width = line_count = -1;
    122   tooltip = ASCIIToUTF16("Small  Tool  t\tip");
    123   TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
    124                                 &line_count);
    125   EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small  Tool  t\tip"), font_list),
    126             width);
    127   EXPECT_EQ(1, line_count);
    128   EXPECT_EQ(ASCIIToUTF16("Small  Tool  t\tip"), tooltip);
    129 }
    130 
    131 }  // namespace corewm
    132 }  // namespace views
    133