summaryrefslogtreecommitdiffstats
path: root/components/web_view/web_view_apptest.cc
blob: 400f6ff0c95a116bfb1dbcf5d7ad0f5b2b4c5a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/web_view/public/cpp/web_view.h"

#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "components/mus/public/cpp/scoped_view_ptr.h"
#include "components/mus/public/cpp/tests/view_manager_test_base.h"
#include "components/mus/public/cpp/view.h"
#include "components/mus/public/cpp/view_tree_connection.h"
#include "mojo/util/filename_util.h"
#include "url/gurl.h"

namespace web_view {

namespace {
const char kTestOneFile[] = "test_one.html";
const char kTestOneTitle[] = "Test Title One";
const char kTestTwoFile[] = "test_two.html";
const char kTestTwoTitle[] = "Test Title Two";
const char kTestThreeFile[] = "test_three.html";
const char kTestThreeTitle[] = "Test Title Three";
}

class WebViewTest : public mus::ViewManagerTestBase,
                    public mojom::WebViewClient {
 public:
  WebViewTest() : web_view_(this) {}
  ~WebViewTest() override {}

  mojom::WebView* web_view() { return web_view_.web_view(); }

  const std::string& last_title() { return last_title_; }
  mojom::ButtonState last_back_button_state() {
    return last_back_button_state_;
  }
  mojom::ButtonState last_forward_button_state() {
    return last_forward_button_state_;
  }

  void StartNestedRunLoopUntilLoadingDone() {
    run_loop_.reset(new base::RunLoop);
    run_loop_->Run();
  }

  void NavigateTo(const std::string& file) {
    base::FilePath data_file;
    ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_file));
    data_file = data_file.AppendASCII("components/test/data/web_view")
                .AppendASCII(file).NormalizePathSeparators();
    ASSERT_TRUE(base::PathExists(data_file));
    mojo::URLRequestPtr request(mojo::URLRequest::New());
    request->url = mojo::util::FilePathToFileURL(data_file).spec();
    web_view()->LoadRequest(request.Pass());
    StartNestedRunLoopUntilLoadingDone();
  }

 private:
  void QuitNestedRunLoop() {
    if (run_loop_) {
      run_loop_->Quit();
    }
  }

  // Overridden from ApplicationDelegate:
  void Initialize(mojo::ApplicationImpl* app) override {
    ViewManagerTestBase::Initialize(app);
    app_ = app;
  }

  // Overridden from ViewTreeDelegate:
  void OnEmbed(mus::View* root) override {
    content_ = root->connection()->CreateView();
    root->AddChild(content_);
    content_->SetVisible(true);

    web_view_.Init(app_, content_);

    ViewManagerTestBase::OnEmbed(root);
  }

  void TearDown() override {
    mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot());
    ViewManagerTestBase::TearDown();
  }

  // Overridden from web_view::mojom::WebViewClient:
  void TopLevelNavigate(mojo::URLRequestPtr request) override {}
  void LoadingStateChanged(bool is_loading) override {
    if (is_loading == false)
      QuitNestedRunLoop();
  }
  void ProgressChanged(double progress) override {}
  void BackForwardChanged(mojom::ButtonState back_button,
                          mojom::ButtonState forward_button) override {
    last_back_button_state_ = back_button;
    last_forward_button_state_ = forward_button;
  }
  void TitleChanged(const mojo::String& title) override {
    last_title_ = title.get();
  }

  mojo::ApplicationImpl* app_;

  mus::View* content_;

  web_view::WebView web_view_;

  scoped_ptr<base::RunLoop> run_loop_;

  std::string last_title_;
  mojom::ButtonState last_back_button_state_;
  mojom::ButtonState last_forward_button_state_;

  DISALLOW_COPY_AND_ASSIGN(WebViewTest);
};

TEST_F(WebViewTest, TestTitleChanged) {
  ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile));

  // Our title should have been set on the navigation.
  EXPECT_EQ(kTestOneTitle, last_title());
}

TEST_F(WebViewTest, CanGoBackAndForward) {
  ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile));

  // We can't go back on first navigation since there's nothing previously on
  // the stack.
  EXPECT_EQ(kTestOneTitle, last_title());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED,
            last_back_button_state());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED,
            last_forward_button_state());

  ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile));

  EXPECT_EQ(kTestTwoTitle, last_title());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED,
            last_forward_button_state());

  web_view()->GoBack();
  StartNestedRunLoopUntilLoadingDone();

  EXPECT_EQ(kTestOneTitle, last_title());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED,
            last_back_button_state());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED,
            last_forward_button_state());

  web_view()->GoForward();
  StartNestedRunLoopUntilLoadingDone();
  EXPECT_EQ(kTestTwoTitle, last_title());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED,
            last_forward_button_state());
}

TEST_F(WebViewTest, NavigationClearsForward) {
  // First navigate somewhere, navigate somewhere else, and go back so we have
  // one item in the forward stack.
  ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile));
  ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile));

  web_view()->GoBack();
  StartNestedRunLoopUntilLoadingDone();

  EXPECT_EQ(kTestOneTitle, last_title());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED,
            last_back_button_state());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED,
            last_forward_button_state());

  // Now navigate to a third file. This should clear the forward stack.
  ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile));

  EXPECT_EQ(kTestThreeTitle, last_title());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state());
  EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED,
            last_forward_button_state());
}

}  // namespace web_view