1 // Copyright 2016 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_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_
6 #define IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_
7 
8 #include <memory>
9 
10 #include "base/compiler_specific.h"
11 #import "base/ios/block_types.h"
12 #include "base/task/task_observer.h"
13 #include "ios/web/public/test/web_test.h"
14 #include "ui/base/page_transition_types.h"
15 #include "url/gurl.h"
16 
17 namespace web {
18 
19 class WebClient;
20 class WebState;
21 
22 // Base test fixture that provides WebState for testing.
23 class WebTestWithWebState : public WebTest, public base::TaskObserver {
24  public:
25   // Destroys underlying WebState. web_state() will return null after this call.
26   void DestroyWebState();
27 
28  protected:
29   explicit WebTestWithWebState(
30       WebTaskEnvironment::Options = WebTaskEnvironment::Options::DEFAULT);
31   WebTestWithWebState(
32       std::unique_ptr<web::WebClient> web_client,
33       WebTaskEnvironment::Options = WebTaskEnvironment::Options::DEFAULT);
34   ~WebTestWithWebState() override;
35 
36   // WebTest overrides.
37   void SetUp() override;
38   void TearDown() override;
39 
40   // Adds a pending item to the NavigationManager associated with the WebState.
41   void AddPendingItem(const GURL& url, ui::PageTransition transition);
42 
43   // Adds a transient item to the NavigationManager associated with the
44   // WebState.
45   void AddTransientItem(const GURL& url);
46 
47   // Loads the specified HTML content with URL into the WebState.
48   void LoadHtml(NSString* html, const GURL& url);
49   // Loads the specified HTML content into the WebState, using test url name.
50   void LoadHtml(NSString* html);
51   // Loads the specified HTML content into the WebState, using test url name.
52   bool LoadHtml(const std::string& html) WARN_UNUSED_RESULT;
53   // Loads the specified HTML content with URL into the WebState. None of the
54   // subresources will be fetched.
55   // This function is only supported on iOS11+. On iOS10, this function simply
56   // calls |LoadHtml|.
57   bool LoadHtmlWithoutSubresources(const std::string& html);
58   // Blocks until both known NSRunLoop-based and known message-loop-based
59   // background tasks have completed
60   void WaitForBackgroundTasks();
61   // Blocks until known NSRunLoop-based have completed, known message-loop-based
62   // background tasks have completed and |condition| evaluates to true.
63   void WaitForCondition(ConditionBlock condition);
64   // Synchronously executes JavaScript and returns result as id.
65   id ExecuteJavaScript(NSString* script);
66 
67   // Returns the base URL of the loaded page.
68   std::string BaseUrl() const;
69 
70   // Returns web state for this web controller.
71   web::WebState* web_state();
72   const web::WebState* web_state() const;
73 
74  private:
75   // base::TaskObserver overrides.
76   void WillProcessTask(const base::PendingTask& pending_task,
77                        bool was_blocked_or_low_priority) override;
78   void DidProcessTask(const base::PendingTask& pending_task) override;
79 
80   // The web state for testing.
81   std::unique_ptr<WebState> web_state_;
82   // true if a task has been processed.
83   bool processed_a_task_;
84 };
85 
86 }  // namespace web
87 
88 #endif  // IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_
89