Skip to content

Commit 028074c

Browse files
Copilotbinarywang
andcommitted
Detailed analysis of Tips control link support
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent 087c85c commit 028074c

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

TipsLinkTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package test;
2+
3+
import me.chanjar.weixin.cp.bean.oa.applydata.ContentValue;
4+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
5+
import java.util.Arrays;
6+
7+
public class TipsLinkTest {
8+
public static void main(String[] args) {
9+
testCurrentStructure();
10+
}
11+
12+
public static void testCurrentStructure() {
13+
System.out.println("Testing current ContentValue.NewTips structure with Link:");
14+
15+
// Create a Tips structure with both plain text and link
16+
ContentValue.NewTips tips = new ContentValue.NewTips();
17+
ContentValue.NewTips.TipsContent tipsContent = new ContentValue.NewTips.TipsContent();
18+
ContentValue.NewTips.TipsContent.Text text = new ContentValue.NewTips.TipsContent.Text();
19+
20+
// Create plain text subtext
21+
ContentValue.NewTips.TipsContent.SubText plainSubText = new ContentValue.NewTips.TipsContent.SubText();
22+
ContentValue.NewTips.TipsContent.SubText.Content plainContent = new ContentValue.NewTips.TipsContent.SubText.Content();
23+
ContentValue.NewTips.TipsContent.SubText.Content.PlainText plainTextContent =
24+
new ContentValue.NewTips.TipsContent.SubText.Content.PlainText();
25+
plainTextContent.setContent("This is plain text. For more info, ");
26+
plainContent.setPlainText(plainTextContent);
27+
plainSubText.setType(1); // Type 1 for plain text
28+
plainSubText.setContent(plainContent);
29+
30+
// Create link subtext
31+
ContentValue.NewTips.TipsContent.SubText linkSubText = new ContentValue.NewTips.TipsContent.SubText();
32+
ContentValue.NewTips.TipsContent.SubText.Content linkContent = new ContentValue.NewTips.TipsContent.SubText.Content();
33+
ContentValue.NewTips.TipsContent.SubText.Content.Link link =
34+
new ContentValue.NewTips.TipsContent.SubText.Content.Link();
35+
link.setTitle("click here");
36+
link.setUrl("https://work.weixin.qq.com");
37+
linkContent.setLink(link);
38+
linkSubText.setType(2); // Type 2 for link
39+
linkSubText.setContent(linkContent);
40+
41+
text.setSubText(Arrays.asList(plainSubText, linkSubText));
42+
tipsContent.setText(text);
43+
tipsContent.setLang("zh_CN");
44+
45+
tips.setTipsContent(Arrays.asList(tipsContent));
46+
47+
// Convert to JSON
48+
String json = WxCpGsonBuilder.create().toJson(tips);
49+
System.out.println("Generated JSON:");
50+
System.out.println(json);
51+
52+
// Try to parse it back
53+
try {
54+
ContentValue.NewTips parsedTips = WxCpGsonBuilder.create().fromJson(json, ContentValue.NewTips.class);
55+
System.out.println("\nParsed back successfully: " + (parsedTips != null));
56+
if (parsedTips != null && parsedTips.getTipsContent() != null && !parsedTips.getTipsContent().isEmpty()) {
57+
ContentValue.NewTips.TipsContent.Text parsedText = parsedTips.getTipsContent().get(0).getText();
58+
if (parsedText != null && parsedText.getSubText() != null && parsedText.getSubText().size() > 1) {
59+
System.out.println("Plain text: " + parsedText.getSubText().get(0).getContent().getPlainText().getContent());
60+
System.out.println("Link title: " + parsedText.getSubText().get(1).getContent().getLink().getTitle());
61+
System.out.println("Link URL: " + parsedText.getSubText().get(1).getContent().getLink().getUrl());
62+
}
63+
}
64+
} catch (Exception e) {
65+
System.out.println("Error parsing: " + e.getMessage());
66+
e.printStackTrace();
67+
}
68+
}
69+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package me.chanjar.weixin.cp.bean.oa.applydata;
2+
3+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
4+
import org.testng.annotations.Test;
5+
6+
import java.util.Arrays;
7+
8+
import static org.testng.Assert.*;
9+
10+
public class ContentValueTipsTest {
11+
12+
@Test
13+
public void testTipsWithLinks() {
14+
System.out.println("Testing ContentValue.NewTips structure with Link:");
15+
16+
// Create a Tips structure with both plain text and link
17+
ContentValue.NewTips tips = new ContentValue.NewTips();
18+
ContentValue.NewTips.TipsContent tipsContent = new ContentValue.NewTips.TipsContent();
19+
ContentValue.NewTips.TipsContent.Text text = new ContentValue.NewTips.TipsContent.Text();
20+
21+
// Create plain text subtext
22+
ContentValue.NewTips.TipsContent.SubText plainSubText = new ContentValue.NewTips.TipsContent.SubText();
23+
ContentValue.NewTips.TipsContent.SubText.Content plainContent = new ContentValue.NewTips.TipsContent.SubText.Content();
24+
ContentValue.NewTips.TipsContent.SubText.Content.PlainText plainTextContent =
25+
new ContentValue.NewTips.TipsContent.SubText.Content.PlainText();
26+
plainTextContent.setContent("This is plain text. For more info, ");
27+
plainContent.setPlainText(plainTextContent);
28+
plainSubText.setType(1); // Type 1 for plain text
29+
plainSubText.setContent(plainContent);
30+
31+
// Create link subtext
32+
ContentValue.NewTips.TipsContent.SubText linkSubText = new ContentValue.NewTips.TipsContent.SubText();
33+
ContentValue.NewTips.TipsContent.SubText.Content linkContent = new ContentValue.NewTips.TipsContent.SubText.Content();
34+
ContentValue.NewTips.TipsContent.SubText.Content.Link link =
35+
new ContentValue.NewTips.TipsContent.SubText.Content.Link();
36+
link.setTitle("click here");
37+
link.setUrl("https://work.weixin.qq.com");
38+
linkContent.setLink(link);
39+
linkSubText.setType(2); // Type 2 for link
40+
linkSubText.setContent(linkContent);
41+
42+
text.setSubText(Arrays.asList(plainSubText, linkSubText));
43+
tipsContent.setText(text);
44+
tipsContent.setLang("zh_CN");
45+
46+
tips.setTipsContent(Arrays.asList(tipsContent));
47+
48+
// Convert to JSON
49+
String json = WxCpGsonBuilder.create().toJson(tips);
50+
System.out.println("Generated JSON:");
51+
System.out.println(json);
52+
53+
// Try to parse it back
54+
try {
55+
ContentValue.NewTips parsedTips = WxCpGsonBuilder.create().fromJson(json, ContentValue.NewTips.class);
56+
assertNotNull(parsedTips);
57+
assertNotNull(parsedTips.getTipsContent());
58+
assertFalse(parsedTips.getTipsContent().isEmpty());
59+
60+
ContentValue.NewTips.TipsContent.Text parsedText = parsedTips.getTipsContent().get(0).getText();
61+
assertNotNull(parsedText);
62+
assertNotNull(parsedText.getSubText());
63+
assertEquals(parsedText.getSubText().size(), 2);
64+
65+
// Verify plain text
66+
assertEquals(parsedText.getSubText().get(0).getType().intValue(), 1);
67+
assertEquals(parsedText.getSubText().get(0).getContent().getPlainText().getContent(), "This is plain text. For more info, ");
68+
69+
// Verify link
70+
assertEquals(parsedText.getSubText().get(1).getType().intValue(), 2);
71+
assertEquals(parsedText.getSubText().get(1).getContent().getLink().getTitle(), "click here");
72+
assertEquals(parsedText.getSubText().get(1).getContent().getLink().getUrl(), "https://work.weixin.qq.com");
73+
74+
System.out.println("Test passed successfully!");
75+
} catch (Exception e) {
76+
System.out.println("Error parsing: " + e.getMessage());
77+
e.printStackTrace();
78+
fail("Failed to parse JSON: " + e.getMessage());
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)