1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/at_exit.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/files/file_path.h"
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/files/scoped_temp_dir.h"
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/observer_list.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/prefs/pref_service.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/run_loop.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/stl_util.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/value_conversions.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/download/chrome_download_manager_delegate.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/download/download_extensions.h"
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/download/download_prefs.h"
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/download/download_target_determiner.h"
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/download/download_target_info.h"
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/history/history_service.h"
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/history/history_service_factory.h"
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/pref_names.h"
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/chrome_render_view_host_test_harness.h"
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/testing_pref_service_syncable.h"
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/history/core/browser/history_types.h"
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/download_interrupt_reasons.h"
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/public/browser/render_process_host.h"
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/web_contents.h"
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/web_contents_delegate.h"
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/test/mock_download_item.h"
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/test/test_renderer_host.h"
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/test/web_contents_tester.h"
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "net/base/mime_util.h"
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(ENABLE_PLUGINS)
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/public/browser/plugin_service.h"
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/plugin_service_filter.h"
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/public/common/webplugininfo.h"
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::AnyNumber;
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::Invoke;
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::Ref;
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::Return;
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::ReturnRef;
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::ReturnRefOfCopy;
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::Truly;
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::WithArg;
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ::testing::_;
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using content::DownloadItem;
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace {
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// No-op delegate.
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class NullWebContentsDelegate : public content::WebContentsDelegate {
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NullWebContentsDelegate() {}
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual ~NullWebContentsDelegate() {}
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Google Mock action that posts a task to the current message loop that invokes
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// the first argument of the mocked method as a callback. Said argument must be
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// bound as that parameter.
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Example:
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//   class FooClass {
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//    public:
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//     virtual void Foo(base::Callback<void(bool)> callback);
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//   };
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//   ...
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//   EXPECT_CALL(mock_fooclass_instance, Foo(callback))
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//     .WillOnce(ScheduleCallback(false));
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ACTION_P(ScheduleCallback, result0) {
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result0));
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Similar to ScheduleCallback, but binds 2 arguments.
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ACTION_P2(ScheduleCallback2, result0, result1) {
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoop::current()->PostTask(
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FROM_HERE, base::Bind(arg0, result0, result1));
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Used with DownloadTestCase. Indicates the type of test case. The expectations
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// for the test is set based on the type.
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)enum TestCaseType {
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SAVE_AS,
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AUTOMATIC,
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FORCED  // Requires that forced_file_path be non-empty.
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Used with DownloadTestCase. Type of intermediate filename to expect.
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)enum TestCaseExpectIntermediate {
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_CRDOWNLOAD,   // Expect path/to/target.crdownload.
98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_UNCONFIRMED,  // Expect path/to/Unconfirmed xxx.crdownload.
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_LOCAL_PATH,   // Expect target path.
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Typical download test case. Used with
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// DownloadTargetDeterminerTest::RunTestCase().
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)struct DownloadTestCase {
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Type of test.
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  TestCaseType test_type;
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Expected danger type. Verified at the end of target determination.
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  content::DownloadDangerType expected_danger_type;
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Value of DownloadItem::GetURL()
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const char* url;
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Value of DownloadItem::GetMimeType()
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const char* mime_type;
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Should be non-empty if |test_type| == FORCED. Value of GetForcedFilePath().
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::FilePath::CharType* forced_file_path;
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Expected local path. Specified relative to the test download path.
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::FilePath::CharType* expected_local_path;
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Expected target disposition. If this is TARGET_DISPOSITION_PROMPT, then the
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // test run will expect ChromeDownloadManagerDelegate to prompt the user for a
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // download location.
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DownloadItem::TargetDisposition expected_disposition;
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Type of intermediate path to expect.
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  TestCaseExpectIntermediate expected_intermediate;
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
132a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)class MockDownloadTargetDeterminerDelegate
133a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    : public DownloadTargetDeterminerDelegate {
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MOCK_METHOD3(CheckDownloadUrl,
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               void(content::DownloadItem*, const base::FilePath&,
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    const CheckDownloadUrlCallback&));
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MOCK_METHOD3(NotifyExtensions,
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               void(content::DownloadItem*, const base::FilePath&,
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    const NotifyExtensionsCallback&));
141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MOCK_METHOD3(PromptUserForDownloadPath,
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               void(content::DownloadItem*, const base::FilePath&,
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    const FileSelectedCallback&));
144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MOCK_METHOD3(DetermineLocalPath,
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               void(DownloadItem*, const base::FilePath&,
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    const LocalPathCallback&));
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MOCK_METHOD5(ReserveVirtualPath,
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               void(DownloadItem*, const base::FilePath&, bool,
149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    DownloadPathReservationTracker::FilenameConflictAction,
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    const ReservedPathCallback&));
151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  MOCK_METHOD2(GetFileMimeType,
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)               void(const base::FilePath&,
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    const GetFileMimeTypeCallback&));
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetupDefaults() {
156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ON_CALL(*this, CheckDownloadUrl(_, _, _))
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillByDefault(WithArg<2>(
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            ScheduleCallback(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)));
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ON_CALL(*this, NotifyExtensions(_, _, _))
160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillByDefault(WithArg<2>(
161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            ScheduleCallback2(base::FilePath(),
162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              DownloadPathReservationTracker::UNIQUIFY)));
163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ON_CALL(*this, ReserveVirtualPath(_, _, _, _, _))
164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillByDefault(Invoke(
165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            &MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath));
166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ON_CALL(*this, PromptUserForDownloadPath(_, _, _))
167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillByDefault(Invoke(
168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            &MockDownloadTargetDeterminerDelegate::NullPromptUser));
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ON_CALL(*this, DetermineLocalPath(_, _, _))
170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillByDefault(Invoke(
171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            &MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath));
172f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ON_CALL(*this, GetFileMimeType(_, _))
173f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        .WillByDefault(WithArg<1>(
174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            ScheduleCallback("")));
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static void NullReserveVirtualPath(
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem* download,
179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const base::FilePath& virtual_path,
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      bool create_directory,
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadPathReservationTracker::FilenameConflictAction conflict_action,
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback);
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static void NullPromptUser(
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem* download, const base::FilePath& suggested_path,
185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const FileSelectedCallback& callback);
186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static void NullDetermineLocalPath(
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem* download, const base::FilePath& virtual_path,
188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const LocalPathCallback& callback);
189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class DownloadTargetDeterminerTest : public ChromeRenderViewHostTestHarness {
192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ::testing::Test
194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void SetUp() OVERRIDE;
195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void TearDown() OVERRIDE;
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Creates MockDownloadItem and sets up default expectations.
198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  content::MockDownloadItem* CreateActiveDownloadItem(
199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int32 id,
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const DownloadTestCase& test_case);
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Sets the AutoOpenBasedOnExtension user preference for |path|.
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void EnableAutoOpenBasedOnExtension(const base::FilePath& path);
204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Set the kDownloadDefaultDirectory managed preference to |path|.
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetManagedDownloadPath(const base::FilePath& path);
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Set the kPromptForDownload user preference to |prompt|.
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetPromptForDownload(bool prompt);
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Given the relative path |path|, returns the full path under the temporary
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // downloads directory.
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path);
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Run |test_case| using |item|.
216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void RunTestCase(const DownloadTestCase& test_case,
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                   const base::FilePath& initial_virtual_path,
218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   content::MockDownloadItem* item);
219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
220f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Runs |test_case| with |item|. When the DownloadTargetDeterminer is done,
221f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // returns the resulting DownloadTargetInfo.
222f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<DownloadTargetInfo> RunDownloadTargetDeterminer(
223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const base::FilePath& initial_virtual_path,
224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      content::MockDownloadItem* item);
225f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem
227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // will be created for each test case and destroyed when the test case is
228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // complete.
229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void RunTestCasesWithActiveItem(const DownloadTestCase test_cases[],
230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                  size_t test_case_count);
231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Verifies that |target_path|, |disposition|, |expected_danger_type| and
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |intermediate_path| matches the expectations of |test_case|. Posts
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |closure| to the current message loop when done.
235f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void VerifyDownloadTarget(const DownloadTestCase& test_case,
236f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                            const DownloadTargetInfo* target_info);
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::FilePath& test_download_dir() const {
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return test_download_dir_.path();
240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::FilePath& test_virtual_dir() const {
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return test_virtual_dir_;
244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MockDownloadTargetDeterminerDelegate* delegate() {
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return &delegate_;
248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DownloadPrefs* download_prefs() {
251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return download_prefs_.get();
252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DownloadPrefs> download_prefs_;
256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_;
257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NullWebContentsDelegate web_contents_delegate_;
258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::ScopedTempDir test_download_dir_;
259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath test_virtual_dir_;
260c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DownloadTargetDeterminerTest::SetUp() {
263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ChromeRenderViewHostTestHarness::SetUp();
264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  CHECK(profile());
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  download_prefs_.reset(new DownloadPrefs(profile()));
266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  web_contents()->SetDelegate(&web_contents_delegate_);
267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir());
268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual"));
269868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  download_prefs_->SetDownloadPath(test_download_dir());
270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  delegate_.SetupDefaults();
271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DownloadTargetDeterminerTest::TearDown() {
274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  download_prefs_.reset();
275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ChromeRenderViewHostTestHarness::TearDown();
276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)content::MockDownloadItem*
279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)DownloadTargetDeterminerTest::CreateActiveDownloadItem(
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int32 id,
281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase& test_case) {
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  content::MockDownloadItem* item =
283c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new ::testing::NiceMock<content::MockDownloadItem>();
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GURL download_url(test_case.url);
285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<GURL> url_chain;
286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  url_chain.push_back(download_url);
287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath forced_file_path =
288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetPathInDownloadDir(test_case.forced_file_path);
289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DownloadItem::TargetDisposition initial_disposition =
290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      (test_case.test_type == SAVE_AS) ?
291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT :
292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE;
293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(test_case.test_type == FORCED,
294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            !forced_file_path.empty());
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetBrowserContext())
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(profile()));
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetDangerType())
299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetForcedFilePath())
301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(ReturnRefOfCopy(forced_file_path));
302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetFullPath())
303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(ReturnRefOfCopy(base::FilePath()));
304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetHash())
305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(ReturnRefOfCopy(std::string()));
306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetId())
307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(id));
308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ON_CALL(*item, GetLastReason())
309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE));
310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetMimeType())
311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(test_case.mime_type));
312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetReferrerUrl())
313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(ReturnRefOfCopy(download_url));
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetState())
315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(DownloadItem::IN_PROGRESS));
316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetTargetDisposition())
317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(initial_disposition));
318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetTargetFilePath())
319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(ReturnRefOfCopy(base::FilePath()));
320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetTransitionType())
3211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      .WillByDefault(Return(ui::PAGE_TRANSITION_LINK));
322c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetURL())
323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(ReturnRefOfCopy(download_url));
324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetUrlChain())
325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(ReturnRefOfCopy(url_chain));
326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, GetWebContents())
327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(web_contents()));
328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, HasUserGesture())
329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(true));
330c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, IsDangerous())
331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(false));
332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*item, IsTemporary())
333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Return(false));
334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return item;
335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension(
338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& path) {
339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(download_prefs_->EnableAutoOpenBasedOnExtension(path));
340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DownloadTargetDeterminerTest::SetManagedDownloadPath(
343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& path) {
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  profile()->GetTestingPrefService()->
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SetManagedPref(prefs::kDownloadDefaultDirectory,
346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                     base::CreateFilePathValue(path));
347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) {
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  profile()->GetTestingPrefService()->
351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SetBoolean(prefs::kPromptForDownload, prompt);
352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir(
355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath::StringType& relative_path) {
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (relative_path.empty())
357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return base::FilePath();
358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath full_path(test_download_dir().Append(relative_path));
359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return full_path.NormalizePathSeparators();
360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DownloadTargetDeterminerTest::RunTestCase(
363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase& test_case,
364868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::FilePath& initial_virtual_path,
365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::MockDownloadItem* item) {
366f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<DownloadTargetInfo> target_info =
367f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      RunDownloadTargetDeterminer(initial_virtual_path, item);
368f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  VerifyDownloadTarget(test_case, target_info.get());
369f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
370f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
371f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void CompletionCallbackWrapper(
372f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const base::Closure& closure,
373f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_ptr<DownloadTargetInfo>* target_info_receiver,
374f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_ptr<DownloadTargetInfo> target_info) {
375f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  target_info_receiver->swap(target_info);
376f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::MessageLoop::current()->PostTask(FROM_HERE, closure);
377f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
378f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
379f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)scoped_ptr<DownloadTargetInfo>
380f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)DownloadTargetDeterminerTest::RunDownloadTargetDeterminer(
381f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const base::FilePath& initial_virtual_path,
382f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    content::MockDownloadItem* item) {
383f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<DownloadTargetInfo> target_info;
384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::RunLoop run_loop;
385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DownloadTargetDeterminer::Start(
386868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      item, initial_virtual_path, download_prefs_.get(), delegate(),
387f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::Bind(&CompletionCallbackWrapper,
388f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                 run_loop.QuitClosure(),
389f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                 &target_info));
390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  run_loop.Run();
391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ::testing::Mock::VerifyAndClearExpectations(delegate());
392f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return target_info.Pass();
393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem(
396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase test_cases[],
397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    size_t test_case_count) {
398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < test_case_count; ++i) {
399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    scoped_ptr<content::MockDownloadItem> item(
400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        CreateActiveDownloadItem(i, test_cases[i]));
401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Running test case " << i);
402868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    RunTestCase(test_cases[i], base::FilePath(), item.get());
403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
406f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void DownloadTargetDeterminerTest::VerifyDownloadTarget(
407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase& test_case,
408f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const DownloadTargetInfo* target_info) {
409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath expected_local_path(
410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetPathInDownloadDir(test_case.expected_local_path));
411f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_EQ(expected_local_path.value(), target_info->target_path.value());
412f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_EQ(test_case.expected_disposition, target_info->target_disposition);
413f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_EQ(test_case.expected_danger_type, target_info->danger_type);
414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  switch (test_case.expected_intermediate) {
416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case EXPECT_CRDOWNLOAD:
417f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      EXPECT_EQ(DownloadTargetDeterminer::GetCrDownloadPath(
418f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    target_info->target_path).value(),
419f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                target_info->intermediate_path.value());
420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case EXPECT_UNCONFIRMED:
423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // The paths (in English) look like: /path/Unconfirmed xxx.crdownload.
424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Of this, we only check that the path is:
425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1. Not "/path/target.crdownload",
426c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 2. Points to the same directory as the target.
427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 3. Has extension ".crdownload".
428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 4. Basename starts with "Unconfirmed ".
42990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      EXPECT_NE(DownloadTargetDeterminer::GetCrDownloadPath(expected_local_path)
43090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                    .value(),
431f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                target_info->intermediate_path.value());
432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(expected_local_path.DirName().value(),
433f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                target_info->intermediate_path.DirName().value());
434f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      EXPECT_TRUE(target_info->intermediate_path.MatchesExtension(
435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          FILE_PATH_LITERAL(".crdownload")));
436f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      EXPECT_EQ(0u,
437f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                target_info->intermediate_path.BaseName().value().find(
438f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    FILE_PATH_LITERAL("Unconfirmed ")));
439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
441c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case EXPECT_LOCAL_PATH:
442f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      EXPECT_EQ(expected_local_path.value(),
443f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                target_info->intermediate_path.value());
444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath(
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DownloadItem* download,
451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& virtual_path,
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool create_directory,
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DownloadPathReservationTracker::FilenameConflictAction conflict_action,
454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback) {
455c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback.Run(virtual_path, true);
456c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
457c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MockDownloadTargetDeterminerDelegate::NullPromptUser(
460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DownloadItem* download, const base::FilePath& suggested_path,
461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const FileSelectedCallback& callback) {
462c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback.Run(suggested_path);
463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
464c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
465c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath(
467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DownloadItem* download, const base::FilePath& virtual_path,
468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const LocalPathCallback& callback) {
469c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback.Run(virtual_path);
470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// NotifyExtensions implementation that overrides the path so that the target
473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// file is in a subdirectory called 'overridden'. If the extension is '.remove',
474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// the extension is removed.
475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void NotifyExtensionsOverridePath(
476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::DownloadItem* download,
477c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& path,
478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTargetDeterminerDelegate::NotifyExtensionsCallback&
479c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        callback) {
480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath new_path =
481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::FilePath()
482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .AppendASCII("overridden")
483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .Append(path.BaseName());
484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (new_path.MatchesExtension(FILE_PATH_LITERAL(".remove")))
485c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    new_path = new_path.RemoveExtension();
486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  callback.Run(new_path, DownloadPathReservationTracker::UNIQUIFY);
487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void CheckDownloadUrlCheckExes(
490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::DownloadItem* download,
491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& path,
492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTargetDeterminerDelegate::CheckDownloadUrlCallback&
493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        callback) {
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (path.MatchesExtension(FILE_PATH_LITERAL(".exe")))
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    callback.Run(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT);
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    callback.Run(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_Basic) {
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kBasicTestCases[] = {
502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic Safe
504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Save_As Safe
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
529c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 2: Automatic Dangerous
530c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
532a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://example.com/foo.crx", "",
533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
535a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 3: Forced Safe
543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
545c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "",
546c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
547c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
548c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
550c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
551c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
552c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
555a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The test assumes that .crx files have a danger level of
556868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // ALLOW_ON_USER_GESTURE.
557868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(download_util::ALLOW_ON_USER_GESTURE,
558c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            download_util::GetFileDangerLevel(
559a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                base::FilePath(FILE_PATH_LITERAL("foo.crx"))));
560c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kBasicTestCases, arraysize(kBasicTestCases));
561c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
563c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_CancelSaveAs) {
564c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kCancelSaveAsTestCases[] = {
565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Save_As Safe, Cancelled.
567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
568c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
569c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
570c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
571c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
572c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
573c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
574c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
575c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
576c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
578c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*delegate(), PromptUserForDownloadPath(_, _, _))
579c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(WithArg<2>(ScheduleCallback(base::FilePath())));
580c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kCancelSaveAsTestCases,
581c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kCancelSaveAsTestCases));
582c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
583c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
584c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// The SafeBrowsing check is performed early. Make sure that a download item
585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// that has been marked as DANGEROUS_URL behaves correctly.
586c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DangerousUrl) {
587c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kSafeBrowsingTestCases[] = {
588c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
589c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic Dangerous URL
590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
591c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
592c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.txt", "",
593c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
594c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
595c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
596c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
597c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
598c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
600c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
601c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
602c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Save As Dangerous URL
603c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
604c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
605c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.txt", "",
606c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
607c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
608c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
609c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
610c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
612c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
613c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
614c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
615c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 2: Forced Dangerous URL
616c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
617c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
618c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.txt", "",
619c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
620c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
621c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
622c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
624c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
625c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
626c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
627c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
628c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 3: Automatic Dangerous URL + Dangerous file. Dangerous URL takes
629cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // precedence.
630c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
631c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
632c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.html", "",
633c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
634c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
635c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.html"),
636c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
637c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
638c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
639c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
640c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
641c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
642c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 4: Save As Dangerous URL + Dangerous file
643c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
644c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
645c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.html", "",
646c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
647c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
648c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.html"),
649c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
650c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
651c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
652c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
653c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
654c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
655c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 5: Forced Dangerous URL + Dangerous file
656c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
657c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
658c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.html", "",
659c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.html"),
660c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
661c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.html"),
662c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
663c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
664c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
665c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
666c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
667c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
668c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*delegate(), CheckDownloadUrl(_, _, _))
669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(WithArg<2>(ScheduleCallback(
670c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL)));
671c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kSafeBrowsingTestCases,
672c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kSafeBrowsingTestCases));
673c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
674c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
675c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// The SafeBrowsing check is performed early. Make sure that a download item
676c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// that has been marked as MAYBE_DANGEROUS_CONTENT behaves correctly.
677c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_MaybeDangerousContent) {
678c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kSafeBrowsingTestCases[] = {
679c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic Maybe dangerous content
681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
682c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
683c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.exe", "",
684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
686c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.exe"),
687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
689c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
690c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Save As Maybe dangerous content
694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.exe", "",
697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.exe"),
700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 2: Forced Maybe dangerous content
707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
708c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://phishing.example.com/foo.exe", "",
710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.exe"),
711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.exe"),
713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
714c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
715c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
716c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
717c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
719c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*delegate(), CheckDownloadUrl(_, _, _))
720c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(WithArg<2>(ScheduleCallback(
721c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT)));
722c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kSafeBrowsingTestCases,
723c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kSafeBrowsingTestCases));
724c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
725c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
726c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test whether the last saved directory is used for 'Save As' downloads.
727c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_LastSavePath) {
728c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kLastSavePathTestCasesPre[] = {
729c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
730c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: If the last save path is empty, then the default download directory
731c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    should be used.
732c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
733c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
734c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
735c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
736c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
737c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
738c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
739c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
740c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
742c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
743c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
744c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // These test cases are run with a last save path set to a non-emtpy local
745c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // download directory.
746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kLastSavePathTestCasesPost[] = {
747c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
748c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: This test case is run with the last download directory set to
749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    '<test_download_dir()>/foo'.
750c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
753c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
754c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo/foo.txt"),
756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
757c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
762c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Start an automatic download. This should be saved to the user's
763c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    default download directory and not the last used Save As directory.
764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
765c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
766c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
767c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
768c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
769c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
770c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
771c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
775c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
776c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This test case is run with the last save path set to a non-empty virtual
777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // directory.
778c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kLastSavePathTestCasesVirtual[] = {
779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
782c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
783c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
785c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("bar.txt"),
786c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
787c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
788c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
789c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
790c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
791c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
792c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  {
793c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message()
794868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 << "Running with default download path");
795c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::FilePath prompt_path =
796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt"));
797c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _));
798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunTestCasesWithActiveItem(kLastSavePathTestCasesPre,
799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               arraysize(kLastSavePathTestCasesPre));
800c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
801c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
802c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Try with a non-empty last save path.
803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  {
804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message()
805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                 << "Running with local last_selected_directory");
806868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    download_prefs()->SetSaveFilePath(test_download_dir().AppendASCII("foo"));
807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::FilePath prompt_path =
808c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GetPathInDownloadDir(FILE_PATH_LITERAL("foo/foo.txt"));
809c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _));
810c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunTestCasesWithActiveItem(kLastSavePathTestCasesPost,
811c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               arraysize(kLastSavePathTestCasesPost));
812c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
813c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
814c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // And again, but this time use a virtual directory.
815c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  {
816c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message()
817c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                 << "Running with virtual last_selected_directory");
818c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::FilePath last_selected_dir = test_virtual_dir().AppendASCII("foo");
819c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::FilePath virtual_path = last_selected_dir.AppendASCII("foo.txt");
820868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    download_prefs()->SetSaveFilePath(last_selected_dir);
821c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(
822c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        _, last_selected_dir.AppendASCII("foo.txt"), _));
823c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), DetermineLocalPath(_, virtual_path, _))
824c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillOnce(WithArg<2>(ScheduleCallback(
825c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt")))));
826c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunTestCasesWithActiveItem(kLastSavePathTestCasesVirtual,
827c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               arraysize(kLastSavePathTestCasesVirtual));
828c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
829c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
830c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
831c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// These tests are run with the default downloads folder set to a virtual
832c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// directory.
833c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DefaultVirtual) {
834c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The default download directory is the virutal path.
835868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  download_prefs()->SetDownloadPath(test_virtual_dir());
836c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
837c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  {
838c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Automatic Safe Download");
839c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase kAutomaticDownloadToVirtualDir = {
840c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
841c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
842c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
843c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
844c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
845c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo-local.txt"),
846c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
847c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
848c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    };
850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _))
851c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillOnce(WithArg<2>(ScheduleCallback(
852c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt")))));
853c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunTestCasesWithActiveItem(&kAutomaticDownloadToVirtualDir, 1);
854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
855c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
856c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  {
857c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Save As to virtual directory");
858c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase kSaveAsToVirtualDir = {
859c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
860c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
861c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/bar.txt", "text/plain",
862c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
863c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
864c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo-local.txt"),
865c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
866c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
867c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
868c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    };
869c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _))
870c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillOnce(WithArg<2>(ScheduleCallback(
871c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt")))));
872c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(
873c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        _, test_virtual_dir().AppendASCII("bar.txt"), _))
874c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillOnce(WithArg<2>(ScheduleCallback(
875c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            test_virtual_dir().AppendASCII("prompted.txt"))));
876c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunTestCasesWithActiveItem(&kSaveAsToVirtualDir, 1);
877c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
878c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
879c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  {
880c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Save As to local directory");
881c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase kSaveAsToLocalDir = {
882c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
883c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
884c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/bar.txt", "text/plain",
885c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
886c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
887c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo-x.txt"),
888c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
889c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
890c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
891c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    };
892c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(
893c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        _, test_virtual_dir().AppendASCII("bar.txt"), _))
894c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .WillOnce(WithArg<2>(ScheduleCallback(
895c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            GetPathInDownloadDir(FILE_PATH_LITERAL("foo-x.txt")))));
896c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunTestCasesWithActiveItem(&kSaveAsToLocalDir, 1);
897c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
898c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
899c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  {
900c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Forced safe download");
901c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase kForcedSafe = {
902c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
903c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
904c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "",
905c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
906c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
907c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
908c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
909c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
910c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
911c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    };
912c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunTestCasesWithActiveItem(&kForcedSafe, 1);
913c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
914c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
915c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
916c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that an inactive download will still get a virtual or local download
917c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// path.
918c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_InactiveDownload) {
919c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kInactiveTestCases[] = {
920c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
921c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
922c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
923c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
924c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
925c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
926c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
928c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
929c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
932c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
934c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
944c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < arraysize(kInactiveTestCases); ++i) {
946c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Running test case " << i);
947c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DownloadTestCase& test_case = kInactiveTestCases[i];
948c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    scoped_ptr<content::MockDownloadItem> item(
949c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        CreateActiveDownloadItem(i, test_case));
95090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_CALL(*item.get(), GetState())
95190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        .WillRepeatedly(Return(content::DownloadItem::CANCELLED));
952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Even though one is a SAVE_AS download, no prompt will be displayed to
953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // the user because the download is inactive.
954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _))
955c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        .Times(0);
956868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    RunTestCase(test_case, base::FilePath(), item.get());
957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
959c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
960c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// If the reserved path could not be verified, then the user should see a
961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// prompt.
962c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ReservationFailed) {
963c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kReservationFailedCases[] = {
964c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
965c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic download. Since the reservation fails, the disposition of
966c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // the target is to prompt, but the returned path is used.
967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
968c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
969c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
970c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
971c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("bar.txt"),
973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
974c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
975c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
976c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
977c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
978c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
979c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Setup ReserveVirtualPath() to fail.
980c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*delegate(), ReserveVirtualPath(_, _, _, _, _))
981c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(WithArg<4>(ScheduleCallback2(
982c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt")), false)));
983c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kReservationFailedCases,
984c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kReservationFailedCases));
985c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
986c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
987c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// If the local path could not be determined, the download should be cancelled.
988c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_LocalPathFailed) {
989c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kLocalPathFailedCases[] = {
990c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
991c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic download.
992c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
993c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
994c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
995c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
996c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
997c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
998c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
999c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1000c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
1001c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1002c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1003c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1004c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The default download directory is the virtual path.
1005868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  download_prefs()->SetDownloadPath(test_virtual_dir());
1006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Simulate failed call to DetermineLocalPath.
1007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*delegate(), DetermineLocalPath(
1008c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      _, GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt")), _))
1009c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillOnce(WithArg<2>(ScheduleCallback(base::FilePath())));
1010c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kLocalPathFailedCases,
1011c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kLocalPathFailedCases));
1012c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1013c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1014868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Downloads that have a danger level of ALLOW_ON_USER_GESTURE should be marked
1015868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// as safe depending on whether there was a user gesture associated with the
1016c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// download and whether the referrer was visited prior to today.
1017c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_VisitedReferrer) {
1018c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kVisitedReferrerCases[] = {
1019c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // http://visited.example.com/ is added to the history as a visit that
1020c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // happened prior to today.
1021c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1022c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Safe download due to visiting referrer before.
1023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1024c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1025a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://visited.example.com/foo.crx", "application/xml",
1026c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1027c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1028a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1029c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1030c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1032c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1034c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1035c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Dangerous due to not having visited referrer before.
1036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1037c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1038a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://not-visited.example.com/foo.crx", "application/xml",
1039c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1040c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1041a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1042c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1043c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1044c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
1045c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1048c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 2: Safe because the user is being prompted.
1049c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
1050c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1051a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://not-visited.example.com/foo.crx", "application/xml",
1052c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1053c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1054a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1055c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
1056c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1057c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1058c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1059c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1060c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1061c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 3: Safe because of forced path.
1062c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
1063c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1064a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://not-visited.example.com/foo.crx", "application/xml",
1065a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1066c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1067a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1068c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1069c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1070c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
1071c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1072c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1073c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1074a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // This test assumes that the danger level of .crx files is
1075868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // ALLOW_ON_USER_GESTURE.
1076868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(download_util::ALLOW_ON_USER_GESTURE,
1077c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)            download_util::GetFileDangerLevel(
1078a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                base::FilePath(FILE_PATH_LITERAL("foo.crx"))));
1079c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1080c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // First the history service must exist.
1081bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  ASSERT_TRUE(profile()->CreateHistoryService(false, false));
1082c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1083c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GURL url("http://visited.example.com/visited-link.html");
1084c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The time of visit is picked to be several seconds prior to the most recent
1085c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // midnight.
1086c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::Time time_of_visit(
1087c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Time::Now().LocalMidnight() - base::TimeDelta::FromSeconds(10));
1088c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  HistoryService* history_service =
1089c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      HistoryServiceFactory::GetForProfile(profile(), Profile::EXPLICIT_ACCESS);
1090c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_TRUE(history_service);
1091c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  history_service->AddPage(url, time_of_visit, history::SOURCE_BROWSED);
1092c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1093c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kVisitedReferrerCases,
1094c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kVisitedReferrerCases));
1095c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1096c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1097c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// These test cases are run with "Prompt for download" user preference set to
1098c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// true.
1099c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways) {
1100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kPromptingTestCases[] = {
1101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Safe Automatic - Should prompt because of "Prompt for download"
1103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    preference setting.
1104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
1110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
1111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Safe Forced - Shouldn't prompt.
1117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
1118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
1121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
1123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
1126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 2: Automatic - The filename extension is marked as one that we will
1130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    open automatically. Shouldn't prompt.
1131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.dummy", "",
1134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.dummy"),
1137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetPromptForDownload(true);
1144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EnableAutoOpenBasedOnExtension(
1145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::FilePath(FILE_PATH_LITERAL("dummy.dummy")));
1146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kPromptingTestCases,
1147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kPromptingTestCases));
1148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if !defined(OS_ANDROID)
1151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// These test cases are run with "Prompt for download" user preference set to
1152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// true. Automatic extension downloads shouldn't cause prompting.
1153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Android doesn't support extensions.
1154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways_Extension) {
1155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kPromptingTestCases[] = {
1156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic Browser Extension download. - Shouldn't prompt for browser
1158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    extension downloads even if "Prompt for download" preference is set.
1159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.crx",
1162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::Extension::kMimeType,
1163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
1169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_WIN)
1172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Automatic User Script - Shouldn't prompt for user script downloads
1174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    even if "Prompt for download" preference is set. ".js" files are
1175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    considered dangerous on Windows.
1176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.user.js", "",
1179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.user.js"),
1182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
1185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else
1187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Automatic User Script - Shouldn't prompt for user script downloads
1189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    even if "Prompt for download" preference is set.
1190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.user.js", "",
1193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.user.js"),
1196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
1201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetPromptForDownload(true);
1204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kPromptingTestCases,
1205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kPromptingTestCases));
1206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
1208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// If the download path is managed, then we don't show any prompts.
1210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Note that if the download path is managed, then PromptForDownload() is false.
1211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ManagedPath) {
1212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kManagedPathTestCases[] = {
1213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic Safe
1215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
1221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Save_As Safe
1228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
1229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
1234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SetManagedDownloadPath(test_download_dir());
1241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_TRUE(download_prefs()->IsDownloadPathManaged());
1242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kManagedPathTestCases,
1243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kManagedPathTestCases));
1244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test basic functionality supporting extensions that want to override download
1247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// filenames.
1248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_NotifyExtensionsSafe) {
1249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kNotifyExtensionsTestCases[] = {
1250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic Safe
1252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("overridden/foo.txt"),
1258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1260c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Save_As Safe
1265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SAVE_AS,
1266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("overridden/foo.txt"),
1271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
1272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_CRDOWNLOAD
1274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 2: Automatic Dangerous
1278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1280a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://example.com/foo.crx", "",
1281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1283a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("overridden/foo.crx"),
1284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
1287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 3: Forced Safe
1291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FORCED,
1292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.txt", "",
1294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
1295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
1297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_LOCAL_PATH
1300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*delegate(), NotifyExtensions(_, _, _))
1304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Invoke(&NotifyExtensionsOverridePath));
1305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kNotifyExtensionsTestCases,
1306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kNotifyExtensionsTestCases));
1307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that filenames provided by extensions are passed into SafeBrowsing
1310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// checks and dangerous download checks.
1311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_NotifyExtensionsUnsafe) {
1312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kNotifyExtensionsTestCases[] = {
1313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 0: Automatic Safe : Later overridden by a dangerous filetype.
1315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1317a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://example.com/foo.crx.remove", "text/plain",
1318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1320a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("overridden/foo.crx"),
1321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1322c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
1324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
1327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // 1: Automatic Safe : Later overridden by a potentially dangerous
1328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // filetype.
1329c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      AUTOMATIC,
1330c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
1331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "http://example.com/foo.exe.remove", "text/plain",
1332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL(""),
1333c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("overridden/foo.exe"),
1335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_UNCONFIRMED
1338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    },
1339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*delegate(), NotifyExtensions(_, _, _))
1342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Invoke(&NotifyExtensionsOverridePath));
1343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ON_CALL(*delegate(), CheckDownloadUrl(_, _, _))
1344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillByDefault(Invoke(&CheckDownloadUrlCheckExes));
1345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  RunTestCasesWithActiveItem(kNotifyExtensionsTestCases,
1346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             arraysize(kNotifyExtensionsTestCases));
1347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that conflict actions set by extensions are passed correctly into
1350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// ReserveVirtualPath.
1351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest,
1352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       TargetDeterminer_NotifyExtensionsConflict) {
1353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kNotifyExtensionsTestCase = {
1354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    AUTOMATIC,
1355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    "http://example.com/foo.txt", "text/plain",
1357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FILE_PATH_LITERAL(""),
1358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FILE_PATH_LITERAL("overridden/foo.txt"),
1360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CRDOWNLOAD
1363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase& test_case = kNotifyExtensionsTestCase;
1366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<content::MockDownloadItem> item(
1367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CreateActiveDownloadItem(0, test_case));
1368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt"));
1369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath full_overridden_path =
1370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetPathInDownloadDir(overridden_path.value());
1371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1372c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // First case: An extension sets the conflict_action to OVERWRITE.
1373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillOnce(WithArg<2>(
1375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ScheduleCallback2(overridden_path,
1376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            DownloadPathReservationTracker::OVERWRITE)));
1377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*delegate(), ReserveVirtualPath(
1378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      _, full_overridden_path, true, DownloadPathReservationTracker::OVERWRITE,
1379c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      _)).WillOnce(WithArg<4>(
1380c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ScheduleCallback2(full_overridden_path, true)));
1381c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1382868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RunTestCase(test_case, base::FilePath(), item.get());
1383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Second case: An extension sets the conflict_action to PROMPT.
1385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillOnce(WithArg<2>(
1387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ScheduleCallback2(overridden_path,
1388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            DownloadPathReservationTracker::PROMPT)));
1389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*delegate(), ReserveVirtualPath(
1390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      _, full_overridden_path, true, DownloadPathReservationTracker::PROMPT, _))
1391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillOnce(WithArg<4>(
1392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ScheduleCallback2(full_overridden_path, true)));
1393868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RunTestCase(test_case, base::FilePath(), item.get());
1394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that relative paths returned by extensions are always relative to the
1397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// default downloads path.
1398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest,
1399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       TargetDeterminer_NotifyExtensionsDefaultPath) {
1400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase kNotifyExtensionsTestCase = {
1401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SAVE_AS,
1402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    "http://example.com/foo.txt", "text/plain",
1404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FILE_PATH_LITERAL(""),
1405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FILE_PATH_LITERAL("overridden/foo.txt"),
1407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DownloadItem::TARGET_DISPOSITION_PROMPT,
1408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CRDOWNLOAD
1410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
1411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DownloadTestCase& test_case = kNotifyExtensionsTestCase;
1413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<content::MockDownloadItem> item(
1414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CreateActiveDownloadItem(0, test_case));
1415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt"));
1416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath full_overridden_path =
1417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetPathInDownloadDir(overridden_path.value());
1418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1419868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  download_prefs()->SetSaveFilePath(GetPathInDownloadDir(
1420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FILE_PATH_LITERAL("last_selected")));
1421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillOnce(WithArg<2>(
1424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ScheduleCallback2(overridden_path,
1425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            DownloadPathReservationTracker::UNIQUIFY)));
1426c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*delegate(),
1427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              PromptUserForDownloadPath(_, full_overridden_path, _))
1428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillOnce(WithArg<2>(
1429c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          ScheduleCallback(full_overridden_path)));
1430868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RunTestCase(test_case, base::FilePath(), item.get());
1431c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1432868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1433868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(DownloadTargetDeterminerTest,
1434868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)       TargetDeterminer_InitialVirtualPathUnsafe) {
1435868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::FilePath::CharType* kInitialPath =
1436868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.html");
1437868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1438868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DownloadTestCase kInitialPathTestCase = {
1439868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // 0: Save As Save. The path generated based on the DownloadItem is safe,
1440868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // but the initial path is unsafe. However, the download is not considered
1441868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // dangerous since the user has been prompted.
1442868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SAVE_AS,
1443868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1444868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    "http://example.com/foo.txt", "text/plain",
1445868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FILE_PATH_LITERAL(""),
1446868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1447868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    kInitialPath,
1448868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DownloadItem::TARGET_DISPOSITION_PROMPT,
1449868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1450868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CRDOWNLOAD
1451868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
1452868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1453868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DownloadTestCase& test_case = kInitialPathTestCase;
1454868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<content::MockDownloadItem> item(
1455868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      CreateActiveDownloadItem(1, test_case));
1456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_CALL(*item, GetLastReason())
1457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      .WillRepeatedly(Return(
1458868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_CALL(*item, GetTargetDisposition())
1460868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_PROMPT));
1461868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1462868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Prompting behavior for resumed downloads is based on the last interrupt
1465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// reason. If the reason indicates that the target path may not be suitable for
1466868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be
1467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// prompted, and not otherwise. These test cases shouldn't result in prompting
1468868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// since the error is set to NETWORK_FAILED.
1469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedNoPrompt) {
1470868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1471868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // path.
1472868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::FilePath::CharType* kInitialPath =
1473868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt");
1474868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1475868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DownloadTestCase kResumedTestCases[] = {
1476868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // 0: Automatic Safe: Initial path is ignored since the user has not been
1478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // prompted before.
1479868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AUTOMATIC,
1480868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1481868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1482868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL(""),
1483868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1484868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
1485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1487868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      EXPECT_CRDOWNLOAD
1488868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1489868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1491868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // 1: Save_As Safe: Initial path used.
1492868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SAVE_AS,
1493868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1494868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1495868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL(""),
1496868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1497868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      kInitialPath,
1498868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
1499868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1500868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      EXPECT_CRDOWNLOAD
1501868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1502868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1503868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1504868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // 2: Automatic Dangerous: Initial path is ignored since the user hasn't
1505868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // been prompted before.
1506868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AUTOMATIC,
1507868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1508a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://example.com/foo.crx", "",
1509868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL(""),
1510868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1511a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1512868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1513868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1514868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      EXPECT_UNCONFIRMED
1515868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1516868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1517868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1518868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // 3: Forced Safe: Initial path is ignored due to the forced path.
1519868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FORCED,
1520868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1521868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "http://example.com/foo.txt", "",
1522868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
1523868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1524868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
1525868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1526868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1527868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      EXPECT_LOCAL_PATH
1528868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1529868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
1530868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1531a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The test assumes that .crx files have a danger level of
1532868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // ALLOW_ON_USER_GESTURE.
1533868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(download_util::ALLOW_ON_USER_GESTURE,
1534868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            download_util::GetFileDangerLevel(
1535a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                base::FilePath(FILE_PATH_LITERAL("foo.crx"))));
1536868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) {
1537868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Running test case " << i);
1538868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const DownloadTestCase& test_case = kResumedTestCases[i];
1539868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    scoped_ptr<content::MockDownloadItem> item(
1540868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        CreateActiveDownloadItem(i, test_case));
1541868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::FilePath expected_path =
1542868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        GetPathInDownloadDir(test_case.expected_local_path);
1543868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ON_CALL(*item.get(), GetLastReason())
1544868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .WillByDefault(Return(
1545868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1546868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Extensions should be notified if a new path is being generated and there
1547868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // is no forced path. In the test cases above, this is true for tests with
1548868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // type == AUTOMATIC.
1549868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1550868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1551868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1552868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _))
1553868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .Times(0);
1554868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1555868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1556868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1557868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1558868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1559868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1560868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test that a forced download doesn't prompt, even if the interrupt reason
1561868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// suggests that the target path may not be suitable for downloads.
1562868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedForcedDownload) {
1563868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::FilePath::CharType* kInitialPath =
1564868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt");
1565868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DownloadTestCase kResumedForcedDownload = {
1566868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // 3: Forced Safe
1567868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FORCED,
1568868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1569868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    "http://example.com/foo.txt", "",
1570868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FILE_PATH_LITERAL("forced-foo.txt"),
1571868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1572868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FILE_PATH_LITERAL("forced-foo.txt"),
1573868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1574868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_LOCAL_PATH
1576868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
1577868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1578868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DownloadTestCase& test_case = kResumedForcedDownload;
1579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::FilePath expected_path =
1580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      GetPathInDownloadDir(test_case.expected_local_path);
1581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<content::MockDownloadItem> item(
1582868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      CreateActiveDownloadItem(0, test_case));
1583868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ON_CALL(*item.get(), GetLastReason())
1584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
1585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1586868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1587868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1588868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _))
1589868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      .Times(0);
1590868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1591868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1592868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1593868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1594868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1595868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Prompting behavior for resumed downloads is based on the last interrupt
1596868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// reason. If the reason indicates that the target path may not be suitable for
1597868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be
1598868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// prompted, and not otherwise. These test cases result in prompting since the
1599868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// error is set to NO_SPACE.
1600868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedWithPrompt) {
1601868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1602868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // path.
1603868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::FilePath::CharType* kInitialPath =
1604868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt");
1605868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1606868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DownloadTestCase kResumedTestCases[] = {
1607868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1608868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // 0: Automatic Safe
1609868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AUTOMATIC,
1610868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1611868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1612868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL(""),
1613868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1614868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("foo.txt"),
1615868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
1616868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1617868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      EXPECT_CRDOWNLOAD
1618868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1619868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1620868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1621868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // 1: Save_As Safe
1622868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SAVE_AS,
1623868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1624868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "http://example.com/foo.txt", "text/plain",
1625868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL(""),
1626868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1627868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      kInitialPath,
1628868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
1629868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1630868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      EXPECT_CRDOWNLOAD
1631868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1632868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1633868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1634868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // 2: Automatic Dangerous
1635868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      AUTOMATIC,
1636868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1637a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "http://example.com/foo.crx", "",
1638868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL(""),
1639868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1640a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FILE_PATH_LITERAL("foo.crx"),
1641868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DownloadItem::TARGET_DISPOSITION_PROMPT,
1642868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1643868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      EXPECT_CRDOWNLOAD
1644868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1645868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
1646868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1647f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The test assumes that .xml files have a danger level of
1648868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // ALLOW_ON_USER_GESTURE.
1649868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(download_util::ALLOW_ON_USER_GESTURE,
1650868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            download_util::GetFileDangerLevel(
1651a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                base::FilePath(FILE_PATH_LITERAL("foo.crx"))));
1652868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) {
1653868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Running test case " << i);
1654868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    download_prefs()->SetSaveFilePath(test_download_dir());
1655868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const DownloadTestCase& test_case = kResumedTestCases[i];
1656868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::FilePath expected_path =
1657868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        GetPathInDownloadDir(test_case.expected_local_path);
1658868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    scoped_ptr<content::MockDownloadItem> item(
1659868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        CreateActiveDownloadItem(i, test_case));
1660868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ON_CALL(*item.get(), GetLastReason())
1661868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .WillByDefault(Return(
1662868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
1663868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1664868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1665868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1666868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _));
1667868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1668868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1669868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1670868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
1671868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
1672868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1673868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test intermediate filename generation for resumed downloads.
1674868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(DownloadTargetDeterminerTest,
1675868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)       TargetDeterminer_IntermediateNameForResumed) {
1676868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1677868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // path.
1678868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::FilePath::CharType kInitialPath[] =
1679868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt");
1680868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1681868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  struct IntermediateNameTestCase {
1682868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // General test case settings.
1683868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DownloadTestCase general;
1684868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1685868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Value of DownloadItem::GetFullPath() during test run, relative
1686868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // to test download path.
1687868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::FilePath::CharType* initial_intermediate_path;
1688868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1689868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Expected intermediate path relatvie to the test download path. An exact
1690868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // match is performed if this string is non-empty. Ignored otherwise.
1691868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const base::FilePath::CharType* expected_intermediate_path;
1692868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } kIntermediateNameTestCases[] = {
1693868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1694868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      {
1695868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        // 0: Automatic Safe
1696868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        AUTOMATIC,
1697868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1698868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        "http://example.com/foo.txt", "text/plain",
1699868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FILE_PATH_LITERAL(""),
1700868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1701868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FILE_PATH_LITERAL("foo.txt"),
1702868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1703868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1704868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        EXPECT_CRDOWNLOAD
1705868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      },
1706868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("bar.txt.crdownload"),
1707868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("foo.txt.crdownload")
1708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1709868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1710868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1711868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      {
1712868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        // 1: Save_As Safe
1713868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        SAVE_AS,
1714868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1715868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        "http://example.com/foo.txt", "text/plain",
1716868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FILE_PATH_LITERAL(""),
1717868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1718868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        kInitialPath,
1719868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_PROMPT,
1720868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1721868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        EXPECT_CRDOWNLOAD
1722868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      },
1723868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("foo.txt.crdownload"),
1724868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt.crdownload")
1725868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1726868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1727868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1728868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      {
1729868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        // 2: Automatic Dangerous
1730868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        AUTOMATIC,
1731868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1732a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        "http://example.com/foo.crx", "",
1733868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FILE_PATH_LITERAL(""),
1734868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1735a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        FILE_PATH_LITERAL("foo.crx"),
1736868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1737868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1738868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        EXPECT_UNCONFIRMED
1739868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      },
1740868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("Unconfirmed abcd.crdownload"),
1741868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("Unconfirmed abcd.crdownload")
1742868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1743868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1744868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1745868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      {
1746868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        // 3: Automatic Dangerous
1747868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        AUTOMATIC,
1748868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1749a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        "http://example.com/foo.crx", "",
1750868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FILE_PATH_LITERAL(""),
1751868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1752a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        FILE_PATH_LITERAL("foo.crx"),
1753868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1754868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1755868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        EXPECT_UNCONFIRMED
1756868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      },
1757868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("other_path/Unconfirmed abcd.crdownload"),
1758868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Rely on the EXPECT_UNCONFIRMED check in the general test settings. A
1759868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // new intermediate path of the form "Unconfirmed <number>.crdownload"
1760868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // should be generated for this case since the initial intermediate path
1761868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // is in the wrong directory.
1762868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("")
1763868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1764868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1765868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    {
1766868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      {
1767868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        // 3: Forced Safe: Initial path is ignored due to the forced path.
1768868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FORCED,
1769868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1770868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        "http://example.com/foo.txt", "",
1771868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FILE_PATH_LITERAL("forced-foo.txt"),
1772868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1773868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FILE_PATH_LITERAL("forced-foo.txt"),
1774868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1775868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1776868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        EXPECT_LOCAL_PATH
1777868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      },
1778868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt"),
1779868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FILE_PATH_LITERAL("forced-foo.txt")
1780868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    },
1781868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
1782868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1783a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The test assumes that .crx files have a danger level of
1784868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // ALLOW_ON_USER_GESTURE.
1785868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(download_util::ALLOW_ON_USER_GESTURE,
1786868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            download_util::GetFileDangerLevel(
1787a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                base::FilePath(FILE_PATH_LITERAL("foo.crx"))));
1788868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1789868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIntermediateNameTestCases); ++i) {
1790868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Running test case " << i);
1791868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const IntermediateNameTestCase& test_case = kIntermediateNameTestCases[i];
1792868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    scoped_ptr<content::MockDownloadItem> item(
1793868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        CreateActiveDownloadItem(i, test_case.general));
1794868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1795868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ON_CALL(*item.get(), GetLastReason())
1796868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .WillByDefault(Return(
1797868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1798868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ON_CALL(*item.get(), GetFullPath())
1799868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .WillByDefault(ReturnRefOfCopy(
1800868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            GetPathInDownloadDir(test_case.initial_intermediate_path)));
1801868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ON_CALL(*item.get(), GetDangerType())
1802868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        .WillByDefault(Return(test_case.general.expected_danger_type));
1803868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1804f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_ptr<DownloadTargetInfo> target_info =
1805f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath),
1806f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                    item.get());
1807f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    VerifyDownloadTarget(test_case.general, target_info.get());
1808f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    base::FilePath expected_intermediate_path =
1809f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        GetPathInDownloadDir(test_case.expected_intermediate_path);
1810f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (!expected_intermediate_path.empty())
1811f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      EXPECT_EQ(expected_intermediate_path, target_info->intermediate_path);
1812f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
1813f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
1814f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1815f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Test MIME type determination based on the target filename.
1816f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTest,
1817f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       TargetDeterminer_MIMETypeDetermination) {
1818f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1819f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // path.
1820f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const base::FilePath::CharType kInitialPath[] =
1821f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt");
1822f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1823f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  struct MIMETypeTestCase {
1824f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // General test case settings.
1825f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DownloadTestCase general;
1826f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1827f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Expected MIME type for test case.
1828f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const char* expected_mime_type;
1829f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } kMIMETypeTestCases[] = {
1830f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    {
1831f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      {
1832f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        // 0:
1833f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        AUTOMATIC,
1834f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1835f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "http://example.com/foo.png", "image/png",
1836f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL(""),
1837f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1838f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL("foo.png"),
1839f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1840f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1841f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        EXPECT_CRDOWNLOAD
1842f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      },
1843f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "image/png"
1844f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
1845f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    {
1846f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      {
1847f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        // 1: Empty MIME type in response.
1848f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        AUTOMATIC,
1849f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1850f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "http://example.com/foo.png", "",
1851f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL(""),
1852f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1853f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL("foo.png"),
1854f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1855f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1856f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        EXPECT_CRDOWNLOAD
1857f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      },
1858f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "image/png"
1859f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
1860f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    {
1861f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      {
1862f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        // 2: Forced path.
1863f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FORCED,
1864f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1865f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "http://example.com/foo.abc", "",
1866f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL("foo.png"),
1867f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1868f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL("foo.png"),
1869f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1870f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1871f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        EXPECT_CRDOWNLOAD
1872f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      },
1873f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "image/png"
1874f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
1875f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    {
1876f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      {
1877f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        // 3: Unknown file type.
1878f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        AUTOMATIC,
1879f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1880f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "http://example.com/foo.notarealext", "",
1881f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL(""),
1882f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1883f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL("foo.notarealext"),
1884f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1885f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1886f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        EXPECT_CRDOWNLOAD
1887f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      },
1888f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ""
1889f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
1890f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    {
1891f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      {
1892f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        // 4: Unknown file type.
1893f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        AUTOMATIC,
1894f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1895f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "http://example.com/foo.notarealext", "image/png",
1896f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL(""),
1897f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1898f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        FILE_PATH_LITERAL("foo.notarealext"),
1899f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1900f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1901f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        EXPECT_CRDOWNLOAD
1902f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      },
1903f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ""
1904f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    },
1905f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  };
1906f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1907f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ON_CALL(*delegate(), GetFileMimeType(
1908f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPathInDownloadDir(FILE_PATH_LITERAL("foo.png")), _))
1909f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .WillByDefault(WithArg<1>(
1910f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          ScheduleCallback("image/png")));
1911f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1912f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kMIMETypeTestCases); ++i) {
1913f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Running test case " << i);
1914f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const MIMETypeTestCase& test_case = kMIMETypeTestCases[i];
1915f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_ptr<content::MockDownloadItem> item(
1916f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        CreateActiveDownloadItem(i, test_case.general));
1917f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_ptr<DownloadTargetInfo> target_info =
1918f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath),
1919f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                    item.get());
1920f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    EXPECT_EQ(test_case.expected_mime_type, target_info->mime_type);
1921f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
1922f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
1923f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1924f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(ENABLE_PLUGINS)
1925f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1926f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void DummyGetPluginsCallback(
1927f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const base::Closure& closure,
1928f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const std::vector<content::WebPluginInfo>& plugins) {
1929f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  closure.Run();
1930f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
1931f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1932f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ForceRefreshOfPlugins() {
1933f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if !defined(OS_WIN)
1934f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Prevent creation of a utility process for loading plugins. Doing so breaks
1935f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // unit_tests since /proc/self/exe can't be run as a utility process.
1936f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::RenderProcessHost::SetRunRendererInProcess(true);
1937f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
1938f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::RunLoop run_loop;
1939f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::PluginService::GetInstance()->GetPlugins(
1940f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::Bind(&DummyGetPluginsCallback, run_loop.QuitClosure()));
1941f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  run_loop.Run();
1942f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if !defined(OS_WIN)
1943f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::RenderProcessHost::SetRunRendererInProcess(false);
1944f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
1945f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
1946f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
19475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockPluginServiceFilter : public content::PluginServiceFilter {
19485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
19495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(MockPluginAvailable, bool(const base::FilePath&));
19505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
19515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsPluginAvailable(int render_process_id,
19525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 int render_view_id,
19535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const void* context,
19545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const GURL& url,
19555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const GURL& policy_url,
19565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 content::WebPluginInfo* plugin) OVERRIDE {
19575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return MockPluginAvailable(plugin->path);
19585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1959f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
19605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool CanLoadPlugin(int render_process_id,
19615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             const base::FilePath& path) OVERRIDE {
19625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
19635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
19645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1965f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1966f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class ScopedRegisterInternalPlugin {
1967f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
1968f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ScopedRegisterInternalPlugin(content::PluginService* plugin_service,
1969f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               content::WebPluginInfo::PluginType type,
1970f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               const base::FilePath& path,
1971f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               const char* mime_type,
1972f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               const char* extension)
1973f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      : plugin_service_(plugin_service),
1974f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        plugin_path_(path) {
1975f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    content::WebPluginMimeType plugin_mime_type(mime_type,
1976f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                extension,
1977f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                "Test file");
1978f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    content::WebPluginInfo plugin_info(base::string16(),
1979f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                       path,
1980f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                       base::string16(),
1981f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                       base::string16());
1982f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    plugin_info.mime_types.push_back(plugin_mime_type);
1983f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    plugin_info.type = type;
1984f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1985f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    plugin_service->RegisterInternalPlugin(plugin_info, true);
1986f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    plugin_service->RefreshPlugins();
1987f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ForceRefreshOfPlugins();
1988f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
1989f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1990f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ~ScopedRegisterInternalPlugin() {
1991f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    plugin_service_->UnregisterInternalPlugin(plugin_path_);
1992f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    plugin_service_->RefreshPlugins();
1993f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ForceRefreshOfPlugins();
1994f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
1995f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1996f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const base::FilePath& path() { return plugin_path_; }
1997f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1998f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
1999f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::PluginService* plugin_service_;
2000f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::FilePath plugin_path_;
2001f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
2002f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2003f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// We use a slightly different test fixture for tests that touch plugins. SetUp
2004f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// needs to disable plugin discovery and we need to use a
2005f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// ShadowingAtExitManager to discard the tainted PluginService. Unfortunately,
2006f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// PluginService carries global state.
20075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class DownloadTargetDeterminerTestWithPlugin
20085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : public DownloadTargetDeterminerTest {
2009f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
20105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DownloadTargetDeterminerTestWithPlugin()
20115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : old_plugin_service_filter_(NULL) {}
20125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2013f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void SetUp() OVERRIDE {
20145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::PluginService* plugin_service =
20155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        content::PluginService::GetInstance();
20165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    plugin_service->Init();
20175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    plugin_service->DisablePluginsDiscoveryForTesting();
20185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    old_plugin_service_filter_ = plugin_service->GetFilter();
20195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    plugin_service->SetFilter(&mock_plugin_filter_);
2020f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DownloadTargetDeterminerTest::SetUp();
2021f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
2022f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
20235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
20245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::PluginService::GetInstance()->SetFilter(
20255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        old_plugin_service_filter_);
20265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DownloadTargetDeterminerTest::TearDown();
20275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
20285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2029f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) protected:
20305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::PluginServiceFilter* old_plugin_service_filter_;
20315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  testing::StrictMock<MockPluginServiceFilter> mock_plugin_filter_;
2032f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The ShadowingAtExitManager destroys the tainted PluginService instance.
2033f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::ShadowingAtExitManager at_exit_manager_;
2034f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
2035f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2036f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Check if secure handling of filetypes is determined correctly for PPAPI
2037f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// plugins.
2038f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTestWithPlugin,
2039f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       TargetDeterminer_CheckForSecureHandling_PPAPI) {
2040f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
2041f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // path.
2042f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const base::FilePath::CharType kInitialPath[] =
2043f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt");
2044f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const char kTestMIMEType[] = "application/x-example-should-not-exist";
2045f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2046f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DownloadTestCase kSecureHandlingTestCase = {
2047f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    AUTOMATIC,
2048f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2049f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    "http://example.com/foo.fakeext", "",
2050f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FILE_PATH_LITERAL(""),
2051f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2052f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FILE_PATH_LITERAL("foo.fakeext"),
2053f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2054f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2055f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    EXPECT_CRDOWNLOAD
2056f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  };
2057f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2058f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::PluginService* plugin_service =
2059f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      content::PluginService::GetInstance();
2060f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2061f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Verify our test assumptions.
2062f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  {
2063f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ForceRefreshOfPlugins();
2064f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    std::vector<content::WebPluginInfo> info;
2065f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ASSERT_FALSE(plugin_service->GetPluginInfoArray(
2066f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        GURL(), kTestMIMEType, false, &info, NULL));
2067f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ASSERT_EQ(0u, info.size())
2068f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        << "Name: " << info[0].name << ", Path: " << info[0].path.value();
2069f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
2070f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2071f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ON_CALL(*delegate(), GetFileMimeType(
2072f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _))
2073f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .WillByDefault(WithArg<1>(
2074f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          ScheduleCallback(kTestMIMEType)));
2075f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<content::MockDownloadItem> item(
2076f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      CreateActiveDownloadItem(1, kSecureHandlingTestCase));
2077f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<DownloadTargetInfo> target_info =
2078f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath),
2079f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  item.get());
20805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(target_info->is_filetype_handled_safely);
2081f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2082f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Register a PPAPI plugin. This should count as handling the filetype
2083f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // securely.
2084f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ScopedRegisterInternalPlugin ppapi_plugin(
2085f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      plugin_service,
2086f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS,
2087f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      test_download_dir().AppendASCII("ppapi"),
2088f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      kTestMIMEType,
2089f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "fakeext");
20905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(ppapi_plugin.path()))
20915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      .WillRepeatedly(Return(true));
2092f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2093f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  target_info = RunDownloadTargetDeterminer(
2094f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPathInDownloadDir(kInitialPath), item.get());
20955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(target_info->is_filetype_handled_safely);
2096f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2097f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Try disabling the plugin. Handling should no longer be considered secure.
20985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(ppapi_plugin.path()))
20995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      .WillRepeatedly(Return(false));
2100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  target_info = RunDownloadTargetDeterminer(
2101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPathInDownloadDir(kInitialPath), item.get());
21025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(target_info->is_filetype_handled_safely);
2103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Now register an unsandboxed PPAPI plug-in. This plugin should not be
2105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // considered secure.
2106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ScopedRegisterInternalPlugin ppapi_unsandboxed_plugin(
2107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      plugin_service,
2108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      content::WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED,
2109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      test_download_dir().AppendASCII("ppapi-nosandbox"),
2110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      kTestMIMEType,
2111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "fakeext");
21125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(mock_plugin_filter_,
21135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              MockPluginAvailable(ppapi_unsandboxed_plugin.path()))
21145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      .WillRepeatedly(Return(true));
2115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  target_info = RunDownloadTargetDeterminer(
2117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPathInDownloadDir(kInitialPath), item.get());
21185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(target_info->is_filetype_handled_safely);
2119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
2120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Check if secure handling of filetypes is determined correctly for NPAPI
2122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// plugins.
2123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(DownloadTargetDeterminerTestWithPlugin,
2124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)       TargetDeterminer_CheckForSecureHandling_NPAPI) {
2125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
2126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // path.
2127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const base::FilePath::CharType kInitialPath[] =
2128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      FILE_PATH_LITERAL("some_path/bar.txt");
2129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const char kTestMIMEType[] = "application/x-example-should-not-exist";
2130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2131f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DownloadTestCase kSecureHandlingTestCase = {
2132f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    AUTOMATIC,
2133f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    "http://example.com/foo.fakeext", "",
2135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FILE_PATH_LITERAL(""),
2136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FILE_PATH_LITERAL("foo.fakeext"),
2138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    EXPECT_CRDOWNLOAD
2141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  };
2142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::PluginService* plugin_service =
2144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      content::PluginService::GetInstance();
2145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Can't run this test if NPAPI isn't supported.
2147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!plugin_service->NPAPIPluginsSupported())
2148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
2149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Verify our test assumptions.
2151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  {
2152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ForceRefreshOfPlugins();
2153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    std::vector<content::WebPluginInfo> info;
2154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ASSERT_FALSE(plugin_service->GetPluginInfoArray(
2155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        GURL(), kTestMIMEType, false, &info, NULL));
2156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ASSERT_EQ(0u, info.size())
2157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        << "Name: " << info[0].name << ", Path: " << info[0].path.value();
2158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
2159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ON_CALL(*delegate(), GetFileMimeType(
2161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _))
2162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      .WillByDefault(WithArg<1>(
2163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          ScheduleCallback(kTestMIMEType)));
2164f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<content::MockDownloadItem> item(
2165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      CreateActiveDownloadItem(1, kSecureHandlingTestCase));
2166f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<DownloadTargetInfo> target_info =
2167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath),
2168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                  item.get());
21695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(target_info->is_filetype_handled_safely);
2170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2171f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Register a NPAPI plugin. This should not count as handling the filetype
2172f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // securely.
2173f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ScopedRegisterInternalPlugin npapi_plugin(
2174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      plugin_service,
2175f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      content::WebPluginInfo::PLUGIN_TYPE_NPAPI,
2176f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      test_download_dir().AppendASCII("npapi"),
2177f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      kTestMIMEType,
2178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "fakeext");
21795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(npapi_plugin.path()))
21805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      .WillRepeatedly(Return(true));
2181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2182f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  target_info = RunDownloadTargetDeterminer(
2183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetPathInDownloadDir(kInitialPath), item.get());
21845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(target_info->is_filetype_handled_safely);
2185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
21865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // defined(ENABLE_PLUGINS)
2187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace
2189