• Main Page
  • Classes
  • Files
  • File List

CClipboardTests.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2011 Nick Bolton
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <gtest/gtest.h>
00020 #include "CClipboard.h"
00021 
00022 TEST(CClipboardTests, empty_openCalled_returnsTrue)
00023 {
00024     CClipboard clipboard;
00025     clipboard.open(0);
00026 
00027     bool actual = clipboard.empty();
00028 
00029     EXPECT_EQ(true, actual);
00030 }
00031 
00032 TEST(CClipboardTests, empty_singleFormat_hasReturnsFalse)
00033 {
00034     CClipboard clipboard;
00035     clipboard.open(0);
00036     clipboard.add(CClipboard::kText, "synergy rocks!");
00037 
00038     clipboard.empty();
00039 
00040     bool actual = clipboard.has(CClipboard::kText);
00041     EXPECT_FALSE(actual);
00042 }
00043 
00044 TEST(CClipboardTests, add_newValue_valueWasStored)
00045 {
00046     CClipboard clipboard;
00047     clipboard.open(0);
00048 
00049     clipboard.add(IClipboard::kText, "synergy rocks!");
00050 
00051     CString actual = clipboard.get(IClipboard::kText);
00052     EXPECT_EQ("synergy rocks!", actual);
00053 }
00054 
00055 TEST(CClipboardTests, add_replaceValue_valueWasReplaced)
00056 {
00057     CClipboard clipboard;
00058     clipboard.open(0);
00059 
00060     clipboard.add(IClipboard::kText, "synergy rocks!");
00061     clipboard.add(IClipboard::kText, "maxivista sucks"); // haha, just kidding.
00062 
00063     CString actual = clipboard.get(IClipboard::kText);
00064     EXPECT_EQ("maxivista sucks", actual);
00065 }
00066 
00067 TEST(CClipboardTests, open_timeIsZero_returnsTrue)
00068 {
00069     CClipboard clipboard;
00070 
00071     bool actual = clipboard.open(0);
00072 
00073     EXPECT_EQ(true, actual);
00074 }
00075 
00076 TEST(CClipboardTests, open_timeIsOne_returnsTrue)
00077 {
00078     CClipboard clipboard;
00079 
00080     bool actual = clipboard.open(1);
00081 
00082     EXPECT_EQ(true, actual);
00083 }
00084 
00085 TEST(CClipboardTests, close_isOpen_noErrors)
00086 {
00087     CClipboard clipboard;
00088     clipboard.open(0);
00089 
00090     clipboard.close();
00091 
00092     // can't assert anything
00093 }
00094 
00095 TEST(CClipboardTests, getTime_openWithNoEmpty_returnsZero)
00096 {
00097     CClipboard clipboard;
00098     clipboard.open(1);
00099 
00100     CClipboard::Time actual = clipboard.getTime();
00101 
00102     EXPECT_EQ(0, actual);
00103 }
00104 
00105 TEST(CClipboardTests, getTime_openAndEmpty_returnsOne)
00106 {
00107     CClipboard clipboard;
00108     clipboard.open(1);
00109     clipboard.empty();
00110 
00111     CClipboard::Time actual = clipboard.getTime();
00112 
00113     EXPECT_EQ(1, actual);
00114 }
00115 
00116 TEST(CClipboardTests, has_withFormatAdded_returnsTrue)
00117 {
00118     CClipboard clipboard;
00119     clipboard.open(0);
00120     clipboard.add(IClipboard::kText, "synergy rocks!");
00121 
00122     bool actual = clipboard.has(IClipboard::kText);
00123 
00124     EXPECT_EQ(true, actual);
00125 }
00126 
00127 TEST(CClipboardTests, has_withNoFormats_returnsFalse)
00128 {
00129     CClipboard clipboard;
00130     clipboard.open(0);
00131 
00132     bool actual = clipboard.has(IClipboard::kText);
00133 
00134     EXPECT_FALSE(actual);
00135 }
00136 
00137 TEST(CClipboardTests, get_withNoFormats_returnsEmpty)
00138 {
00139     CClipboard clipboard;
00140     clipboard.open(0);
00141 
00142     CString actual = clipboard.get(IClipboard::kText);
00143 
00144     EXPECT_EQ("", actual);
00145 }
00146 
00147 TEST(CClipboardTests, get_withFormatAdded_returnsExpected)
00148 {
00149     CClipboard clipboard;
00150     clipboard.open(0);
00151     clipboard.add(IClipboard::kText, "synergy rocks!");
00152 
00153     CString actual = clipboard.get(IClipboard::kText);
00154 
00155     EXPECT_EQ("synergy rocks!", actual);
00156 }
00157 
00158 TEST(CClipboardTests, marshall_addNotCalled_firstCharIsZero)
00159 {
00160     CClipboard clipboard;
00161 
00162     CString actual = clipboard.marshall();
00163 
00164     // seems to return "\0\0\0\0" but EXPECT_EQ can't assert this,
00165     // so instead, just assert that first char is '\0'.
00166     EXPECT_EQ(0, (int)actual[0]);
00167 }
00168 
00169 TEST(CClipboardTests, marshall_withTextAdded_typeCharIsText)
00170 {
00171     CClipboard clipboard;
00172     clipboard.open(0);
00173     clipboard.add(IClipboard::kText, "synergy rocks!");
00174     clipboard.close();
00175 
00176     CString actual = clipboard.marshall();
00177 
00178     // string contains other data, but 8th char should be kText.
00179     EXPECT_EQ(IClipboard::kText, (int)actual[7]);
00180 }
00181 
00182 TEST(CClipboardTests, marshall_withTextAdded_lastSizeCharIs14)
00183 {
00184     CClipboard clipboard;
00185     clipboard.open(0);
00186     clipboard.add(IClipboard::kText, "synergy rocks!"); // 14 chars
00187     clipboard.close();
00188 
00189     CString actual = clipboard.marshall();
00190 
00191     EXPECT_EQ(14, (int)actual[11]);
00192 }
00193 
00194 // TODO: there's some integer -> char encoding going on here. i find it 
00195 // hard to believe that the clipboard is the only thing doing this. maybe
00196 // we should refactor this stuff out of the clipboard.
00197 TEST(CClipboardTests, marshall_withTextSize285_sizeCharsValid)
00198 {
00199     // 285 chars
00200     CString data;
00201     data.append("Synergy is Free and Open Source Software that lets you ");
00202     data.append("easily share your mouse and keyboard between multiple ");
00203     data.append("computers, where each computer has it's own display. No ");
00204     data.append("special hardware is required, all you need is a local area ");
00205     data.append("network. Synergy is supported on Windows, Mac OS X and Linux.");
00206 
00207     CClipboard clipboard;
00208     clipboard.open(0);
00209     clipboard.add(IClipboard::kText, data);
00210     clipboard.close();
00211 
00212     CString actual = clipboard.marshall();
00213 
00214     // 4 asserts here, but that's ok because we're really just asserting 1 
00215     // thing. the 32-bit size value is split into 4 chars. if the size is 285
00216     // (29 more than the 8-bit max size), the last char "rolls over" to 29 
00217     // (this is caused by a bit-wise & on 0xff and 8-bit truncation). each 
00218     // char before the last stores a bit-shifted version of the number, each 
00219     // 1 more power than the last, which is done by bit-shifting [0] by 24, 
00220     // [1] by 16, [2] by 8 ([3] is not bit-shifted).
00221     EXPECT_EQ(0, actual[8]); // 285 >> 24 = 285 / (256^3) = 0
00222     EXPECT_EQ(0, actual[9]); // 285 >> 16 = 285 / (256^2) = 0
00223     EXPECT_EQ(1, actual[10]); // 285 >> 8 = 285 / (256^1) = 1(.11328125)
00224     EXPECT_EQ(29, actual[11]); // 285 - 256 = 29
00225 }
00226 
00227 TEST(CClipboardTests, marshall_withHtmlAdded_typeCharIsHtml)
00228 {
00229     CClipboard clipboard;
00230     clipboard.open(0);
00231     clipboard.add(IClipboard::kHTML, "html sucks");
00232     clipboard.close();
00233 
00234     CString actual = clipboard.marshall();
00235 
00236     // string contains other data, but 8th char should be kHTML.
00237     EXPECT_EQ(IClipboard::kHTML, (int)actual[7]);
00238 }
00239 
00240 TEST(CClipboardTests, marshall_withHtmlAndText_has2Formats)
00241 {
00242     CClipboard clipboard;
00243     clipboard.open(0);
00244     clipboard.add(IClipboard::kText, "synergy rocks");
00245     clipboard.add(IClipboard::kHTML, "html sucks");
00246     clipboard.close();
00247 
00248     CString actual = clipboard.marshall();
00249 
00250     // the number of formats is stored inside the first 4 chars.
00251     // the writeUInt32 function right-aligns numbers in 4 chars,
00252     // so if you right align 2, it will be "\0\0\0\2" in a string.
00253     // we assert that the char at the 4th index is 2 (the number of
00254     // formats that we've added).
00255     EXPECT_EQ(2, (int)actual[3]);
00256 }
00257 
00258 TEST(CClipboardTests, marshall_withTextAdded_endsWithAdded)
00259 {
00260     CClipboard clipboard;
00261     clipboard.open(0);
00262     clipboard.add(IClipboard::kText, "synergy rocks!");
00263     clipboard.close();
00264 
00265     CString actual = clipboard.marshall();
00266 
00267     // string contains other data, but should end in the string we added.
00268     EXPECT_EQ("synergy rocks!", actual.substr(12));
00269 }
00270 
00271 TEST(CClipboardTests, unmarshall_emptyData_hasTextIsFalse)
00272 {
00273     CClipboard clipboard;
00274 
00275     CString data;
00276     data += (char)0;
00277     data += (char)0;
00278     data += (char)0;
00279     data += (char)0; // 0 formats added
00280 
00281     clipboard.unmarshall(data, 0);
00282 
00283     clipboard.open(0);
00284     bool actual = clipboard.has(IClipboard::kText);
00285     EXPECT_FALSE(actual);
00286 }
00287 
00288 TEST(CClipboardTests, unmarshall_withTextSize285_getTextIsValid)
00289 {
00290     CClipboard clipboard;
00291 
00292     // 285 chars
00293     CString text;
00294     text.append("Synergy is Free and Open Source Software that lets you ");
00295     text.append("easily share your mouse and keyboard between multiple ");
00296     text.append("computers, where each computer has it's own display. No ");
00297     text.append("special hardware is required, all you need is a local area ");
00298     text.append("network. Synergy is supported on Windows, Mac OS X and Linux.");
00299 
00300     CString data;
00301     data += (char)0;
00302     data += (char)0;
00303     data += (char)0;
00304     data += (char)1; // 1 format added
00305     data += (char)0;
00306     data += (char)0;
00307     data += (char)0;
00308     data += (char)IClipboard::kText;
00309     data += (char)0; // 285 >> 24 = 285 / (256^3) = 0
00310     data += (char)0; // 285 >> 16 = 285 / (256^2) = 0
00311     data += (char)1; // 285 >> 8 = 285 / (256^1) = 1(.11328125)
00312     data += (char)29; // 285 - 256 = 29
00313     data += text;
00314 
00315     clipboard.unmarshall(data, 0);
00316 
00317     clipboard.open(0);
00318     CString actual = clipboard.get(IClipboard::kText);
00319     EXPECT_EQ(text, actual);
00320 }
00321 
00322 TEST(CClipboardTests, unmarshall_withTextAndHtml_getTextIsValid)
00323 {
00324     CClipboard clipboard;
00325     CString data;
00326     data += (char)0;
00327     data += (char)0;
00328     data += (char)0;
00329     data += (char)2; // 2 formats added
00330     data += (char)0;
00331     data += (char)0;
00332     data += (char)0;
00333     data += (char)IClipboard::kText;
00334     data += (char)0;
00335     data += (char)0;
00336     data += (char)0;
00337     data += (char)14;
00338     data += "synergy rocks!";
00339     data += (char)0;
00340     data += (char)0;
00341     data += (char)0;
00342     data += (char)IClipboard::kHTML;
00343     data += (char)0;
00344     data += (char)0;
00345     data += (char)0;
00346     data += (char)10;
00347     data += "html sucks";
00348 
00349     clipboard.unmarshall(data, 0);
00350 
00351     clipboard.open(0);
00352     CString actual = clipboard.get(IClipboard::kText);
00353     EXPECT_EQ("synergy rocks!", actual);
00354 }
00355 
00356 TEST(CClipboardTests, unmarshall_withTextAndHtml_getHtmlIsValid)
00357 {
00358     CClipboard clipboard;
00359     CString data;
00360     data += (char)0;
00361     data += (char)0;
00362     data += (char)0;
00363     data += (char)2; // 2 formats added
00364     data += (char)0;
00365     data += (char)0;
00366     data += (char)0;
00367     data += (char)IClipboard::kText;
00368     data += (char)0;
00369     data += (char)0;
00370     data += (char)0;
00371     data += (char)14;
00372     data += "synergy rocks!";
00373     data += (char)0;
00374     data += (char)0;
00375     data += (char)0;
00376     data += (char)IClipboard::kHTML;
00377     data += (char)0;
00378     data += (char)0;
00379     data += (char)0;
00380     data += (char)10;
00381     data += "html sucks";
00382 
00383     clipboard.unmarshall(data, 0);
00384 
00385     clipboard.open(0);
00386     CString actual = clipboard.get(IClipboard::kHTML);
00387     EXPECT_EQ("html sucks", actual);
00388 }
00389 
00390 TEST(CClipboardTests, copy_withSingleText_clipboardsAreEqual)
00391 {
00392     CClipboard clipboard1;
00393     clipboard1.open(0);
00394     clipboard1.add(CClipboard::kText, "synergy rocks!");
00395     clipboard1.close();
00396 
00397     CClipboard clipboard2;
00398     CClipboard::copy(&clipboard2, &clipboard1);
00399 
00400     clipboard2.open(0);
00401     CString actual = clipboard2.get(CClipboard::kText);
00402     EXPECT_EQ("synergy rocks!", actual);
00403 }

Generated on Sat May 25 2013 00:00:03 for Synergy by  doxygen 1.7.1