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 "ui/message_center/views/bounded_label.h" | 5 #include "ui/message_center/views/bounded_label.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "ui/gfx/font.h" | 13 #include "ui/gfx/font_list.h" |
| 14 #include "ui/gfx/text_utils.h" |
14 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
15 | 16 |
16 namespace message_center { | 17 namespace message_center { |
17 | 18 |
18 namespace test { | 19 namespace test { |
19 | 20 |
20 /* Test fixture ***************************************************************/ | 21 /* Test fixture ***************************************************************/ |
21 | 22 |
22 class BoundedLabelTest : public testing::Test { | 23 class BoundedLabelTest : public testing::Test { |
23 public: | 24 public: |
24 BoundedLabelTest() { | 25 BoundedLabelTest() { |
25 digit_pixels_ = font_.GetStringWidth(UTF8ToUTF16("0")); | 26 digit_pixels_ = gfx::GetStringWidth(UTF8ToUTF16("0"), font_list_); |
26 space_pixels_ = font_.GetStringWidth(UTF8ToUTF16(" ")); | 27 space_pixels_ = gfx::GetStringWidth(UTF8ToUTF16(" "), font_list_); |
27 ellipsis_pixels_ = font_.GetStringWidth(UTF8ToUTF16("\xE2\x80\xA6")); | 28 ellipsis_pixels_ = gfx::GetStringWidth(UTF8ToUTF16("\xE2\x80\xA6"), |
| 29 font_list_); |
28 } | 30 } |
29 | 31 |
30 virtual ~BoundedLabelTest() {} | 32 virtual ~BoundedLabelTest() {} |
31 | 33 |
32 // Replaces all occurences of three periods ("...") in the specified string | 34 // Replaces all occurences of three periods ("...") in the specified string |
33 // with an ellipses character (UTF8 "\xE2\x80\xA6") and returns a string16 | 35 // with an ellipses character (UTF8 "\xE2\x80\xA6") and returns a string16 |
34 // with the results. This allows test strings to be specified as ASCII const | 36 // with the results. This allows test strings to be specified as ASCII const |
35 // char* strings, making tests more readable and easier to write. | 37 // char* strings, making tests more readable and easier to write. |
36 string16 ToString(const char* string) { | 38 string16 ToString(const char* string) { |
37 const string16 periods = UTF8ToUTF16("..."); | 39 const string16 periods = UTF8ToUTF16("..."); |
(...skipping 17 matching lines...) Expand all Loading... |
55 ellipsis_pixels_ * (width % 10); | 57 ellipsis_pixels_ * (width % 10); |
56 } | 58 } |
57 | 59 |
58 // Exercise BounderLabel::GetWrappedText() using the fixture's test label. | 60 // Exercise BounderLabel::GetWrappedText() using the fixture's test label. |
59 string16 GetWrappedText(int width) { | 61 string16 GetWrappedText(int width) { |
60 return label_->GetWrappedTextForTest(width, lines_); | 62 return label_->GetWrappedTextForTest(width, lines_); |
61 } | 63 } |
62 | 64 |
63 // Exercise BounderLabel::GetLinesForWidthAndLimit() using the test label. | 65 // Exercise BounderLabel::GetLinesForWidthAndLimit() using the test label. |
64 int GetLinesForWidth(int width) { | 66 int GetLinesForWidth(int width) { |
65 label_->SetBounds(0, 0, width, font_.GetHeight() * lines_); | 67 label_->SetBounds(0, 0, width, font_list_.GetHeight() * lines_); |
66 return label_->GetLinesForWidthAndLimit(width, lines_); | 68 return label_->GetLinesForWidthAndLimit(width, lines_); |
67 } | 69 } |
68 | 70 |
69 protected: | 71 protected: |
70 // Creates a label to test with. Returns this fixture, which can be used to | 72 // Creates a label to test with. Returns this fixture, which can be used to |
71 // test the newly created label using the exercise methods above. | 73 // test the newly created label using the exercise methods above. |
72 BoundedLabelTest& Label(string16 text, int lines) { | 74 BoundedLabelTest& Label(string16 text, int lines) { |
73 lines_ = lines; | 75 lines_ = lines; |
74 label_.reset(new BoundedLabel(text, font_)); | 76 label_.reset(new BoundedLabel(text, font_list_)); |
75 label_->SetLineLimit(lines_); | 77 label_->SetLineLimit(lines_); |
76 return *this; | 78 return *this; |
77 } | 79 } |
78 | 80 |
79 private: | 81 private: |
80 gfx::Font font_; // The default font, which will be used for tests. | 82 // The default font list, which will be used for tests. |
| 83 gfx::FontList font_list_; |
81 int digit_pixels_; | 84 int digit_pixels_; |
82 int space_pixels_; | 85 int space_pixels_; |
83 int ellipsis_pixels_; | 86 int ellipsis_pixels_; |
84 scoped_ptr<BoundedLabel> label_; | 87 scoped_ptr<BoundedLabel> label_; |
85 int lines_; | 88 int lines_; |
86 }; | 89 }; |
87 | 90 |
88 /* Test macro *****************************************************************/ | 91 /* Test macro *****************************************************************/ |
89 | 92 |
90 #define TEST_WRAP(expected, text, width, lines) \ | 93 #define TEST_WRAP(expected, text, width, lines) \ |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 211 |
209 // TODO(dharcourt): Add test cases to verify that: | 212 // TODO(dharcourt): Add test cases to verify that: |
210 // - SetMaxLines() affects the return values of some methods but not others. | 213 // - SetMaxLines() affects the return values of some methods but not others. |
211 // - Bound changes affects GetPreferredLines(), GetTextSize(), and | 214 // - Bound changes affects GetPreferredLines(), GetTextSize(), and |
212 // GetWrappedText() return values. | 215 // GetWrappedText() return values. |
213 // - GetTextFlags are as expected. | 216 // - GetTextFlags are as expected. |
214 | 217 |
215 } // namespace test | 218 } // namespace test |
216 | 219 |
217 } // namespace message_center | 220 } // namespace message_center |
OLD | NEW |