summaryrefslogtreecommitdiffstats
path: root/chrome/test/automation/browser_proxy.cc
blob: c91025d37294744ecdc3a6e05be583038343c60e (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
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
// Copyright (c) 2010 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 "chrome/test/automation/browser_proxy.h"

#include <algorithm>

#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/threading/platform_thread.h"
#include "base/time.h"
#include "chrome/common/automation_constants.h"
#include "chrome/common/automation_messages.h"
#include "chrome/test/automation/autocomplete_edit_proxy.h"
#include "chrome/test/automation/automation_proxy.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/automation/window_proxy.h"
#include "gfx/point.h"

using base::TimeDelta;
using base::TimeTicks;


bool BrowserProxy::ActivateTab(int tab_index) {
  if (!is_valid())
    return false;

  int activate_tab_response = -1;

  if (!sender_->Send(new AutomationMsg_ActivateTab(
                         handle_, tab_index, &activate_tab_response))) {
    return false;
  }

  if (activate_tab_response >= 0)
    return true;

  return false;
}

bool BrowserProxy::BringToFront() {
  if (!is_valid())
    return false;

  bool succeeded = false;

  if (!sender_->Send(new AutomationMsg_BringBrowserToFront(
                         handle_, &succeeded))) {
    return false;
  }

  return succeeded;
}

bool BrowserProxy::IsMenuCommandEnabled(int id, bool* enabled) {
  if (!is_valid())
    return false;

  return sender_->Send(new AutomationMsg_IsMenuCommandEnabled(handle_, id,
                                                              enabled));
}

bool BrowserProxy::AppendTab(const GURL& tab_url) {
  if (!is_valid())
    return false;

  int append_tab_response = -1;

  sender_->Send(new AutomationMsg_AppendTab(handle_, tab_url,
                                            &append_tab_response));
  return append_tab_response >= 0;
}

bool BrowserProxy::GetActiveTabIndex(int* active_tab_index) const {
  if (!is_valid())
    return false;

  if (!active_tab_index) {
    NOTREACHED();
    return false;
  }

  int active_tab_index_response = -1;

  if (!sender_->Send(new AutomationMsg_ActiveTabIndex(
                         handle_, &active_tab_index_response))) {
    return false;
  }

  if (active_tab_index_response >= 0) {
    *active_tab_index = active_tab_index_response;
    return true;
  }

  return false;
}

scoped_refptr<TabProxy> BrowserProxy::GetTab(int tab_index) const {
  if (!is_valid())
    return NULL;

  int tab_handle = 0;

  sender_->Send(new AutomationMsg_Tab(handle_, tab_index, &tab_handle));
  if (!tab_handle)
    return NULL;

  TabProxy* tab = static_cast<TabProxy*>(tracker_->GetResource(tab_handle));
  if (!tab) {
    tab = new TabProxy(sender_, tracker_, tab_handle);
    tab->AddRef();
  }

  // Since there is no scoped_refptr::attach.
  scoped_refptr<TabProxy> result;
  result.swap(&tab);
  return result;
}

scoped_refptr<TabProxy> BrowserProxy::GetActiveTab() const {
  int active_tab_index;
  if (!GetActiveTabIndex(&active_tab_index))
    return NULL;
  return GetTab(active_tab_index);
}

bool BrowserProxy::GetTabCount(int* num_tabs) const {
  if (!is_valid())
    return false;

  if (!num_tabs) {
    NOTREACHED();
    return false;
  }

  int tab_count_response = -1;

  if (!sender_->Send(new AutomationMsg_TabCount(
                         handle_, &tab_count_response))) {
    return false;
  }

  if (tab_count_response >= 0) {
    *num_tabs = tab_count_response;
    return true;
  }

  return false;
}

bool BrowserProxy::GetType(Browser::Type* type) const {
  if (!is_valid())
    return false;

  if (!type) {
    NOTREACHED();
    return false;
  }

  int type_as_int;
  if (!sender_->Send(new AutomationMsg_Type(handle_, &type_as_int)))
    return false;

  *type = static_cast<Browser::Type>(type_as_int);
  return true;
}

bool BrowserProxy::ApplyAccelerator(int id) {
  return RunCommandAsync(id);
}

bool BrowserProxy::SimulateDrag(const gfx::Point& start,
                                const gfx::Point& end,
                                int flags,
                                bool press_escape_en_route) {
  if (!is_valid())
    return false;

  std::vector<gfx::Point> drag_path;
  drag_path.push_back(start);
  drag_path.push_back(end);

  bool result = false;

  if (!sender_->Send(new AutomationMsg_WindowDrag(
          handle_, drag_path, flags, press_escape_en_route, &result))) {
    return false;
  }

  return result;
}

bool BrowserProxy::WaitForTabCountToBecome(int count) {
  bool success = false;
  if (!sender_->Send(new AutomationMsg_WaitForTabCountToBecome(
                         handle_, count, &success))) {
    return false;
  }

  return success;
}

bool BrowserProxy::WaitForTabToBecomeActive(int tab,
                                            int wait_timeout) {
  const TimeTicks start = TimeTicks::Now();
  const TimeDelta timeout = TimeDelta::FromMilliseconds(wait_timeout);
  while (TimeTicks::Now() - start < timeout) {
    base::PlatformThread::Sleep(automation::kSleepTime);
    int active_tab;
    if (GetActiveTabIndex(&active_tab) && active_tab == tab)
      return true;
  }
  // If we get here, the active tab hasn't changed.
  return false;
}

bool BrowserProxy::OpenFindInPage() {
  if (!is_valid())
    return false;

  return sender_->Send(new AutomationMsg_OpenFindInPage(handle_));
  // This message expects no response.
}

bool BrowserProxy::GetFindWindowLocation(int* x, int* y) {
  if (!is_valid() || !x || !y)
    return false;

  return sender_->Send(new AutomationMsg_FindWindowLocation(handle_, x, y));
}

bool BrowserProxy::IsFindWindowFullyVisible(bool* is_visible) {
  if (!is_valid())
    return false;

  if (!is_visible) {
    NOTREACHED();
    return false;
  }

  return sender_->Send(
      new AutomationMsg_FindWindowVisibility(handle_, is_visible));
}

bool BrowserProxy::RunCommandAsync(int browser_command) const {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_WindowExecuteCommandAsync(handle_,
                                                            browser_command,
                                                            &result));

  return result;
}

