summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_danger_prompt_browsertest.cc
blob: e15c116ffdc228a5c252b3ebf21aebd379aad636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/file_path.h"
#include "chrome/browser/download/download_danger_prompt.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/mock_download_item.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::ByRef;
using ::testing::Eq;
using ::testing::Return;
using ::testing::SaveArg;

class DownloadDangerPromptTest : public InProcessBrowserTest {
 public:
  DownloadDangerPromptTest()
    : download_observer_(NULL),
      prompt_(NULL),
      expected_action_(DownloadDangerPrompt::CANCEL),
      did_receive_callback_(false) {
  }

  virtual ~DownloadDangerPromptTest() {
  }

  // Opens a new tab and waits for navigations to finish. If there are pending
  // navigations, the constrained prompt might be dismissed when the navigation
  // completes.
  void OpenNewTab() {
    ui_test_utils::NavigateToURLWithDisposition(
        browser(), GURL("about:blank"),
        NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
        ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
  }

  void SetUpExpectations(DownloadDangerPrompt::Action expected_action) {
    did_receive_callback_ = false;
    expected_action_ = expected_action;
    SetUpDownloadItemExpectations();
    CreatePrompt();
  }

  void VerifyExpectations() {
    ui_test_utils::RunAllPendingInMessageLoop();
    // At the end of each test, we expect no more activity from the prompt. The
    // prompt shouldn't exist anymore either.
    EXPECT_TRUE(did_receive_callback_);
    EXPECT_FALSE(prompt_);
    testing::Mock::VerifyAndClearExpectations(&download_);
  }

  void SimulatePromptAction(DownloadDangerPrompt::Action action) {
    prompt_->InvokeActionForTesting(action);
  }

  content::MockDownloadItem& download() { return download_; }

  content::DownloadItem::Observer* download_observer() {
    return download_observer_;
  }

  DownloadDangerPrompt* prompt() { return prompt_; }

 private:
  void SetUpDownloadItemExpectations() {
    EXPECT_CALL(download_, GetFileNameToReportUser()).WillRepeatedly(Return(
        FilePath(FILE_PATH_LITERAL("evil.exe"))));
    EXPECT_CALL(download_, AddObserver(_))
      .WillOnce(SaveArg<0>(&download_observer_));
    EXPECT_CALL(download_, RemoveObserver(Eq(ByRef(download_observer_))));
  }

  void CreatePrompt() {
    prompt_ = DownloadDangerPrompt::Create(
        &download_,
        browser()->GetActiveTabContents(),
        base::Bind(&DownloadDangerPromptTest::PromptCallback, this,
                   DownloadDangerPrompt::ACCEPT),
        base::Bind(&DownloadDangerPromptTest::PromptCallback, this,
                   DownloadDangerPrompt::CANCEL));
    ui_test_utils::RunAllPendingInMessageLoop();
  }

  void PromptCallback(DownloadDangerPrompt::Action action) {
    EXPECT_FALSE(did_receive_callback_);
    EXPECT_EQ(expected_action_, action);
    did_receive_callback_ = true;
    prompt_ = NULL;
  }

  content::MockDownloadItem download_;
  content::DownloadItem::Observer* download_observer_;
  DownloadDangerPrompt* prompt_;
  DownloadDangerPrompt::Action expected_action_;
  bool did_receive_callback_;

  DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptTest);
};

IN_PROC_BROWSER_TEST_F(DownloadDangerPromptTest, TestAll) {
  OpenNewTab();

  // The Accept action should cause the accept callback to be invoked.
  SetUpExpectations(DownloadDangerPrompt::ACCEPT);
  SimulatePromptAction(DownloadDangerPrompt::ACCEPT);
  VerifyExpectations();

  // The Discard action should cause the discard callback to be invoked.
  SetUpExpectations(DownloadDangerPrompt::CANCEL);
  SimulatePromptAction(DownloadDangerPrompt::CANCEL);
  VerifyExpectations();

  // If the download is no longer in-progress, the dialog should dismiss itself.
  SetUpExpectations(DownloadDangerPrompt::CANCEL);
  EXPECT_CALL(download(), IsInProgress()).WillOnce(Return(false));
  download_observer()->OnDownloadUpdated(&download());
  VerifyExpectations();

  // If the download is no longer dangerous (because it was accepted), the
  // dialog should dismiss itself.
  SetUpExpectations(DownloadDangerPrompt::CANCEL);
  EXPECT_CALL(download(), IsInProgress()).WillOnce(Return(true));
  EXPECT_CALL(download(), IsDangerous()).WillOnce(Return(false));
  download_observer()->OnDownloadUpdated(&download());
  VerifyExpectations();

  // If the containing tab is closed, the dialog should be canceled.
  OpenNewTab();
  SetUpExpectations(DownloadDangerPrompt::CANCEL);
  browser()->CloseTab();
  VerifyExpectations();
}