From 34e1800716ef5a0f951b7b2fe46848f50c341842 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 29 Aug 2013 11:47:07 +0800 Subject: [PATCH] Add 'loading-state-changed' event for BrowserWindow. It's required for testing the BrowserWindow class. --- browser/api/atom_api_window.cc | 6 ++++++ browser/api/atom_api_window.h | 1 + browser/native_window_observer.h | 2 +- docs/browser-window.md | 9 ++++++++- spec/api/window.coffee | 16 ++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index df70545c005e..4749f7ec04b0 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -56,6 +56,12 @@ void Window::OnPageTitleUpdated(bool* prevent_default, *prevent_default = Emit("page-title-updated", &args); } +void Window::OnLoadingStateChanged(bool is_loading) { + base::ListValue args; + args.AppendBoolean(is_loading); + Emit("loading-state-changed", &args); +} + void Window::WillCloseWindow(bool* prevent_default) { *prevent_default = Emit("close"); } diff --git a/browser/api/atom_api_window.h b/browser/api/atom_api_window.h index 48e7678924d7..1de71781eb58 100644 --- a/browser/api/atom_api_window.h +++ b/browser/api/atom_api_window.h @@ -35,6 +35,7 @@ class Window : public EventEmitter, // Implementations of NativeWindowObserver. virtual void OnPageTitleUpdated(bool* prevent_default, const std::string& title) OVERRIDE; + virtual void OnLoadingStateChanged(bool is_loading) OVERRIDE; virtual void WillCloseWindow(bool* prevent_default) OVERRIDE; virtual void OnWindowClosed() OVERRIDE; virtual void OnWindowBlur() OVERRIDE; diff --git a/browser/native_window_observer.h b/browser/native_window_observer.h index 54f8d01ddc47..5d06f983bd55 100644 --- a/browser/native_window_observer.h +++ b/browser/native_window_observer.h @@ -17,7 +17,7 @@ class NativeWindowObserver { virtual void OnPageTitleUpdated(bool* prevent_default, const std::string& title) {} - // Called when the window is starting or is done loading a resource. + // Called when the window is starting or is done loading a page. virtual void OnLoadingStateChanged(bool is_loading) {} // Called when the window is gonna closed. diff --git a/docs/browser-window.md b/docs/browser-window.md index f9fcc9bd9e92..f6a3fa9c963c 100644 --- a/docs/browser-window.md +++ b/docs/browser-window.md @@ -45,6 +45,13 @@ Creates a new `BrowserWindow` with native properties set by the `options`. Usual Emitted when the document changed its title, calling `event.preventDefault()` would prevent the native window's title to change. +### Event: 'loading-state-changed' + +* `event` Event +* `isLoading` Boolean + +Emitted when the window is starting or is done loading a page. + ### Event: 'close' * `event` Event @@ -325,4 +332,4 @@ Reloads current window. ### BrowserWindow.reloadIgnoringCache() -Reloads current window and ignores cache. \ No newline at end of file +Reloads current window and ignores cache. diff --git a/spec/api/window.coffee b/spec/api/window.coffee index a0c5e795e005..cd832e9618b0 100644 --- a/spec/api/window.coffee +++ b/spec/api/window.coffee @@ -31,3 +31,19 @@ describe 'window module', -> assert.equal String(content), 'close' done() w.loadUrl 'file://' + path.join(fixtures, 'api', 'close.html') + + describe 'BrowserWindow.loadUrl(url)', -> + it 'should emit loading-state-changed event', (done) -> + w = new BrowserWindow(show: false) + count = 0 + w.on 'loading-state-changed', (event, isLoading) -> + if count == 0 + assert.equal isLoading, true + else if count == 1 + assert.equal isLoading, false + done() + else + assert false + + ++count + w.loadUrl 'about:blank'