Home | History | Annotate | Download | only in webui
      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.
      4 
      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"
     14 
     15 using content::BrowserThread;
     16 
     17 namespace {
     18 
     19 class TestFileIconSource : public FileIconSource {
     20  public:
     21   explicit TestFileIconSource() {}
     22 
     23   MOCK_METHOD4(FetchFileIcon,
     24                void(const base::FilePath& path,
     25                     float scale_factor,
     26                     IconLoader::IconSize icon_size,
     27                     const content::URLDataSource::GotDataCallback& callback));
     28 
     29   virtual ~TestFileIconSource() {}
     30 };
     31 
     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()) {}
     37 
     38   static TestFileIconSource* CreateFileIconSource() {
     39     return new TestFileIconSource();
     40   }
     41 
     42  private:
     43   base::MessageLoopForUI loop_;
     44   content::TestBrowserThread ui_thread_;
     45   content::TestBrowserThread file_thread_;
     46 };
     47 
     48 const struct FetchFileIconExpectation {
     49   const char* request_path;
     50   const base::FilePath::CharType* unescaped_path;
     51   float scale_factor;
     52   IconLoader::IconSize size;
     53 } kBasicExpectations[] = {
     54   { "foo?bar", FILE_PATH_LITERAL("foo"), 1.0f, IconLoader::NORMAL },
     55   { "foo?bar&scale=2x", FILE_PATH_LITERAL("foo"), 2.0f, IconLoader::NORMAL },
     56   { "foo?iconsize=small", FILE_PATH_LITERAL("foo"), 1.0f, IconLoader::SMALL },
     57   { "foo?iconsize=normal", FILE_PATH_LITERAL("foo"), 1.0f, IconLoader::NORMAL },
     58   { "foo?iconsize=large", FILE_PATH_LITERAL("foo"), 1.0f, IconLoader::LARGE },
     59   { "foo?iconsize=asdf", FILE_PATH_LITERAL("foo"), 1.0f, IconLoader::NORMAL },
     60   { "foo?blah=b&iconsize=small", FILE_PATH_LITERAL("foo"), 1.0f,
     61     IconLoader::SMALL },
     62   { "foo?blah&iconsize=small", FILE_PATH_LITERAL("foo"), 1.0f,
     63     IconLoader::SMALL },
     64   { "a%3Fb%26c%3Dd.txt?iconsize=small", FILE_PATH_LITERAL("a?b&c=d.txt"), 1.0f,
     65     IconLoader::SMALL },
     66   { "a%3Ficonsize%3Dsmall?iconsize=large",
     67     FILE_PATH_LITERAL("a?iconsize=small"), 1.0f, IconLoader::LARGE },
     68   { "o%40%23%24%25%26*()%20%2B%3D%3F%2C%3A%3B%22.jpg",
     69     FILE_PATH_LITERAL("o@#$%&*() +=?,:;\".jpg"), 1.0f, IconLoader::NORMAL },
     70 #if defined(OS_WIN)
     71   { "c:/foo/bar/baz", FILE_PATH_LITERAL("c:\\foo\\bar\\baz"), 1.0f,
     72     IconLoader::NORMAL },
     73   { "/foo?bar=asdf&asdf", FILE_PATH_LITERAL("\\foo"), 1.0f,
     74     IconLoader::NORMAL },
     75   { "c%3A%2Fusers%2Ffoo%20user%2Fbar.txt",
     76     FILE_PATH_LITERAL("c:\\users\\foo user\\bar.txt"), 1.0f,
     77     IconLoader::NORMAL },
     78   { "c%3A%2Fusers%2F%C2%A9%202000.pdf",
     79     FILE_PATH_LITERAL("c:\\users\\\xa9 2000.pdf"), 1.0f, IconLoader::NORMAL },
     80   { "%E0%B6%9A%E0%B6%BB%E0%B7%9D%E0%B6%B8%E0%B7%8A",
     81     FILE_PATH_LITERAL("\x0d9a\x0dbb\x0ddd\x0db8\x0dca"), 1.0f,
     82     IconLoader::NORMAL },
     83   { "%2Ffoo%2Fbar", FILE_PATH_LITERAL("\\foo\\bar"), 1.0f, IconLoader::NORMAL },
     84   { "%2Fbaz%20(1).txt?iconsize=small", FILE_PATH_LITERAL("\\baz (1).txt"),
     85     1.0f, IconLoader::SMALL },
     86 #else
     87   { "/foo/bar/baz", FILE_PATH_LITERAL("/foo/bar/baz"), 1.0f,
     88     IconLoader::NORMAL },
     89   { "/foo?bar", FILE_PATH_LITERAL("/foo"), 1.0f, IconLoader::NORMAL },
     90   { "%2Ffoo%2f%E0%B6%9A%E0%B6%BB%E0%B7%9D%E0%B6%B8%E0%B7%8A",
     91     FILE_PATH_LITERAL("/foo/\xe0\xb6\x9a\xe0\xb6\xbb\xe0\xb7\x9d")
     92     FILE_PATH_LITERAL("\xe0\xb6\xb8\xe0\xb7\x8a"), 1.0f, IconLoader::NORMAL },
     93   { "%2Ffoo%2Fbar", FILE_PATH_LITERAL("/foo/bar"), 1.0f, IconLoader::NORMAL },
     94   { "%2Fbaz%20(1).txt?iconsize=small", FILE_PATH_LITERAL("/baz (1).txt"), 1.0f,
     95     IconLoader::SMALL },
     96 #endif
     97 };
     98 
     99 // Test that the callback is NULL.
    100 MATCHER(CallbackIsNull, "") {
    101   return arg.is_null();
    102 }
    103 
    104 }  // namespace
    105 
    106 TEST_F(FileIconSourceTest, FileIconSource_Parse) {
    107   std::vector<ui::ScaleFactor> supported_scale_factors;
    108   supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
    109   supported_scale_factors.push_back(ui::SCALE_FACTOR_200P);
    110   ui::test::ScopedSetSupportedScaleFactors scoped_supported(
    111       supported_scale_factors);
    112 
    113   for (unsigned i = 0; i < arraysize(kBasicExpectations); i++) {
    114     scoped_ptr<TestFileIconSource> source(CreateFileIconSource());
    115     content::URLDataSource::GotDataCallback callback;
    116     EXPECT_CALL(*source.get(),
    117                 FetchFileIcon(
    118                     base::FilePath(kBasicExpectations[i].unescaped_path),
    119                     kBasicExpectations[i].scale_factor,
    120                     kBasicExpectations[i].size, CallbackIsNull()));
    121     source->StartDataRequest(kBasicExpectations[i].request_path, -1, -1,
    122                              callback);
    123   }
    124 }
    125