bool BrowserProxy::RunCommand(int browser_command) const {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_WindowExecuteCommand(handle_,
                                                       browser_command,
                                                       &result));

  return result;
}

bool BrowserProxy::GetBookmarkBarVisibility(bool* is_visible,
                                            bool* is_animating) {
  if (!is_valid())
    return false;

  if (!is_visible || !is_animating) {
    NOTREACHED();
    return false;
  }

  return sender_->Send(new AutomationMsg_BookmarkBarVisibility(
      handle_, is_visible, is_animating));
}

bool BrowserProxy::GetBookmarksAsJSON(std::string *json_string) {
  if (!is_valid())
    return false;

  if (!WaitForBookmarkModelToLoad())
    return false;

  bool result = false;
  sender_->Send(new AutomationMsg_GetBookmarksAsJSON(handle_,
                                                     json_string,
                                                     &result));
  return result;
}

bool BrowserProxy::WaitForBookmarkModelToLoad() {
  if (!is_valid())
    return false;

  bool result = false;
  sender_->Send(new AutomationMsg_WaitForBookmarkModelToLoad(handle_, &result));
  return result;
}

bool BrowserProxy::AddBookmarkGroup(int64 parent_id, int index,
                                    std::wstring& title) {
  if (!is_valid())
    return false;
  bool result = false;
  sender_->Send(new AutomationMsg_AddBookmarkGroup(handle_,
                                                   parent_id, index,
                                                   title,
                                                   &result));
  return result;
}

bool BrowserProxy::AddBookmarkURL(int64 parent_id, int index,
                                  std::wstring& title, const GURL& url) {
  if (!is_valid())
    return false;
  bool result = false;
  sender_->Send(new AutomationMsg_AddBookmarkURL(handle_,
                                                 parent_id, index,
                                                 title, url,
                                                 &result));
  return result;
}

