Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: chrome/browser/download/download_item_model_unittest.cc

Issue 2219953004: Refactor download image-MIME-type-detection code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add more tests Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
33 using ::testing::ReturnRefOfCopy; 34 using ::testing::ReturnRefOfCopy;
34 using ::testing::SetArgPointee; 35 using ::testing::SetArgPointee;
35 using ::testing::_; 36 using ::testing::_;
36 37
37 namespace { 38 namespace {
38 39
39 // Create a char array that has as many elements as there are download 40 // Create a char array that has as many elements as there are download
40 // interrupt reasons. We can then use that in a static_assert to make sure 41 // interrupt reasons. We can then use that in a static_assert to make sure
41 // that all the interrupt reason codes are accounted for. The reason codes are 42 // that all the interrupt reason codes are accounted for. The reason codes are
42 // unfortunately sparse, making this necessary. 43 // unfortunately sparse, making this necessary.
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 TEST_F(DownloadItemModelTest, DangerLevel) { 376 TEST_F(DownloadItemModelTest, DangerLevel) {
376 SetupDownloadItemDefaults(); 377 SetupDownloadItemDefaults();
377 378
378 // Default danger level is NOT_DANGEROUS. 379 // Default danger level is NOT_DANGEROUS.
379 EXPECT_EQ(DownloadFileType::NOT_DANGEROUS, model().GetDangerLevel()); 380 EXPECT_EQ(DownloadFileType::NOT_DANGEROUS, model().GetDangerLevel());
380 381
381 model().SetDangerLevel(DownloadFileType::ALLOW_ON_USER_GESTURE); 382 model().SetDangerLevel(DownloadFileType::ALLOW_ON_USER_GESTURE);
382 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, model().GetDangerLevel()); 383 EXPECT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE, model().GetDangerLevel());
383 } 384 }
384 385
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
385 TEST_F(DownloadItemModelTest, ShouldRemoveFromShelfWhenComplete) { 413 TEST_F(DownloadItemModelTest, ShouldRemoveFromShelfWhenComplete) {
386 const struct TestCase { 414 const struct TestCase {
387 DownloadItem::DownloadState state; 415 DownloadItem::DownloadState state;
388 bool is_dangerous; // Expectation for IsDangerous(). 416 bool is_dangerous; // Expectation for IsDangerous().
389 bool is_auto_open; // Expectation for GetOpenWhenComplete(). 417 bool is_auto_open; // Expectation for GetOpenWhenComplete().
390 bool auto_opened; // Whether the download was successfully 418 bool auto_opened; // Whether the download was successfully
391 // auto-opened. Expecation for GetAutoOpened(). 419 // auto-opened. Expecation for GetAutoOpened().
392 bool expected_result; 420 bool expected_result;
393 } kTestCases[] = { 421 } kTestCases[] = {
394 // All the valid combinations of state, is_dangerous, is_auto_open and 422 // All the valid combinations of state, is_dangerous, is_auto_open and
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 EXPECT_CALL(item(), GetAutoOpened()) 457 EXPECT_CALL(item(), GetAutoOpened())
430 .WillRepeatedly(Return(test_case.auto_opened)); 458 .WillRepeatedly(Return(test_case.auto_opened));
431 459
432 EXPECT_EQ(test_case.expected_result, 460 EXPECT_EQ(test_case.expected_result,
433 model().ShouldRemoveFromShelfWhenComplete()) 461 model().ShouldRemoveFromShelfWhenComplete())
434 << "Test case: " << i; 462 << "Test case: " << i;
435 Mock::VerifyAndClearExpectations(&item()); 463 Mock::VerifyAndClearExpectations(&item());
436 Mock::VerifyAndClearExpectations(&model()); 464 Mock::VerifyAndClearExpectations(&model());
437 } 465 }
438 } 466 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_item_model.cc ('k') | chrome/browser/download/notification/download_item_notification.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698