Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | 2 * Copyright (c) 2013, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 #include "core/fetch/ImageResource.h" | 37 #include "core/fetch/ImageResource.h" |
| 38 #include "core/fetch/MemoryCache.h" | 38 #include "core/fetch/MemoryCache.h" |
| 39 #include "core/fetch/MockResourceClients.h" | 39 #include "core/fetch/MockResourceClients.h" |
| 40 #include "core/fetch/RawResource.h" | 40 #include "core/fetch/RawResource.h" |
| 41 #include "core/fetch/ResourceLoader.h" | 41 #include "core/fetch/ResourceLoader.h" |
| 42 #include "platform/WebTaskRunner.h" | 42 #include "platform/WebTaskRunner.h" |
| 43 #include "platform/exported/WrappedResourceResponse.h" | 43 #include "platform/exported/WrappedResourceResponse.h" |
| 44 #include "platform/heap/Handle.h" | 44 #include "platform/heap/Handle.h" |
| 45 #include "platform/heap/HeapAllocator.h" | 45 #include "platform/heap/HeapAllocator.h" |
| 46 #include "platform/heap/Member.h" | 46 #include "platform/heap/Member.h" |
| 47 #include "platform/network/ResourceError.h" | |
| 47 #include "platform/network/ResourceRequest.h" | 48 #include "platform/network/ResourceRequest.h" |
| 48 #include "platform/network/ResourceTimingInfo.h" | 49 #include "platform/network/ResourceTimingInfo.h" |
| 49 #include "platform/scheduler/test/fake_web_task_runner.h" | 50 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 50 #include "platform/testing/URLTestHelpers.h" | 51 #include "platform/testing/URLTestHelpers.h" |
| 51 #include "platform/testing/weburl_loader_mock.h" | 52 #include "platform/testing/weburl_loader_mock.h" |
| 52 #include "platform/weborigin/KURL.h" | 53 #include "platform/weborigin/KURL.h" |
| 53 #include "public/platform/Platform.h" | 54 #include "public/platform/Platform.h" |
| 54 #include "public/platform/WebURLLoaderMockFactory.h" | 55 #include "public/platform/WebURLLoaderMockFactory.h" |
| 56 #include "public/platform/WebURLLoaderTestDelegate.h" | |
| 55 #include "public/platform/WebURLResponse.h" | 57 #include "public/platform/WebURLResponse.h" |
| 56 #include "testing/gtest/include/gtest/gtest.h" | 58 #include "testing/gtest/include/gtest/gtest.h" |
| 57 #include "wtf/Allocator.h" | 59 #include "wtf/Allocator.h" |
| 58 #include "wtf/PtrUtil.h" | 60 #include "wtf/PtrUtil.h" |
| 59 #include "wtf/Vector.h" | 61 #include "wtf/Vector.h" |
| 60 #include <memory> | 62 #include <memory> |
| 61 | 63 |
| 62 namespace blink { | 64 namespace blink { |
| 63 | 65 |
| 64 namespace { | 66 namespace { |
| (...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 709 FetchRequest(resourceRequest, FetchInitiatorInfo()); | 711 FetchRequest(resourceRequest, FetchInitiatorInfo()); |
| 710 Platform::current()->getURLLoaderMockFactory()->registerURL( | 712 Platform::current()->getURLLoaderMockFactory()->registerURL( |
| 711 url, WebURLResponse(), ""); | 713 url, WebURLResponse(), ""); |
| 712 Resource* newResource = RawResource::fetch(fetchRequest, fetcher); | 714 Resource* newResource = RawResource::fetch(fetchRequest, fetcher); |
| 713 fetcher->stopFetching(); | 715 fetcher->stopFetching(); |
| 714 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); | 716 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
| 715 | 717 |
| 716 EXPECT_NE(resource, newResource); | 718 EXPECT_NE(resource, newResource); |
| 717 } | 719 } |
| 718 | 720 |
| 721 // Wrapper for accessing private members of FontResource. | |
| 722 class FontResourceTestWrapper { | |
| 723 STACK_ALLOCATED(); | |
| 724 | |
| 725 public: | |
| 726 FontResourceTestWrapper(FontResource* resource) : m_resource(resource) {} | |
| 727 FontResource* operator->() const { return m_resource.get(); } | |
|
yhirano
2016/11/09 07:29:52
I generally don't like define operators: Are these
Shao-Chuan Lee
2016/11/09 08:04:31
Now using 1. and methods are made static. This sho
| |
| 728 operator FontResource*() const { return m_resource.get(); } | |
| 729 | |
| 730 void initLoadLimitState() { | |
| 731 m_resource->m_loadLimitState = FontResource::UnderLimit; | |
| 732 } | |
| 733 | |
| 734 void callFontLoadShortLimitCallback() { | |
| 735 m_resource->fontLoadShortLimitCallback(nullptr); | |
| 736 } | |
| 737 | |
| 738 void callFontLoadLongLimitCallback() { | |
| 739 m_resource->fontLoadLongLimitCallback(nullptr); | |
| 740 } | |
| 741 | |
| 742 private: | |
| 743 Member<FontResource> m_resource; | |
| 744 }; | |
| 745 | |
| 746 TEST_F(ResourceFetcherTest, CacheAwareFontLoading) { | |
| 747 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff"); | |
| 748 ResourceResponse response; | |
| 749 response.setURL(url); | |
| 750 response.setHTTPStatusCode(200); | |
| 751 Platform::current()->getURLLoaderMockFactory()->registerURL( | |
| 752 url, WrappedResourceResponse(response), ""); | |
| 753 | |
| 754 ResourceFetcher* fetcher = | |
| 755 ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create()); | |
| 756 | |
| 757 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); | |
| 758 fetchRequest.setCacheAwareLoadingEnabled(IsCacheAwareLoadingEnabled); | |
| 759 FontResourceTestWrapper resource(FontResource::fetch(fetchRequest, fetcher)); | |
| 760 ASSERT_TRUE(resource); | |
| 761 | |
| 762 Persistent<MockFontResourceClient> client = | |
| 763 new MockFontResourceClient(resource); | |
| 764 fetcher->startLoad(resource); | |
| 765 EXPECT_TRUE(resource->loader()->isCacheAwareLoadingActivated()); | |
| 766 resource.initLoadLimitState(); | |
| 767 | |
| 768 // FontResource callbacks should be blocked during cache-aware loading. | |
| 769 resource.callFontLoadShortLimitCallback(); | |
| 770 EXPECT_FALSE(client->fontLoadShortLimitExceededCalled()); | |
| 771 | |
| 772 // Fail first request as disk cache miss. | |
| 773 resource->loader()->didFail(ResourceError::cacheMissError(url)); | |
| 774 | |
| 775 // Once cache miss error returns, previously blocked callbacks should be | |
| 776 // called immediately. | |
| 777 EXPECT_FALSE(resource->loader()->isCacheAwareLoadingActivated()); | |
| 778 EXPECT_TRUE(client->fontLoadShortLimitExceededCalled()); | |
| 779 EXPECT_FALSE(client->fontLoadLongLimitExceededCalled()); | |
| 780 | |
| 781 // FontResource callbacks are not blocked now. | |
| 782 resource.callFontLoadLongLimitCallback(); | |
| 783 EXPECT_TRUE(client->fontLoadLongLimitExceededCalled()); | |
| 784 | |
| 785 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); | |
| 786 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); | |
| 787 memoryCache()->remove(resource); | |
| 788 } | |
| 789 | |
| 719 } // namespace blink | 790 } // namespace blink |
| OLD | NEW |