bool BrowserProxy::ReparentBookmark(int64 id, int64 new_parent_id, int index) {
  if (!is_valid())
    return false;
  bool result = false;
  sender_->Send(new AutomationMsg_ReparentBookmark(handle_,
                                                   id, new_parent_id,
                                                   index,
                                                   &result));
  return result;
}

bool BrowserProxy::SetBookmarkTitle(int64 id, const std::wstring& title) {
  if (!is_valid())
    return false;
  bool result = false;
  sender_->Send(new AutomationMsg_SetBookmarkTitle(handle_,
                                                   id, title,
                                                   &result));
  return result;
}

bool BrowserProxy::SetBookmarkURL(int64 id, const GURL& url) {
  if (!is_valid())
    return false;
  bool result = false;
  sender_->Send(new AutomationMsg_SetBookmarkURL(handle_,
                                                 id, url,
                                                 &result));
  return result;
}

bool BrowserProxy::RemoveBookmark(int64 id) {
  if (!is_valid())
    return false;
  bool result = false;
  sender_->Send(new AutomationMsg_RemoveBookmark(handle_,
                                                 id,
                                                 &result));
  return result;
}

bool BrowserProxy::IsShelfVisible(bool* is_visible) {
  if (!is_valid())
    return false;

  if (!is_visible) {
    NOTREACHED();
    return false;
  }

  return sender_->Send(new AutomationMsg_ShelfVisibility(handle_,
                                                         is_visible));
}

bool BrowserProxy::SetShelfVisible(bool is_visible) {
  if (!is_valid())
    return false;

  return sender_->Send(new AutomationMsg_SetShelfVisibility(handle_,
                                                            is_visible));
}

bool BrowserProxy::SetIntPreference(const std::string& name, int value) {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_SetIntPreference(handle_, name, value,
                                                   &result));
  return result;
}

bool BrowserProxy::SetStringPreference(const std::string& name,
                                       const std::string& value) {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_SetStringPreference(handle_, name, value,
                                                      &result));
  return result;
}

bool BrowserProxy::GetBooleanPreference(const std::string& name,
                                        bool* value) {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_GetBooleanPreference(handle_, name, value,
                                                       &result));
  return result;
}

bool BrowserProxy::SetBooleanPreference(const std::string& name,
                                        bool value) {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_SetBooleanPreference(handle_, name,
                                                       value, &result));
  return result;
}

bool BrowserProxy::SetDefaultContentSetting(ContentSettingsType content_type,
                                            ContentSetting setting) {
  return SetContentSetting(std::string(), content_type, setting);
}

bool BrowserProxy::SetContentSetting(const std::string& host,
                                     ContentSettingsType content_type,
                                     ContentSetting setting) {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_SetContentSetting(handle_, host,
                                                    content_type, setting,
                                                    &result));
  return result;
}

bool BrowserProxy::TerminateSession() {
  if (!is_valid())
    return false;

  bool result = false;

  sender_->Send(new AutomationMsg_TerminateSession(handle_, &result));

  return result;
}

scoped_refptr<WindowProxy> BrowserProxy::GetWindow() const {
  if (!is_valid())
    return NULL;

  bool handle_ok = false;
  int window_handle = 0;

  sender_->Send(new AutomationMsg_WindowForBrowser(handle_, &handle_ok,
                                                   &window_handle));
  if (!handle_ok)
    return NULL;

  WindowProxy* window =
      static_cast<WindowProxy*>(tracker_->GetResource(window_handle));
  if (!window) {
    window = new WindowProxy(sender_, tracker_, window_handle);
    window->AddRef();
  }

  // Since there is no scoped_refptr::attach.
  scoped_refptr<WindowProxy> result;
  result.swap(&window);
  return result;
}

scoped_refptr<AutocompleteEditProxy> BrowserProxy::GetAutocompleteEdit() {
  if (!is_valid())
    return NULL;

  bool handle_ok = false;
  int autocomplete_edit_handle = 0;

  sender_->Send(new AutomationMsg_AutocompleteEditForBrowser(
      handle_, &handle_ok, &autocomplete_edit_handle));

  if (!handle_ok)
    return NULL;

  AutocompleteEditProxy* p = static_cast<AutocompleteEditProxy*>(
        tracker_->GetResource(autocomplete_edit_handle));

  if (!p) {
    p = new AutocompleteEditProxy(sender_, tracker_, autocomplete_edit_handle);
    p->AddRef();
  }

  // Since there is no scoped_refptr::attach.
  scoped_refptr<AutocompleteEditProxy> result;
  result.swap(&p);
  return result;
}

