2013-02-08 17:42:22 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework 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 <list>
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "include/base/cef_bind.h"
|
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2013-02-08 17:42:22 +01:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "tests/unittests/test_handler.h"
|
2013-02-08 17:42:22 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// How it works:
|
|
|
|
// 1. Load kTitleUrl1 (title should be kTitleStr1)
|
|
|
|
// 2. Load kTitleUrl2 (title should be kTitleStr2)
|
|
|
|
// 3. History back to kTitleUrl1 (title should be kTitleStr1)
|
|
|
|
// 4. History forward to kTitleUrl2 (title should be kTitleStr2)
|
|
|
|
// 5. Set title via JavaScript (title should be kTitleStr3)
|
|
|
|
|
|
|
|
const char kTitleUrl1[] = "http://tests-title/nav1.html";
|
|
|
|
const char kTitleUrl2[] = "http://tests-title/nav2.html";
|
|
|
|
const char kTitleStr1[] = "Title 1";
|
|
|
|
const char kTitleStr2[] = "Title 2";
|
|
|
|
const char kTitleStr3[] = "Title 3";
|
|
|
|
|
|
|
|
// Browser side.
|
|
|
|
class TitleTestHandler : public TestHandler {
|
|
|
|
public:
|
|
|
|
TitleTestHandler()
|
2015-11-10 21:18:16 +01:00
|
|
|
: step_(0),
|
|
|
|
got_title_change_(false),
|
|
|
|
got_loading_state_change_(false) {}
|
2013-02-08 17:42:22 +01:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2013-02-08 17:42:22 +01:00
|
|
|
// Add the resources that we will navigate to/from.
|
|
|
|
AddResource(kTitleUrl1,
|
|
|
|
"<html><head><title>" + std::string(kTitleStr1) +
|
|
|
|
"</title></head>Nav1</html>", "text/html");
|
|
|
|
AddResource(kTitleUrl2,
|
|
|
|
"<html><head><title>" + std::string(kTitleStr2) +
|
|
|
|
"</title></head>Nav2" +
|
|
|
|
"<script>function setTitle() { window.document.title = '" +
|
|
|
|
std::string(kTitleStr3) + "'; }</script>" +
|
|
|
|
"</html>", "text/html");
|
|
|
|
|
|
|
|
// Create the browser.
|
|
|
|
CreateBrowser(kTitleUrl1);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
2013-02-08 17:42:22 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnTitleChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& title) override {
|
2015-11-10 21:18:16 +01:00
|
|
|
// Ignore the 2nd OnTitleChange call which arrives after navigation
|
|
|
|
// completion.
|
|
|
|
if (got_title_change_)
|
|
|
|
return;
|
|
|
|
|
2013-02-08 17:42:22 +01:00
|
|
|
std::string title_str = title;
|
|
|
|
if (step_ == 0 || step_ == 2) {
|
|
|
|
EXPECT_STREQ(kTitleStr1, title_str.c_str());
|
|
|
|
} else if (step_ == 1 || step_ == 3) {
|
|
|
|
EXPECT_STREQ(kTitleStr2, title_str.c_str());
|
|
|
|
} else if (step_ == 4) {
|
|
|
|
EXPECT_STREQ(kTitleStr3, title_str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
got_title_[step_].yes();
|
|
|
|
|
2015-11-10 21:18:16 +01:00
|
|
|
if (step_ == 4) {
|
2013-02-08 17:42:22 +01:00
|
|
|
DestroyTest();
|
2015-11-10 21:18:16 +01:00
|
|
|
} else {
|
|
|
|
got_title_change_ = true;
|
|
|
|
NextIfReady(browser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool isLoading,
|
|
|
|
bool canGoBack,
|
|
|
|
bool canGoForward) override {
|
|
|
|
if (isLoading)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Call NextIfReady asynchronously because an additional call to
|
|
|
|
// OnTitleChange will be triggered later in the current call stack due to
|
|
|
|
// navigation completion and we want that call to arrive before execution of
|
|
|
|
// NextIfReady.
|
|
|
|
got_loading_state_change_ = true;
|
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::Bind(&TitleTestHandler::NextIfReady, this, browser));
|
2013-02-08 17:42:22 +01:00
|
|
|
}
|
|
|
|
|
2015-11-10 21:18:16 +01:00
|
|
|
private:
|
|
|
|
void NextIfReady(CefRefPtr<CefBrowser> browser) {
|
|
|
|
if (!got_title_change_ || !got_loading_state_change_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
got_title_change_ = false;
|
|
|
|
got_loading_state_change_ = false;
|
|
|
|
|
2013-02-08 17:42:22 +01:00
|
|
|
switch (step_++) {
|
|
|
|
case 0:
|
2015-11-10 21:18:16 +01:00
|
|
|
browser->GetMainFrame()->LoadURL(kTitleUrl2);
|
2013-02-08 17:42:22 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
browser->GoBack();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
browser->GoForward();
|
|
|
|
break;
|
|
|
|
case 3:
|
2015-11-10 21:18:16 +01:00
|
|
|
browser->GetMainFrame()->ExecuteJavaScript("setTitle()", kTitleUrl2, 0);
|
2013-02-08 17:42:22 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void DestroyTest() override {
|
2013-02-08 17:42:22 +01:00
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
EXPECT_TRUE(got_title_[i]) << "step " << i;
|
|
|
|
|
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
int step_;
|
|
|
|
|
2015-11-10 21:18:16 +01:00
|
|
|
bool got_title_change_;
|
|
|
|
bool got_loading_state_change_;
|
|
|
|
|
2013-02-08 17:42:22 +01:00
|
|
|
TrackCallback got_title_[5];
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TitleTestHandler);
|
2013-02-08 17:42:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Test title notifications.
|
|
|
|
TEST(DisplayTest, Title) {
|
|
|
|
CefRefPtr<TitleTestHandler> handler = new TitleTestHandler();
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-02-08 17:42:22 +01:00
|
|
|
}
|