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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string>
#include "base/file_util.h"
#include "base/string_util.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/view_ids.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/test/automation/constrained_window_proxy.h"
#include "chrome/test/automation/browser_proxy.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/automation/window_proxy.h"
#include "chrome/test/ui/ui_test.h"
#include "chrome/views/event.h"
#include "net/base/net_util.h"
typedef UITest AutomationProxyTest;
class AutomationProxyVisibleTest : public UITest {
protected:
AutomationProxyVisibleTest() {
show_window_ = true;
}
};
TEST_F(AutomationProxyTest, GetBrowserWindowCount) {
int window_count = 0;
EXPECT_TRUE(automation()->GetBrowserWindowCount(&window_count));
EXPECT_EQ(1, window_count);
#ifdef NDEBUG
ASSERT_FALSE(automation()->GetBrowserWindowCount(NULL));
#endif
}
TEST_F(AutomationProxyTest, GetBrowserWindow) {
{
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
}
{
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(-1));
ASSERT_FALSE(window.get());
}
{
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(1));
ASSERT_FALSE(window.get());
}
};
TEST_F(AutomationProxyVisibleTest, WindowGetViewBounds) {
{
scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
ASSERT_TRUE(browser.get());
scoped_ptr<WindowProxy> window(
automation()->GetWindowForBrowser(browser.get()));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab1(browser->GetTab(0));
ASSERT_TRUE(tab1.get());
GURL tab1_url;
ASSERT_TRUE(tab1->GetCurrentURL(&tab1_url));
// Add another tab so we can simulate dragging.
ASSERT_TRUE(browser->AppendTab(GURL("about:")));
scoped_ptr<TabProxy> tab2(browser->GetTab(1));
ASSERT_TRUE(tab2.get());
GURL tab2_url;
ASSERT_TRUE(tab2->GetCurrentURL(&tab2_url));
EXPECT_NE(tab1_url.spec(), tab2_url.spec());
gfx::Rect bounds;
ASSERT_TRUE(window->GetViewBounds(VIEW_ID_TAB_0, &bounds, false));
EXPECT_GT(bounds.x(), 0);
EXPECT_GT(bounds.width(), 0);
EXPECT_GT(bounds.height(), 0);
gfx::Rect bounds2;
ASSERT_TRUE(window->GetViewBounds(VIEW_ID_TAB_LAST, &bounds2, false));
EXPECT_GT(bounds2.width(), 0);
EXPECT_GT(bounds2.height(), 0);
EXPECT_GT(bounds2.x(), bounds.x());
EXPECT_EQ(bounds2.y(), bounds.y());
gfx::Rect urlbar_bounds;
ASSERT_TRUE(window->GetViewBounds(VIEW_ID_LOCATION_BAR, &urlbar_bounds,
false));
EXPECT_GT(urlbar_bounds.x(), 0);
EXPECT_GT(urlbar_bounds.y(), 0);
EXPECT_GT(urlbar_bounds.width(), 0);
EXPECT_GT(urlbar_bounds.height(), 0);
/*
TODO(beng): uncomment this section or move to interactive_ui_tests post
haste!
// Now that we know where the tabs are, let's try dragging one.
POINT start;
POINT end;
start.x = bounds.x() + bounds.width() / 2;
start.y = bounds.y() + bounds.height() / 2;
end.x = start.x + 2 * bounds.width() / 3;
end.y = start.y;
ASSERT_TRUE(browser->SimulateDrag(start, end,
ChromeViews::Event::EF_LEFT_BUTTON_DOWN));
// Check to see that the drag event successfully swapped the two tabs.
tab1.reset(browser->GetTab(0));
ASSERT_TRUE(tab1.get());
GURL tab1_new_url;
ASSERT_TRUE(tab1->GetCurrentURL(&tab1_new_url));
tab2.reset(browser->GetTab(1));
ASSERT_TRUE(tab2.get());
GURL tab2_new_url;
ASSERT_TRUE(tab2->GetCurrentURL(&tab2_new_url));
EXPECT_EQ(tab1_url.spec(), tab2_new_url.spec());
EXPECT_EQ(tab2_url.spec(), tab1_new_url.spec());
*/
}
}
TEST_F(AutomationProxyTest, GetTabCount) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
int tab_count = 0;
ASSERT_TRUE(window->GetTabCount(&tab_count));
ASSERT_EQ(1, tab_count);
}
TEST_F(AutomationProxyTest, GetActiveTabIndex) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
int active_tab_index = -1;
ASSERT_TRUE(window->GetActiveTabIndex(&active_tab_index));
ASSERT_EQ(0, active_tab_index);
}
TEST_F(AutomationProxyVisibleTest, AppendTab) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
int original_tab_count;
ASSERT_TRUE(window->GetTabCount(&original_tab_count));
ASSERT_EQ(1, original_tab_count); // By default there are 2 tabs opened.
int original_active_tab_index;
ASSERT_TRUE(window->GetActiveTabIndex(&original_active_tab_index));
ASSERT_EQ(0, original_active_tab_index); // By default 0-th tab is active
ASSERT_TRUE(window->AppendTab(GURL("about:blank")));
int tab_count;
ASSERT_TRUE(window->GetTabCount(&tab_count));
ASSERT_EQ(original_tab_count + 1, tab_count);
int active_tab_index = -1;
ASSERT_TRUE(window->GetActiveTabIndex(&active_tab_index));
ASSERT_EQ(tab_count - 1, active_tab_index);
ASSERT_NE(original_active_tab_index, active_tab_index);
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"title2.html");
ASSERT_TRUE(window->AppendTab(net::FilePathToFileURL(filename)));
int appended_tab_index;
// Append tab will also be active tab
ASSERT_TRUE(window->GetActiveTabIndex(&appended_tab_index));
scoped_ptr<TabProxy> tab(window->GetTab(appended_tab_index));
ASSERT_TRUE(tab.get());
std::wstring title;
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"Title Of Awesomeness", title.c_str());
}
TEST_F(AutomationProxyTest, ActivateTab) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
ASSERT_TRUE(window->AppendTab(GURL("about:blank")));
int at_index = 1;
ASSERT_TRUE(window->ActivateTab(at_index));
int active_tab_index = -1;
ASSERT_TRUE(window->GetActiveTabIndex(&active_tab_index));
ASSERT_EQ(at_index, active_tab_index);
at_index = 0;
ASSERT_TRUE(window->ActivateTab(at_index));
ASSERT_TRUE(window->GetActiveTabIndex(&active_tab_index));
ASSERT_EQ(at_index, active_tab_index);
}
TEST_F(AutomationProxyTest, GetTab) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
{
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring title;
ASSERT_TRUE(tab->GetTabTitle(&title));
// BUG [634097] : expected title should be "about:blank"
ASSERT_STREQ(L"", title.c_str());
}
{
ASSERT_FALSE(window->GetTab(-1));
}
{
scoped_ptr<TabProxy> tab;
tab.reset(window->GetTab(1));
ASSERT_FALSE(tab.get());
}
};
TEST_F(AutomationProxyTest, NavigateToURL) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring title;
ASSERT_TRUE(tab->GetTabTitle(&title));
// BUG [634097] : expected title should be "about:blank"
ASSERT_STREQ(L"", title.c_str());
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"title2.html");
tab->NavigateToURL(net::FilePathToFileURL(filename));
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"Title Of Awesomeness", title.c_str());
// TODO(vibhor) : Add a test using testserver.
}
// This test is disabled. See bug 794412
TEST_F(AutomationProxyTest, DISABLED_NavigateToURLWithTimeout1) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"title2.html");
bool is_timeout;
tab->NavigateToURLWithTimeout(net::FilePathToFileURL(filename),
10000, &is_timeout);
ASSERT_FALSE(is_timeout);
std::wstring title;
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"Title Of Awesomeness", title.c_str());
tab->NavigateToURLWithTimeout(net::FilePathToFileURL(filename),
1, &is_timeout);
ASSERT_TRUE(is_timeout);
Sleep(10);
}
// This test is disabled. See bug 794412.
TEST_F(AutomationProxyTest, DISABLED_NavigateToURLWithTimeout2) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
tab.reset(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring filename1(test_data_directory_);
file_util::AppendToPath(&filename1, L"title1.html");
bool is_timeout;
tab->NavigateToURLWithTimeout(net::FilePathToFileURL(filename1),
1, &is_timeout);
ASSERT_TRUE(is_timeout);
std::wstring filename2(test_data_directory_);
file_util::AppendToPath(&filename2, L"title2.html");
tab->NavigateToURLWithTimeout(net::FilePathToFileURL(filename2),
10000, &is_timeout);
ASSERT_FALSE(is_timeout);
Sleep(10);
}
TEST_F(AutomationProxyTest, GoBackForward) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring title;
ASSERT_TRUE(tab->GetTabTitle(&title));
// BUG [634097] : expected title should be "about:blank"
ASSERT_STREQ(L"", title.c_str());
ASSERT_FALSE(tab->GoBack());
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"", title.c_str());
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"title2.html");
ASSERT_TRUE(tab->NavigateToURL(net::FilePathToFileURL(filename)));
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"Title Of Awesomeness", title.c_str());
ASSERT_TRUE(tab->GoBack());
ASSERT_TRUE(tab->GetTabTitle(&title));
// BUG [634097] : expected title should be "about:blank"
ASSERT_STREQ(L"", title.c_str());
ASSERT_TRUE(tab->GoForward());
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"Title Of Awesomeness", title.c_str());
ASSERT_FALSE(tab->GoForward());
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"Title Of Awesomeness", title.c_str());
}
TEST_F(AutomationProxyTest, GetCurrentURL) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
GURL url;
ASSERT_TRUE(tab->GetCurrentURL(&url));
ASSERT_STREQ("about:blank", url.spec().c_str());
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"cookie1.html");
GURL newurl = net::FilePathToFileURL(filename);
ASSERT_TRUE(tab->NavigateToURL(newurl));
ASSERT_TRUE(tab->GetCurrentURL(&url));
// compare canonical urls...
ASSERT_STREQ(newurl.spec().c_str(), url.spec().c_str());
}
class AutomationProxyTest2 : public AutomationProxyVisibleTest {
protected:
AutomationProxyTest2() {
document1_ = test_data_directory_;
file_util::AppendToPath(&document1_, L"title1.html");
document2_ = test_data_directory_;
file_util::AppendToPath(&document2_, L"title2.html");
launch_arguments_ = document1_ + L" " + document2_;
}
std::wstring document1_;
std::wstring document2_;
};
TEST_F(AutomationProxyTest2, GetActiveTabIndex) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
int active_tab_index = -1;
ASSERT_TRUE(window->GetActiveTabIndex(&active_tab_index));
int tab_count;
ASSERT_TRUE(window->GetTabCount(&tab_count));
ASSERT_EQ(0, active_tab_index);
int at_index = 1;
ASSERT_TRUE(window->ActivateTab(at_index));
ASSERT_TRUE(window->GetActiveTabIndex(&active_tab_index));
ASSERT_EQ(at_index, active_tab_index);
}
TEST_F(AutomationProxyTest2, GetTabTitle) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring title;
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"title1.html", title.c_str());
tab.reset(window->GetTab(1));
ASSERT_TRUE(tab.get());
ASSERT_TRUE(tab->GetTabTitle(&title));
ASSERT_STREQ(L"Title Of Awesomeness", title.c_str());
}
TEST_F(AutomationProxyTest, Cookies) {
GURL url(L"http://mojo.jojo.google.com");
std::string value_result;
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
// test setting the cookie:
ASSERT_TRUE(tab->SetCookie(url, "foo=baz"));
ASSERT_TRUE(tab->GetCookieByName(url, "foo", &value_result));
ASSERT_FALSE(value_result.empty());
ASSERT_STREQ("baz", value_result.c_str());
// test clearing the cookie:
ASSERT_TRUE(tab->SetCookie(url, "foo="));
ASSERT_TRUE(tab->GetCookieByName(url, "foo", &value_result));
ASSERT_TRUE(value_result.empty());
// now, test that we can get multiple cookies:
ASSERT_TRUE(tab->SetCookie(url, "foo1=baz1"));
ASSERT_TRUE(tab->SetCookie(url, "foo2=baz2"));
ASSERT_TRUE(tab->GetCookies(url, &value_result));
ASSERT_FALSE(value_result.empty());
EXPECT_TRUE(value_result.find("foo1=baz1") != std::string::npos);
EXPECT_TRUE(value_result.find("foo2=baz2") != std::string::npos);
}
TEST_F(AutomationProxyTest, GetHWND) {
scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
ASSERT_TRUE(browser.get());
scoped_ptr<WindowProxy> window(
automation()->GetWindowForBrowser(browser.get()));
ASSERT_TRUE(window.get());
HWND handle;
ASSERT_TRUE(window->GetHWND(&handle));
ASSERT_TRUE(handle);
}
TEST_F(AutomationProxyTest, NavigateToURLAsync) {
AutomationProxy* automation_object = automation();
scoped_ptr<BrowserProxy> window(automation_object->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"cookie1.html");
GURL newurl = net::FilePathToFileURL(filename);
ASSERT_TRUE(tab->NavigateToURLAsync(newurl));
std::string value = WaitUntilCookieNonEmpty(tab.get(), newurl,
"foo", 250, 5*1000);
ASSERT_STREQ("baz", value.c_str());
}
TEST_F(AutomationProxyTest, AcceleratorNewTab) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
int old_tab_count = -1;
ASSERT_TRUE(window->GetTabCount(&old_tab_count));
ASSERT_TRUE(window->ApplyAccelerator(IDC_NEWTAB));
int new_tab_count;
ASSERT_TRUE(window->WaitForTabCountToChange(old_tab_count, &new_tab_count,
5000));
if (new_tab_count == -1)
FAIL();
ASSERT_EQ(old_tab_count + 1, new_tab_count);
scoped_ptr<TabProxy> tab(window->GetTab(new_tab_count - 1));
ASSERT_TRUE(tab.get());
std::wstring title;
int i;
for (i = 0; i < 10; ++i) {
Sleep(kWaitForActionMaxMsec / 10);
ASSERT_TRUE(tab->GetTabTitle(&title));
if (title == L"Destinations" || title == L"New Tab")
break;
}
// If we got to 10, the new tab failed to open.
ASSERT_NE(10, i);
}
class AutomationProxyTest4 : public UITest {
protected:
AutomationProxyTest4() : UITest() {
dom_automation_enabled_ = true;
}
};
std::wstring SynthesizeJSURL(const std::wstring& value) {
std::wstring jscript;
SStringPrintf(&jscript,
L"javascript:void(window.domAutomationController.send(%ls));",
value.c_str());
return jscript;
}
TEST_F(AutomationProxyTest4, StringValueIsEchoedByDomAutomationController) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring expected(L"string");
std::wstring jscript = SynthesizeJSURL(L"\"" + expected + L"\"");
std::wstring actual;
ASSERT_TRUE(tab->ExecuteAndExtractString(L"", jscript, &actual));
ASSERT_STREQ(expected.c_str(), actual.c_str());
}
std::wstring BooleanToString(bool bool_value) {
Value* value = Value::CreateBooleanValue(bool_value);
std::string json_string;
JSONStringValueSerializer serializer(&json_string);
serializer.Serialize(*value);
return UTF8ToWide(json_string);
}
TEST_F(AutomationProxyTest4, BooleanValueIsEchoedByDomAutomationController) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
bool expected = true;
std::wstring jscript = SynthesizeJSURL(BooleanToString(expected));
bool actual = false;
ASSERT_TRUE(tab->ExecuteAndExtractBool(L"", jscript, &actual));
ASSERT_EQ(expected, actual);
}
TEST_F(AutomationProxyTest4, NumberValueIsEchoedByDomAutomationController) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
int expected = 1;
int actual = 0;
std::wstring expected_string;
SStringPrintf(&expected_string, L"%d", expected);
std::wstring jscript = SynthesizeJSURL(expected_string);
ASSERT_TRUE(tab->ExecuteAndExtractInt(L"", jscript, &actual));
ASSERT_EQ(expected, actual);
}
// TODO(vibhor): Add a test for ExecuteAndExtractValue() for JSON Dictionary
// type value
class AutomationProxyTest3 : public UITest {
protected:
AutomationProxyTest3() : UITest() {
document1_ = test_data_directory_;
file_util::AppendToPath(&document1_, L"frame_dom_access");
file_util::AppendToPath(&document1_, L"frame_dom_access.html");
dom_automation_enabled_ = true;
launch_arguments_ = document1_;
}
std::wstring document1_;
};
std::wstring SynthesizeJSURLForDOMQuery(const std::wstring& id) {
std::wstring jscript;
SStringPrintf(&jscript,
L"javascript:void(window.domAutomationController)");
StringAppendF(&jscript, L".send(document.getElementById('%ls').nodeName);",
id.c_str());
return jscript;
}
TEST_F(AutomationProxyTest3, FrameDocumentCanBeAccessed) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring actual;
std::wstring xpath1 = L""; // top level frame
std::wstring jscript1 = SynthesizeJSURLForDOMQuery(L"myinput");
ASSERT_TRUE(tab->ExecuteAndExtractString(xpath1, jscript1, &actual));
ASSERT_EQ(L"INPUT", actual);
std::wstring xpath2 = L"/html/body/iframe";
std::wstring jscript2 = SynthesizeJSURLForDOMQuery(L"myspan");
ASSERT_TRUE(tab->ExecuteAndExtractString(xpath2, jscript2, &actual));
ASSERT_EQ(L"SPAN", actual);
std::wstring xpath3 = L"/html/body/iframe\n/html/body/iframe";
std::wstring jscript3 = SynthesizeJSURLForDOMQuery(L"mydiv");
ASSERT_TRUE(tab->ExecuteAndExtractString(xpath3, jscript3, &actual));
ASSERT_EQ(L"DIV", actual);
// TODO(evanm): fix or remove this.
// This part of the test appears to verify that executing JS fails
// non-HTML pages, but the new tab is now HTML so this test isn't
// correct.
#if 0
// Open a new Destinations tab to execute script inside.
window->ApplyAccelerator(IDC_NEWTAB);
tab.reset(window->GetTab(1));
ASSERT_TRUE(tab.get());
ASSERT_TRUE(window->ActivateTab(1));
std::wstring title;
int i;
for (i = 0; i < 10; ++i) {
Sleep(kWaitForActionMaxMsec / 10);
ASSERT_TRUE(tab->GetTabTitle(&title));
if (title == L"Destinations")
break;
}
// If we got to 10, the new tab failed to open.
ASSERT_NE(10, i);
ASSERT_FALSE(tab->ExecuteAndExtractString(xpath1, jscript1, &actual));
#endif
}
TEST_F(AutomationProxyTest, ConstrainedWindowTest) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
tab.reset(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"constrained_files");
file_util::AppendToPath(&filename, L"constrained_window.html");
ASSERT_TRUE(tab->NavigateToURL(net::FilePathToFileURL(filename)));
int count;
ASSERT_TRUE(tab->WaitForChildWindowCountToChange(0, &count, 5000));
ASSERT_EQ(2, count);
ConstrainedWindowProxy* cwindow = tab->GetConstrainedWindow(0);
ASSERT_TRUE(cwindow);
std::wstring title;
ASSERT_TRUE(cwindow->GetTitle(&title));
ASSERT_STREQ(L"Constrained Window 0 - Google Chrome", title.c_str());
delete cwindow;
cwindow = tab->GetConstrainedWindow(1);
ASSERT_TRUE(cwindow);
ASSERT_TRUE(cwindow->GetTitle(&title));
ASSERT_STREQ(L"Constrained Window 1 - Google Chrome", title.c_str());
delete cwindow;
}
TEST_F(AutomationProxyTest, CantEscapeByOnloadMoveto) {
scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0));
ASSERT_TRUE(window.get());
scoped_ptr<TabProxy> tab(window->GetTab(0));
tab.reset(window->GetTab(0));
ASSERT_TRUE(tab.get());
std::wstring filename(test_data_directory_);
file_util::AppendToPath(&filename, L"constrained_files");
file_util::AppendToPath(&filename, L"constrained_window_onload_moveto.html");
ASSERT_TRUE(tab->NavigateToURL(net::FilePathToFileURL(filename)));
int count;
ASSERT_TRUE(tab->WaitForChildWindowCountToChange(0, &count, 5000));
ASSERT_EQ(1, count);
ConstrainedWindowProxy* cwindow = tab->GetConstrainedWindow(0);
ASSERT_TRUE(cwindow);
gfx::Rect rect;
bool is_timeout = false;
ASSERT_TRUE(cwindow->GetBoundsWithTimeout(&rect, 1000, &is_timeout));
ASSERT_FALSE(is_timeout);
ASSERT_NE(20, rect.x());
ASSERT_NE(20, rect.y());
}
bool ExternalTabHandler(HWND external_tab_window) {
static const wchar_t class_name[] = L"External_Tab_UI_Test_Class";
static const wchar_t window_title[] = L"External Tab Tester";
WNDCLASSEX wnd_class = {0};
wnd_class.cbSize = sizeof(wnd_class);
wnd_class.style = CS_HREDRAW | CS_VREDRAW;
wnd_class.lpfnWndProc = DefWindowProc;
wnd_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wnd_class.lpszClassName = class_name;
ATOM result = RegisterClassEx(&wnd_class);
if (0 == result) {
return false;
}
unsigned long style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
HWND external_tab_ui_parent = CreateWindow(class_name, window_title,
style, CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0, NULL, NULL,
NULL, NULL);
style = GetWindowLong(external_tab_window, GWL_STYLE);
style |= WS_CHILD;
style &= ~WS_POPUP;
SetWindowLong(external_tab_window, GWL_STYLE, style);
SetParent(external_tab_window, external_tab_ui_parent);
RECT client_rect = {0};
GetClientRect(external_tab_ui_parent, &client_rect);
SetWindowPos(external_tab_window, NULL, 0, 0, client_rect.right,
client_rect.bottom, SWP_NOZORDER);
ShowWindow(external_tab_window, SW_SHOW);
ShowWindow(external_tab_ui_parent, SW_SHOW);
// Allow the renderers to connect.
Sleep(1000);
DestroyWindow(external_tab_ui_parent);
return true;
}
TEST_F(AutomationProxyVisibleTest, CreateExternalTab) {
HWND external_tab_container = NULL;
scoped_ptr<TabProxy> tab(automation()->CreateExternalTab(
&external_tab_container));
EXPECT_TRUE(tab != NULL);
EXPECT_NE(FALSE, ::IsWindow(external_tab_container));
if (tab != NULL) {
tab->NavigateInExternalTab(GURL(L"http://www.google.com"));
EXPECT_EQ(true, ExternalTabHandler(external_tab_container));
// Since the tab goes away lazily, wait a bit
Sleep(1000);
EXPECT_FALSE(tab->is_valid());
}
}
TEST_F(AutomationProxyTest, AutocompleteGetSetText) {
scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
ASSERT_TRUE(browser.get());
scoped_ptr<AutocompleteEditProxy> edit(
automation()->GetAutocompleteEditForBrowser(browser.get()));
ASSERT_TRUE(edit.get());
EXPECT_TRUE(edit->is_valid());
const std::wstring text_to_set = L"Lollerskates";
std::wstring actual_text;
EXPECT_TRUE(edit->SetText(text_to_set));
EXPECT_TRUE(edit->GetText(&actual_text));
EXPECT_EQ(text_to_set, actual_text);
scoped_ptr<AutocompleteEditProxy> edit2(
automation()->GetAutocompleteEditForBrowser(browser.get()));
EXPECT_TRUE(edit2->GetText(&actual_text));
EXPECT_EQ(text_to_set, actual_text);
}
TEST_F(AutomationProxyTest, AutocompleteParallelProxy)
{
scoped_ptr<BrowserProxy> browser1(automation()->GetBrowserWindow(0));
ASSERT_TRUE(browser1.get());
scoped_ptr<AutocompleteEditProxy> edit1(
automation()->GetAutocompleteEditForBrowser(browser1.get()));
ASSERT_TRUE(edit1.get());
EXPECT_TRUE(browser1->ApplyAccelerator(IDC_DUPLICATE));
scoped_ptr<BrowserProxy> browser2(automation()->GetBrowserWindow(1));
ASSERT_TRUE(browser2.get());
scoped_ptr<AutocompleteEditProxy> edit2(
automation()->GetAutocompleteEditForBrowser(browser2.get()));
ASSERT_TRUE(edit2.get());
EXPECT_TRUE(browser2->GetTab(0)->WaitForTabToBeRestored(kWaitForActionMsec));
const std::wstring text_to_set1 = L"Lollerskates";
const std::wstring text_to_set2 = L"Roflcopter";
std::wstring actual_text1, actual_text2;
EXPECT_TRUE(edit1->SetText(text_to_set1));
EXPECT_TRUE(edit2->SetText(text_to_set2));
EXPECT_TRUE(edit1->GetText(&actual_text1));
EXPECT_TRUE(edit2->GetText(&actual_text2));
EXPECT_EQ(text_to_set1, actual_text1);
EXPECT_EQ(text_to_set2, actual_text2);
}
TEST_F(AutomationProxyVisibleTest, AutocompleteMatchesTest) {
scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
ASSERT_TRUE(browser.get());
scoped_ptr<AutocompleteEditProxy> edit(
automation()->GetAutocompleteEditForBrowser(browser.get()));
ASSERT_TRUE(edit.get());
EXPECT_TRUE(browser->ApplyAccelerator(IDC_FOCUS_LOCATION));
EXPECT_TRUE(edit->is_valid());
EXPECT_TRUE(edit->SetText(L"Roflcopter"));
EXPECT_TRUE(edit->WaitForQuery(30000));
bool query_in_progress;
EXPECT_TRUE(edit->IsQueryInProgress(&query_in_progress));
EXPECT_FALSE(query_in_progress);
std::vector<AutocompleteMatchData> matches;
EXPECT_TRUE(edit->GetAutocompleteMatches(&matches));
EXPECT_FALSE(matches.empty());
}
|