1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted_memory.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/icon_manager.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/webui/fileicon_source.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using content::BrowserThread
;
19 class TestFileIconSource
: public FileIconSource
{
21 explicit TestFileIconSource() {}
23 MOCK_METHOD4(FetchFileIcon
,
24 void(const base::FilePath
& path
,
25 ui::ScaleFactor scale_factor
,
26 IconLoader::IconSize icon_size
,
27 const content::URLDataSource::GotDataCallback
& callback
));
29 virtual ~TestFileIconSource() {}
32 class FileIconSourceTest
: public testing::Test
{
35 : ui_thread_(BrowserThread::UI
, base::MessageLoop::current()),
36 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {}
38 static TestFileIconSource
* CreateFileIconSource() {
39 return new TestFileIconSource();
43 base::MessageLoopForUI loop_
;
44 content::TestBrowserThread ui_thread_
;
45 content::TestBrowserThread file_thread_
;
48 const struct FetchFileIconExpectation
{
49 const char* request_path
;
50 const base::FilePath::CharType
* unescaped_path
;
51 ui::ScaleFactor scale_factor
;
52 IconLoader::IconSize size
;
53 } kBasicExpectations
[] = {
54 { "foo?bar", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P
,
56 { "foo?bar&scale=2x", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_200P
,
58 { "foo?iconsize=small", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P
,
60 { "foo?iconsize=normal", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P
,
62 { "foo?iconsize=large", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P
,
64 { "foo?iconsize=asdf", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P
,
66 { "foo?blah=b&iconsize=small", FILE_PATH_LITERAL("foo"),
67 ui::SCALE_FACTOR_100P
, IconLoader::SMALL
},
68 { "foo?blah&iconsize=small", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P
,
70 { "a%3Fb%26c%3Dd.txt?iconsize=small", FILE_PATH_LITERAL("a?b&c=d.txt"),
71 ui::SCALE_FACTOR_100P
, IconLoader::SMALL
},
72 { "a%3Ficonsize%3Dsmall?iconsize=large",
73 FILE_PATH_LITERAL("a?iconsize=small"), ui::SCALE_FACTOR_100P
,
75 { "o%40%23%24%25%26*()%20%2B%3D%3F%2C%3A%3B%22.jpg",
76 FILE_PATH_LITERAL("o@#$%&*() +=?,:;\".jpg"), ui::SCALE_FACTOR_100P
,
79 { "c:/foo/bar/baz", FILE_PATH_LITERAL("c:\\foo\\bar\\baz"),
80 ui::SCALE_FACTOR_100P
, IconLoader::NORMAL
},
81 { "/foo?bar=asdf&asdf", FILE_PATH_LITERAL("\\foo"), ui::SCALE_FACTOR_100P
,
83 { "c%3A%2Fusers%2Ffoo%20user%2Fbar.txt",
84 FILE_PATH_LITERAL("c:\\users\\foo user\\bar.txt"), ui::SCALE_FACTOR_100P
,
86 { "c%3A%2Fusers%2F%C2%A9%202000.pdf",
87 FILE_PATH_LITERAL("c:\\users\\\xa9 2000.pdf"), ui::SCALE_FACTOR_100P
,
89 { "%E0%B6%9A%E0%B6%BB%E0%B7%9D%E0%B6%B8%E0%B7%8A",
90 FILE_PATH_LITERAL("\x0d9a\x0dbb\x0ddd\x0db8\x0dca"), ui::SCALE_FACTOR_100P
,
92 { "%2Ffoo%2Fbar", FILE_PATH_LITERAL("\\foo\\bar"), ui::SCALE_FACTOR_100P
,
94 { "%2Fbaz%20(1).txt?iconsize=small", FILE_PATH_LITERAL("\\baz (1).txt"),
95 ui::SCALE_FACTOR_100P
, IconLoader::SMALL
},
97 { "/foo/bar/baz", FILE_PATH_LITERAL("/foo/bar/baz"), ui::SCALE_FACTOR_100P
,
99 { "/foo?bar", FILE_PATH_LITERAL("/foo"), ui::SCALE_FACTOR_100P
,
100 IconLoader::NORMAL
},
101 { "%2Ffoo%2f%E0%B6%9A%E0%B6%BB%E0%B7%9D%E0%B6%B8%E0%B7%8A",
102 FILE_PATH_LITERAL("/foo/\xe0\xb6\x9a\xe0\xb6\xbb\xe0\xb7\x9d")
103 FILE_PATH_LITERAL("\xe0\xb6\xb8\xe0\xb7\x8a"), ui::SCALE_FACTOR_100P
,
104 IconLoader::NORMAL
},
105 { "%2Ffoo%2Fbar", FILE_PATH_LITERAL("/foo/bar"), ui::SCALE_FACTOR_100P
,
106 IconLoader::NORMAL
},
107 { "%2Fbaz%20(1).txt?iconsize=small", FILE_PATH_LITERAL("/baz (1).txt"),
108 ui::SCALE_FACTOR_100P
, IconLoader::SMALL
},
112 // Test that the callback is NULL.
113 MATCHER(CallbackIsNull
, "") {
114 return arg
.is_null();
119 TEST_F(FileIconSourceTest
, FileIconSource_Parse
) {
120 std::vector
<ui::ScaleFactor
> supported_scale_factors
;
121 supported_scale_factors
.push_back(ui::SCALE_FACTOR_100P
);
122 supported_scale_factors
.push_back(ui::SCALE_FACTOR_200P
);
123 ui::test::ScopedSetSupportedScaleFactors
scoped_supported(
124 supported_scale_factors
);
126 for (unsigned i
= 0; i
< arraysize(kBasicExpectations
); i
++) {
127 scoped_ptr
<TestFileIconSource
> source(CreateFileIconSource());
128 content::URLDataSource::GotDataCallback callback
;
129 EXPECT_CALL(*source
.get(),
131 base::FilePath(kBasicExpectations
[i
].unescaped_path
),
132 kBasicExpectations
[i
].scale_factor
,
133 kBasicExpectations
[i
].size
, CallbackIsNull()));
134 source
->StartDataRequest(kBasicExpectations
[i
].request_path
, -1, -1,