| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/browser_theme_pack.h" | 5 #include "chrome/browser/browser_theme_pack.h" |
| 6 | 6 |
| 7 #include "app/gfx/color_utils.h" | 7 #include "app/gfx/color_utils.h" |
| 8 #include "app/theme_provider.h" | 8 #include "app/theme_provider.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 void LoadDisplayPropertiesJSON(const std::string& json) { | 98 void LoadDisplayPropertiesJSON(const std::string& json) { |
| 99 scoped_ptr<Value> value(base::JSONReader::Read(json, false)); | 99 scoped_ptr<Value> value(base::JSONReader::Read(json, false)); |
| 100 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); | 100 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); |
| 101 LoadDisplayPropertiesDictionary(static_cast<DictionaryValue*>(value.get())); | 101 LoadDisplayPropertiesDictionary(static_cast<DictionaryValue*>(value.get())); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void LoadDisplayPropertiesDictionary(DictionaryValue* value) { | 104 void LoadDisplayPropertiesDictionary(DictionaryValue* value) { |
| 105 theme_pack_->BuildDisplayPropertiesFromJSON(value); | 105 theme_pack_->BuildDisplayPropertiesFromJSON(value); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void ParseImageNames(const std::string& json, | 108 void ParseImageNamesJSON(const std::string& json, |
| 109 std::map<int, FilePath>* out_file_paths) { | 109 std::map<int, FilePath>* out_file_paths) { |
| 110 scoped_ptr<Value> value(base::JSONReader::Read(json, false)); | 110 scoped_ptr<Value> value(base::JSONReader::Read(json, false)); |
| 111 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); | 111 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); |
| 112 theme_pack_->ParseImageNamesFromJSON( | 112 ParseImageNamesDictionary(static_cast<DictionaryValue*>(value.get()), |
| 113 static_cast<DictionaryValue*>(value.get()), | 113 out_file_paths); |
| 114 FilePath(), out_file_paths); | 114 } |
| 115 |
| 116 void ParseImageNamesDictionary(DictionaryValue* value, |
| 117 std::map<int, FilePath>* out_file_paths) { |
| 118 theme_pack_->ParseImageNamesFromJSON(value, FilePath(), out_file_paths); |
| 115 } | 119 } |
| 116 | 120 |
| 117 FilePath GetStarGazingPath() { | 121 FilePath GetStarGazingPath() { |
| 118 FilePath test_path; | 122 FilePath test_path; |
| 119 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_path)) { | 123 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_path)) { |
| 120 NOTREACHED(); | 124 NOTREACHED(); |
| 121 return test_path; | 125 return test_path; |
| 122 } | 126 } |
| 123 | 127 |
| 124 test_path = test_path.AppendASCII("profiles"); | 128 test_path = test_path.AppendASCII("profiles"); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 149 EXPECT_TRUE(pack->GetTint(BrowserThemeProvider::TINT_BUTTONS, &actual)); | 153 EXPECT_TRUE(pack->GetTint(BrowserThemeProvider::TINT_BUTTONS, &actual)); |
| 150 EXPECT_DOUBLE_EQ(expected.h, actual.h); | 154 EXPECT_DOUBLE_EQ(expected.h, actual.h); |
| 151 EXPECT_DOUBLE_EQ(expected.s, actual.s); | 155 EXPECT_DOUBLE_EQ(expected.s, actual.s); |
| 152 EXPECT_DOUBLE_EQ(expected.l, actual.l); | 156 EXPECT_DOUBLE_EQ(expected.l, actual.l); |
| 153 | 157 |
| 154 int val; | 158 int val; |
| 155 EXPECT_TRUE(pack->GetDisplayProperty( | 159 EXPECT_TRUE(pack->GetDisplayProperty( |
| 156 BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT, &val)); | 160 BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT, &val)); |
| 157 EXPECT_EQ(BrowserThemeProvider::ALIGN_TOP, val); | 161 EXPECT_EQ(BrowserThemeProvider::ALIGN_TOP, val); |
| 158 | 162 |
| 163 // Every theme should have the following images, because they need to be |
| 164 // tinted. |
| 159 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME)); | 165 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME)); |
| 166 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INACTIVE)); |
| 167 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO)); |
| 168 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO_INACTIVE)); |
| 169 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND)); |
| 170 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_INCOGNITO)); |
| 160 | 171 |
| 161 // Make sure we don't have phantom data. | 172 // Make sure we don't have phantom data. |
| 162 EXPECT_FALSE(pack->GetColor(BrowserThemeProvider::COLOR_CONTROL_BACKGROUND, | 173 EXPECT_FALSE(pack->GetColor(BrowserThemeProvider::COLOR_CONTROL_BACKGROUND, |
| 163 &color)); | 174 &color)); |
| 164 EXPECT_FALSE(pack->GetTint(BrowserThemeProvider::TINT_FRAME, &actual)); | 175 EXPECT_FALSE(pack->GetTint(BrowserThemeProvider::TINT_FRAME, &actual)); |
| 165 } | 176 } |
| 166 | 177 |
| 167 MessageLoop message_loop; | 178 MessageLoop message_loop; |
| 168 ChromeThread fake_ui_thread; | 179 ChromeThread fake_ui_thread; |
| 169 ChromeThread fake_file_thread; | 180 ChromeThread fake_file_thread; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 EXPECT_TRUE(theme_pack_->GetDisplayProperty( | 274 EXPECT_TRUE(theme_pack_->GetDisplayProperty( |
| 264 BrowserThemeProvider::NTP_BACKGROUND_TILING, &out_val)); | 275 BrowserThemeProvider::NTP_BACKGROUND_TILING, &out_val)); |
| 265 EXPECT_EQ(BrowserThemeProvider::REPEAT_X, out_val); | 276 EXPECT_EQ(BrowserThemeProvider::REPEAT_X, out_val); |
| 266 | 277 |
| 267 EXPECT_TRUE(theme_pack_->GetDisplayProperty( | 278 EXPECT_TRUE(theme_pack_->GetDisplayProperty( |
| 268 BrowserThemeProvider::NTP_LOGO_ALTERNATE, &out_val)); | 279 BrowserThemeProvider::NTP_LOGO_ALTERNATE, &out_val)); |
| 269 EXPECT_EQ(0, out_val); | 280 EXPECT_EQ(0, out_val); |
| 270 } | 281 } |
| 271 | 282 |
| 272 TEST_F(BrowserThemePackTest, CanParsePaths) { | 283 TEST_F(BrowserThemePackTest, CanParsePaths) { |
| 273 std::string tint_json = "{ \"theme_button_background\": \"one\", " | 284 std::string path_json = "{ \"theme_button_background\": \"one\", " |
| 274 " \"theme_toolbar\": \"two\" }"; | 285 " \"theme_toolbar\": \"two\" }"; |
| 275 std::map<int, FilePath> out_file_paths; | 286 std::map<int, FilePath> out_file_paths; |
| 276 ParseImageNames(tint_json, &out_file_paths); | 287 ParseImageNamesJSON(path_json, &out_file_paths); |
| 277 | 288 |
| 278 EXPECT_EQ(2u, out_file_paths.size()); | 289 EXPECT_EQ(2u, out_file_paths.size()); |
| 279 EXPECT_TRUE(FilePath(FILE_PATH_LITERAL("one")) == out_file_paths[ | 290 // "12" and "5" are internal constants to BrowserThemePack and are |
| 280 ThemeResourcesUtil::GetId("theme_button_background")]); | 291 // PRS_THEME_BUTTON_BACKGROUND and PRS_THEME_TOOLBAR, but they are |
| 281 EXPECT_TRUE(FilePath(FILE_PATH_LITERAL("two")) == | 292 // implementation details that shouldn't be exported. |
| 282 out_file_paths[ThemeResourcesUtil::GetId("theme_toolbar")]); | 293 EXPECT_TRUE(FilePath(FILE_PATH_LITERAL("one")) == out_file_paths[12]); |
| 294 EXPECT_TRUE(FilePath(FILE_PATH_LITERAL("two")) == out_file_paths[5]); |
| 295 } |
| 296 |
| 297 TEST_F(BrowserThemePackTest, InvalidPathNames) { |
| 298 std::string path_json = "{ \"wrong\": [1], " |
| 299 " \"theme_button_background\": \"one\", " |
| 300 " \"not_a_thing\": \"blah\" }"; |
| 301 std::map<int, FilePath> out_file_paths; |
| 302 ParseImageNamesJSON(path_json, &out_file_paths); |
| 303 |
| 304 // We should have only parsed one valid path out of that mess above. |
| 305 EXPECT_EQ(1u, out_file_paths.size()); |
| 283 } | 306 } |
| 284 | 307 |
| 285 TEST_F(BrowserThemePackTest, InvalidColors) { | 308 TEST_F(BrowserThemePackTest, InvalidColors) { |
| 286 std::string invalid_color = "{ \"toolbar\": [\"dog\", \"cat\", [12]], " | 309 std::string invalid_color = "{ \"toolbar\": [\"dog\", \"cat\", [12]], " |
| 287 " \"sound\": \"woof\" }"; | 310 " \"sound\": \"woof\" }"; |
| 288 LoadColorJSON(invalid_color); | 311 LoadColorJSON(invalid_color); |
| 289 std::map<int, SkColor> colors = GetDefaultColorMap(); | 312 std::map<int, SkColor> colors = GetDefaultColorMap(); |
| 290 VerifyColorMap(colors); | 313 VerifyColorMap(colors); |
| 291 } | 314 } |
| 292 | 315 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 305 std::string invalid_properties = "{ \"ntp_background_alignment\": [15], " | 328 std::string invalid_properties = "{ \"ntp_background_alignment\": [15], " |
| 306 " \"junk\": [15.3] }"; | 329 " \"junk\": [15.3] }"; |
| 307 LoadDisplayPropertiesJSON(invalid_properties); | 330 LoadDisplayPropertiesJSON(invalid_properties); |
| 308 | 331 |
| 309 int out_val; | 332 int out_val; |
| 310 EXPECT_FALSE(theme_pack_->GetDisplayProperty( | 333 EXPECT_FALSE(theme_pack_->GetDisplayProperty( |
| 311 BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT, &out_val)); | 334 BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT, &out_val)); |
| 312 } | 335 } |
| 313 | 336 |
| 314 // These three tests should just not cause a segmentation fault. | 337 // These three tests should just not cause a segmentation fault. |
| 338 TEST_F(BrowserThemePackTest, NullPaths) { |
| 339 std::map<int, FilePath> out_file_paths; |
| 340 ParseImageNamesDictionary(NULL, &out_file_paths); |
| 341 } |
| 342 |
| 315 TEST_F(BrowserThemePackTest, NullTints) { | 343 TEST_F(BrowserThemePackTest, NullTints) { |
| 316 LoadTintDictionary(NULL); | 344 LoadTintDictionary(NULL); |
| 317 } | 345 } |
| 318 | 346 |
| 319 TEST_F(BrowserThemePackTest, NullColors) { | 347 TEST_F(BrowserThemePackTest, NullColors) { |
| 320 LoadColorDictionary(NULL); | 348 LoadColorDictionary(NULL); |
| 321 } | 349 } |
| 322 | 350 |
| 323 TEST_F(BrowserThemePackTest, NullDisplayProperties) { | 351 TEST_F(BrowserThemePackTest, NullDisplayProperties) { |
| 324 LoadDisplayPropertiesDictionary(NULL); | 352 LoadDisplayPropertiesDictionary(NULL); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 | 385 |
| 358 // Part 2: Try to read back the data pack that we just wrote to disk. | 386 // Part 2: Try to read back the data pack that we just wrote to disk. |
| 359 { | 387 { |
| 360 scoped_refptr<BrowserThemePack> pack = | 388 scoped_refptr<BrowserThemePack> pack = |
| 361 BrowserThemePack::BuildFromDataPack( | 389 BrowserThemePack::BuildFromDataPack( |
| 362 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); | 390 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); |
| 363 ASSERT_TRUE(pack.get()); | 391 ASSERT_TRUE(pack.get()); |
| 364 VerifyStarGazing(pack.get()); | 392 VerifyStarGazing(pack.get()); |
| 365 } | 393 } |
| 366 } | 394 } |
| OLD | NEW |