1 // Copyright 2015 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 "ios/web/webui/url_fetcher_block_adapter.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_status.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest_mac.h"
16 #include "testing/platform_test.h"
21 // Test fixture for URLFetcherBlockAdapter.
22 class URLFetcherBlockAdapterTest : public PlatformTest {
24 // Required for base::MessageLoop::current().
25 base::MessageLoopForUI loop_;
28 // Tests that URLFetcherBlockAdapter calls its completion handler with the
29 // appropriate data for a text resource.
30 TEST_F(URLFetcherBlockAdapterTest, FetchTextResource) {
31 GURL test_url("http://test");
32 std::string response("<html><body>Hello World!</body></html>");
33 NSData* expected_data =
34 [NSData dataWithBytes:response.c_str() length:response.size()];
35 web::URLFetcherBlockAdapterCompletion completion_handler =
36 ^(NSData* data, web::URLFetcherBlockAdapter* fetcher) {
37 EXPECT_NSEQ(expected_data, data);
39 web::URLFetcherBlockAdapter web_ui_fetcher(test_url, nil, completion_handler);
40 net::FakeURLFetcher fake_fetcher(test_url, &web_ui_fetcher, response,
42 net::URLRequestStatus::SUCCESS);
44 base::MessageLoop::current()->RunUntilIdle();
47 // Tests that URLFetcherBlockAdapter calls its completion handler with the
48 // appropriate data for a png resource.
49 TEST_F(URLFetcherBlockAdapterTest, FetchPNGResource) {
50 GURL test_url("http://test");
51 base::FilePath favicon_path;
52 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path));
53 favicon_path = favicon_path.AppendASCII("ios/web/test/data/testfavicon.png");
54 NSData* expected_data = [NSData
55 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())];
56 web::URLFetcherBlockAdapterCompletion completion_handler =
57 ^(NSData* data, URLFetcherBlockAdapter* fetcher) {
58 EXPECT_NSEQ(expected_data, data);
60 web::URLFetcherBlockAdapter web_ui_fetcher(test_url, nil, completion_handler);
62 EXPECT_TRUE(ReadFileToString(favicon_path, &response));
63 net::FakeURLFetcher fake_fetcher(test_url, &web_ui_fetcher, response,
65 net::URLRequestStatus::SUCCESS);
67 base::MessageLoop::current()->RunUntilIdle();