summaryrefslogtreecommitdiffstats
path: root/chrome/test/base/test_web_dialog_observer.cc
blob: 20565920fc7ec21f941d5be96b38f361dfbf48b4 (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
// Copyright (c) 2012 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/base/test_web_dialog_observer.h"

#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/test/js_injection_ready_observer.h"

using content::NavigationController;

TestWebDialogObserver::TestWebDialogObserver(
    content::JsInjectionReadyObserver* js_injection_ready_observer)
    : js_injection_ready_observer_(js_injection_ready_observer),
      web_ui_(NULL),
      done_(false),
      running_(false) {
}

TestWebDialogObserver::~TestWebDialogObserver() {
}

void TestWebDialogObserver::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  switch (type) {
      break;
    case content::NOTIFICATION_LOAD_STOP:
      DCHECK(web_ui_);
      registrar_.Remove(this, content::NOTIFICATION_LOAD_STOP,
                        content::Source<NavigationController>(
                            &web_ui_->GetWebContents()->GetController()));
      done_ = true;
      // If the message loop is running stop it.
      if (running_) {
        running_ = false;
        MessageLoopForUI::current()->Quit();
      }
      break;
    default:
      NOTREACHED();
  };
}

void TestWebDialogObserver::OnDialogShown(
      content::WebUI* webui,
      content::RenderViewHost* render_view_host) {
  if (js_injection_ready_observer_) {
    js_injection_ready_observer_->OnJsInjectionReady(render_view_host);
  }
  web_ui_ = webui;
  // Wait for navigation on the new WebUI instance to complete. This depends
  // on receiving the notification of the WebDialog being shown before the
  // NavigationController finishes loading. The WebDialog notification is
  // issued from web_dialog_ui.cc on RenderView creation which results from
  // the call to render_manager_.Navigate in the method
  // WebContents::NavigateToEntry. The new RenderView is later told to
  // navigate in this method, ensuring that this is not a race condition.
  registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
                 content::Source<NavigationController>(
                     &web_ui_->GetWebContents()->GetController()));
}

content::WebUI* TestWebDialogObserver::GetWebUI() {
  if (!done_) {
    EXPECT_FALSE(running_);
    running_ = true;
    ui_test_utils::RunMessageLoop();
  }
  return web_ui_;
}