| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <iostream> | |
| 6 #include "base/bind.h" | |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "mojo/application/application_runner_chromium.h" | |
| 9 #include "mojo/public/c/system/main.h" | |
| 10 #include "mojo/public/cpp/application/application_delegate.h" | |
| 11 #include "mojo/public/cpp/application/application_impl.h" | |
| 12 #include "mojo/public/cpp/application/connect.h" | |
| 13 #include "mojo/public/cpp/application/service_provider_impl.h" | |
| 14 #include "mojo/services/input_events/public/interfaces/input_events.mojom.h" | |
| 15 #include "mojo/services/view_manager/public/cpp/view_manager.h" | |
| 16 #include "mojo/services/view_manager/public/cpp/view_manager_delegate.h" | |
| 17 #include "mojo/services/view_manager/public/cpp/view_observer.h" | |
| 18 #include "services/window_manager/window_manager_app.h" | |
| 19 #include "services/window_manager/window_manager_delegate.h" | |
| 20 #include "sky/tools/tester/test_runner.h" | |
| 21 | |
| 22 namespace sky { | |
| 23 namespace tester { | |
| 24 namespace { | |
| 25 | |
| 26 struct UrlData { | |
| 27 std::string url; | |
| 28 std::string expected_pixel_hash; | |
| 29 bool enable_pixel_dumping = false; | |
| 30 }; | |
| 31 | |
| 32 void WaitForURL(UrlData& data) { | |
| 33 // A test name is formated like file:///path/to/test'--pixel-test'pixelhash | |
| 34 std::cin >> data.url; | |
| 35 | |
| 36 std::string pixel_switch; | |
| 37 std::string::size_type separator_position = data.url.find('\''); | |
| 38 if (separator_position != std::string::npos) { | |
| 39 pixel_switch = data.url.substr(separator_position + 1); | |
| 40 data.url.erase(separator_position); | |
| 41 } | |
| 42 | |
| 43 std::string pixel_hash; | |
| 44 separator_position = pixel_switch.find('\''); | |
| 45 if (separator_position != std::string::npos) { | |
| 46 pixel_hash = pixel_switch.substr(separator_position + 1); | |
| 47 pixel_switch.erase(separator_position); | |
| 48 } | |
| 49 | |
| 50 data.enable_pixel_dumping = pixel_switch == "--pixel-test"; | |
| 51 data.expected_pixel_hash = pixel_hash; | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 class SkyTester : public mojo::ApplicationDelegate, | |
| 57 public mojo::ViewManagerDelegate, | |
| 58 public window_manager::WindowManagerDelegate, | |
| 59 public mojo::ViewObserver, | |
| 60 public TestRunnerClient { | |
| 61 public: | |
| 62 SkyTester() | |
| 63 : window_manager_app_(new window_manager::WindowManagerApp(this, this)), | |
| 64 root_(NULL), | |
| 65 content_(NULL), | |
| 66 weak_ptr_factory_(this) {} | |
| 67 ~SkyTester() override {} | |
| 68 | |
| 69 private: | |
| 70 // Overridden from mojo::ApplicationDelegate: | |
| 71 void Initialize(mojo::ApplicationImpl* impl) override { | |
| 72 window_manager_app_->Initialize(impl); | |
| 73 | |
| 74 if (impl->args().size() >= 2) | |
| 75 url_from_args_ = impl->args()[1]; | |
| 76 } | |
| 77 bool ConfigureIncomingConnection( | |
| 78 mojo::ApplicationConnection* connection) override { | |
| 79 window_manager_app_->ConfigureIncomingConnection(connection); | |
| 80 if (test_runner_) | |
| 81 connection->AddService(test_runner_.get()); | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 // Overridden from mojo::ViewManagerDelegate: | |
| 86 void OnEmbed(mojo::View* root, | |
| 87 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
| 88 mojo::ServiceProviderPtr exposed_services) override { | |
| 89 root_ = root; | |
| 90 root_->AddObserver(this); | |
| 91 | |
| 92 content_ = root->view_manager()->CreateView(); | |
| 93 content_->SetBounds(root_->bounds()); | |
| 94 root_->AddChild(content_); | |
| 95 content_->SetVisible(true); | |
| 96 | |
| 97 std::cout << "#READY\n"; | |
| 98 std::cout.flush(); | |
| 99 ScheduleRun(); | |
| 100 } | |
| 101 | |
| 102 // Overridden from window_manager::WindowManagerDelegate: | |
| 103 void Embed(const mojo::String& url, | |
| 104 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
| 105 mojo::ServiceProviderPtr exposed_services) override {} | |
| 106 | |
| 107 void OnViewManagerDisconnected(mojo::ViewManager* view_manager) override { | |
| 108 root_ = NULL; | |
| 109 } | |
| 110 | |
| 111 void OnViewDestroyed(mojo::View* view) override { | |
| 112 view->RemoveObserver(this); | |
| 113 } | |
| 114 | |
| 115 void OnViewBoundsChanged(mojo::View* view, | |
| 116 const mojo::Rect& old_bounds, | |
| 117 const mojo::Rect& new_bounds) override { | |
| 118 content_->SetBounds(new_bounds); | |
| 119 } | |
| 120 | |
| 121 void ScheduleRun() { | |
| 122 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 123 base::Bind(&SkyTester::Run, weak_ptr_factory_.GetWeakPtr())); | |
| 124 } | |
| 125 | |
| 126 void Run() { | |
| 127 DCHECK(!test_runner_); | |
| 128 | |
| 129 UrlData data; | |
| 130 if (url_from_args_.length()) { | |
| 131 data.url = url_from_args_; | |
| 132 } else { | |
| 133 WaitForURL(data); | |
| 134 } | |
| 135 | |
| 136 test_runner_.reset(new TestRunner(this, content_, data.url, | |
| 137 data.enable_pixel_dumping)); | |
| 138 } | |
| 139 | |
| 140 void OnTestComplete() override { | |
| 141 test_runner_.reset(); | |
| 142 if (url_from_args_.length()) | |
| 143 exit(0); | |
| 144 ScheduleRun(); | |
| 145 } | |
| 146 | |
| 147 void DispatchInputEvent(mojo::EventPtr event) override { | |
| 148 window_manager_app_->DispatchInputEventToView(content_, event.Pass()); | |
| 149 } | |
| 150 | |
| 151 scoped_ptr<window_manager::WindowManagerApp> window_manager_app_; | |
| 152 | |
| 153 std::string url_from_args_; | |
| 154 | |
| 155 mojo::View* root_; | |
| 156 mojo::View* content_; | |
| 157 | |
| 158 scoped_ptr<TestRunner> test_runner_; | |
| 159 | |
| 160 base::WeakPtrFactory<SkyTester> weak_ptr_factory_; | |
| 161 | |
| 162 DISALLOW_COPY_AND_ASSIGN(SkyTester); | |
| 163 }; | |
| 164 | |
| 165 } // namespace tester | |
| 166 } // namespace examples | |
| 167 | |
| 168 MojoResult MojoMain(MojoHandle application_request) { | |
| 169 mojo::ApplicationRunnerChromium runner(new sky::tester::SkyTester); | |
| 170 return runner.Run(application_request); | |
| 171 } | |
| OLD | NEW |