Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / download / download_item_controller_unittest.mm
blob0302f4e622c6294a9794acf68552f25880668fd5
1 // Copyright 2014 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.
5 #import <Cocoa/Cocoa.h>
7 #import "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
11 #import "chrome/browser/ui/cocoa/download/download_item_button.h"
12 #import "chrome/browser/ui/cocoa/download/download_item_controller.h"
13 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
14 #include "content/public/test/mock_download_item.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17 #import "third_party/ocmock/OCMock/OCMock.h"
18 #import "third_party/ocmock/gtest_support.h"
20 using ::testing::Return;
21 using ::testing::ReturnRefOfCopy;
23 @interface DownloadItemController (DownloadItemControllerTest)
24 - (void)verifyProgressViewIsVisible:(bool)visible;
25 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible;
26 @end
28 @implementation DownloadItemController (DownloadItemControllerTest)
29 - (void)verifyProgressViewIsVisible:(bool)visible {
30   EXPECT_EQ(visible, ![progressView_ isHidden]);
33 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible {
34   EXPECT_EQ(visible, ![dangerousDownloadView_ isHidden]);
36 @end
38 @interface DownloadItemButton(DownloadItemButtonTest)
40 - (BOOL)showingContextMenu;
42 @end
44 namespace {
46 class DownloadItemControllerTest : public CocoaProfileTest {
47  public:
48   void SetUp() override {
49     CocoaProfileTest::SetUp();
50     ASSERT_TRUE(browser());
52     id shelf_controller =
53         [OCMockObject mockForClass:[DownloadShelfController class]];
54     shelf_.reset([shelf_controller retain]);
56     download_item_.reset(new ::testing::NiceMock<content::MockDownloadItem>);
57     ON_CALL(*download_item_, GetState())
58         .WillByDefault(Return(content::DownloadItem::IN_PROGRESS));
59     ON_CALL(*download_item_, GetFileNameToReportUser())
60         .WillByDefault(Return(base::FilePath()));
61     ON_CALL(*download_item_, GetDangerType())
62         .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
63     ON_CALL(*download_item_, GetTargetFilePath())
64         .WillByDefault(ReturnRefOfCopy(base::FilePath()));
65     ON_CALL(*download_item_, GetLastReason())
66         .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE));
67     ON_CALL(*download_item_, GetURL()).WillByDefault(ReturnRefOfCopy(GURL()));
68     ON_CALL(*download_item_, GetReferrerUrl())
69         .WillByDefault(ReturnRefOfCopy(GURL()));
70     ON_CALL(*download_item_, GetTargetDisposition()).WillByDefault(
71         Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE));
72   }
74   void TearDown() override {
75     download_item_.reset();
76     [(id)shelf_ verify];
77     CocoaProfileTest::TearDown();
78   }
80   DownloadItemController* CreateItemController() {
81     // In OSX 10.10, the owner of a nib file is retain/autoreleased during the
82     // initialization of the nib. Wrapping the constructor in an
83     // autoreleasepool ensures that tests can control the destruction timing of
84     // the DownloadItemController.
85     @autoreleasepool {
86       base::RunLoop run_loop;
87       base::scoped_nsobject<DownloadItemController> item(
88           [[DownloadItemController alloc] initWithDownload:download_item_.get()
89                                                      shelf:shelf_.get()
90                                                  navigator:NULL]);
92       [[test_window() contentView] addSubview:[item view]];
93       run_loop.RunUntilIdle();
94       return item.release();
95     }
96   }
98  protected:
99   scoped_ptr<content::MockDownloadItem> download_item_;
100   base::scoped_nsobject<DownloadShelfController> shelf_;
103 TEST_F(DownloadItemControllerTest, ShelfNotifiedOfOpenedDownload) {
104   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
105   [[(id)shelf_ expect] downloadWasOpened:[OCMArg any]];
106   download_item_->NotifyObserversDownloadOpened();
109 TEST_F(DownloadItemControllerTest, RemovesSelfWhenDownloadIsDestroyed) {
110   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
111   [[(id)shelf_ expect] remove:[OCMArg any]];
112   download_item_.reset();
115 TEST_F(DownloadItemControllerTest, NormalDownload) {
116   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
118   [item verifyProgressViewIsVisible:true];
119   [item verifyDangerousDownloadPromptIsVisible:false];
122 TEST_F(DownloadItemControllerTest, DangerousDownload) {
123   [[(id)shelf_ expect] layoutItems];
124   ON_CALL(*download_item_, GetDangerType())
125       .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
126   ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true));
127   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
129   [item verifyProgressViewIsVisible:false];
130   [item verifyDangerousDownloadPromptIsVisible:true];
133 TEST_F(DownloadItemControllerTest, NormalDownloadBecomesDangerous) {
134   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
136   [item verifyProgressViewIsVisible:true];
137   [item verifyDangerousDownloadPromptIsVisible:false];
139   // The download is now marked as dangerous.
140   [[(id)shelf_ expect] layoutItems];
141   ON_CALL(*download_item_, GetDangerType())
142       .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
143   ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true));
144   download_item_->NotifyObserversDownloadUpdated();
146   [item verifyProgressViewIsVisible:false];
147   [item verifyDangerousDownloadPromptIsVisible:true];
148   [(id)shelf_ verify];
150   // And then marked as safe again.
151   [[(id)shelf_ expect] layoutItems];
152   ON_CALL(*download_item_, GetDangerType())
153       .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
154   ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false));
155   download_item_->NotifyObserversDownloadUpdated();
157   [item verifyProgressViewIsVisible:true];
158   [item verifyDangerousDownloadPromptIsVisible:false];
159   [(id)shelf_ verify];
162 TEST_F(DownloadItemControllerTest, DismissesContextMenuWhenRemovedFromWindow) {
163   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
164   DownloadItemButton* downloadItemButton = nil;
165   for (NSView *nextSubview in [[item view] subviews]) {
166     if ([nextSubview isKindOfClass:[DownloadItemButton class]]) {
167       downloadItemButton = static_cast<DownloadItemButton *>(nextSubview);
168       break;
169     }
170   }
172   // showContextMenu: calls [NSMenu popUpContextMenu:...], which blocks until
173   // the menu is dismissed. Use a block to cancel the menu while waiting for
174   // [NSMenu popUpContextMenu:...] to return (this block will execute on the
175   // main thread, on the next pass through the run loop).
176   [[NSOperationQueue mainQueue] addOperationWithBlock:^{
177     EXPECT_TRUE([downloadItemButton showingContextMenu]);
178     // Simulate the item's removal from the shelf. Ideally we would call an
179     // actual shelf removal method like [item remove] but the shelf and
180     // download item are mock objects.
181     [downloadItemButton removeFromSuperview];
182   }];
183   // The unit test will stop here until the block causes the DownloadItemButton
184   // to dismiss the menu.
185   [item showContextMenu:nil];
189 }  // namespace