bool BrowserProxy::IsFullscreen(bool* is_fullscreen) {
  DCHECK(is_fullscreen);

  if (!is_valid())
    return false;

  return sender_->Send(new AutomationMsg_IsFullscreen(handle_, is_fullscreen));
}

bool BrowserProxy::IsFullscreenBubbleVisible(bool* is_visible) {
  DCHECK(is_visible);

  if (!is_valid())
    return false;

  return sender_->Send(new AutomationMsg_IsFullscreenBubbleVisible(handle_,
                                                                   is_visible));
}

bool BrowserProxy::ShutdownSessionService() {
  bool did_shutdown = false;
  bool succeeded = sender_->Send(
      new AutomationMsg_ShutdownSessionService(handle_, &did_shutdown));

  if (!succeeded) {
    DLOG(ERROR) <<
        "ShutdownSessionService did not complete in a timely fashion";
    return false;
  }

  return did_shutdown;
}

bool BrowserProxy::StartTrackingPopupMenus() {
  if (!is_valid())
    return false;

  bool result = false;
  if (!sender_->Send(new AutomationMsg_StartTrackingPopupMenus(
          handle_, &result)))
    return false;
  return result;
}

bool BrowserProxy::WaitForPopupMenuToOpen() {
  if (!is_valid())
    return false;

  bool result = false;
  if (!sender_->Send(new AutomationMsg_WaitForPopupMenuToOpen(&result)))
    return false;
  return result;
}

bool BrowserProxy::SendJSONRequest(const std::string& request,
                                   std::string* response) {
  if (!is_valid())
    return false;

  bool result = false;
  return sender_->Send(new AutomationMsg_SendJSONRequest(handle_,
                                                         request, response,
                                                         &result));
  return result;
}

bool BrowserProxy::GetInitialLoadTimes(float* min_start_time,
                                       float* max_stop_time,
                                       std::vector<float>* stop_times) {
  std::string json_response;
  const char* kJSONCommand = "{\"command\": \"GetInitialLoadTimes\"}";

  *max_stop_time = 0;
  *min_start_time = -1;
  if (!SendJSONRequest(kJSONCommand, &json_response)) {
    // Older browser versions do not support GetInitialLoadTimes.
    // Fail gracefully and do not record them in this case.
    return false;
  }
  std::string error;
  base::JSONReader reader;
  scoped_ptr<Value> values(reader.ReadAndReturnError(json_response, true,
                                                     NULL, &error));
  if (!error.empty() || values->GetType() != Value::TYPE_DICTIONARY)
    return false;

  DictionaryValue* values_dict = static_cast<DictionaryValue*>(values.get());

  Value* tabs_value;
  if (!values_dict->Get("tabs", &tabs_value) ||
      tabs_value->GetType() != Value::TYPE_LIST)
    return false;

  ListValue* tabs_list = static_cast<ListValue*>(tabs_value);

  for (size_t i = 0; i < tabs_list->GetSize(); i++) {
    float stop_ms = 0;
    float start_ms = 0;
    Value* tab_value;
    DictionaryValue* tab_dict;

    if (!tabs_list->Get(i, &tab_value) ||
        tab_value->GetType() != Value::TYPE_DICTIONARY)
      return false;
    tab_dict = static_cast<DictionaryValue*>(tab_value);

    double temp;
    if (!tab_dict->GetDouble("load_start_ms", &temp))
      return false;
    start_ms = static_cast<float>(temp);
    // load_stop_ms can only be null if WaitForInitialLoads did not run.
    if (!tab_dict->GetDouble("load_stop_ms", &temp))
      return false;
    stop_ms = static_cast<float>(temp);

    if (i == 0)
      *min_start_time = start_ms;

    *min_start_time = std::min(start_ms, *min_start_time);
    *max_stop_time = std::max(stop_ms, *max_stop_time);
    stop_times->push_back(stop_ms);
  }
  std::sort(stop_times->begin(), stop_times->end());
  return true;
}