Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / fileicon_source_unittest.cc
blobbd5b555501f57b84cce487ceb11404eaf569030c
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;
17 namespace {
19 class TestFileIconSource : public FileIconSource {
20 public:
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 {
33 public:
34 FileIconSourceTest()
35 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()),
36 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {}
38 static TestFileIconSource* CreateFileIconSource() {
39 return new TestFileIconSource();
42 private:
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,
55 IconLoader::NORMAL },
56 { "foo?bar&scale=2x", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_200P,
57 IconLoader::NORMAL },
58 { "foo?iconsize=small", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P,
59 IconLoader::SMALL },
60 { "foo?iconsize=normal", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P,
61 IconLoader::NORMAL },
62 { "foo?iconsize=large", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P,
63 IconLoader::LARGE },
64 { "foo?iconsize=asdf", FILE_PATH_LITERAL("foo"), ui::SCALE_FACTOR_100P,
65 IconLoader::NORMAL },
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,
69 IconLoader::SMALL },
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,
74 IconLoader::LARGE },
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,
77 IconLoader::NORMAL },
78 #if defined(OS_WIN)
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,
82 IconLoader::NORMAL },
83 { "c%3A%2Fusers%2Ffoo%20user%2Fbar.txt",
84 FILE_PATH_LITERAL("c:\\users\\foo user\\bar.txt"), ui::SCALE_FACTOR_100P,
85 IconLoader::NORMAL },
86 { "c%3A%2Fusers%2F%C2%A9%202000.pdf",
87 FILE_PATH_LITERAL("c:\\users\\\xa9 2000.pdf"), ui::SCALE_FACTOR_100P,
88 IconLoader::NORMAL },
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,
91 IconLoader::NORMAL },
92 { "%2Ffoo%2Fbar", FILE_PATH_LITERAL("\\foo\\bar"), ui::SCALE_FACTOR_100P,
93 IconLoader::NORMAL },
94 { "%2Fbaz%20(1).txt?iconsize=small", FILE_PATH_LITERAL("\\baz (1).txt"),
95 ui::SCALE_FACTOR_100P, IconLoader::SMALL },
96 #else
97 { "/foo/bar/baz", FILE_PATH_LITERAL("/foo/bar/baz"), ui::SCALE_FACTOR_100P,
98 IconLoader::NORMAL },
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 },
109 #endif
112 // Test that the callback is NULL.
113 MATCHER(CallbackIsNull, "") {
114 return arg.is_null();
117 } // namespace
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(),
130 FetchFileIcon(
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,
135 callback);