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.
4 
5 #ifndef IOS_WEB_TEST_WEB_INT_TEST_H_
6 #define IOS_WEB_TEST_WEB_INT_TEST_H_
7 
8 #import <WebKit/WebKit.h>
9 
10 #include "base/compiler_specific.h"
11 #import "base/ios/block_types.h"
12 #include "base/macros.h"
13 #import "ios/web/public/navigation/navigation_manager.h"
14 #import "ios/web/public/test/fakes/test_web_state_delegate.h"
15 #include "ios/web/public/test/web_test.h"
16 #import "ios/web/public/web_state.h"
17 
18 class GURL;
19 
20 namespace web {
21 
22 // A test fixture for integration tests that need a WebState which loads pages.
23 class WebIntTest : public WebTest {
24  protected:
25   WebIntTest();
26   ~WebIntTest() override;
27 
28   // WebTest methods.
29   void SetUp() override;
30   void TearDown() override;
31 
32   // The WebState and NavigationManager used by this test fixture.
web_state()33   WebState* web_state() { return web_state_.get(); }
navigation_manager()34   NavigationManager* navigation_manager() {
35     return web_state()->GetNavigationManager();
36   }
37 
38   // Returns the last committed NavigationItem in |navigation_manager|.
GetLastCommittedItem()39   NavigationItem* GetLastCommittedItem() {
40     return navigation_manager()->GetLastCommittedItem();
41   }
42 
43   // Synchronously executes |script| on |web_state|'s JS injection receiver and
44   // returns the result.
45   id ExecuteJavaScript(NSString* script);
46 
47   // Executes |block| and waits until |url| is successfully loaded in
48   // |web_state_|.
49   bool ExecuteBlockAndWaitForLoad(const GURL& url,
50                                   ProceduralBlock block) WARN_UNUSED_RESULT;
51 
52   // Navigates |web_state_| to |url| and waits for the page to be loaded.
53   bool LoadUrl(const GURL& url) WARN_UNUSED_RESULT;
54 
55   // Navigates |web_state_| using |params| and waits for the page to be loaded.
56   bool LoadWithParams(const NavigationManager::WebLoadParams& params)
57       WARN_UNUSED_RESULT;
58 
59   // Synchronously removes data from |data_store|.
60   // |websiteDataTypes| is from the constants defined in
61   // "WebKit/WKWebsiteDataRecord".
62   void RemoveWKWebViewCreatedData(WKWebsiteDataStore* data_store,
63                                   NSSet* websiteDataTypes);
64 
65   // Returns the index of |item| in the |navigation_manager|'s session history,
66   // or NSNotFound if it is not present.
67   NSInteger GetIndexOfNavigationItem(const web::NavigationItem* item);
68 
69   web::TestWebStateDelegate web_state_delegate_;
70 
71  private:
72   // WebState used to load pages.
73   std::unique_ptr<WebState> web_state_;
74 
75   DISALLOW_COPY_AND_ASSIGN(WebIntTest);
76 };
77 
78 }  // namespace web
79 
80 #endif  // IOS_WEB_TEST_WEB_INT_TEST_H_
81