Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp

Issue 2453813004: WebFonts cache-aware timeout adaptation (Closed)
Patch Set: test callbacks in didAddClient(), rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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"
55 #include "public/platform/WebURLResponse.h" 56 #include "public/platform/WebURLResponse.h"
56 #include "testing/gtest/include/gtest/gtest.h" 57 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 FetchRequest(resourceRequest, FetchInitiatorInfo()); 710 FetchRequest(resourceRequest, FetchInitiatorInfo());
710 Platform::current()->getURLLoaderMockFactory()->registerURL( 711 Platform::current()->getURLLoaderMockFactory()->registerURL(
711 url, WebURLResponse(), ""); 712 url, WebURLResponse(), "");
712 Resource* newResource = RawResource::fetch(fetchRequest, fetcher); 713 Resource* newResource = RawResource::fetch(fetchRequest, fetcher);
713 fetcher->stopFetching(); 714 fetcher->stopFetching();
714 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); 715 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url);
715 716
716 EXPECT_NE(resource, newResource); 717 EXPECT_NE(resource, newResource);
717 } 718 }
718 719
720 // Helper for accessing private members of FontResource.
tkent 2016/11/15 05:59:23 You don't need such class. You should add FRIEND_
Shao-Chuan Lee 2016/11/15 06:47:45 Done, thanks for noting this!
721 class FontResourceTestHelper {
722 public:
723 static void initLoadLimitState(FontResource* resource) {
724 resource->m_loadLimitState = FontResource::UnderLimit;
725 }
726
727 static void callFontLoadShortLimitCallback(FontResource* resource) {
728 resource->fontLoadShortLimitCallback(nullptr);
729 }
730
731 static void callFontLoadLongLimitCallback(FontResource* resource) {
732 resource->fontLoadLongLimitCallback(nullptr);
733 }
734 };
735
736 TEST_F(ResourceFetcherTest, CacheAwareFontLoading) {
737 KURL url(ParsedURLString, "http://127.0.0.1:8000/font.woff");
738 ResourceResponse response;
739 response.setURL(url);
740 response.setHTTPStatusCode(200);
741 Platform::current()->getURLLoaderMockFactory()->registerURL(
742 url, WrappedResourceResponse(response), "");
743
744 ResourceFetcher* fetcher =
745 ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create());
746
747 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo());
748 fetchRequest.setCacheAwareLoadingEnabled(IsCacheAwareLoadingEnabled);
749 FontResource* resource = FontResource::fetch(fetchRequest, fetcher);
750 ASSERT_TRUE(resource);
751
752 Persistent<MockFontResourceClient> client =
753 new MockFontResourceClient(resource);
754 fetcher->startLoad(resource);
755 EXPECT_TRUE(resource->loader()->isCacheAwareLoadingActivated());
756 FontResourceTestHelper::initLoadLimitState(resource);
757
758 // FontResource callbacks should be blocked during cache-aware loading.
759 FontResourceTestHelper::callFontLoadShortLimitCallback(resource);
760 EXPECT_FALSE(client->fontLoadShortLimitExceededCalled());
761
762 // Fail first request as disk cache miss.
763 resource->loader()->didFail(ResourceError::cacheMissError(url));
764
765 // Once cache miss error returns, previously blocked callbacks should be
766 // called immediately.
767 EXPECT_FALSE(resource->loader()->isCacheAwareLoadingActivated());
768 EXPECT_TRUE(client->fontLoadShortLimitExceededCalled());
769 EXPECT_FALSE(client->fontLoadLongLimitExceededCalled());
770
771 // Add client now, fontLoadShortLimitExceeded() should be called.
772 Persistent<MockFontResourceClient> client2 =
773 new MockFontResourceClient(resource);
774 EXPECT_TRUE(client2->fontLoadShortLimitExceededCalled());
775 EXPECT_FALSE(client2->fontLoadLongLimitExceededCalled());
776
777 // FontResource callbacks are not blocked now.
778 FontResourceTestHelper::callFontLoadLongLimitCallback(resource);
779 EXPECT_TRUE(client->fontLoadLongLimitExceededCalled());
780
781 // Add client now, both callbacks should be called.
782 Persistent<MockFontResourceClient> client3 =
783 new MockFontResourceClient(resource);
784 EXPECT_TRUE(client3->fontLoadShortLimitExceededCalled());
785 EXPECT_TRUE(client3->fontLoadLongLimitExceededCalled());
786
787 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests();
788 Platform::current()->getURLLoaderMockFactory()->unregisterURL(url);
789 memoryCache()->remove(resource);
790 }
791
719 } // namespace blink 792 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698