| 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/download/download_item_model.h" | 5 #include "chrome/browser/download/download_item_model.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 24 #include "ui/base/text/bytes_formatting.h" | 24 #include "ui/base/text/bytes_formatting.h" |
| 25 #include "ui/gfx/font_list.h" | 25 #include "ui/gfx/font_list.h" |
| 26 #include "ui/gfx/text_utils.h" | 26 #include "ui/gfx/text_utils.h" |
| 27 | 27 |
| 28 using content::DownloadItem; | 28 using content::DownloadItem; |
| 29 using safe_browsing::DownloadFileType; | 29 using safe_browsing::DownloadFileType; |
| 30 using ::testing::Mock; | 30 using ::testing::Mock; |
| 31 using ::testing::NiceMock; | 31 using ::testing::NiceMock; |
| 32 using ::testing::Return; | 32 using ::testing::Return; |
| 33 using ::testing::ReturnRef; | |
| 34 using ::testing::ReturnRefOfCopy; | 33 using ::testing::ReturnRefOfCopy; |
| 35 using ::testing::SetArgPointee; | 34 using ::testing::SetArgPointee; |
| 36 using ::testing::_; | 35 using ::testing::_; |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 40 // Create a char array that has as many elements as there are download | 39 // Create a char array that has as many elements as there are download |
| 41 // interrupt reasons. We can then use that in a static_assert to make sure | 40 // interrupt reasons. We can then use that in a static_assert to make sure |
| 42 // that all the interrupt reason codes are accounted for. The reason codes are | 41 // that all the interrupt reason codes are accounted for. The reason codes are |
| 43 // unfortunately sparse, making this necessary. | 42 // unfortunately sparse, making this necessary. |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 TEST_F(DownloadItemModelTest, DangerLevel) { | 375 TEST_F(DownloadItemModelTest, DangerLevel) { |
| 377 SetupDownloadItemDefaults(); | 376 SetupDownloadItemDefaults(); |
| 378 | 377 |
| 379 // Default danger level is NOT_DANGEROUS. | 378 // Default danger level is NOT_DANGEROUS. |
| 380 EXPECT_EQ(DownloadFileType::NOT_DANGEROUS, model().GetDangerLevel()); | 379 EXPECT_EQ(DownloadFileType::NOT_DANGEROUS, model().GetDangerLevel()); |
| 381 | 380 |
| 382 model().SetDangerLevel(DownloadFileType::ALLOW_ON_USER_GESTURE); | 381 model().SetDangerLevel(DownloadFileType::ALLOW_ON_USER_GESTURE); |
| 383 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, model().GetDangerLevel()); | 382 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, model().GetDangerLevel()); |
| 384 } | 383 } |
| 385 | 384 |
| 386 TEST_F(DownloadItemModelTest, HasSupportedImageMimeType) { | |
| 387 SetupDownloadItemDefaults(); | |
| 388 | |
| 389 // When the item has a supported image MIME type, true should be returned. | |
| 390 ON_CALL(item(), GetMimeType()).WillByDefault(Return("image/png")); | |
| 391 EXPECT_TRUE(model().HasSupportedImageMimeType()); | |
| 392 | |
| 393 // An unsupported MIME type should result in false being returned... | |
| 394 ON_CALL(item(), GetMimeType()).WillByDefault(Return("image/unsupported")); | |
| 395 EXPECT_FALSE(model().HasSupportedImageMimeType()); | |
| 396 | |
| 397 // ... unless the target path has a well-known image extension. | |
| 398 const base::FilePath kImagePath(FILE_PATH_LITERAL("/foo/image.png")); | |
| 399 ON_CALL(item(), GetTargetFilePath()).WillByDefault(ReturnRef(kImagePath)); | |
| 400 EXPECT_TRUE(model().HasSupportedImageMimeType()); | |
| 401 | |
| 402 // .txt and missing extensions should also result in false being returned. | |
| 403 const base::FilePath kTextPath(FILE_PATH_LITERAL("/foo/image.txt")); | |
| 404 ON_CALL(item(), GetTargetFilePath()).WillByDefault(ReturnRef(kTextPath)); | |
| 405 EXPECT_FALSE(model().HasSupportedImageMimeType()); | |
| 406 | |
| 407 const base::FilePath kNoExtensionPath(FILE_PATH_LITERAL("/foo/image.")); | |
| 408 ON_CALL(item(), GetTargetFilePath()) | |
| 409 .WillByDefault(ReturnRef(kNoExtensionPath)); | |
| 410 EXPECT_FALSE(model().HasSupportedImageMimeType()); | |
| 411 } | |
| 412 | |
| 413 TEST_F(DownloadItemModelTest, ShouldRemoveFromShelfWhenComplete) { | 385 TEST_F(DownloadItemModelTest, ShouldRemoveFromShelfWhenComplete) { |
| 414 const struct TestCase { | 386 const struct TestCase { |
| 415 DownloadItem::DownloadState state; | 387 DownloadItem::DownloadState state; |
| 416 bool is_dangerous; // Expectation for IsDangerous(). | 388 bool is_dangerous; // Expectation for IsDangerous(). |
| 417 bool is_auto_open; // Expectation for GetOpenWhenComplete(). | 389 bool is_auto_open; // Expectation for GetOpenWhenComplete(). |
| 418 bool auto_opened; // Whether the download was successfully | 390 bool auto_opened; // Whether the download was successfully |
| 419 // auto-opened. Expecation for GetAutoOpened(). | 391 // auto-opened. Expecation for GetAutoOpened(). |
| 420 bool expected_result; | 392 bool expected_result; |
| 421 } kTestCases[] = { | 393 } kTestCases[] = { |
| 422 // All the valid combinations of state, is_dangerous, is_auto_open and | 394 // All the valid combinations of state, is_dangerous, is_auto_open and |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 EXPECT_CALL(item(), GetAutoOpened()) | 429 EXPECT_CALL(item(), GetAutoOpened()) |
| 458 .WillRepeatedly(Return(test_case.auto_opened)); | 430 .WillRepeatedly(Return(test_case.auto_opened)); |
| 459 | 431 |
| 460 EXPECT_EQ(test_case.expected_result, | 432 EXPECT_EQ(test_case.expected_result, |
| 461 model().ShouldRemoveFromShelfWhenComplete()) | 433 model().ShouldRemoveFromShelfWhenComplete()) |
| 462 << "Test case: " << i; | 434 << "Test case: " << i; |
| 463 Mock::VerifyAndClearExpectations(&item()); | 435 Mock::VerifyAndClearExpectations(&item()); |
| 464 Mock::VerifyAndClearExpectations(&model()); | 436 Mock::VerifyAndClearExpectations(&model()); |
| 465 } | 437 } |
| 466 } | 438 } |
| OLD | NEW |