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 #import "ios/web/web_view_counter_impl.h"
7 #import "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "ios/web/public/browser_state.h"
10 #include "ios/web/public/test/test_browser_state.h"
11 #import "ios/web/public/test/test_web_client.h"
12 #include "ios/web/public/test/test_web_thread_bundle.h"
13 #include "ios/web/public/test/web_test_util.h"
14 #include "ios/web/public/web_client.h"
15 #import "ios/web/public/web_view_counter.h"
16 #import "ios/web/public/web_view_creation_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h"
23 // A test fixture that sets up web-related classes for testing a
25 class WebViewCounterTest : public PlatformTest {
27 void SetUp() override {
28 PlatformTest::SetUp();
29 SetWebClient(&test_web_client_);
30 browser_state_.reset(new TestBrowserState());
32 void TearDown() override {
33 // The BrowserState needs to be destroyed first so that it is outlived by
34 // the WebThreadBundle.
35 browser_state_.reset();
36 SetWebClient(nullptr);
37 PlatformTest::TearDown();
40 // The BrowserState used for testing purposes.
41 scoped_ptr<BrowserState> browser_state_;
44 // Used to create TestWebThreads.
45 TestWebThreadBundle thread_bundle_;
46 // Required by web::CreateWebView/web::CreateWKWebView functions.
47 web::TestWebClient test_web_client_;
52 // Tests that WebViewCounter correctly maintains the count of WKWebViews.
53 TEST_F(WebViewCounterTest, WKWebViewCount) {
54 CR_TEST_REQUIRES_WK_WEB_VIEW();
56 WebViewCounterImpl* web_view_counter =
57 WebViewCounterImpl::FromBrowserState(browser_state_.get());
58 ASSERT_TRUE(web_view_counter);
60 base::scoped_nsobject<WKWebView> web_view_1(
61 web::CreateWKWebView(CGRectZero, browser_state_.get()));
62 web_view_counter->InsertWKWebView(web_view_1);
63 base::scoped_nsobject<WKWebView> web_view_2(
64 web::CreateWKWebView(CGRectZero, browser_state_.get()));
65 web_view_counter->InsertWKWebView(web_view_2);
67 EXPECT_EQ(2U, web_view_counter->GetWKWebViewCount());
69 // Deallocate WKWebViews.
71 EXPECT_EQ(1U, web_view_counter->GetWKWebViewCount());
73 EXPECT_EQ(0U, web_view_counter->GetWKWebViewCount());