Introduce Tizen.WebView
[platform/core/csapi/webview.git] / Tizen.WebView.Test / SimpleWebviewApp.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21
22 using ElmSharp;
23 using Tizen.Applications;
24 using Tizen.WebView;
25
26
27 namespace Tizen.WebView.Test
28 {
29     public class SimpleWebviewApp : CoreUIApplication
30     {
31         private const string LogTag = "WebViewApp";
32
33         private const string _windowName = "Simple WebView App";
34         private const string _defaultUrl = "http://www.google.com";
35         private WebView _webview;
36         private Entry _addressEntry;
37         private Button _reloadButton;
38
39         public SimpleWebviewApp()
40         {
41
42         }
43
44         protected override void OnCreate()
45         {
46             base.OnCreate();
47             Chromium.Initialize();
48             // Create webview and toolbox
49             CreateUI();
50         }
51
52         protected override void OnTerminate()
53         {
54             Chromium.Shutdown();
55             base.OnTerminate();
56         }
57
58         private void CreateUI()
59         {
60             // Create Window
61             Window window = new Window(_windowName);
62             window.Show();
63
64             // Create Box for main window
65             Box mainBox = CreateBaseUI(window);
66
67             // Create top bar
68             Box topBar = CreateTopBar(window);
69
70             // Create Webview
71             _webview = new WebView(window)
72             {
73                 AlignmentX = -1,
74                 AlignmentY = -1,
75                 WeightX = 1,
76                 WeightY = 1
77             };
78             _webview.Show();
79
80             // Create bottom bar
81             Box bottomBar = CreateBottomBar(window);
82
83             mainBox.PackEnd(topBar);
84             mainBox.PackEnd(_webview);
85             mainBox.PackEnd(bottomBar);
86
87             InitializeWebView();
88
89             // Load default URL
90             _webview.LoadUrl(_defaultUrl);
91         }
92
93         private Box CreateBaseUI(Window window)
94         {
95             // Create Background
96             Background background = new Background(window)
97             {
98                 AlignmentX = -1,
99                 AlignmentY = -1,
100                 WeightX = 1,
101                 WeightY = 1,
102                 Color = Color.White
103             };
104             background.Show();
105             window.AddResizeObject(background);
106
107             // Create Conformant
108             Conformant conformant = new Conformant(window);
109             conformant.Show();
110
111             // Create Box for all contents
112             Box box = new Box(window)
113             {
114                 AlignmentX = -1,
115                 AlignmentY = -1,
116                 WeightX = 1,
117                 WeightY = 1
118             };
119             box.Show();
120             conformant.SetContent(box);
121
122             return box;
123         }
124
125         private Box CreateTopBar(Window window)
126         {
127             // Create Box for address bar
128             Box topBar = new Box(window)
129             {
130                 AlignmentX = -1,
131                 AlignmentY = 0,
132                 WeightX = 1,
133                 WeightY = 0,
134                 IsHorizontal = true
135             };
136             topBar.Show();
137
138             // Create address entry
139             _addressEntry = new Entry(window)
140             {
141                 AlignmentX = -1,
142                 AlignmentY = -1,
143                 WeightX = 1,
144                 WeightY = 1,
145                 IsSingleLine = true,
146                 Scrollable = true,
147                 Text = _defaultUrl
148             };
149             _addressEntry.SetInputPanelLayout(InputPanelLayout.Url);
150             _addressEntry.Activated += (s, e) =>
151             {
152                 _webview.LoadUrl(((Entry)s).Text);
153             };
154             _addressEntry.Show();
155
156             // Create reload button
157             _reloadButton = new Button(window)
158             {
159                 AlignmentX = -1,
160                 AlignmentY = -1,
161                 WeightX = 0.3,
162                 WeightY = 1,
163                 Text = "Reload"
164             };
165             _reloadButton.Clicked += (s, e) =>
166             {
167                 if (_reloadButton.Text.Equals("Reload"))
168                 {
169                     _webview.Reload();
170                 }
171                 else if (_reloadButton.Text.Equals("Stop"))
172                 {
173                     _webview.StopLoading();
174                 }
175             };
176             _reloadButton.Show();
177
178             topBar.PackEnd(_addressEntry);
179             topBar.PackEnd(_reloadButton);
180
181             return topBar;
182         }
183
184         private Box CreateBottomBar(Window window)
185         {
186             // Create Box for bottom bar
187             Box bottomBar = new Box(window)
188             {
189                 AlignmentX = -1,
190                 AlignmentY = 1,
191                 WeightX = 1,
192                 WeightY = 0,
193                 IsHorizontal = true
194             };
195             bottomBar.Show();
196
197             // Create back/forward buttons
198             Button backButton = new Button(window)
199             {
200                 AlignmentX = -1,
201                 AlignmentY = 0.5,
202                 WeightX = 1,
203                 WeightY = 1,
204                 Text = "Back"
205
206             };
207             backButton.Clicked += (s, e) =>
208             {
209                 if (_webview.CanGoBack())
210                     _webview.GoBack();
211             };
212             backButton.Show();
213
214             Button forwardButton = new Button(window)
215             {
216                 AlignmentX = -1,
217                 AlignmentY = 0.5,
218                 WeightX = 1,
219                 WeightY = 1,
220                 Text = "Forward"
221
222             };
223             forwardButton.Clicked += (s, e) =>
224             {
225                 if (_webview.CanGoForward())
226                     _webview.GoForward();
227             };
228             forwardButton.Show();
229
230             bottomBar.PackEnd(backButton);
231             bottomBar.PackEnd(forwardButton);
232
233             return bottomBar;
234         }
235
236         private void InitializeWebView()
237         {
238             _webview.LoadStarted += (s, e) =>
239             {
240                 Log.Info(LogTag, "Load started");
241                 _reloadButton.Text = "Stop";
242             };
243
244             _webview.LoadFinished += (s, e) =>
245             {
246                 Log.Info(LogTag, "Load finished");
247                 _reloadButton.Text = "Reload";
248             };
249
250             _webview.LoadError += (s, e) =>
251             {
252                 Log.Info(LogTag, "Load error(" + e.Code + "): " + e.Description);
253             };
254
255             _webview.UrlChanged += (s, e) =>
256             {
257                 Log.Info(LogTag, "Url changed: " + e.GetAsString());
258                 _addressEntry.Text = e.GetAsString();
259             };
260
261             CookieManager cookieManager = _webview.GetContext().GetCookieManager();
262             if (cookieManager != null)
263             {
264                 cookieManager.SetCookieAcceptPolicy(CookieAcceptPolicy.Always);
265                 cookieManager.SetPersistentStorage(DirectoryInfo.Data, CookiePersistentStorage.SqlLite);
266             }
267         }
268
269         static void Main(string[] args)
270         {
271             Elementary.Initialize();
272             Elementary.ThemeOverlay();
273
274             SimpleWebviewApp app = new Test.SimpleWebviewApp();
275             app.Run(args);
276         }
277     }
278 }