sync ElmSharp source code latest
[platform/core/csapi/tizenfx.git] / test / ElmSharp.Test / TC / Wearable / ImageTest1.cs
1 /*
2  * Copyright (c) 2016 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.IO;
19
20 namespace ElmSharp.Test.Wearable
21 {
22     public class ImageTest1 : WearableTestCase
23     {
24         public override string TestName => "ImageTest1";
25         public override string TestDescription => "To test basic operation of Image";
26
27         Image image;
28         Label lbInfo;
29         string[] btn_names = new string[] {"File1", "File2", "Uri", "Strm", "FileA1", "FileA2", "UriA", "StrmA"};
30
31         public override void Run(Window window)
32         {
33             Rect square = window.GetInnerSquare();
34
35             Button[] btns = new Button[8];
36             Size btnSize = new Size(square.Width / 4 - 2, square.Height / 5 - 1);
37             for (int i = 0; i < 2; i++)
38             {
39                 for (int j = 0; j < 4; j++)
40                 {
41                     btns[i * 4 + j] = new Button(window)
42                     {
43                         Text = "<span color=#ffffff font_size=12>" + btn_names[i * 4 + j] + "</span>",
44                     };
45                     int x = j * btnSize.Width + j *2;
46                     int y = i * btnSize.Height + i;
47                     btns[i * 4 + j].Geometry = new Rect(square.X + x, square.Y + y, btnSize.Width, btnSize.Height);
48                     btns[i * 4 + j].Show();
49                 }
50             }
51
52             lbInfo = new Label(window)
53             {
54                 Color = Color.White,
55                 AlignmentX = -1,
56                 AlignmentY = 0,
57                 WeightX = 1
58             };
59             lbInfo.Show();
60             lbInfo.Geometry = new Rect(square.X,  square.Y + square.Height, square.Width, 15);
61
62             image = new Image(window)
63             {
64                 IsFixedAspect = true,
65                 AlignmentX = -1,
66                 AlignmentY = -1,
67             };
68             image.Show();
69             image.Load(Path.Combine(TestRunner.ResourceDir, "picture.png"));
70             image.Geometry = new Rect(square.X, square.Y + btnSize.Height * 2 + 2 , square.Width, btnSize.Height * 3);
71             image.Clicked += (s, e) =>
72             {
73                 Console.WriteLine("Image has been clicked. (IsFixedAspect = {0}", image.IsFixedAspect);
74                 image.IsFixedAspect = image.IsFixedAspect == true ? false : true;
75             };
76
77             btns[0].Clicked += (s, e) => LoadFile("TED/large/a.jpg");
78             btns[1].Clicked += (s, e) => LoadFile("TED/large/b.jpg");
79             btns[2].Clicked += (s, e) => LoadUri("http://pe.tedcdn.com/images/ted/2e306b9655267cee35e45688ace775590b820510_615x461.jpg");
80             btns[3].Clicked += (s, e) => LoadStream(new FileStream(Path.Combine(TestRunner.ResourceDir, "TED/large/c.jpg"), FileMode.Open, FileAccess.Read));
81
82             btns[4].Clicked += (s, e) => LoadFileAsync("TED/large/d.jpg");
83             btns[5].Clicked += (s, e) => LoadFileAsync("TED/large/e.jpg");
84             btns[6].Clicked += (s, e) => LoadUriAsync("http://pe.tedcdn.com/images/ted/2e306b9655267cee35e45688ace775590b820510_615x461.jpg");
85             btns[7].Clicked += (s, e) => LoadStreamAsync(new FileStream(Path.Combine(TestRunner.ResourceDir, "TED/large/f.jpg"), FileMode.Open, FileAccess.Read));
86         }
87
88         void LoadFile(string file)
89         {
90             bool ret = image.Load(Path.Combine(TestRunner.ResourceDir, file));
91             if (ret)
92                 UpdateLabelText(lbInfo, image.File);
93             else
94                 UpdateLabelText(lbInfo, "Loading Failed.");
95         }
96
97         void LoadUri(string uri)
98         {
99             bool ret = image.Load(uri);
100             if (ret)
101                 UpdateLabelText(lbInfo, image.File);
102             else
103                 UpdateLabelText(lbInfo, "Loading Failed.");
104         }
105
106         void LoadStream(Stream stream)
107         {
108             bool ret = image.Load(stream);
109             if (ret)
110                 UpdateLabelText(lbInfo, image.File);
111             else
112                 UpdateLabelText(lbInfo, "Loading Failed.");
113         }
114
115         async void LoadFileAsync(string file)
116         {
117             var ret = await image.LoadAsync(Path.Combine(TestRunner.ResourceDir, file));
118             if (ret)
119                 UpdateLabelText(lbInfo, image.File);
120             else
121                 UpdateLabelText(lbInfo, "Loading Failed.");
122         }
123
124         async void LoadUriAsync(string uri)
125         {
126             var ret = await image.LoadAsync(uri);
127             if (ret)
128                 UpdateLabelText(lbInfo, image.File);
129             else
130                 UpdateLabelText(lbInfo, "Loading Failed.");
131         }
132
133         async void LoadStreamAsync(Stream stream)
134         {
135             var ret = await image.LoadAsync(stream);
136             if (ret)
137                 UpdateLabelText(lbInfo, image.File);
138             else
139                 UpdateLabelText(lbInfo, "Loading Failed.");
140         }
141
142         void UpdateLabelText(Label lable, string text)
143         {
144             lable.Text = "<span color=#ffffff font_size=12>" + text + "</span>";
145         }
146     }
147 }