Skip to content

Commit 1ad3419

Browse files
Ported 6 samples to core-unittests (#4212)
Ported the following samples to `maven/core-unittests`: - `MultiListTest` -> `MultiListSampleTest` - `MySample` -> `MySampleTest` - `NestedFormWithSwipeableTabsTest2776` -> `NestedFormWithSwipeableTabsTest2776Test` - `NestedTabsTest3023` -> `NestedTabsTest3023Test` - `NullPointerOnEDTSample2992` -> `NullPointerOnEDTSample2992Test` - `Oauth2Login` -> `Oauth2LoginSampleTest` Adapted logic to use `UITestBase`, `@FormTest`, and Java 8 syntax. Ensured no side effects on other tests (except strictly reproducing sample logic where necessary). Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent e809418 commit 1ad3419

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Form;
6+
import com.codename1.ui.layouts.BorderLayout;
7+
import com.codename1.ui.list.MultiList;
8+
import com.codename1.ui.list.DefaultListModel;
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import static com.codename1.ui.CN.*;
13+
14+
public class MultiListSampleTest extends UITestBase {
15+
16+
@FormTest
17+
public void testMultiList() {
18+
Form current = new MyForm();
19+
current.show();
20+
// Just checking that it renders without error
21+
}
22+
23+
class MyForm extends Form {
24+
MultiList cmp = new MultiList();
25+
26+
MyForm() {
27+
Map<String, Object> entry = new HashMap<String, Object>();
28+
29+
entry.put("Line1", "somedata");
30+
entry.put("Line2", "somedata");
31+
entry.put("Line3", "somedata");
32+
entry.put("Line4", "somedata");
33+
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
34+
35+
data.add(entry);
36+
37+
38+
cmp.setModel(new DefaultListModel(data));
39+
setLayout(new BorderLayout());
40+
add(BorderLayout.CENTER, cmp);
41+
}
42+
}
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Form;
6+
import com.codename1.ui.Label;
7+
import com.codename1.ui.layouts.BoxLayout;
8+
import static com.codename1.ui.CN.*;
9+
10+
public class MySampleTest extends UITestBase {
11+
12+
@FormTest
13+
public void testMySample() {
14+
Form hi = new Form("Hi World", BoxLayout.y());
15+
hi.add(new Label("Hi World"));
16+
hi.show();
17+
// Simple smoke test to check if it displays
18+
}
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.components.SpanLabel;
4+
import com.codename1.junit.FormTest;
5+
import com.codename1.junit.UITestBase;
6+
import com.codename1.ui.Container;
7+
import com.codename1.ui.Form;
8+
import com.codename1.ui.Tabs;
9+
import com.codename1.ui.Toolbar;
10+
import com.codename1.ui.layouts.BorderLayout;
11+
import static com.codename1.ui.CN.*;
12+
13+
public class NestedFormWithSwipeableTabsTest2776Test extends UITestBase {
14+
15+
@FormTest
16+
public void testNestedFormWithSwipeableTabs() {
17+
Toolbar.setGlobalToolbar(true);
18+
Form outer = new Form("", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_SCALE));
19+
outer.getToolbar().hideToolbar();
20+
Form inner = new Form("Tabs", new BorderLayout());
21+
Container layeredPane1 = inner.getLayeredPane(NestedFormWithSwipeableTabsTest2776Test.class, 1);
22+
layeredPane1.setLayout(new BorderLayout());
23+
Container layeredPane2 = inner.getLayeredPane(AnotherClass.class, 2);
24+
layeredPane2.setLayout(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
25+
26+
Tabs tab = new Tabs();
27+
tab.addTab("Tab1", new SpanLabel("Tab 1"));
28+
tab.addTab("Tab2", new SpanLabel("Tab 2"));
29+
30+
inner.add(BorderLayout.CENTER, tab);
31+
outer.add(BorderLayout.CENTER, inner);
32+
outer.revalidate();
33+
outer.show();
34+
// Smoke test to ensure no exceptions
35+
}
36+
37+
private static class AnotherClass {
38+
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Container;
6+
import com.codename1.ui.Form;
7+
import com.codename1.ui.Label;
8+
import com.codename1.ui.Tabs;
9+
import com.codename1.ui.layouts.BorderLayout;
10+
import com.codename1.ui.table.TableLayout;
11+
import static com.codename1.ui.CN.*;
12+
13+
public class NestedTabsTest3023Test extends UITestBase {
14+
15+
@FormTest
16+
public void testNestedTabs() {
17+
Form f = new Form("custom tabs", new BorderLayout());
18+
Tabs outerTabs = new Tabs();
19+
20+
Tabs tabs = new Tabs();
21+
22+
tabs.addTab("inner1", new Label("content1"));
23+
tabs.addTab("inner2", new Label("content2"));
24+
tabs.addTab("inner3", new Label("content3"));
25+
26+
Container tabsCont = tabs.getTabsContainer();
27+
28+
TableLayout newLayout = new TableLayout(1, 2);
29+
tabsCont.getComponentAt(0).setHidden(true);
30+
tabsCont.setLayout(newLayout);
31+
32+
newLayout.addLayoutComponent(newLayout.cc(0,0).widthPercentage(50), tabsCont.getComponentAt(1), tabsCont);
33+
newLayout.addLayoutComponent(newLayout.cc(0,1).widthPercentage(50), tabsCont.getComponentAt(2), tabsCont);
34+
35+
outerTabs.addTab("outer1", new Container());
36+
outerTabs.addTab("outer2", tabs);
37+
38+
39+
f.addComponent(BorderLayout.CENTER, outerTabs);
40+
f.show();
41+
// Smoke test to ensure no exceptions
42+
}
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Form;
6+
import com.codename1.ui.TextField;
7+
import com.codename1.ui.layouts.BoxLayout;
8+
import static com.codename1.ui.CN.*;
9+
10+
public class NullPointerOnEDTSample2992Test extends UITestBase {
11+
12+
@FormTest
13+
public void testNullPointerOnEDTSample() {
14+
Form hi = new Form("Hi World", BoxLayout.y());
15+
hi.add(new TextField("", "Write here"));
16+
hi.show();
17+
// Smoke test to ensure no exceptions
18+
}
19+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.io.AccessToken;
4+
import com.codename1.io.Log;
5+
import com.codename1.io.Oauth2;
6+
import com.codename1.junit.FormTest;
7+
import com.codename1.junit.UITestBase;
8+
import com.codename1.ui.Button;
9+
import com.codename1.ui.Dialog;
10+
import com.codename1.ui.Form;
11+
import com.codename1.ui.Label;
12+
import com.codename1.ui.layouts.BoxLayout;
13+
import static com.codename1.ui.CN.*;
14+
15+
public class Oauth2LoginSampleTest extends UITestBase {
16+
private static String REDIRECT_URI = "https://weblite.ca/a/auth4/loggedin.html";
17+
private AccessToken accessToken;
18+
19+
@FormTest
20+
public void testOauth2Login() {
21+
if (Oauth2.handleRedirect(e->{
22+
23+
24+
if(e.getSource() instanceof AccessToken) {
25+
Log.p("Logged in: " + e);
26+
AccessToken token = (AccessToken)e.getSource();
27+
setAccessToken(token);
28+
showLoggedIn();
29+
} else {
30+
Log.p("Failed to login " + e.getSource());
31+
showLogin();
32+
33+
}
34+
return;
35+
36+
})) {
37+
return;
38+
}
39+
40+
showLogin();
41+
// Smoke test to ensure no exceptions
42+
}
43+
44+
private void doLogin() {
45+
46+
Oauth2.setBackToParent(false);
47+
Oauth2 login = new Oauth2("https://auth.codenameone.com/auth/realms/Realm/protocol/openid-connect/auth?client_id=cn1cloudapp&scope=openid%20address&response_type=code&state=a2V5Y2xvYWs=", "cn1cloudapp", REDIRECT_URI, "openid address",
48+
"https://auth.codenameone.com/auth/realms/Realm/protocol/openid-connect/token", null);
49+
login.setUseRedirectForWeb(true);
50+
login.showAuthentication(e -> {
51+
Log.p("Logged in: " + e);
52+
if(e.getSource() instanceof AccessToken) {
53+
AccessToken token = (AccessToken)e.getSource();
54+
setAccessToken(token);
55+
showLoggedIn();
56+
}
57+
});
58+
}
59+
60+
private void showLoggedIn() {
61+
Form f = new Form("Logged In", BoxLayout.y());
62+
f.add(new Label("You are logged in"));
63+
f.show();
64+
}
65+
66+
private void showLogin() {
67+
Form f = new Form("Login", BoxLayout.y());
68+
Button b = new Button("Login");
69+
b.addActionListener(evt->{
70+
doLogin();
71+
});
72+
f.add(b);
73+
f.show();
74+
}
75+
76+
private void setAccessToken(AccessToken tok) {
77+
accessToken = tok;
78+
}
79+
}

0 commit comments

Comments
 (0)