This source file includes following definitions.
- load_stop_notification_count_
- pdf_test_server
- load_stop_notification_count
- GetPDFTestDir
- Load
- VerifySnapshot
- WaitForResponse
- GetSnapshotFromRendererCallback
- Observe
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/file_util.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/hash.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/screen.h"
using content::NavigationController;
using content::WebContents;
namespace {
static const int kBrowserWidth = 1000;
static const int kBrowserHeight = 600;
class PDFBrowserTest : public InProcessBrowserTest,
public testing::WithParamInterface<int>,
public content::NotificationObserver {
public:
PDFBrowserTest()
: snapshot_different_(true),
next_dummy_search_value_(0),
load_stop_notification_count_(0) {
base::FilePath src_dir;
EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir));
pdf_test_server_.ServeFilesFromDirectory(src_dir.AppendASCII("pdf/test"));
}
protected:
net::test_server::EmbeddedTestServer* pdf_test_server() {
return &pdf_test_server_;
}
int load_stop_notification_count() const {
return load_stop_notification_count_;
}
base::FilePath GetPDFTestDir() {
return base::FilePath(base::FilePath::kCurrentDirectory).AppendASCII("..").
AppendASCII("..").AppendASCII("..").AppendASCII("pdf").
AppendASCII("test");
}
void Load() {
gfx::Rect bounds(gfx::Rect(0, 0, kBrowserWidth, kBrowserHeight));
gfx::Rect screen_bounds =
gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().bounds();
ASSERT_GT(screen_bounds.width(), kBrowserWidth);
ASSERT_GT(screen_bounds.height(), kBrowserHeight);
browser()->window()->SetBounds(bounds);
GURL url(ui_test_utils::GetTestUrl(
GetPDFTestDir(),
base::FilePath(FILE_PATH_LITERAL("pdf_browsertest.pdf"))));
ui_test_utils::NavigateToURL(browser(), url);
}
bool VerifySnapshot(const std::string& expected_filename) {
snapshot_different_ = true;
expected_filename_ = expected_filename;
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
DCHECK(web_contents);
content::RenderWidgetHost* rwh = web_contents->GetRenderViewHost();
rwh->GetSnapshotFromRenderer(gfx::Rect(), base::Bind(
&PDFBrowserTest::GetSnapshotFromRendererCallback, this));
content::RunMessageLoop();
if (snapshot_different_) {
LOG(INFO) << "Rendering didn't match, see result " <<
snapshot_filename_.value().c_str();
}
return !snapshot_different_;
}
void WaitForResponse() {
base::string16 query = base::UTF8ToUTF16(
std::string("xyzxyz" + base::IntToString(next_dummy_search_value_++)));
ASSERT_EQ(0, ui_test_utils::FindInPage(
browser()->tab_strip_model()->GetActiveWebContents(),
query, true, false, NULL, NULL));
}
private:
void GetSnapshotFromRendererCallback(bool success,
const SkBitmap& bitmap) {
base::MessageLoopForUI::current()->Quit();
ASSERT_EQ(success, true);
base::FilePath reference = ui_test_utils::GetTestFilePath(
GetPDFTestDir(),
base::FilePath().AppendASCII(expected_filename_));
base::File::Info info;
ASSERT_TRUE(base::GetFileInfo(reference, &info));
int size = static_cast<size_t>(info.size);
scoped_ptr<char[]> data(new char[size]);
ASSERT_EQ(size, base::ReadFile(reference, data.get(), size));
int w, h;
std::vector<unsigned char> decoded;
ASSERT_TRUE(gfx::PNGCodec::Decode(
reinterpret_cast<unsigned char*>(data.get()), size,
gfx::PNGCodec::FORMAT_BGRA, &decoded, &w, &h));
int32* ref_pixels = reinterpret_cast<int32*>(&decoded[0]);
int32* pixels = static_cast<int32*>(bitmap.getPixels());
int32 bg_color = ref_pixels[0];
int ref_x_offset, snapshot_x_offset;
for (ref_x_offset = 0; ref_x_offset < w; ++ref_x_offset) {
if (ref_pixels[ref_x_offset] != bg_color)
break;
}
for (snapshot_x_offset = 0; snapshot_x_offset < bitmap.width();
++snapshot_x_offset) {
if (pixels[snapshot_x_offset] != bg_color)
break;
}
int x_max = std::min(
w - ref_x_offset, bitmap.width() - snapshot_x_offset);
int y_max = std::min(h, bitmap.height());
int stride = bitmap.rowBytes();
snapshot_different_ = false;
for (int y = 0; y < y_max && !snapshot_different_; ++y) {
for (int x = 0; x < x_max && !snapshot_different_; ++x) {
if (pixels[y * stride / sizeof(int32) + x + snapshot_x_offset] !=
ref_pixels[y * w + x + ref_x_offset])
snapshot_different_ = true;
}
}
if (snapshot_different_) {
std::vector<unsigned char> png_data;
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data);
if (base::CreateTemporaryFile(&snapshot_filename_)) {
base::WriteFile(snapshot_filename_,
reinterpret_cast<char*>(&png_data[0]), png_data.size());
}
}
}
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == content::NOTIFICATION_LOAD_STOP) {
load_stop_notification_count_++;
}
}
bool snapshot_different_;
int next_dummy_search_value_;
std::string expected_filename_;
base::FilePath snapshot_filename_;
int load_stop_notification_count_;
net::test_server::EmbeddedTestServer pdf_test_server_;
};
IN_PROC_BROWSER_TEST_F(PDFBrowserTest, DISABLED_Basic) {
ASSERT_NO_FATAL_FAILURE(Load());
ASSERT_NO_FATAL_FAILURE(WaitForResponse());
#if defined(OS_MACOSX)
ASSERT_TRUE(VerifySnapshot("pdf_browsertest_mac.png") ||
VerifySnapshot("pdf_browsertest_mac2.png"));
#elif defined(OS_LINUX)
ASSERT_TRUE(VerifySnapshot("pdf_browsertest_linux.png"));
#else
ASSERT_TRUE(VerifySnapshot("pdf_browsertest.png"));
#endif
}
#if (!defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS)) || \
(defined(OS_LINUX) || defined(OS_MACOSX))
#define MAYBE_Scroll DISABLED_Scroll
#else
#define MAYBE_Scroll Scroll
#endif
IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Scroll) {
ASSERT_NO_FATAL_FAILURE(Load());
blink::WebMouseWheelEvent wheel_event;
wheel_event.type = blink::WebInputEvent::MouseWheel;
wheel_event.deltaY = -200;
wheel_event.wheelTicksY = -2;
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
web_contents->GetRenderViewHost()->ForwardWheelEvent(wheel_event);
ASSERT_NO_FATAL_FAILURE(WaitForResponse());
int y_offset = 0;
ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
browser()->tab_strip_model()->GetActiveWebContents(),
"window.domAutomationController.send(plugin.pageYOffset())",
&y_offset));
ASSERT_GT(y_offset, 0);
}
#if (!defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS)) || \
(defined(OS_LINUX) || defined(OS_MACOSX))
#define MAYBE_FindAndCopy DISABLED_FindAndCopy
#else
#define MAYBE_FindAndCopy FindAndCopy
#endif
IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_FindAndCopy) {
ASSERT_NO_FATAL_FAILURE(Load());
ASSERT_EQ(3, ui_test_utils::FindInPage(
browser()->tab_strip_model()->GetActiveWebContents(),
base::UTF8ToUTF16("adipiscing"),
true, false, NULL, NULL));
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
browser()->tab_strip_model()->GetActiveWebContents()->
GetMainFrame()->Copy();
ASSERT_NO_FATAL_FAILURE(WaitForResponse());
std::string text;
clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &text);
ASSERT_EQ("adipiscing", text);
}
const int kLoadingNumberOfParts = 10;
#if !defined(GOOGLE_CHROME_BUILD)
#define MAYBE_Loading DISABLED_Loading
#else
#define MAYBE_Loading Loading
#endif
IN_PROC_BROWSER_TEST_P(PDFBrowserTest, MAYBE_Loading) {
ASSERT_TRUE(pdf_test_server()->InitializeAndWaitUntilReady());
NavigationController* controller =
&(browser()->tab_strip_model()->GetActiveWebContents()->GetController());
content::NotificationRegistrar registrar;
registrar.Add(this,
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(controller));
std::string base_url = std::string("/");
base::FileEnumerator file_enumerator(
ui_test_utils::GetTestFilePath(GetPDFTestDir(), base::FilePath()),
false,
base::FileEnumerator::FILES,
FILE_PATH_LITERAL("*.pdf"));
for (base::FilePath file_path = file_enumerator.Next();
!file_path.empty();
file_path = file_enumerator.Next()) {
std::string filename = file_path.BaseName().MaybeAsASCII();
ASSERT_FALSE(filename.empty());
#if defined(OS_POSIX)
if (filename == "sample.pdf")
continue;
#endif
if (static_cast<int>(base::Hash(filename) % kLoadingNumberOfParts) !=
GetParam()) {
continue;
}
LOG(WARNING) << "PDFBrowserTest.Loading: " << filename;
GURL url = pdf_test_server()->GetURL(base_url + filename);
ui_test_utils::NavigateToURL(browser(), url);
while (true) {
int last_count = load_stop_notification_count();
bool complete = false;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
browser()->tab_strip_model()->GetActiveWebContents(),
"window.domAutomationController.send(plugin.documentLoadComplete())",
&complete));
if (complete)
break;
if (last_count != load_stop_notification_count())
continue;
content::WaitForLoadStop(
browser()->tab_strip_model()->GetActiveWebContents());
}
}
}
INSTANTIATE_TEST_CASE_P(PDFTestFiles,
PDFBrowserTest,
testing::Range(0, kLoadingNumberOfParts));
#if !defined(GOOGLE_CHROME_BUILD) || defined(OS_MACOSX)
#define MAYBE_Action DISABLED_Action
#else
#define MAYBE_Action Action
#endif
IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Action) {
ASSERT_NO_FATAL_FAILURE(Load());
ASSERT_TRUE(content::ExecuteScript(
browser()->tab_strip_model()->GetActiveWebContents(),
"document.getElementsByName('plugin')[0].fitToHeight();"));
std::string zoom1, zoom2;
ASSERT_TRUE(content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
"window.domAutomationController.send("
" document.getElementsByName('plugin')[0].getZoomLevel().toString())",
&zoom1));
ASSERT_TRUE(content::ExecuteScript(
browser()->tab_strip_model()->GetActiveWebContents(),
"document.getElementsByName('plugin')[0].fitToWidth();"));
ASSERT_TRUE(content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
"window.domAutomationController.send("
" document.getElementsByName('plugin')[0].getZoomLevel().toString())",
&zoom2));
ASSERT_NE(zoom1, zoom2);
}
IN_PROC_BROWSER_TEST_F(PDFBrowserTest, DISABLED_OnLoadAndReload) {
ASSERT_TRUE(pdf_test_server()->InitializeAndWaitUntilReady());
GURL url = pdf_test_server()->GetURL("/onload_reload.html");
ui_test_utils::NavigateToURL(browser(), url);
content::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(
&browser()->tab_strip_model()->GetActiveWebContents()->
GetController()));
ASSERT_TRUE(content::ExecuteScript(
browser()->tab_strip_model()->GetActiveWebContents(),
"reloadPDF();"));
observer.Wait();
ASSERT_EQ("success",
browser()->tab_strip_model()->GetActiveWebContents()->
GetURL().query());
}
}