OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #import "ios/shared/chrome/browser/tabs/web_state_list_fast_enumeration_helper.h
" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #import "ios/shared/chrome/browser/tabs/web_state_list.h" |
| 10 #import "ios/web/public/test/fakes/test_web_state.h" |
| 11 #import "net/base/mac/url_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/gtest_mac.h" |
| 14 #include "testing/platform_test.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." |
| 19 #endif |
| 20 |
| 21 namespace { |
| 22 const char kURL0[] = "https://chromium.org/0"; |
| 23 const char kURL1[] = "https://chromium.org/1"; |
| 24 const char kURL2[] = "https://chromium.org/2"; |
| 25 const char* const kURLs[] = {kURL0, kURL1, kURL2}; |
| 26 } // namespace |
| 27 |
| 28 @interface WebStateProxyFactoryForWebStateListFastEnumerationHelperTest |
| 29 : NSObject<WebStateProxyFactory> |
| 30 @end |
| 31 |
| 32 @implementation WebStateProxyFactoryForWebStateListFastEnumerationHelperTest |
| 33 |
| 34 - (id)proxyForWebState:(web::WebState*)webState { |
| 35 return net::NSURLWithGURL(webState->GetVisibleURL()); |
| 36 } |
| 37 |
| 38 @end |
| 39 |
| 40 class WebStateListFastEnumerationHelperTest : public PlatformTest { |
| 41 public: |
| 42 WebStateListFastEnumerationHelperTest() |
| 43 : web_state_list_(WebStateList::WebStateOwned) {} |
| 44 |
| 45 NSArray* ArrayFromWebStateList() { |
| 46 id<WebStateProxyFactory> proxy = |
| 47 [[WebStateProxyFactoryForWebStateListFastEnumerationHelperTest alloc] |
| 48 init]; |
| 49 WebStateListFastEnumerationHelper* helper = |
| 50 [[WebStateListFastEnumerationHelper alloc] |
| 51 initWithWebStateList:&web_state_list_ |
| 52 proxyFactory:proxy]; |
| 53 NSMutableArray* array = [NSMutableArray array]; |
| 54 for (id wrapper in helper) { |
| 55 // NSArray* cannot store a nil pointer. The NSFastEnumeration protocol |
| 56 // allows returning nil as one of the value in the iterated container, |
| 57 // so skip the value in that case (see NilWrapper test). |
| 58 if (wrapper) |
| 59 [array addObject:wrapper]; |
| 60 } |
| 61 return [array copy]; |
| 62 } |
| 63 |
| 64 void InsertTestWebStateForURLs(const char* const* urls, size_t count) { |
| 65 for (size_t index = 0; index < count; ++index) { |
| 66 auto test_web_state = base::MakeUnique<web::TestWebState>(); |
| 67 test_web_state->SetCurrentURL(GURL(urls[index])); |
| 68 |
| 69 web_state_list_.InsertWebState(web_state_list_.count(), |
| 70 test_web_state.release()); |
| 71 } |
| 72 } |
| 73 |
| 74 private: |
| 75 WebStateList web_state_list_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(WebStateListFastEnumerationHelperTest); |
| 78 }; |
| 79 |
| 80 TEST_F(WebStateListFastEnumerationHelperTest, Empty) { |
| 81 NSArray* array = ArrayFromWebStateList(); |
| 82 EXPECT_EQ(0u, [array count]); |
| 83 } |
| 84 |
| 85 TEST_F(WebStateListFastEnumerationHelperTest, FastEnumeration) { |
| 86 InsertTestWebStateForURLs(kURLs, arraysize(kURLs)); |
| 87 NSArray* array = ArrayFromWebStateList(); |
| 88 ASSERT_EQ(arraysize(kURLs), [array count]); |
| 89 for (size_t index = 0; index < arraysize(kURLs); ++index) { |
| 90 EXPECT_NSEQ([NSURL URLWithString:@(kURLs[index])], array[index]); |
| 91 } |
| 92 } |
| 93 |
| 94 TEST_F(WebStateListFastEnumerationHelperTest, NilWrapper) { |
| 95 const char* const urls[] = {""}; |
| 96 InsertTestWebStateForURLs(urls, arraysize(urls)); |
| 97 NSArray* array = ArrayFromWebStateList(); |
| 98 ASSERT_EQ(0u, [array count]); |
| 99 } |
OLD | NEW |